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

Vikram Purohit
4 min readFeb 3, 2021

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.

https://github.com/Vikram0811/php-web-app.git

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 https://dev.azure.com/fabrikam/_apis/git/WebApp
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 — https://portal.azure.com
Name the App service as phptestapp that will be utilized to configure pipeline.

From your Azure Devops, click on new pipeline and select the code location as Azure Repos Git

Select the repository WebApp

Select PHP as Linux Web App on Azure to configure your pipeline

Next steps would be selecting your Azure subscription and Web App name to validate and configure the build pipeline. You will be asked to review pipeline YAML (azure-pipelines-1.yml) generated by Azure that is responsible to build and run the pipeline based on the variables defined.

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.

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

trigger:
- main

variables:

# 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’

stages:
- stage: Archive
displayName: Archive stage
jobs:
— job: Archive
displayName: Archive
pool:
vmImage: $(vmImageName)
steps:
— task: AzureAppServiceSettings@1
inputs:
azureSubscription: $(azureSubscription)
appName: $(webAppName)
resourceGroupName: $(resourceGroupName)
appSettings: |
[
{
“name”: “SCM_DO_BUILD_DURING_DEPLOYMENT”,
“value”: “true”
}
]
— task: ArchiveFiles@2
displayName: ‘Archive files’
inputs:
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()
jobs:
— deployment: Deploy
displayName: Deploy
environment: $(environmentName)
pool:
vmImage: $(vmImageName)
strategy:
runOnce:
deploy:
steps:
— task: AzureWebApp@1
displayName: ‘Azure Web App Deploy: PHP Web App Deploy’
inputs:
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.

--

--