Our WordPress Workflow with Github Action

Learn how to use Github to automatically deploy your WordPress theme changes based on commit.

Github Action is an automation tool to deploy your code whenever a new commit is pushed to Github.

This article will teach you how to automatically upload the changes using FTP. We opted to use FTP instead of SSH because it works in all hosting.

Let’s take a look at how we do it:

Git Folder Structure

We basically committed the whole WordPress folder while ignoring everything except the theme and custom plugins.

Resulting in a repository that looks like this:

/
├── wp-content/
│   ├── themes/
│   │   ├── my-theme/
│   ├── plugins/
│   │   ├── custom-plugin-if-any/
├── .github
│   ├── workflow/
│   │   ├── deploy.yml
├── composer.json
└── .gitignore

composer.json

All the plugins that are available in wordpress.org are managed with Composer. The custom ones are committed to the git.

If you don’t know how to use Composer for WP plugins, read the guide below:

.gitignore

This will ignore all except your custom theme and custom plugins.

You need to specify included plugins and excluded themes like shown below:

vendor/
wp-admin/
wp-includes/
wp-content/*

!wp-content/plugins/
wp-content/plugins/*
!wp-content/plugins/custom-plugin-if-any

!wp-content/themes/
wp-content/themes/twentynineteen
wp-content/themes/twentytwenty
wp-content/themes/twentytwentyone
wp-content/themes/twentytwentytwo
wp-content/themes/storefront

/*.php
/.htaccess
/wp-cli.local.yml
/composer.lock
/index.php

nginx.conf
.DS_Store
node_modules/
logs
*.log
npm-debug.log*
readme.html
license.txt

deploy.yml

This file contains the Action commands. It resides in the folder .github/workflows/.

We have prepared two variations depending on whether your hosting supports SFTP or not.

For SFTP: (only a few hosting support this)

name: Deploy via SFTP
on:
  push:
    branches:
      - main
jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1
    - name: Deploy app
      uses: sebastianpopp/git-ftp-action@fix-ssl-issue
      with:
        url: "sftp://myhosting.com"
        user: ${{ secrets.FTP_USERNAME }}
        password: ${{ secrets.FTP_PASSWORD }}
        options: "--auto-init --insecure --remote-root /home/path/to/your/folder"

If you’re using an old repo, the branch name might be “master” instead of “main”. So change that in the code above.

For FTP: (most hostings only support this)

name: Deploy via FTP
on:
  push:
    branches:
      - main
jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1
    - name: git-ftp push
      uses: sebastianpopp/git-ftp-action@releases/v3
      with:
        url: "ftp.myhosting.com/public_html/mysite"
        user: ${{ secrets.FTP_USERNAME }}
        password: ${{ secrets.FTP_PASSWORD }}

Getting the correct URL is a bit tricky.

It starts with the FTP base address. For example ftp.myhosting.com. Followed by the path to the site folder.

But the path starts from where you landed in Filezilla. So if you landed in /www and the path is /www/public_html/mysite, no need to include the /www.

Setup the Secrets

This is to replace the FTP_USERNAME and FTP_PASSWORD in our command. Follow the guide below:

  1. Go to your Github and click “Settings” tab.
  2. Click “Secrets” in the left sidebar.
  3. Click “New repository secret” and create two variables named FTP_USERNAME and FTP_PASSWORD. Fill the values accordingly.
  4. Done!

Test It Out

Make changes in your code and commit it. Then open the “Actions” tab in Github. You should see the deployment in progress:

The initial commit will take quite a while (5-10 mins) depending on the number of files. After that, it will be much faster since it only uploads the modified file.

Conclusion

We have been using this workflow since 2016 with great success. Back then we were using a similar service called Bitbucket Pipeline.

In short, we are using different methods for deploying different parts of WordPress:

  1. Deploying theme – uses Github Action.
  2. Deploying plugins – uses Composer If hosting supports SSH, otherwise use FTP manually.
  3. Deploying database and images – use WP Sync DB.

Although it looks confusing to use different methods, we rarely need to do #2 and #3. So we only need to focus on how to make #1 as quick and seamless as possible.

If you have any question, let us know in the comment below

Default image
Henner Setyono
A web developer who mainly build custom theme for WordPress since 2013. Young enough to never had to deal with using Table as layout.
Leave a Reply

2 Comments

  1. Are you still using this deployment workflow? I'm curious because I have a similar repo structure, in which I have a custom WordPress plugin and custom theme that I'm trying to deploy via Github Workflow Actions. I've been tinkering with using rsync to deploy the code, but it's deleting everything that isn't in version control. So your approach is appealing, but I'm concerned that the sebastianpopp/git-ftp-action Action hasn't been updated in three years.

    • Hi Kory, Yes we are still using it for all of our WordPress projects.

      The best thing for Git-FTP is that it doesn't delete everything before deployment and only push changed files.

      Also as noted in the article, be careful of getting the right Host path.