Only Run Github Actions on Specific Branches
In this tutorial, we focus on running Github Actions on the push event for specific branches. This may be useful if you only want to test on feature branches and do a deployment if master. This was the use case for me. After the tests have passed, the the code review has been approved. It's time to deploy to production. This is merging to master and running a series of steps. Github Actions makes it very simple to run specific actions/workflows on specific branches.
As I write this tutorial Github Actions is in beta. You should check to see if you have access first. There would be a tab called
Actions if you do have access.
Project Setup on Local
Start by creating a project or you can add to your existing one.
Clone your Git project to your local.
Sub in values for
Master Branch Workflow
We are going to start by building our first workflow. This workflow will be just for master. This is our make-shift go-to-prod workflow.
Create a file at
.github/workflows/master.yml and insert:
As you can see, on the
push event to
master it's going to run the steps. The steps are checkout (Uses the checkout workflow provided by Github), Run a one-line script and Run a multi-line script. You can trigger it by pushing to the master branch.
You can see the code now on Github.
Heads up: My workflow had an issue so it will have an x in the image but the workflow above should work. Please ignore.
You can watch it build.
You can see each step complete. Each step can be clicked and opened for more information.
Feature Branch Workflow
Let's create a feature branch. You would do this if you were doing development work and didn't want interfere with master.
In your project run:
This has created an empty file. You can see Github has the change.
But no workflow was triggered. The master workflow only triggers on a push to master.
We need to create a workflow for the feature branches. Create a file at
This will trigger for all branches. This is fine, you can test master twice. That's not a negative. Again push your changes up:
You can see it kick off on Github. You can dig into each step.
This was my first pass at Github Actions. It was an extremely enjoyable experience. Being able to run different workflows per branch is incredibly important and allows for much more extensible Git flows. Want to add a release branch and use master for QA? Not a problem. Have a small application and simply want to zip and place it into releases? Not a problem.
I'm going to continue to dig into Github Actions. I'll use them for my own projects and report any interesting discoveries back.