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:

public/
├── wp-content/
│   ├── themes/
│   │   ├── my-theme/
│   ├── plugins/
│   │   ├── custom-plugin-if-any/
├── .github
│   ├── workflow/
│   │   ├── composer.json
├── 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 list which plugin you want to include just like shown in Line 8.

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:

name: Deploy via SFTP
on:
  push:
    branches:
      - main
jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/[email protected]
    - name: Deploy app
      uses: sebastianpopp/[email protected]
      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/[email protected]
    - name: git-ftp push
      uses: sebastianpopp/[email protected]/v3
      with:
        url: "ftp.myhosting.com/public_html/mysite"
        user: ${{ secrets.FTP_USERNAME }}
        password: ${{ secrets.FTP_PASSWORD }}

Getting the right 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 separate 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 uploads – use WP Sync DB.

Although it looks like a mess, 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