Repository navigation
176 lines (154 loc) · 5.66 KB
/
Copy pathrelease.yml
File metadata and controls
176 lines (154 loc) · 5.66 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
name: Release
on:
push:
branches: [main]
paths-ignore:
- "*.md"
- "docs/**"
- "standards/**"
- "LICENSE"
- ".github/release-drafter.yml"
workflow_dispatch:
permissions:
contents: read
concurrency:
group: release-${{ github.workflow }}
cancel-in-progress: false
jobs:
check-version:
name: Check VERSION vs latest tag
runs-on: ubuntu-latest
if: "!contains(github.event.head_commit.message, '[skip ci]')"
outputs:
should-release: ${{ steps.compare.outputs.should-release }}
version: ${{ steps.compare.outputs.version }}
tag: ${{ steps.compare.outputs.tag }}
previous-tag: ${{ steps.compare.outputs.previous-tag }}
steps:
- uses: actions/checkout@v5
with:
fetch-depth: 0
- name: Read VERSION and compare with latest tag
id: compare
shell: bash
run: |
set -euo pipefail
if [ ! -f VERSION ]; then
echo "::error::VERSION file is missing at repo root"
exit 1
fi
VERSION="$(tr -d ' \t\r\n' < VERSION)"
if [ -z "$VERSION" ]; then
echo "::error::VERSION file is empty"
exit 1
fi
if ! echo "$VERSION" | grep -qE '^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?(\+[0-9A-Za-z.-]+)?$'; then
echo "::error::VERSION is not a valid semver: '$VERSION'"
exit 1
fi
LATEST_TAG="$(git describe --tags --abbrev=0 2>/dev/null || echo '')"
LATEST_VERSION="${LATEST_TAG#v}"
echo "VERSION file: $VERSION"
echo "Latest tag: ${LATEST_TAG:-<none>}"
if [ -z "$LATEST_TAG" ]; then
echo "No prior tag; releasing v$VERSION"
{
echo "should-release=true"
echo "version=$VERSION"
echo "tag=v$VERSION"
echo "previous-tag="
} >> "$GITHUB_OUTPUT"
exit 0
fi
if [ "$VERSION" = "$LATEST_VERSION" ]; then
echo "VERSION matches latest tag. No release needed."
{
echo "should-release=false"
echo "version=$VERSION"
echo "tag=v$VERSION"
echo "previous-tag=$LATEST_TAG"
} >> "$GITHUB_OUTPUT"
exit 0
fi
HIGHEST="$(printf '%s\n%s\n' "$VERSION" "$LATEST_VERSION" | sort -V | tail -n1)"
if [ "$HIGHEST" != "$VERSION" ]; then
echo "::error::VERSION ($VERSION) is lower than the latest tag ($LATEST_TAG). VERSION must not decrease; to force, delete the newer tag first."
exit 1
fi
if git rev-parse --verify "refs/tags/v$VERSION" >/dev/null 2>&1; then
echo "::error::Tag v$VERSION already exists but does not match latest tag ($LATEST_TAG). Resolve tag history before proceeding."
exit 1
fi
echo "VERSION is ahead of latest tag. Proceeding to release."
{
echo "should-release=true"
echo "version=$VERSION"
echo "tag=v$VERSION"
echo "previous-tag=$LATEST_TAG"
} >> "$GITHUB_OUTPUT"
release:
name: Tag and release
needs: check-version
if: needs.check-version.outputs.should-release == 'true'
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: read
steps:
- uses: actions/checkout@v5
with:
fetch-depth: 0
- name: Create annotated tag
env:
TAG: ${{ needs.check-version.outputs.tag }}
run: |
set -euo pipefail
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: Assemble release notes
id: notes
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
TAG: ${{ needs.check-version.outputs.tag }}
PREV_TAG: ${{ needs.check-version.outputs.previous-tag }}
shell: bash
run: |
set -euo pipefail
NOTES_FILE="$(mktemp)"
# Option B: prefer release-drafter's current draft body if present.
DRAFT_BODY="$(gh api "repos/${GITHUB_REPOSITORY}/releases" \
--jq 'map(select(.draft==true)) | .[0].body // ""' 2>/dev/null || echo '')"
if [ -n "$DRAFT_BODY" ] && [ "$(echo "$DRAFT_BODY" | tr -d '[:space:]')" != "" ]; then
echo "Using release-drafter draft body."
echo "$DRAFT_BODY" > "$NOTES_FILE"
elif [ -n "$PREV_TAG" ]; then
echo "No drafter draft. Falling back to commit log since $PREV_TAG."
{
echo "## Changes since $PREV_TAG"
echo ""
git log "$PREV_TAG..HEAD" --pretty=format:'- %s (%h)' --no-merges
echo ""
echo ""
echo "**Full Changelog**: https://github.com/${GITHUB_REPOSITORY}/compare/${PREV_TAG}...${TAG}"
} > "$NOTES_FILE"
else
echo "No prior tag and no drafter draft. Using generic first-release notes."
{
echo "## $TAG"
echo ""
echo "Initial tagged release."
} > "$NOTES_FILE"
fi
echo "notes-file=$NOTES_FILE" >> "$GITHUB_OUTPUT"
- name: Create GitHub Release
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
TAG: ${{ needs.check-version.outputs.tag }}
NOTES_FILE: ${{ steps.notes.outputs.notes-file }}
run: |
set -euo pipefail
gh release create "$TAG" \
--title "$TAG" \
--notes-file "$NOTES_FILE"