Deploy WordPress using Azure DevOps CI-CD Pipeline-Part 2

Setting up the CD part.

This is the second and final part of the WordPress CI-CD series. You can check the part 1 here.

Objective:

Here, we are going to create a CD pipeline in Azure DevOps for a WordPress site. WordPress site will be deployed on the Azure web app. All the changes made on the local environment’s WordPress site will be deployed on a web app.

Prerequisite: Follow the prerequisite here.

  • ACR (Azure Container Registry) for WordPress
  • Azure web app for WordPress deployment (Staging Env.).
  • Azure MySQL for Azure Web App.
  • Allow the connectivity between the Azure web app to Azure Databases for MySQL.
  • Setup a local environment that has WordPress installed and configured.

Note: You can use this sample source code for WordPress setup and for Dockerfile.

Step 1. Create a Release Pipeline in Azure DevOps.

Go to the Release and create a new release pipeline and choose an empty job in next step.

Step 2. Add artifact in the Artifacts section.

  • Source Type: Choose Azure Container Registry.
  • Service connection: Choose the Azure service connection created earlier.
  • Resource Group: Choose the Resource Group that has ACR, Web app, and other project-related resources.
  • Azure Container Registry: Choose the appropriate registry
  • Repository: Choose the appropriate repository

Then click Add.

Step 3. Create a Staging pipeline.

Click on the stage name under Tasks, Now add the following tasks by clicking on the + icon at Agent Job

  • 3 Jobs of SSH
  • Azure App Service Deploy
  • Restart Azure App Service
wordpress-CD

Step 4. Adding Variables

We have to add some variables that are going to be used herein Staging CD.

To add variables click on the Variables tab and add the following variables:

  • destmysqlhost – DB hostname of the Azure MySQL database of Staging Env.
  • destmysqlpass – DB password of the Azure MySQL database of Staging Env.
  • destmysqluser – DB username of the Azure MySQL database of Staging Env.
  • desturl – App service URL for staging Env.
  • migrationfile – name of the temp file which will execute DB backup restore operation
  • mysqldestdb – DB name of the Azure MySQL database of Staging Env.
  • mysqlhost – DB hostname of the MySQL database of the local system.
  • mysqlpass – DB password of the MySQL database of the local system.
  • mysqlport – DB port of the MySQL database of the local system.
  • mysqlsourcedb – DB name of the MySQL database of the local system.
  • mysqluser – DB username of the MySQL database of the local system.
  • resultfile – name of the temp MySQL dump file.
  • sourceurl – local system URL or IP address.
wordpress-cicd-variables

You can always use the Azure key vault for variables.

Step 5. Now edit SSH tasks

SSH service connection: click on Manage and configure the local system to run scripts in SSH tasks

  • 1st task: db backup to file

SSH service connection: Select the appropriate SSH service connection.

Run: check Commands

Paste the following in given box:

mysqldump -P $(mysqlport) -h $(mysqlhost) -u $(mysqluser) -p$(mysqlpass) $(mysqlsourcedb) > $(resultfile) 2>/dev/null
  • 2nd task: Replace the src. URLs with dest. URLs

SSH service connection: Select the appropriate SSH service connection.

Run: check Commands

Paste the following in given box:

sed 's/$(sourceurl)/$(desturl)/g' $(resultfile) > $(migrationfile)
  • 3rd task: Import DB changes to dest. DB

SSH service connection: Select the appropriate SSH service connection.

Run: check Commands

Paste the following in given box:

mysql -h $(destmysqlhost) -u $(destmysqluser) -p$(destmysqlpass) $(mysqldestdb) < $(migrationfile) 2>/dev/null

Step 6. Configure 4th task: Azure App Service Deploy

  • Connection type: Azure Resource Manager
  • Azure subscription: Choose the subscription
  • App Service type: Web App for Containers (Linux)
  • App Service name: select the right app service name
  • Registry or Namespace: use the Azure Container Registry login server value
  • Image: Docker Image name from the CI pipeline or you can check in ACR as well.
  • Tag: Image tag i.e. $(Build.BuildId)
  • Application and Configuration Settings:
    • App settings:
      • -DB_ENV_NAME $(mysqldestdb) -DB_ENV_USER $(destmysqluser) -DB_ENV_PASSWORD $(destmysqlpass) -DB_ENV_HOST $(destmysqlhost)

Step 7. Configure Restart Azure App Service

  • Azure subscription: Choose the subscription
  • Action: Restart App Service
  • App Service name: select the right app service name

The Staging CD pipeline is completed.

Note: You can create another Env. Also such as Dev. or Prod. by cloning the staging stage, adding other variables for other env. and making appropriate changes in tasks.

Step 8. Enabling Continuous Delivery (CD)

Click on the Pipeline tab > click on the lightning icon in the Artifact section > Enable the CD.

enable-cd

Summary:

We have learned how to setup a CI-CD pipeline for a WordPress site deployed on Azure services from the two blog post series. One can achieve this CI-CD for WordPress on other pipeline tools or in another cloud provider if the base concept is the same. Hope this was helpful.

One thought on “Deploy WordPress using Azure DevOps CI-CD Pipeline-Part 2

Leave a Reply

Your email address will not be published. Required fields are marked *