DEV Community

Play Button Pause Button
Brian Douglas for GitHub

Posted on • Updated on

Running GitHub Actions CI/CD triggers on specific branches

GitHub Actions allows you to automate, customize and execute your software development workflows inside your repository.

You can configure your workflows to run when specific activity on GitHub happens, at a scheduled time, or when an event outside of GitHub occurs.

The most common events are push and pull_request events to trigger workflow runs on any change to the main branch. The workflow will run for any pull request opened and show your workflows directly in the pull request.

on: [push, pull_request]
Enter fullscreen mode Exit fullscreen mode

You want certain events to run on specific branches. You adjust the YAML to include branch names to only run CI checks on branches you needed them. Below I have changed my events to run on the main branch, even though it is named the push. It will run when you run git push or merge a pull request into the main branch.

on:
  push:
    branches:
    - main
  pull_request:
    branches:
    - main
Enter fullscreen mode Exit fullscreen mode

Finally, you can leverage YAML syntax to run an array of branches. You can also set up wildcards to run on similarly named branches as well. So now you can set up a workflow to run in multiple branches and maintain separate release tracks. For example:

on:
  push:
    branches:
    - master
    - 'releases/**'
  pull_request:
    branches:
    - master
    - 'releases/**'
Enter fullscreen mode Exit fullscreen mode

This is part of my 28 days of Actions series. To get notified of more GitHub Action tips, follow the GitHub organization right here on Dev.

Top comments (0)