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 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.
# Test and package your PHP project.
# Add steps that run tests, save build artifacts, deploy, and more:
# Azure Resource Manager connection created during pipeline creation
# Web app name
# Resource group
# Environment name
# Agent VM image name
- stage: Archive
displayName: Archive stage
— job: Archive
— task: AzureAppServiceSettings@1
— task: ArchiveFiles@2
displayName: ‘Archive files’
— upload: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
- stage: Deploy
displayName: Deploy stage
— deployment: Deploy
— task: AzureWebApp@1
displayName: ‘Azure Web App Deploy: PHP Web App Deploy’
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.