Azure DevOps — Create CI/CD Pipeline for PHP Web App

The CI/CD is one of the best practices for DevOps teams to implement for delivering code changes more frequently and reliably. It is also an agile methodology best practice, as it enables software development teams to focus on meeting business requirements, code quality, and security because deployment steps are automated.

This post will guide how to create a continuous integration (CI) and continuous deployment (CD) pipeline for a simple PHP web app using Azure DevOps. Your team can then automatically build each commit and deploy the change to an Azure App Service. You can select different PHP versions. In this tutorial, I would be covering workflow as below

1. Developers pull remote repository on local machine and apply latest changes2. Commit code and push to Azure repository develop branch3. The latest changes are reviewed and merged to Azure repository main or master branch4. Automatically trigger the build and deploy web app on Azure app service

P.S. — This tutorial will focus more on build deployment with YAML (azure-pipelines.yml) configuration and won’t cover topics like how to create Azure Project, Repository, Subscription, Resource Group or App Service.

To start, create Azure repository WebApp with branches main and develop. Clone the remote repository (hosted on Github) on local machine. The repository is a simple PHP web app that contains index.php file with sample ‘Hello World’ code.

Next step is to import the project (local git) to Azure DevOps repository using commands as below in the root directory of the project. After successfully executing the commands, the project in the local machine will be managed in Azure Git repository.

git init
git add .
git commit -m ‘initial commit’
git remote add origin
git push -u origin main

Note: Replace the remote origin with your Azure repository URL. If the local project has already been managed in a local git repository, you can skip git init command.

To create a build pipeline in Azure DevOps, you must first create Azure subscription, Resource group and App service here —
Name the App service as phptestapp that will be utilized to configure pipeline.

Replace the azure-pipelines.yml file content with below and click on Save and Run. This will create the file azure-pipelines.yml in the root directory of the project.

# Test and package your PHP project.
# Add steps that run tests, save build artifacts, deploy, and more:

- main


# Azure Resource Manager connection created during pipeline creation
azureSubscription: ‘YourSubscription’

# Web app name
webAppName: ‘phptestapp’

# Resource group
resourceGroupName: ‘YourResourceGroupName’

# Environment name
environmentName: ‘phptestapp’

# Agent VM image name
vmImageName: ‘ubuntu-latest’

- stage: Archive
displayName: Archive stage
— job: Archive
displayName: Archive
vmImage: $(vmImageName)
— task: AzureAppServiceSettings@1
azureSubscription: $(azureSubscription)
appName: $(webAppName)
resourceGroupName: $(resourceGroupName)
appSettings: |
“value”: “true”
— task: ArchiveFiles@2
displayName: ‘Archive files’
rootFolderOrFile: ‘$(System.DefaultWorkingDirectory)’
includeRootFolder: false
archiveType: zip
archiveFile: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
replaceExistingArchive: true

— upload: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
artifact: drop

- stage: Deploy
displayName: Deploy stage
dependsOn: Archive
condition: succeeded()
— deployment: Deploy
displayName: Deploy
environment: $(environmentName)
vmImage: $(vmImageName)
— task: AzureWebApp@1
displayName: ‘Azure Web App Deploy: PHP Web App Deploy’
azureSubscription: $(azureSubscription)
appType: webAppLinux
appName: $(webAppName)
runtimeStack: ‘PHP|7.2’
package: $(Pipeline.Workspace)/drop/$(Build.BuildId).zip

The YAML file configuration triggers the pipeline to run whenever there is any update in the main branch of the repository. It builds and releases the code without having to make separate release pipeline. The stages will show that the code has been archived and deployed and the changes can be verified by reloading app service URL.

YAML based Pipelines are a great new feature to Azure DevOps that enables you to configure your CI/CD strategy as code. For this tutorial, we have used multi-stage YAML pipeline for CI and CD together.