Skip to content

Commit b4ceb78

Browse files
committed
Verify tag matches version before publishing
1 parent 749360e commit b4ceb78

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

.github/check-version.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env python
2+
"""
3+
Check the version poetry is building matches the version from `GITHUB_REF` environment variable.
4+
"""
5+
import os
6+
import re
7+
import subprocess
8+
import sys
9+
10+
11+
def main() -> int:
12+
version_ref = os.getenv('GITHUB_REF')
13+
if version_ref:
14+
version = re.sub('^refs/tags/v*', '', version_ref.lower())
15+
else:
16+
exit('✖ "GITHUB_REF" env variables not found')
17+
18+
project_version = subprocess.check_output(['poetry', 'version', '--short'], encoding='utf-8').strip()
19+
20+
if project_version == version:
21+
print(f'✓ GITHUB_REF matches version {project_version!r}')
22+
return 0
23+
else:
24+
print(f'✖ GITHUB_REF version {version!r} does not poetry version {project_version!r}')
25+
return 1
26+
27+
28+
if __name__ == '__main__':
29+
sys.exit(main())

.github/workflows/ci.yml

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ name: Test
33
on:
44
push:
55
branches: [ main ]
6+
tags:
7+
- '**'
68
pull_request:
79
branches: [ main ]
810

9-
1011
env:
1112
PRE_COMMIT_COLOR: always
1213

@@ -160,15 +161,7 @@ jobs:
160161
cache: 'poetry'
161162

162163
- name: check package version
163-
run: |
164-
set -euo pipefail
165-
package_ver="$(poetry version --short)"
166-
if [[ "refs/tags/v${package_ver}" == "$GITHUB_REF" ]]
167-
echo "✓ GITHUB_REF version matches ${package_ver}"
168-
else
169-
echo >&2 "✖ GITHUB_REF '${GITHUB_REF}' does not match package version '${package_ver}'
170-
exit 1
171-
fi
164+
run: .github/check-version.py
172165

173166
- name: get dist artifacts
174167
uses: actions/download-artifact@v3

0 commit comments

Comments
 (0)