CI/CD for WordPress on Azure with GitHub Actions
Tags: azure, wordpress, github
Published: 2022-04-21 12:33:41

Running WordPress on Azure recently got a whole lot better. In February '22 Microsoft announced [The new and better ‘WordPress on App Service’](https://techcommunity.microsoft.com/t5/apps-on-azure-blog/the-new-and-better-wordpress-on-app-service/ba-p/3202594) with maintenance and performance enhancements and a streamlined deployment process.

I'm not going to pontificate about the many benefits of WordPress hosting on Azure suffice to say **SLA**, **Scale** and **Security**. My aim for this post is to address one concern: **how to deploy custom code to your WordPress site running in Azure App Service**.

> By custom code, I am referring to plugins and themes. If you want to hack WordPress core, you're on your own :grin:

## Assumptions
1. Your code is in a GitHub repository
2. You will use the FTPS credentials of your web app. There is a dev.to article for that: [Connect to Azure WordPress App via FTP](https://dev.to/sfoteini/connect-to-azure-wordpress-app-via-ftp-35m9)
3. You'll use GitHub Actions for Continuous Integration / Continuous Deployment (CI/CD) to deploy code updates to your WordPress site.

## Some Background
WordPress on App Service installs with an Application setting `WEBSITES_ENABLE_APP_SERVICE_STORAGE` set to `true`. This enables persistent storage for your site in an Azure Storage mount having the path `/home` OR `~/`. Off of this path is the WordPress code (among other stuff like log files and backups).

As of April 13, 2022, the WordPress source code maintained by Microsoft includes three plugins in the path `~/site/wwwroot/wp-content/plugins`. When you install a plugin via the wp-admin UI, this is the path into which they are installed. Themes follow the same pattern.

## CI/CD For Custom Plugin and Theme Code
Needless to say, for custom code you'll want to have source code version control and an efficient deployment workflow. This is accomplished with GitHub repositories and GitHub Actions.

For the sake of example, my custom code is a fork of the public repository **[tommcfarlin/wp-hello-world](https://github.com/tommcfarlin/wp-hello-world)**.

To our custom code we'll add a directory for our GitHub Action, `.github\workflows`.

For our GitHub Action we'll use the **[FTP Deploy](https://github.com/marketplace/actions/ftp-deploy)** action available in the GitHub Marketplace.

In our `.github\workflows` directory we'll add a `main.yml` file into which we'll insert the following code:
```
on: push
name: 🚀 Deploy website on push
jobs:
  web-deploy:
    name: 🎉 Deploy
    runs-on: ubuntu-latest
    steps:
    - name: 🚚 Get latest code
      uses: actions/checkout@v2

    - name: 📂 Sync files
      uses: SamKirkland/FTP-Deploy-Action@4.3.0
      with:
        server: <mysite>.ftp.azurewebsites.windows.net
        username: ${{ secrets.WP_USER }}
        password: ${{ secrets.WP_PASSWORD }}
        protocol: ftps        
        server-dir: /site/wwwroot/wp-content/plugins/wp-hello-world/
        # dry-run: true
```
For this to work for your site, you need to:
1. [Add Actions secrets](https://github.com/Azure/actions-workflow-samples/blob/master/assets/create-secrets-for-GitHub-workflows.md) for the FTPS credentials. In our example the secret names are `WP_USER` and `WP_PASSWORD`.
2. For the `server:` value replace the `<mysite>` placeholder with the unique FTPS endpoint prefix of your site.

### Tips

- Note the `# dry-run: true` attribute at the end of the action code. I recommend this for your initial testing of the GitHub Action.
- Refer to the [FTP Deploy](https://github.com/marketplace/actions/ftp-deploy) documentation for other settings you can employ for your GitHub Action.

## Going Deeper
CI/CD really shines when you have staged deployments. With Azure Web Apps you can have [deployment slots](https://docs.microsoft.com/en-us/azure/app-service/deploy-staging-slots) for pre-production environments such as Dev, Stage, and QA. Each has its own FTPS Endpoint allowing you to leverage GitHub Actions for each stage of your code life-cycle.

## In Closing

Thanks for reading this post. I hope you found it helpful. Feel free to share your feedback and suggestions for other readers. 

### Resources
- [App Service overview](https://docs.microsoft.com/en-us/azure/app-service/overview)
- [Create a WordPress site - Azure App Service](https://docs.microsoft.com/en-us/azure/app-service/quickstart-wordpress)
- [Get Started with GitHub Actions](https://docs.github.com/en/actions)