Trigger Github Pages with Actions cover image

Trigger Github Pages with Actions

João Santos • January 8, 2019

In the last post I briefly wrote about getting started this new year with creating my own blog. For that, I’m using the GitHub Pages to host the blog since is a free service.

This tweet from James Brooks introduced me to Jigsaw, a static site generator, and GitHub Actions to automate the deployment process.

I followed the GitHub workflow configuration setup from James but ran into some issues where the website build was failing even though the files were being correctly committed to the master branch.

It turns out that git commands used in actions do not trigger the build process. The token used by Github Actions - GITHUB_TOKEN - is not enough to start the build.

In order to initialize a new build everytime you push to the master (or gh-pages) branch you need some extra steps.

As an example, here is the final workflow file after creating up a repo secret named GH_PAGES_TOKEN:

name: Build & Publish

on:
  push:
    branches:
      - source
  schedule:
    - cron: "0 0 * * 1-5"

jobs:
  build-site:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          token: ${{ secrets.GH_PAGES_TOKEN }}
      - name: Install Composer Dependencies
        run: composer install --no-ansi --no-interaction --no-scripts --no-suggest --no-progress --prefer-dist
      - name: Install NPM Dependencies
        run: npm install
      - name: Build Site
        run: npm run production
      - name: Stage Files
        run: git add -f build_production
      - name: Commit files
        run: |
          git config --local user.email "actions@github.com"
          git config --local user.name "GitHub Actions"
          git commit -m "Build for deploy"
      - name: Publish
        run: |
          git subtree split --prefix build_production -b master
          git push -f origin master:master
          git branch -D master

And we're done! Now everytime you commit your work to your develop branch, the automated deployment action will successfully trigger the GitHub Pages building process.