-
Notifications
You must be signed in to change notification settings - Fork 3.4k
83 lines (77 loc) · 2.59 KB
/
release-tag.yml
File metadata and controls
83 lines (77 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
name: Tag release on merge
on:
pull_request:
types:
- closed
branches:
- main
permissions:
contents: write
jobs:
tag-release:
if: >-
github.event.pull_request.merged == true &&
startsWith(github.event.pull_request.head.ref, 'release/v')
runs-on: ubuntu-latest
steps:
- name: Validate merge commit
env:
MERGE_SHA: ${{ github.event.pull_request.merge_commit_sha }}
run: |
if [ -z "$MERGE_SHA" ]; then
echo "merge_commit_sha is empty; refusing to tag to avoid tagging the wrong commit." >&2
exit 1
fi
- name: Checkout merge commit
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd
with:
fetch-depth: 0
ref: ${{ github.event.pull_request.merge_commit_sha }}
- name: Setup Python
uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405
with:
python-version: "3.11"
- name: Configure git
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
- name: Fetch tags
run: git fetch origin --tags --prune
- name: Resolve version
id: version
env:
HEAD_REF: ${{ github.event.pull_request.head.ref }}
run: |
python - <<'PY'
import os
import pathlib
import sys
import tomllib
path = pathlib.Path("pyproject.toml")
data = tomllib.loads(path.read_text())
version = data.get("project", {}).get("version")
if not version:
print("Missing project.version in pyproject.toml.", file=sys.stderr)
sys.exit(1)
head_ref = os.environ.get("HEAD_REF", "")
if head_ref.startswith("release/v"):
expected = head_ref[len("release/v") :]
if expected != version:
print(
f"Version mismatch: branch {expected} vs pyproject {version}.",
file=sys.stderr,
)
sys.exit(1)
output_path = pathlib.Path(os.environ["GITHUB_OUTPUT"])
output_path.write_text(f"version={version}\n")
PY
- name: Create tag
env:
VERSION: ${{ steps.version.outputs.version }}
run: |
if git tag -l "v${VERSION}" | grep -q "v${VERSION}"; then
echo "Tag v${VERSION} already exists; skipping."
exit 0
fi
git tag -a "v${VERSION}" -m "Release v${VERSION}"
git push origin "v${VERSION}"