1- name : Publish Python Package
1+ name : Continuous Delivery
22
33on :
44 push :
5- tags :
6- - " v*.*.*"
5+ branches :
6+ - main
7+ - rc
78
89jobs :
910 publish :
1011 runs-on : ubuntu-latest
12+ concurrency :
13+ group : ${{ github.workflow }}-release-${{ github.ref_name }}
14+ cancel-in-progress : false
1115 environment : pypi
1216 permissions :
1317 id-token : write
1620 steps :
1721 - name : Checkout repository
1822 uses : actions/checkout@v4
23+ with :
24+ ref : ${{ github.ref_name }}
25+ fetch-depth : 0
26+
27+ - name : Setup | Force release branch to be at workflow sha
28+ run : |
29+ git reset --hard ${{ github.sha }}
1930
2031 - name : Set up Python
2132 uses : actions/setup-python@v5
@@ -32,19 +43,61 @@ jobs:
3243 virtualenvs-create : false
3344 installer-parallel : true
3445
35- - name : Build package
36- run : poetry build
46+ - name : Evaluate | Verify upstream has NOT changed
47+ # Last chance to abort before causing an error as another PR/push was applied to
48+ # the upstream branch while this workflow was running. This is important
49+ # because we are committing a version change (--commit). You may omit this step
50+ # if you have 'commit: false' in your configuration.
51+ #
52+ # You may consider moving this to a repo script and call it from this step instead
53+ # of writing it in-line.
54+ shell : bash
55+ run : |
56+ set +o pipefail
3757
38- - name : Publish to PyPI
39- uses : pypa/gh-action-pypi-publish@release/v1
58+ UPSTREAM_BRANCH_NAME="$(git status -sb | head -n 1 | cut -d' ' -f2 | grep -E '\.{3}' | cut -d'.' -f4)"
59+ printf '%s\n' "Upstream branch name: $UPSTREAM_BRANCH_NAME"
60+
61+ set -o pipefail
62+
63+ if [ -z "$UPSTREAM_BRANCH_NAME" ]; then
64+ printf >&2 '%s\n' "::error::Unable to determine upstream branch name!"
65+ exit 1
66+ fi
67+
68+ git fetch "${UPSTREAM_BRANCH_NAME%%/*}"
69+
70+ if ! UPSTREAM_SHA="$(git rev-parse "$UPSTREAM_BRANCH_NAME")"; then
71+ printf >&2 '%s\n' "::error::Unable to determine upstream branch sha!"
72+ exit 1
73+ fi
4074
41- - name : Upload package to GitHub Releases
42- uses : softprops/action-gh-release@v1
75+ HEAD_SHA="$(git rev-parse HEAD)"
76+
77+ if [ "$HEAD_SHA" != "$UPSTREAM_SHA" ]; then
78+ printf >&2 '%s\n' "[HEAD SHA] $HEAD_SHA != $UPSTREAM_SHA [UPSTREAM SHA]"
79+ printf >&2 '%s\n' "::error::Upstream has changed, aborting release..."
80+ exit 1
81+ fi
82+
83+ printf '%s\n' "Verified upstream branch has not changed, continuing with release..."
84+
85+ - name : Action | Semantic Version Release
86+ id : release
87+ # Adjust tag with desired version if applicable.
88+ uses : python-semantic-release/python-semantic-release@v9.21.1
89+ with :
90+ github_token : ${{ secrets.GITHUB_TOKEN }}
91+ git_committer_name : " github-actions"
92+ git_committer_email : " actions@users.noreply.github.com"
93+
94+ - name : Publish | Upload to GitHub Release Assets
95+ uses : python-semantic-release/publish-action@v9.21.1
96+ if : steps.release.outputs.released == 'true'
4397 with :
44- tag_name : ${{ github.ref }}
45- generate_release_notes : true
46- files : |
47- dist/*.tar.gz
48- dist/*.whl
49- env :
50- GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
98+ github_token : ${{ secrets.GITHUB_TOKEN }}
99+ tag : ${{ steps.release.outputs.tag }}
100+
101+ - name : Publish to PyPI
102+ uses : pypa/gh-action-pypi-publish@release/v1
103+ if : steps.release.outputs.released == 'true'
0 commit comments