Lessons
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.
1 2
git clone <your_git_project> cd <your_git_project_folder_name>
Sub in values for <your_git_project>
and <your_git_project_folder_name>
.
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
name: MasterCI on: push: branches: - master jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 - name: Run a one-line script run: echo Hello, world! - name: Run a multi-line script run: | echo Add other actions to build, echo test, and deploy your project.
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.
1 2 3
git add --all git commit -m "Let there be light: First commit to master" git push
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:
1 2 3 4 5 6
git fetch git checkout feature-123 touch test.txt git add --all git commit -m "Added an empty file" git push origin HEAD:feature-123
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 .github/workflows/feature.yml
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
name: FeatureCI on: push: branches: - '*' jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 - name: Run a one-line script run: echo "Only on featuer branches" - name: Run a multi-line script run: | echo "Only on featuer branches1" echo "Only on featuer branches2"
This will trigger for all branches. This is fine, you can test master twice. That's not a negative. Again push your changes up:
1 2 3
git add --all git commit -m "Added feature workflow" git push origin HEAD:feature-123
You can see it kick off on Github. You can dig into each step.
Conclusion
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.