1.2. CI/CD#
1.2.1. Test GitHub Actions Workflows Locally with act
#
Don’t waste time when testing Github Actions.
Instead, use act
.
act
allows you to run your Github Actions workflows locally.
Instead of committing/pushing every time you make changes to your Workflow files.
It uses Docker to pull or build the necessary images, as defined in your workflow files. So you need Docker installed on your machine.
!curl -s https://raw.githubusercontent.com/nektos/act/master/install.sh | sudo bash
act
1.2.2. Cache Python Dependencies in GitHub Actions#
Don’t waste time for your GitHub Actions workflows.
Whenever a new run is triggered, your dependencies are probably installed again and again and again…
Even if you didn’t change anything in your dependency file.
With the small snippet below, the action caches your dependencies so you can skip the install step the next time your workflow runs.
name: ci
on:
push:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
with:
python-version: '3.9'
cache: 'pip' # caching pip dependencies (supports poetry and pipenv too)
- run: pip install -r requirements.txt
- name: Run Python unit tests
run: python3 -u -m unittest tests/tests.py
1.2.3. Lint Github Actions Workflow Files with actionlint
#
To lint your Github actions workflow files, use actionlint
.
actionlint
is a tool for detecting potential problems and smells in your workflow files.
This includes
Syntax checks
Security checks
Cron syntax checking
Checks for access to non-existent properties
It has a pre-commit hook too.
!go install github.com/rhysd/actionlint/cmd/actionlint@latest
!actionlint