1010 branches : [ "main" ]
1111
1212permissions :
13- id-token : write
1413 contents : read
1514
1615jobs :
@@ -33,96 +32,110 @@ jobs:
3332 fail-fast : false
3433 matrix :
3534 os : [ubuntu-latest]
36- python-version : ["3.9", "3. 10", "3.11", "3.12", "3.13"]
35+ python-version : ["3.10", "3.11", "3.12", "3.13"]
3736 env :
3837 package : ${{ needs.prepare.outputs.package }}
38+ cov_python_version : " 3.13"
3939 steps :
40- - uses : actions/checkout@v5
40+ - uses : actions/checkout@v7
4141 - name : Set up Python ${{ matrix.python-version }}
42- uses : actions/setup-python@v6
42+ uses : actions/setup-python@v7
4343 with :
4444 python-version : ${{ matrix.python-version }}
4545 - name : Install ${{ env.package }}
4646 run : |
4747 python -m pip install --upgrade pip
48- python -m pip install pytest pytest-cov coverage
48+ python -m pip install pytest pytest-cov
4949 pip install -r requirements.txt
50- pip install tinyscript>=1.31
50+ pip install " tinyscript>=1.31"
5151 pip install .
5252 - name : Test ${{ env.package }} with pytest
5353 run : |
54- pytest --cov=$package
54+ pytest --cov=$package --cov-report=xml
55+ - name : Upload coverage report
56+ if : matrix.python-version == env.cov_python_version
57+ uses : actions/upload-artifact@v7
58+ with :
59+ name : coverage-report
60+ path : coverage.xml
5561 coverage :
56- needs : [prepare, build]
62+ # only publish the badge for commits on main (not for pull requests)
63+ if : github.event_name == 'push' && github.ref == 'refs/heads/main'
64+ needs : build
5765 permissions :
5866 contents : write
5967 runs-on : ubuntu-latest
6068 env :
6169 cov_badge_path : docs/coverage.svg
62- package : ${{ needs.prepare.outputs.package }}
63- python_version : " 3.13"
6470 steps :
65- - uses : actions/checkout@v5
71+ - uses : actions/checkout@v7
72+ - name : Set up Python
73+ uses : actions/setup-python@v7
6674 with :
67- fetch-depth : 0
68- ref : ${{ (github.event_name == 'pull_request' || github.event_name == 'pull_request_target') && github.event.pull_request.head.sha || github.sha }}
69- - name : Set up Python ${{ env.python_version }}
70- uses : actions/setup-python@v6
75+ python-version : " 3.13"
76+ - name : Download coverage report
77+ uses : actions/download-artifact@v8
7178 with :
72- python-version : ${{ env.python_version }}
73- - name : Install ${{ env.package }}
74- run : |
75- python -m pip install --upgrade pip
76- python -m pip install pytest pytest-cov
77- pip install -r requirements.txt
78- pip install .
79- - name : Make coverage badge for ${{ env.package }}
79+ name : coverage-report
80+ - name : Make coverage badge
8081 run : |
8182 pip install genbadge[coverage]
82- pytest --cov=$package --cov-report=xml
8383 genbadge coverage -i coverage.xml -o $cov_badge_path
84- - name : Verify Changed files
85- uses : tj-actions/verify-changed-files@v20
86- id : changed_files
87- with :
88- files : ${{ env.cov_badge_path }}
8984 - name : Push coverage badge
90- if : steps.changed_files.outputs.files_changed == 'true'
9185 run : |
86+ # keep the badge alone on an orphan branch so that main is never modified by the workflow
9287 git config --local user.email "github-actions[bot]@users.noreply.github.com"
9388 git config --local user.name "github-actions[bot]"
94- git fetch origin
95- git checkout coverage-badge || git checkout -b coverage-badge
89+ if git fetch --depth=1 origin coverage-badge 2>/dev/null && \
90+ git show FETCH_HEAD:$cov_badge_path | cmp -s - $cov_badge_path; then
91+ echo "Coverage badge unchanged"
92+ exit 0
93+ fi
94+ git checkout --orphan coverage-badge
95+ git rm -rqf --cached .
9696 git add $cov_badge_path
97- git diff --cached --quiet && exit 0
9897 git commit -m "Update coverage badge"
9998 git push origin coverage-badge --force
10099 deploy :
100+ # only release from main, never from pull requests
101+ if : github.event_name == 'push' && github.ref == 'refs/heads/main'
101102 environment :
102103 name : pypi
103104 url : ${{ needs.prepare.outputs.pypi_url }}
105+ permissions :
106+ contents : read
107+ id-token : write # for PyPI trusted publishing (OIDC)
104108 runs-on : ubuntu-latest
105- needs : [prepare, coverage]
109+ needs : [prepare, build]
110+ env :
111+ package : ${{ needs.prepare.outputs.package }}
106112 steps :
107- - uses : actions/checkout@v5
108- with :
109- fetch-depth : 0
110- - name : Check for version change
111- uses : dorny/paths-filter@v4
112- id : filter
113+ - uses : actions/checkout@v7
114+ - name : Check whether the version is already on PyPI
115+ # comparing with PyPI (not with the previous commit) also releases a version whose push did not trigger a deploy
116+ id : version
117+ run : |
118+ version=$(tr -d '[:space:]' < src/$package/VERSION.txt)
119+ status=$(curl -s -o /dev/null -w '%{http_code}' --retry 3 "https://pypi.org/pypi/$package/$version/json")
120+ case "$status" in
121+ 200) echo "Version $version is already on PyPI" ; echo "new=false" >> "$GITHUB_OUTPUT" ;;
122+ 404) echo "Version $version is not on PyPI yet" ; echo "new=true" >> "$GITHUB_OUTPUT" ;;
123+ *) echo "::error::Unexpected HTTP status $status from PyPI" ; exit 1 ;;
124+ esac
125+ - if : steps.version.outputs.new == 'true'
126+ name : Set up Python
127+ uses : actions/setup-python@v7
113128 with :
114- filters : |
115- version:
116- - '**/VERSION.txt'
117- - if : steps.filter.outputs.version == 'true'
129+ python-version : " 3.13"
130+ - if : steps.version.outputs.new == 'true'
118131 name : Cleanup README
119132 run : |
120133 sed -ri 's/^(##*)\s*:.*:\s*/\1 /g' README.md
121134 awk '{if (match($0,"## Supporters")) exit; print}' README.md > README
122135 mv -f README README.md
123- - if : steps.filter .outputs.version == 'true'
136+ - if : steps.version .outputs.new == 'true'
124137 name : Build ${{ needs.prepare.outputs.package }} package
125- run : python3 -m pip install --upgrade build && python3 -m build
126- - if : steps.filter .outputs.version == 'true'
138+ run : python -m pip install --upgrade build && python -m build
139+ - if : steps.version .outputs.new == 'true'
127140 name : Publish to PyPI
128141 uses : pypa/gh-action-pypi-publish@release/v1
0 commit comments