Skip to content

Commit 42dd445

Browse files
authored
Use zizmor to lint GitHub Actions (#544)
Make use of `zizmor` to lint GitHub Actions workflows and catch potential security issues. Add `zizmor` to the `environment.yml` and to the `requirements-style.txt`. Add a new `check-actions` target in the `Makefile` that runs `zizmor` on every workflow. Add a new workflow for running `zizmor` on PRs and on pushes to `main`.
1 parent 9e84571 commit 42dd445

File tree

5 files changed

+68
-15
lines changed

5 files changed

+68
-15
lines changed

.github/workflows/actions.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Lint GitHub Actions for common security issues using zizmor.
2+
# Docs: https://woodruffw.github.io/zizmor
3+
4+
name: lint-actions
5+
6+
# Only run on PRs and the main branch.
7+
# Pushes to branches will only trigger a run when a PR is opened.
8+
on:
9+
pull_request:
10+
push:
11+
branches:
12+
- main
13+
14+
jobs:
15+
lint:
16+
runs-on: ubuntu-latest
17+
steps:
18+
- name: Checkout
19+
uses: actions/checkout@v4
20+
with:
21+
persist-credentials: false
22+
23+
- name: Setup Python
24+
uses: actions/setup-python@v5
25+
with:
26+
python-version: "3.12"
27+
28+
- name: Install requirements
29+
run: python -m pip install -r env/requirements-style.txt
30+
31+
- name: List installed packages
32+
run: python -m pip freeze
33+
34+
- name: Lint GitHub Actions
35+
run: make check-actions
36+
env:
37+
# Set GH_TOKEN to allow zizmor to check online vulnerabilities
38+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/docs.yml

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ jobs:
145145
steps:
146146
- name: Checkout
147147
uses: actions/checkout@v4
148+
with:
149+
# The GitHub token is preserved by default but this job doesn't need
150+
# to be able to push to GitHub.
151+
persist-credentials: false
148152

149153
# Fetch the built docs from the "build" job
150154
- name: Download HTML documentation artifact
@@ -161,33 +165,36 @@ jobs:
161165
path: deploy
162166
# Download the entire history
163167
fetch-depth: 0
168+
# The GitHub token is preserved by default but this job doesn't need
169+
# to be able to push to GitHub.
170+
persist-credentials: false
164171

165172
- name: Push the built HTML to gh-pages
166173
run: |
167174
# Detect if this is a release or from the main branch
168175
if [[ "${{ github.event_name }}" == "release" ]]; then
169-
# Get the tag name without the "refs/tags/" part
170-
version="${GITHUB_REF#refs/*/}"
176+
# Get the tag name without the "refs/tags/" part
177+
version="${GITHUB_REF#refs/*/}"
171178
else
172-
version=dev
179+
version=dev
173180
fi
174181
echo "Deploying version: $version"
175182
# Make the new commit message. Needs to happen before cd into deploy
176183
# to get the right commit hash.
177184
message="Deploy $version from $(git rev-parse --short HEAD)"
178-
cd deploy
185+
cd deploy || exit 1
179186
# Need to have this file so that Github doesn't try to run Jekyll
180187
touch .nojekyll
181188
# Delete all the files and replace with our new set
182189
echo -e "\nRemoving old files from previous builds of ${version}:"
183-
rm -rvf ${version}
190+
rm -rvf "${version}"
184191
echo -e "\nCopying HTML files to ${version}:"
185-
cp -Rvf ../doc/_build/html/ ${version}/
192+
cp -Rvf ../doc/_build/html/ "${version}/"
186193
# If this is a new release, update the link from /latest to it
187194
if [[ "${version}" != "dev" ]]; then
188-
echo -e "\nSetup link from ${version} to 'latest'."
189-
rm -f latest
190-
ln -sf ${version} latest
195+
echo -e "\nSetup link from ${version} to 'latest'."
196+
rm -f latest
197+
ln -sf "${version}" latest
191198
fi
192199
# Stage the commit
193200
git add -A .
@@ -199,15 +206,15 @@ jobs:
199206
# If this is a dev build and the last commit was from a dev build
200207
# (detect if "dev" was in the previous commit message), reuse the
201208
# same commit
202-
if [[ "${version}" == "dev" && `git log -1 --format='%s'` == *"dev"* ]]; then
203-
echo -e "\nAmending last commit:"
204-
git commit --amend --reset-author -m "$message"
209+
if [[ "${version}" == "dev" && $(git log -1 --format='%s') == *"dev"* ]]; then
210+
echo -e "\nAmending last commit:"
211+
git commit --amend --reset-author -m "$message"
205212
else
206-
echo -e "\nMaking a new commit:"
207-
git commit -m "$message"
213+
echo -e "\nMaking a new commit:"
214+
git commit -m "$message"
208215
fi
209216
# Make the push quiet just in case there is anything that could leak
210217
# sensitive information.
211218
echo -e "\nPushing changes to gh-pages."
212-
git push -fq origin gh-pages 2>&1 >/dev/null
219+
{ git push -fq origin gh-pages >/dev/null; } 2>&1
213220
echo -e "\nFinished uploading generated files."

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ TESTDIR=tmp-test-dir-with-unique-name
44
PYTEST_ARGS=--cov-config=../.coveragerc --cov-report=term-missing --cov=$(PROJECT) --doctest-modules --doctest-continue-on-failure -v --pyargs
55
NUMBATEST_ARGS=--doctest-modules -v --pyargs -m use_numba
66
STYLE_CHECK_FILES=$(PROJECT) examples doc
7+
GITHUB_ACTIONS=.github/workflows
8+
9+
.PHONY: build install test test_coverage test_numba format check check-format check-style check-actions clean
710

811
help:
912
@echo "Commands:"
@@ -54,6 +57,9 @@ check-format:
5457
check-style:
5558
flake8 $(STYLE_CHECK_FILES)
5659

60+
check-actions:
61+
zizmor $(GITHUB_ACTIONS)
62+
5763
clean:
5864
find . -name "*.pyc" -exec rm -v {} \;
5965
find . -name ".coverage.*" -exec rm -v {} \;

env/requirements-style.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ flake8-simplify==0.21.*
1212
flake8-unused-arguments==0.0.13
1313
pep8-naming==0.14.*
1414
burocrata==0.2.*
15+
zizmor

environment.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,6 @@ dependencies:
5555
- flake8-simplify==0.21.*
5656
- flake8-unused-arguments==0.0.13
5757
- pep8-naming==0.14.*
58+
- zizmor
5859
- pip:
5960
- burocrata==0.2.*

0 commit comments

Comments
 (0)