-
Notifications
You must be signed in to change notification settings - Fork 1
110 lines (91 loc) · 3.28 KB
/
auto-release.yml
File metadata and controls
110 lines (91 loc) · 3.28 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
name: Auto Release
on:
workflow_run:
workflows: ["CI"]
branches: [main]
types: [completed]
workflow_dispatch:
permissions:
actions: write
contents: write
concurrency:
group: auto-release
cancel-in-progress: false
jobs:
tag:
name: Tag new project version
if: github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success'
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@v6
with:
fetch-depth: 0
ref: ${{ github.event.workflow_run.head_sha || github.ref }}
- name: Read project version
id: version
run: |
python - <<'PY' >> "$GITHUB_OUTPUT"
import re
import tomllib
with open("pyproject.toml", "rb") as file:
version = tomllib.load(file)["project"]["version"]
if not re.fullmatch(r"\d+\.\d+\.\d+(?:[a-zA-Z0-9.+-]+)?", version):
raise SystemExit(f"Unsupported release version: {version}")
print(f"version={version}")
print(f"tag=v{version}")
PY
- name: Check whether tag already exists
id: tag
env:
TAG: ${{ steps.version.outputs.tag }}
run: |
if git rev-parse -q --verify "refs/tags/${TAG}" >/dev/null; then
echo "exists=true" >> "$GITHUB_OUTPUT"
exit 0
fi
if git ls-remote --exit-code --tags origin "refs/tags/${TAG}" >/dev/null 2>&1; then
echo "exists=true" >> "$GITHUB_OUTPUT"
exit 0
fi
echo "exists=false" >> "$GITHUB_OUTPUT"
- name: Ensure version moves forward
if: steps.tag.outputs.exists == 'false'
env:
VERSION: ${{ steps.version.outputs.version }}
run: |
latest_tag="$(git tag -l 'v[0-9]*' --sort=-v:refname | head -n 1)"
if [ -z "${latest_tag}" ]; then
exit 0
fi
LATEST_TAG="${latest_tag}" python - <<'PY'
import os
import re
def parse(version: str) -> tuple[int, int, int]:
match = re.fullmatch(r"v?(\d+)\.(\d+)\.(\d+)(?:[a-zA-Z0-9.+-]+)?", version)
if not match:
raise SystemExit(f"Unsupported version tag: {version}")
return tuple(int(part) for part in match.groups())
current = parse(os.environ["VERSION"])
latest = parse(os.environ["LATEST_TAG"])
if current <= latest:
raise SystemExit(
f"Refusing to tag v{os.environ['VERSION']}: latest tag is "
f"{os.environ['LATEST_TAG']}"
)
PY
- name: Create and push release tag
if: steps.tag.outputs.exists == 'false'
env:
TAG: ${{ steps.version.outputs.tag }}
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git tag -a "${TAG}" -m "Release ${TAG}"
git push origin "${TAG}"
- name: Dispatch release workflow
if: steps.tag.outputs.exists == 'false'
env:
GH_TOKEN: ${{ github.token }}
TAG: ${{ steps.version.outputs.tag }}
run: gh workflow run release.yml --ref "${TAG}"