ProTip™: Decouple Building & Deploying in Your GitHub Actions Pipeline

Published

tl;dr if: github.ref == 'refs/heads/master'

I wanted to decouple the build and deploy steps in one of my personal projects for the sake of being able to have confidence when doing code changes but limit actual deploys to commits to the main default branch (in this repository’s case master). Here’s the diff:

# .github/workflows/ci.yml
name: CI

on:
  push:
    branches: [ master ]

+  pull_request:

  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-24.04

    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0

      # Business logic omitted for brevity

      - name: Deploy
+        if: github.ref == 'refs/heads/master'
        uses: peaceiris/actions-gh-pages@v3
        with:
          github_token: $

Took me three tries1 to get there, but that’s all that was ultimately required. Originally I tried decoupling the jobs: into a build: and deploy: which technically worked but I immediately hit the roadblock of the filesystem being ephemeral so in order to actually persist the filesystem changes e.g. the built static site directory I would need to do something with the upload-artifact and download-artifact actions. That seemed more complicated than I wanted, but I suppose for a more complex pipeline it could be useful. GitHub has some docs that go into more detail: https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/storing-and-sharing-data-from-a-workflow

That’s all, folks. May your builds be successful and business logic decoupled.

🖖


  1. In order: https://github.com/Pinjasaur/bic-example/pull/15, https://github.com/Pinjasaur/bic-example/pull/16, https://github.com/Pinjasaur/bic-example/pull/17↩︎


I love hearing from readers so please feel free to reach out.

Reply via emailSubscribe via RSS or email

Last modified  #protip   #programming   #cicd 


← Newer post  •  Older post →