Introduction
GitHub Actions are event-driven (new commits, pull requests, tags etc.). Based on the config, it runs a series of commands after a specified event has occurred. For example, every time someone creates a pull request for a repository, it can automatically run a set of tests, coverage and code quality checks.
It makes it so much easier to create a cohesive “GitOps” flow as you can now see everything in a single place - without creating too much custom plumbing.
In this example, we will enable it on a repo with sample python project and use it to run linting, tests and coverage on pull-requests. This is the repo we will use - https://github.com/abiydv/python-github-actions
Configuration
To enable Github actions, we simply add this file under .github/workflows/main.yml
1name: pr-checks
2
3on:
4 pull_request:
5 branches: [master]
6
7jobs:
8 checks:
9 runs-on: ubuntu-latest
10 steps:
11 - uses: actions/checkout@v2
12 - name: Set up Python 3.8
13 uses: actions/setup-python@v2
14 with:
15 python-version: 3.8
16 - name: install dependencies
17 run: |
18 python -m pip install --upgrade pip
19 pip install -r requirements.txt
20 - name: lint
21 run: |
22 flake8 src/*.py --count --select=E9,F63,F7,F82 --show-source --statistics
23 flake8 src/*.py --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
24 - name: tests with pytest
25 run: |
26 pytest
27 - name: coverage
28 run: |
29 coverage run -m pytest
30 coverage report --fail-under=50
Some important sections of this file are -
Filter events - This block filters the event(s) for which this action will execute. In this case, it will trigger whenever a PR is raised for the
master
branch.3on: 4 pull_request: 5 branches: [master]
Docker Image - The docker image you run this action on.
9runs-on: ubuntu-latest
Code checkout - Checkout the code before running anything else
10steps: 11- uses: actions/checkout@v2
Rest of the steps are the usual lint, pytests, coverage test commands etc. The only thing to take care of while adding such commands is to make sure they exit with a non-zero code incase of any failures.
Actions in Action
A new PR is created on the master
branch from the bug/coverage
branch. This causes the Github action to trigger. While it is running, a yellow dot denotes the in-progress status.
Since the action fails in this case (due to coverage being less than 50%), a red cross against the commit id denotes the failure. ❌
To see what exactly caused the check to fail, we can navigate to the actions, and then the failed step. This brings up the detailed logs screen to show the output.
Service Limits
There are service and usage limits on Github actions for free tier accounts. Details here.
Conclusion
While this example shows how to setup a sample CI/CD flow for a basic python application, the high level steps are pretty much the same for most languages.
Github actions makes it extremely convinient to run checks and builds right from the “codebase” with minimal configuration.
Over the subsequent posts, we will explore more about how to extend this functionality.
Hope the post proves useful and helps you along on your Github Actions adoption journey 👍
Note: Code mentioned above is here