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.

github-actions

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

Github Actions Configuration

To enable Github actions, we simply add this file under .github/workflows/main.yml

name: pr-checks

on: 
  pull_request:
    branches: [master]

jobs:
  checks:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Set up Python 3.8
      uses: actions/setup-python@v2
      with:
        python-version: 3.8
    - name: install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.txt        
    - name: lint
      run: |
        flake8 src/*.py --count --select=E9,F63,F7,F82 --show-source --statistics
        flake8 src/*.py --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics        
    - name: tests with pytest
      run: |
                pytest
    - name: coverage
      run: |
        coverage run -m pytest
        coverage report --fail-under=50        
Some important sections of this file are -

  1. 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.

        on: 
          pull_request:
            branches: [master]
        

  2. Docker Image - The docker image you run this action on.

        runs-on: ubuntu-latest
        

  3. Code checkout - Checkout the code before running anything else

        steps:
        - 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.

Github 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.

github-actions-inprogress

Since the action fails in this case (due to coverage being less than 50%), a red cross against the commit id denotes the failure. ❌

github-actions-failed

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.

github-actions-details

Service Limits

There are service and usage limits on Github actions for free tier accounts. Details here - https://docs.github.com/en/free-pro-team@latest/actions/reference/usage-limits-billing-and-administration

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: All the code mentioned in this post is here - https://github.com/abiydv/python-github-actions

Cheers!

📝 This post was written by Abhinav

📌 Explore more Posts, Tags or Archives


📚 References: