DEV Community

Mike Brooks
Mike Brooks

Posted on

CI/CD for WordPress on Azure with GitHub Actions

Running WordPress on Azure recently got a whole lot better. In February '22 Microsoft announced The new and better โ€˜WordPress on App Serviceโ€™ 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 ๐Ÿ˜

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
  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.

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 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
Enter fullscreen mode Exit fullscreen mode

For this to work for your site, you need to:

  1. Add Actions secrets 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 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 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

Top comments (0)