In this part of the blog series, we will focus on setting up a CI/CD pipeline using Azure Pipelines with YAML as code. We’ll walk through an example azure-pipelines.yml file that demonstrates the necessary steps for deploying your WordPress site.
YAML Configuration Overview Link to heading
Here’s an example of an azure-pipelines.yml file that outlines the configuration for our CI/CD pipeline:
trigger:
- none
pool:
vmImage: ubuntu-latest
steps:
- task: CopyFiles@2
displayName: 'Copy Files to: $(Build.ArtifactStagingDirectory)'
inputs:
SourceFolder: '$(Build.SourcesDirectory)'
Contents: |
**/*
!.git/**/*
!*.yml
!*.yaml
TargetFolder: '$(Build.ArtifactStagingDirectory)/public'
- task: replacetokens@5
# Task to replace tokens in PHP files
# (Custom tokens enclosed in #_ and _# will be replaced)
# This is useful for replacing sensitive information in the wp-config.php file
# with values provided during the CI/CD process.
# This task ensures that sensitive information is not exposed in the source code repository.
# Replace the tokens with the appropriate values specific to your environment.
# Refer to the documentation for further details on configuring the replacetokens task.
# Remove sensitive configuration information from the example below.
inputs:
rootDirectory: '$(Build.ArtifactStagingDirectory)/public'
targetFiles: '**/*.php'
encoding: 'auto'
tokenPattern: 'custom'
tokenPrefix: '#_'
tokenSuffix: '_#'
writeBOM: true
actionOnMissing: 'warn'
keepToken: false
actionOnNoFiles: 'continue'
enableTransforms: false
enableRecursion: false
useLegacyPattern: false
enableTelemetry: true
- task: Bash@3
# Task to display the modified wp-config.php file contents.
# This step is for verification purposes and can be removed in a production scenario.
inputs:
targetType: 'inline'
script: 'cat $(Build.ArtifactStagingDirectory)/public/wp-config.php'
- task: ArchiveFiles@2
# Task to create a zip archive of the files to be deployed.
# This ensures a portable artifact for deployment.
inputs:
rootFolderOrFile: "$(Build.ArtifactStagingDirectory)/public"
includeRootFolder: false
archiveType: "zip"
archiveFile: "$(Build.ArtifactStagingDirectory)/public/$(Build.BuildId).zip"
replaceExistingArchive: true
- task: PublishBuildArtifacts@1
# Task to publish the zip artifact to the Azure Pipelines artifact store.
# This makes the artifact available for the release pipeline or deployment.
inputs:
PathtoPublish: "$(Build.ArtifactStagingDirectory)/public/$(Build.BuildId).zip"
ArtifactName: "drop"
- task: AzureRmWebAppDeployment@4
# Task to deploy the WordPress site to Azure App Service.
# This task leverages the AzureRmWebAppDeployment task, specifically for webAppLinux.
# Provide the appropriate values for the ConnectedServiceName and WebAppName parameters.
# Replace the sensitive configuration information in the example below.
inputs:
ConnectionType: 'AzureRM'
ConnectedServiceName: 'AzureConectionServiceName'
appType: 'webAppLinux'
WebAppName: 'WebAppName'
packageForLinux: '$(Build.ArtifactStagingDirectory)/public/$(Build.BuildId).zip'
Let’s understand the purpose of each task in the pipeline: Link to heading
-
CopyFiles: This task copies the necessary WordPress files to the staging directory, excluding Git-related files and YAML configuration files.
-
replacetokens: This task replaces specific tokens in PHP files (such as wp-config.php) with environment-specific values provided during the CI/CD process. This helps ensure sensitive information is not exposed in the source code repository.
-
Bash: This task displays the modified contents of the wp-config.php file, providing verification and debugging information during development. Remove this step in a production scenario.
-
ArchiveFiles: This task creates a zip archive of the files to be deployed, ensuring a portable artifact for deployment.
-
PublishBuildArtifacts: This task publishes the zip artifact to the Azure Pipelines artifact store, making it available for the release pipeline or deployment.
-
AzureRmWebAppDeployment: This task deploys the WordPress site to Azure App Service using the AzureRmWebAppDeployment task specifically designed for webAppLinux. Provide the appropriate values for the ConnectedServiceName and WebAppName parameters.
Ensure that you update the configuration values in the pipeline to match your specific Azure configuration and requirements.
Configuring Azure Service Connection Link to heading
To deploy your WordPress app to Azure using Azure Pipelines, you need to set up an Azure Service Connection. Follow these steps to configure the service connection:
-
Go to your Azure DevOps project and navigate to Project Settings.
-
Under the Pipelines section, click on Service Connections.
-
Click on “New service connection” and select “Azure Resource Manager.”
-
Choose the appropriate authentication method to connect to your Azure subscription. You can select options like Service principal (automatic) or Service principal (manual) based on your preference and security requirements.
-
Provide the necessary details to authenticate and establish the connection with your Azure subscription.
-
Once the service connection is created, remember the name you assigned to it. You will use this name in the ConnectedServiceName parameter in the azure-pipelines.yml file.
Ensuring Azure App Service Configuration Link to heading
Before executing the pipeline, make sure you have an Azure App Service configured to host your WordPress site. Follow these steps to set up the Azure App Service:
-
Go to the Azure portal and navigate to the App Services section.
-
Create a new App Service, providing a unique name and selecting the appropriate configuration (e.g., Linux, Windows).
-
Configure the App Service with the necessary settings, such as the runtime stack, region, and pricing tier.
-
Ensure you have the necessary configurations within the App Service, such as the database connection details, environment variables, and any required extensions or modules.
More on this will be discussed in the next part of this series.
Update the Pipeline Configuration Link to heading
Once you have set up the Azure Service Connection and configured the Azure App Service, update the azure-pipelines.yml file with the appropriate values:
-
In the ConnectedServiceName parameter of the AzureRmWebAppDeployment task, specify the name of the Azure Service Connection you created.
-
In the WebAppName parameter, provide the name of the Azure App Service you configured.
After updating the pipeline configuration, commit and push the changes to your Git repository. Azure Pipelines will then execute the CI/CD pipeline, deploying your WordPress site to the specified Azure App Service.
In the next part of this blog series, we will focus on configuring Azure App Service for hosting your WordPress site. Stay tuned!