DEV Community

Cover image for Deploy a WordPress Theme with GitHub Actions
Matteo Barbero
Matteo Barbero

Posted on

Deploy a WordPress Theme with GitHub Actions

Is it possible to deploy a WordPress theme directly from GitHub?

Minified code, “bundler” of various types, package manager for JavaScript and PHP and so forth… These are just some of the reasons for choosing to automate the deployment of a WordPress Theme.

Sure it’s always possible to upload files with ftp every time, but why do it manually when GitHub can take care of everything?

Welcome to the guide to automate the deployment

First, the theme code must be in a GitHub repository.

Create a .github folder inside the repo. Inside this folder create another folder called workflows and inside it a main.yml file. You can give this file any name you like, the important thing is to keep the .yml extension.

The folder structure will look like this: .github / workflows / main.yml

The .yml file

Let’s start creating our workflow to automate the deployment

name: Deploy via FTP
on:
  push:
    branches: [ main ]
Enter fullscreen mode Exit fullscreen mode

name will simply be the name that we will display in the Actions tab of the repository

Image description
on: things get interesting here. We define our trigger here. In this case we want to deploy to the push, but we have added a filter: we are only interested in the push done to the main branch. In this way we can have a development branch, perhaps automating the deployment of this on a staging site as well.

jobs:
  build:
    runs-on: ubuntu-latest
    defaults:
      run:
        shell: bash
Enter fullscreen mode Exit fullscreen mode

Now we come to the actions, jobs, which will follow, by default in parallel, our workflow. build will be the name of our only job

runs-on: where do we want to run our workflow? GitHub gives us several possibilities listed here.

The last three lines are used to set a default shell for all runs

 steps:
    - uses: actions/checkout@v2
      with:
        fetch-depth: 2
    - name: FTP Deploy WP Theme
      uses: SamKirkland/FTP-Deploy-Action@4.3.2
      with:
        server: FTP SERVER
        username: FTP USERNAME
        password: ${{ secrets.FTP_PASSWORD }}
        server-dir: FTP DIR
        exclude: |
          **/.git*
          **/.git*/**
          **/node_modules/**
Enter fullscreen mode Exit fullscreen mode

We are at the final steps of our workflow. We just have to worry about writing our ftp server address and username, as well as the destination folder (our theme folder).

Since this file will be accessible from the repository, we can keep some settings like our password secret. To do this we can set a new secret key from the Setting / Secrets / Actions tab

Image description

Secret key GitHub repository
Are you tired of reading and just want to make a nice copy and paste? Find the .yml file and ready-made folder structure here

Top comments (0)