LanguagesPHPWorking with PHP, Git, and Azure DevOps

Working with PHP, Git, and Azure DevOps

Microsoft’s Azure DevOps offering provides a robust mechanism for managing version control and project collaboration. It also easily integrates with Visual Studio and allows for any project developed with that offering to be managed without a significant amount of extra effort.

But what about software that is developed outside of Visual Studio, especially software that is not developed using .NET or some other development environment that is not a Microsoft creation, or even a non-Microsoft Operating System? It would be great if Azure DevOps could be leveraged to manage these projects, and by using the git version control system, it is definitely doable. That is the topic of today’s PHP programming tutorial.

While this should work with any git client, this article will make use of the standard command line-based git client. The following demonstration project will also be used:

Example Project GitHub and Azure DevOps

Demonstration Project in Azure DevOps

This project was created in a free, personal Azure DevOps account that is not connected to any enterprise or other organization. However, the concepts discussed here have worked in an Enterprise-grade Azure DevOps setup. This programming tutorial also assumes that the reader has a basic knowledge of git, as once the DevOps repository is configured, it will function like any other git repository.

Read: The Repository Pattern in PHP

How to Access a Git Repository in Azure DevOps

While an Azure DevOps project contains its own repository, that repository needs to be specifically configured to work with the git client. To begin this process, click on the Repos link within the project:

Access to Git Repository from Azure DevOps

Setting up access to the repository via git

Within the Repos screen, there are two things which should be noted:

Azure DevOps Repos page

The Repos screen

If the Repos screen does not look like the one above, it may be necessary to click the Files link underneath the Repos link.

Note the Demo PHP Project text in the purple rectangular outline above. This is the name of the default git repository that needs to be made available to the git client. To begin the process, click on the Generate Git Credentials button highlighted in the red outline above. This will now generate a set of git credentials. The password is pixelated out:

Generate Git credentials

Generated git credentials

As DevOps warns, these credentials must be saved someplace secure, as they will not be saved on the site.

How to Clone a Git Repository Locally

It is assumed that the standard git client for the client OS is installed. If this is not the case it will need to be installed before the next step will work. At this point, the repository can be cloned to a development computer. To clone the repository, enter the following command in the directory in which the directory for the repository will live.

$ git clone https://[email protected]/philhajjar/Demo%20PHP%20Project/_git/Demo%20PHP%20Project

This command will ask for the password from above and will give output similar to what is shown below:

Clone a git repository

Initial cloning of the repository

The second command lists the contents of the directory and a new subdirectory with the URL-encoded name of the repository has now been created. The subdirectory name is highlighted in purple. Note, all of the code for the project must reside in this new subdirectory or else it will not be managed by git or DevOps.

Read: PHP Database Options: More Than Just MySQL

Adding PHP Code to a Git Repository Directory

Now change to the subdirectory and add some sample code to it:

Add code to a git repository directory

Adding some very basic code to the repository directory.

If this was a non-trivial software project, all of the code for that project could be copied here.

Now the code can be pushed to Azure DevOps. Be sure to keep that password handy, as it may be needed for future actions!

Next, use the git add * command to add the initial files:

Working with the git add command

Initial Add and Status

Note that there is no output from the git add * command. The git status command can be invoked to see what is about to be committed. Once the files are committed, they can be pushed to the server.

Now we can use the git commit command to commit the files. Depending on your system setup, there may be prompts for the developer’s name and email address.

Git Commit command

First Commit attempt – note the configuration requests

Re-invoking the git commit command now opens up the system-preferred text editor to add a mandatory commit message:

Entering a git commit message

Entering the commit message.

Once the editor is saved and exited from, the commit will take place:

Successful git commit

Successful Commit

How to Push Code from Git to Azure DevOps

To push the code from git to Azure DevOps, simply invoke the git push command:

Push Code from git to Azure DevOps

Pushing the code to Azure DevOps

The results of this initial commit can be shown in the ReposFiles section for this project’s page in Azure DevOps:

git and Azure DevOps examples

The final pushed code

Unlike with Visual Studio, no separate Sync step is needed here. The changes show up immediately in the Azure DevOps repo.

Read: Using PHP for System Administration

Conclusion to PHP, Git, and Azure DevOps Tutorial

That concludes the first part of this two-part series. In this tutorial, we learned how to access a git repository, how to clone a git repository, add PHP code to a directory, and push code from git to Azure DevOps. We covered a lot of ground, but there is still more to come, as in the next programming guide, developers will learn how to use Azure DevOps Work Items and more.

Read more PHP programming and software development tutorials.

Latest Posts

Related Stories