GitHub Actions Day 1: CI/CD Triggers

December 1, 2019

This is day 1 of my GitHub Actions Advent Calendar. If you want to see the whole list of tips as they're published, see the index.

GitHub Actions is a unique system: it provides CI/CD build functionality - the ability to build and test pull requests and merges into your master branch - but it's more than just a build system. It's integrated into GitHub and can run workflows whenever any event happens in your repository, like a release being created or an issue being commented on.

I'll talk more about those repository automation scenarios throughout the month, but knowing that this flexibility exists is helpful for understanding how to get your CI/CD builds setup. GitHub Actions lets you define a trigger that controls when your workflow runs. Whenever an action matching that trigger happens in your repository, a workflow run will be queued up.

For CI/CD workflows, I like to use the push and pull_request triggers, and scope it to the branches that I'm interested in. For example:

on:
  push:
    branches:
    - master
  pull_request:
    branches:
    - master

This trigger will run your workflow on any change to the master branch - (even though it's named the push trigger, it will run when you run git push or when you merge a pull request into the master branch). The workflow will also run for any pull request that's opened against the master branch, and it will show you validation in the pull request.

Pull Request Validation

If you're familiar with YAML syntax, you might note that branches takes an array. So you can easily set up a workflow to run in multiple branches, which is useful if you maintain separate release tracks. For example:

on:
  push:
    branches:
    - master
    - 'releases/**'
  pull_request:
    branches:
    - master
    - 'releases/**'

Will run your workflow whenever a pull request is opened against the master branch or a branch whose name starts with releases/.

The push and pull_request triggers make it easy to set up a CI/CD style workflow to validate pull requests and merges into your master branch with GitHub Actions.