-
Notifications
You must be signed in to change notification settings - Fork 0
111 lines (101 loc) · 4.51 KB
/
Copy pathtag-release.yml
File metadata and controls
111 lines (101 loc) · 4.51 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
111
name: Tag and draft release
# Cuts a release for the version on main: builds release notes (from the merged
# release PR body, falling back to auto-generated notes), creates a *draft*
# GitHub Release, then pushes the version tag. The tag push triggers the
# dist-generated release.yml, which builds the cross-platform binaries and
# installers, uploads them to the draft, and undrafts it. The draft must exist
# before the tag lands — dist uploads into it rather than creating its own.
on:
push:
branches: [main]
jobs:
release:
name: Tag and draft GitHub release
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
# Push the tag as the PAT, not the default GITHUB_TOKEN: tag pushes made
# with GITHUB_TOKEN do not trigger other workflows, so the dist
# release.yml (push: tags) would never fire. Same token release-pr.yml
# uses so the PRs it opens trigger CI.
token: ${{ secrets.RELEASE_PR_TOKEN }}
- name: Read version from Cargo.toml
id: version
run: |
VERSION=$(grep -m1 '^version = ' Cargo.toml | sed -E 's/version = "(.*)"/\1/')
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
echo "tag=v$VERSION" >> "$GITHUB_OUTPUT"
echo "Version on main: $VERSION"
- name: Check if tag already exists (idempotency)
id: tag-check
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
if git rev-parse "${{ steps.version.outputs.tag }}" >/dev/null 2>&1; then
echo "Tag ${{ steps.version.outputs.tag }} already exists — skipping release."
echo "exists=true" >> "$GITHUB_OUTPUT"
else
echo "exists=false" >> "$GITHUB_OUTPUT"
# A draft without a tag means a previous run failed between
# draft-create and tag-push; delete it so this run starts clean.
if gh release view "${{ steps.version.outputs.tag }}" --json isDraft --jq '.isDraft' 2>/dev/null | grep -q true; then
echo "Deleting stale draft release for ${{ steps.version.outputs.tag }}."
gh release delete "${{ steps.version.outputs.tag }}" --yes
fi
fi
- name: Build release notes
if: steps.tag-check.outputs.exists == 'false'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
PR_BODY=$(gh pr list \
--state merged \
--base main \
--limit 1 \
--json body \
--jq '.[0].body // ""')
if [ -n "$PR_BODY" ] && ! echo "$PR_BODY" | grep -q "release-notes:todo"; then
echo "Using release PR body for notes."
printf '%s\n' "$PR_BODY" > /tmp/release-notes.md
else
echo "Falling back to auto-generated notes."
PREVIOUS_TAG=$(git tag --sort=-creatordate \
| grep -v "^${{ steps.version.outputs.tag }}$" \
| head -n1)
if [ -n "$PREVIOUS_TAG" ]; then
gh api "repos/${{ github.repository }}/releases/generate-notes" \
-f tag_name="${{ steps.version.outputs.tag }}" \
-f previous_tag_name="$PREVIOUS_TAG" \
--jq '.body' > /tmp/release-notes.md
else
gh api "repos/${{ github.repository }}/releases/generate-notes" \
-f tag_name="${{ steps.version.outputs.tag }}" \
--jq '.body' > /tmp/release-notes.md
fi
fi
echo "--- Release notes preview ---"
cat /tmp/release-notes.md
echo "--- end preview ---"
- name: Create draft GitHub release
if: steps.tag-check.outputs.exists == 'false'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh release create "${{ steps.version.outputs.tag }}" \
--draft \
--target "$GITHUB_SHA" \
--title "eval-magic ${{ steps.version.outputs.tag }}" \
--notes-file /tmp/release-notes.md
# Last on purpose: the tag push triggers the dist release workflow, which
# expects the draft above to already exist.
- name: Create and push tag
if: steps.tag-check.outputs.exists == 'false'
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git tag "${{ steps.version.outputs.tag }}"
git push origin "${{ steps.version.outputs.tag }}"