55 push :
66 tags :
77 - " v*" # Triggers on tags like v0.1.0, v1.2.3, etc.
8+ workflow_run :
9+ workflows : ["Release on Github"]
10+ types :
11+ - completed
12+ branches :
13+ - main
814 workflow_dispatch :
915 inputs :
1016 testpypi_only :
1622jobs :
1723 publish :
1824 runs-on : ubuntu-latest
25+ if : |
26+ github.event_name == 'workflow_run' && github.event.workflow_run.conclusion == 'success' ||
27+ github.event_name == 'push' ||
28+ github.event_name == 'workflow_dispatch'
1929 environment : pypi
2030 permissions :
2131 id-token : write
2535 uses : actions/checkout@v5
2636 with :
2737 fetch-depth : 0
38+ ref : ${{ github.event.workflow_run.head_branch || github.ref }}
39+ token : ${{ secrets.GITHUB_TOKEN }}
40+
41+ - name : Check if new tag exists
42+ id : check_tag
43+ run : |
44+ # Fetch all tags
45+ git fetch --tags
46+ # Get the latest tag matching v* pattern
47+ LATEST_TAG=$(git tag -l "v*" --sort=-version:refname | head -n 1)
48+ if [ -z "$LATEST_TAG" ]; then
49+ echo "No tag matching v* pattern found"
50+ echo "tag_exists=false" >> $GITHUB_OUTPUT
51+ exit 0
52+ fi
53+
54+ echo "Found latest tag: $LATEST_TAG"
55+
56+ # For workflow_run events, verify tag is associated with this workflow run
57+ if [ "${{ github.event_name }}" = "workflow_run" ]; then
58+ WORKFLOW_COMMIT="${{ github.event.workflow_run.head_sha }}"
59+ TAG_COMMIT=$(git rev-list -n 1 "$LATEST_TAG")
60+
61+ # Check if tag commit is reachable from workflow commit (same or newer)
62+ # This handles cases where semantic-release creates a new commit or tags existing commit
63+ if git merge-base --is-ancestor "$WORKFLOW_COMMIT" "$TAG_COMMIT" 2>/dev/null || [ "$TAG_COMMIT" = "$WORKFLOW_COMMIT" ]; then
64+ echo "Tag $LATEST_TAG is associated with this workflow run"
65+ echo "tag_exists=true" >> $GITHUB_OUTPUT
66+ echo "tag=$LATEST_TAG" >> $GITHUB_OUTPUT
67+ else
68+ echo "Tag $LATEST_TAG is not associated with this workflow run (no new release created)"
69+ echo "tag_exists=false" >> $GITHUB_OUTPUT
70+ fi
71+ else
72+ # For push (on tag) or workflow_dispatch, assume tag exists
73+ echo "tag_exists=true" >> $GITHUB_OUTPUT
74+ echo "tag=$LATEST_TAG" >> $GITHUB_OUTPUT
75+ fi
2876
2977 - name : Set up Python
3078 uses : actions/setup-python@v6
@@ -40,15 +88,23 @@ jobs:
4088 run : poetry install --no-interaction --no-root
4189
4290 - name : Build package
91+ if : |
92+ github.event_name == 'push' ||
93+ github.event_name == 'workflow_dispatch' ||
94+ steps.check_tag.outputs.tag_exists == 'true'
4395 run : poetry build
4496
4597 - name : Publish to TestPyPI
46- if : github.event.inputs.testpypi_only == 'true'
98+ if : |
99+ (github.event_name == 'push' || github.event_name == 'workflow_dispatch' || steps.check_tag.outputs.tag_exists == 'true') &&
100+ github.event.inputs.testpypi_only == 'true'
47101 uses : pypa/gh-action-pypi-publish@release/v1
48102 with :
49103 repository-url : https://test.pypi.org/legacy/
50104
51105 - name : Publish to PyPI
52- if : github.event.inputs.testpypi_only != 'true'
106+ if : |
107+ (github.event_name == 'push' || github.event_name == 'workflow_dispatch' || steps.check_tag.outputs.tag_exists == 'true') &&
108+ github.event.inputs.testpypi_only != 'true'
53109 uses : pypa/gh-action-pypi-publish@release/v1
54110
0 commit comments