Continuous Integration for Python Applications with GitHub Actions
We can use GitHub Actions for continuous integration purposes, to run our automated tests and other automated checks comprising an automated build process.
Workflow Setup
To implement GitHub Actions for CI, we need to setup a corresponding workflow configuration file.
From your repository’s “Actions” tab on GitHub, add the action specifically called “Python application”. Conclude the commit to add a config file called “.github/workflows/python-app.yml” to the repository on GitHub. The file’s contents will look something like this:
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python
name: Python application
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
permissions:
contents: read
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python 3.10
uses: actions/setup-python@v3
with:
python-version: "3.10"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pytestRemember to pull down these changes to update your local repository to include this config file as well, before proceeding.
Now, anytime we push or merge a new commit to the repository’s main branch, that will trigger the workflow to execute, and run the tests as a part of the automated build process.
Workflow Customization
For further customization of the workflow config file, see the examples below.
App with Environment Variables
If your app uses environment variables, you’ll need to add them as repository secrets and update your build file to reference the secrets. See the app with secrets example workflow file, with relevant section copied below:
# ...
- name: Test with pytest
env:
# set environment variables to use during testing
# ... using the repository secret values set securely via repo settings
MY_API_KEY: ${{ secrets.MY_API_KEY }}
MY_API_SECRET: ${{ secrets.MY_API_SECRET }}
run: |
pytestApp with Google Service Account Credentials
If your app uses Google Service Account Credentials JSON file, you’ll need to paste the JSON contents into a new repository secret specifically called GOOGLE_API_CREDENTIALS, and update your build file to reference those credentials. See the app with google credentials example workflow file, with relevant sections copied below:
# ...
#
# USE OFFICIAL GOOGLE ACTION TO CREATE A CREDENTIALS JSON FILE
# https://github.com/google-github-actions/auth
#
- id: 'auth'
name: 'Authenticate to Google Cloud'
uses: 'google-github-actions/auth@v0'
with:
# uses this encrypted secret set via github repo settings
# which is essentially a copy of the JSON credentials file contents (for the dev project)
credentials_json: '${{ secrets.GOOGLE_API_CREDENTIALS }}'
# this will create a credentials file with a randomized name
create_credentials_file: true
#
# RUN TESTS
#
- name: Test with pytest
env:
# access path of credentials file created by earlier auth step:
GOOGLE_APPLICATION_CREDENTIALS: ${{ steps.auth.outputs.credentials_file_path }}
run: |
pytestApp with Database
See the app with database example workflow file.
Workflow Status Badge
After you have setup GitHub Actions for CI, consider adding a workflow status badge to your repository’s README file, to showcase the status of the CI build.