Skip to content

Commit c63ec26

Browse files
llucaxstefan-brus-frequenz
authored andcommitted
Improve the CI workflow
A lot of improvements coming from the repo-config cookiecutter template are introduced: - Support for merge queues - Standard workflow, jobs and step names - More descriptive names for jobs and steps - Use protolint action which shows inline comments in PR diffs - Job for testing of new mkdocs documentation - Job for publishing of new mkdocs documentation Signed-off-by: Leandro Lucarella <[email protected]>
1 parent e392bb3 commit c63ec26

File tree

1 file changed

+177
-56
lines changed

1 file changed

+177
-56
lines changed

.github/workflows/ci.yaml

Lines changed: 177 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
1-
name: frequenz-api-dispatch
1+
name: CI
22

3-
on: [pull_request, push]
3+
on:
4+
merge_group:
5+
pull_request:
6+
push:
7+
# We need to explicitly include tags because otherwise when adding
8+
# `branches-ignore` it will only trigger on branches.
9+
tags:
10+
- '*'
11+
branches-ignore:
12+
# Ignore pushes to merge queues.
13+
# We only want to test the merge commit (`merge_group` event), the hashes
14+
# in the push were already tested by the PR checks
15+
- 'gh-readonly-queue/**'
16+
workflow_dispatch:
417

5-
# Ensure old CI runs still being in progress are cancelled.
6-
concurrency:
7-
group: ${{ github.ref }}
8-
cancel-in-progress: true
18+
env:
19+
# Please make sure this version is included in the `matrix`, as the
20+
# `matrix` section can't use `env`, so it must be entered manually
21+
DEFAULT_PYTHON_VERSION: '3.11'
22+
# It would be nice to be able to also define a DEFAULT_UBUNTU_VERSION
23+
# but sadly `env` can't be used either in `runs-on`.
924

1025
jobs:
1126
protolint:
27+
name: Check proto files with protolint
1228
runs-on: ubuntu-20.04
1329

1430
steps:
@@ -17,51 +33,30 @@ jobs:
1733
with:
1834
submodules: true
1935

20-
- name: Get protolint
21-
run: |
22-
# checksum taken from: https://github.com/yoheimuta/protolint/releases/download/v0.44.0/checksums.txt
23-
echo "33627c1fd4392edc9363b414651f60692286c27e54424fc535ebb373a47a3004 protolint_0.44.0_Linux_x86_64.tar.gz" > checksum.txt
24-
25-
curl -LO https://github.com/yoheimuta/protolint/releases/download/v0.44.0/protolint_0.44.0_Linux_x86_64.tar.gz \
26-
&& sha256sum --check checksum.txt \
27-
&& tar xf protolint_0.44.0_Linux_x86_64.tar.gz \
28-
&& chmod +x protolint \
29-
&& sudo mv protolint /usr/local/bin/protolint
30-
3136
- name: Run protolint
32-
run: protolint lint proto
33-
34-
gen-docs:
35-
runs-on: ubuntu-20.04
36-
37-
steps:
38-
- name: Fetch sources
39-
uses: actions/checkout@v3
37+
# Only use hashes here, as we are passing the github token, we want to
38+
# make sure updates are done consciously to avoid security issues if the
39+
# action repo gets hacked
40+
uses: yoheimuta/action-protolint@e94cc01b1ad085ed9427098442f66f2519c723eb # v1.0.0
4041
with:
41-
submodules: true
42+
fail_on_error: true
43+
filter_mode: nofilter
44+
github_token: ${{ secrets.github_token }}
45+
protolint_flags: proto/
46+
protolint_version: "0.45.0"
47+
reporter: github-check
4248

43-
- name: Generate documentation
44-
run: |
45-
docker run --rm -v $PWD:$PWD pseudomuto/protoc-gen-doc \
46-
-I $PWD/proto \
47-
-I $PWD/submodules/api-common-protos \
48-
-I $PWD/submodules/frequenz-api-common/proto \
49-
--doc_opt=markdown,gen-docs.md --doc_out=$PWD \
50-
$(find $PWD/proto -name *.proto)
51-
52-
- name: Write summary
53-
run: cat gen-docs.md > $GITHUB_STEP_SUMMARY
54-
55-
tests:
56-
needs: ["protolint"]
49+
nox:
50+
name: Test with nox
5751
strategy:
5852
fail-fast: false
5953
matrix:
6054
os:
6155
- ubuntu-20.04
62-
python-version:
56+
python:
6357
- "3.11"
6458
runs-on: ${{ matrix.os }}
59+
6560
steps:
6661
- name: Fetch sources
6762
uses: actions/checkout@v3
@@ -71,16 +66,22 @@ jobs:
7166
- name: Set up Python
7267
uses: actions/setup-python@v4
7368
with:
74-
python-version: ${{ matrix.python-version }}
69+
python-version: ${{ matrix.python }}
70+
cache: 'pip'
7571

7672
- name: Install required Python packages
7773
run: |
7874
python -m pip install --upgrade pip
79-
python -m pip install .[dev-noxfile]
75+
python -m pip install -e .[dev-noxfile]
76+
8077
- name: Run nox
81-
run: nox
78+
# To speed things up a bit we use the special ci_checks_max session
79+
# that uses the same venv to run multiple linting sessions
80+
run: nox -e ci_checks_max pytest_min
81+
timeout-minutes: 10
8282

83-
build-dist:
83+
build:
84+
name: Build distribution packages
8485
runs-on: ubuntu-20.04
8586
steps:
8687
- name: Fetch sources
@@ -91,38 +92,158 @@ jobs:
9192
- name: Set up Python
9293
uses: actions/setup-python@v4
9394
with:
94-
python-version: "3.11"
95+
python-version: ${{ env.DEFAULT_PYTHON_VERSION }}
96+
cache: 'pip'
9597

96-
- name: Install build dependencies
98+
- name: Install required Python packages
9799
run: |
98100
python -m pip install -U pip
99101
python -m pip install -U build
100102
101103
- name: Build the source and binary distribution
102104
run: python -m build
103105

104-
- name: Upload dist files
106+
- name: Upload distribution files
105107
uses: actions/upload-artifact@v3
106108
with:
107-
name: frequenz-api-dispatch-dist
109+
name: dist-packages
108110
path: dist/
109111
if-no-files-found: error
110112

113+
test-docs:
114+
name: Test documentation website generation
115+
if: github.event_name != 'push'
116+
runs-on: ubuntu-20.04
117+
steps:
118+
- name: Fetch sources
119+
uses: actions/checkout@v3
120+
with:
121+
submodules: true
122+
123+
- name: Setup Git user and e-mail
124+
uses: frequenz-floss/setup-git-user@v2
125+
126+
- name: Set up Python
127+
uses: actions/setup-python@v4
128+
with:
129+
python-version: ${{ env.DEFAULT_PYTHON_VERSION }}
130+
cache: 'pip'
131+
132+
- name: Install build dependencies
133+
run: |
134+
python -m pip install -U pip
135+
python -m pip install .[dev-mkdocs]
136+
137+
- name: Generate the documentation
138+
env:
139+
MIKE_VERSION: gh-${{ github.job }}
140+
run: |
141+
mike deploy $MIKE_VERSION
142+
mike set-default $MIKE_VERSION
143+
144+
- name: Upload site
145+
uses: actions/upload-artifact@v3
146+
with:
147+
name: docs-site
148+
path: site/
149+
if-no-files-found: error
150+
151+
publish-docs:
152+
name: Publish documentation website to GitHub pages
153+
needs: ["nox", "build", "protolint"]
154+
if: github.event_name == 'push'
155+
runs-on: ubuntu-20.04
156+
permissions:
157+
contents: write
158+
steps:
159+
- name: Calculate and check version
160+
id: mike-metadata
161+
env:
162+
REF: ${{ github.ref }}
163+
REF_NAME: ${{ github.ref_name }}
164+
DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
165+
run: |
166+
aliases=
167+
version=
168+
if test "$REF_NAME" = "$DEFAULT_BRANCH"
169+
then
170+
version=next
171+
# A tag that starts with vX.Y or X.Y
172+
elif echo "$REF" | grep -q '^refs/tags' && echo "$REF_NAME" | grep -Pq '^v?\d+\.\d+\.'
173+
then
174+
if echo "$REF_NAME" | grep -Pq -- "-" # pre-release
175+
then
176+
echo "::notice title=Documentation was not published::" \
177+
"The tag '$REF_NAME' looks like a pre-release."
178+
exit 0
179+
fi
180+
version=$(echo "$REF_NAME" | sed -r 's/^(v?[0-9]+\.[0-9]+)\..*$/\1/') # vX.Y
181+
major=$(echo "$REF_NAME" | sed -r 's/^(v?[0-9]+)\..*$/\1/') # vX
182+
default_major=$(echo "$DEFAULT_BRANCH" | sed -r 's/^(v?[0-9]+)\..*$/\1/') # vX
183+
aliases=$major
184+
if test "$major" = "$default_major"
185+
then
186+
aliases="$aliases latest"
187+
fi
188+
else
189+
echo "::warning title=Documentation was not published::" \
190+
"Don't know how to handle '$REF' to make 'mike' version."
191+
exit 0
192+
fi
193+
echo "version=$version" >> $GITHUB_OUTPUT
194+
echo "aliases=$aliases" >> $GITHUB_OUTPUT
195+
196+
- name: Fetch sources
197+
if: steps.mike-metadata.outputs.version
198+
uses: actions/checkout@v3
199+
with:
200+
submodules: true
201+
202+
- name: Setup Git user and e-mail
203+
if: steps.mike-metadata.outputs.version
204+
uses: frequenz-floss/setup-git-user@v2
205+
206+
- name: Set up Python
207+
if: steps.mike-metadata.outputs.version
208+
uses: actions/setup-python@v4
209+
with:
210+
python-version: ${{ env.DEFAULT_PYTHON_VERSION }}
211+
cache: 'pip'
212+
213+
- name: Install build dependencies
214+
if: steps.mike-metadata.outputs.version
215+
run: |
216+
python -m pip install -U pip
217+
python -m pip install .[dev-mkdocs]
218+
219+
- name: Fetch the gh-pages branch
220+
if: steps.mike-metadata.outputs.version
221+
run: git fetch origin gh-pages --depth=1
222+
223+
- name: Publish site
224+
if: steps.mike-metadata.outputs.version
225+
env:
226+
VERSION: ${{ steps.mike-metadata.outputs.version }}
227+
ALIASES: ${{ steps.mike-metadata.outputs.aliases }}
228+
run: |
229+
mike deploy --push --update-aliases "$VERSION" $ALIASES
230+
111231
create-github-release:
112-
needs: ["protolint", "gen-docs", "tests", "build-dist"]
232+
name: Create GitHub release
233+
needs: ["publish-docs"]
113234
# Create a release only on tags creation
114-
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
235+
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')
115236
permissions:
116237
# We need write permissions on contents to create GitHub releases and on
117238
# discussions to create the release announcement in the discussion forums
118239
contents: write
119240
discussions: write
120241
runs-on: ubuntu-20.04
121242
steps:
122-
- name: Download dist files
243+
- name: Download distribution files
123244
uses: actions/download-artifact@v3
124245
with:
125-
name: frequenz-api-dispatch-dist
246+
name: dist-packages
126247
path: dist
127248

128249
- name: Download RELEASE_NOTES.md
@@ -146,7 +267,6 @@ jobs:
146267
if echo "$REF_NAME" | grep -- -; then extra_opts=" --prerelease"; fi
147268
gh release create \
148269
-R "$REPOSITORY" \
149-
--discussion-category announcements \
150270
--notes-file RELEASE_NOTES.md \
151271
--generate-notes \
152272
$extra_opts \
@@ -158,17 +278,18 @@ jobs:
158278
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
159279

160280
publish-to-pypi:
281+
name: Publish packages to PyPI
161282
needs: ["create-github-release"]
162283
runs-on: ubuntu-20.04
163284
permissions:
164285
# For trusted publishing. See:
165286
# https://blog.pypi.org/posts/2023-04-20-introducing-trusted-publishers/
166287
id-token: write
167288
steps:
168-
- name: Download dist files
289+
- name: Download distribution files
169290
uses: actions/download-artifact@v3
170291
with:
171-
name: frequenz-api-dispatch-dist
292+
name: dist-packages
172293
path: dist
173294

174295
- name: Publish the Python distribution to PyPI

0 commit comments

Comments
 (0)