forked from elastic/terraform-provider-elasticstack
-
Notifications
You must be signed in to change notification settings - Fork 0
221 lines (206 loc) · 8.19 KB
/
Copy pathprep-release.yml
File metadata and controls
221 lines (206 loc) · 8.19 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
name: Prepare Release
on:
workflow_dispatch:
inputs:
bump:
description: 'Version bump mode'
required: true
default: 'patch'
type: choice
options:
- patch
- minor
- major
permissions:
contents: write
pull-requests: write
jobs:
prep-release:
name: Prepare release branch and PR
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
with:
fetch-tags: true
fetch-depth: 0
- name: Configure git
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
- name: Resolve previous release tag
id: prev-tag
run: |
PREV_TAG=$(git tag --list 'v[0-9]*.[0-9]*.[0-9]*' --sort=-version:refname | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | head -1)
if [ -z "$PREV_TAG" ]; then
PREV_TAG="v0.0.0"
echo "::warning::No existing tags matching 'v[0-9]*.[0-9]*.[0-9]*' found; defaulting to v0.0.0"
else
echo "Previous release tag: ${PREV_TAG}"
fi
echo "PREV_TAG=${PREV_TAG}" >> "$GITHUB_OUTPUT"
- name: Compute target version
id: version
run: |
PREV_TAG="${{ steps.prev-tag.outputs.PREV_TAG }}"
# Strip leading 'v'
VERSION="${PREV_TAG#v}"
# Split into components
MAJOR=$(echo "$VERSION" | cut -d. -f1)
MINOR=$(echo "$VERSION" | cut -d. -f2)
PATCH=$(echo "$VERSION" | cut -d. -f3)
BUMP="${{ inputs.bump }}"
case "$BUMP" in
patch)
PATCH=$((PATCH + 1))
;;
minor)
MINOR=$((MINOR + 1))
PATCH=0
;;
major)
MAJOR=$((MAJOR + 1))
MINOR=0
PATCH=0
;;
esac
TARGET_VERSION="${MAJOR}.${MINOR}.${PATCH}"
echo "Target version: ${TARGET_VERSION}"
echo "TARGET_VERSION=${TARGET_VERSION}" >> "$GITHUB_OUTPUT"
- name: Derive branch and PR title
id: meta
run: |
TARGET_VERSION="${{ steps.version.outputs.TARGET_VERSION }}"
BRANCH="prep-release-${TARGET_VERSION}"
PR_TITLE="Prepare ${TARGET_VERSION} release"
echo "BRANCH=${BRANCH}" >> "$GITHUB_OUTPUT"
echo "PR_TITLE=${PR_TITLE}" >> "$GITHUB_OUTPUT"
- name: Check if release branch already exists
id: branch-check
run: |
BRANCH="${{ steps.meta.outputs.BRANCH }}"
if git ls-remote --exit-code --heads origin "${BRANCH}" > /dev/null 2>&1; then
echo "Branch ${BRANCH} already exists on remote"
echo "EXISTS=true" >> "$GITHUB_OUTPUT"
else
echo "Branch ${BRANCH} does not exist on remote"
echo "EXISTS=false" >> "$GITHUB_OUTPUT"
fi
- name: Create or switch to release branch
run: |
BRANCH="${{ steps.meta.outputs.BRANCH }}"
EXISTS="${{ steps.branch-check.outputs.EXISTS }}"
if [ "$EXISTS" = "true" ]; then
git fetch origin "${BRANCH}"
git checkout "${BRANCH}"
git merge --no-edit origin/main || { git merge --abort; exit 1; }
else
git checkout main
git pull origin main
git checkout -b "${BRANCH}"
fi
- name: Apply version bump to Makefile
run: |
TARGET_VERSION="${{ steps.version.outputs.TARGET_VERSION }}"
sed -i "s/^VERSION ?= .*/VERSION ?= ${TARGET_VERSION}/" Makefile
if git diff --quiet Makefile; then
echo "Makefile already has VERSION ?= ${TARGET_VERSION}"
else
echo "Makefile updated: VERSION set to ${TARGET_VERSION}"
fi
- uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7
with:
go-version-file: 'go.mod'
cache: true
cache-dependency-path: go.sum
- name: Run changelog engine (release mode)
id: changelog_engine
env:
MODE: release
TARGET_VERSION: ${{ steps.version.outputs.TARGET_VERSION }}
CHANGELOG_PATH: CHANGELOG.md
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: go run ./scripts/changelog run-engine
- name: Commit release preparation
run: |
TARGET_VERSION="${{ steps.version.outputs.TARGET_VERSION }}"
git add Makefile CHANGELOG.md
if git diff --cached --quiet; then
echo "No changes to commit for release preparation; Makefile and CHANGELOG.md already match target ${TARGET_VERSION}."
exit 0
fi
git commit -m "chore(release): prepare ${TARGET_VERSION}"
- name: Push release branch
env:
GH_AW_CI_TRIGGER_TOKEN: ${{ secrets.GH_AW_CI_TRIGGER_TOKEN }}
run: |
BRANCH="${{ steps.meta.outputs.BRANCH }}"
git push origin "${BRANCH}"
if [ -n "${GH_AW_CI_TRIGGER_TOKEN}" ]; then
SERVER_URL="${GITHUB_SERVER_URL#https://}"
SERVER_URL="${SERVER_URL#http://}"
git remote set-url origin "https://x-access-token:${GH_AW_CI_TRIGGER_TOKEN}@${SERVER_URL}/${GITHUB_REPOSITORY}.git"
git commit --allow-empty -m "chore: trigger CI"
git push origin "${BRANCH}"
else
echo "::warning::GH_AW_CI_TRIGGER_TOKEN is not set; CI will not be triggered automatically"
fi
- name: Check if release PR already exists
id: pr-check
env:
GH_TOKEN: ${{ github.token }}
run: |
BRANCH="${{ steps.meta.outputs.BRANCH }}"
PR_URL=$(gh pr list --head "${BRANCH}" --state open --json url --jq '.[0].url')
if [ -n "$PR_URL" ]; then
echo "PR already exists: ${PR_URL}"
echo "EXISTS=true" >> "$GITHUB_OUTPUT"
echo "PR_URL=${PR_URL}" >> "$GITHUB_OUTPUT"
else
echo "No open PR found for branch ${BRANCH}"
echo "EXISTS=false" >> "$GITHUB_OUTPUT"
fi
- name: Create release PR
if: steps.pr-check.outputs.EXISTS == 'false'
env:
GH_TOKEN: ${{ github.token }}
run: |
BRANCH="${{ steps.meta.outputs.BRANCH }}"
PR_TITLE="${{ steps.meta.outputs.PR_TITLE }}"
TARGET_VERSION="${{ steps.version.outputs.TARGET_VERSION }}"
printf '%s\n' \
"This pull request prepares the ${TARGET_VERSION} release." \
"" \
"## Changes" \
"" \
"- Bumps \`VERSION\` in \`Makefile\` to \`${TARGET_VERSION}\`" \
"- Regenerates the \`## [${TARGET_VERSION}]\` section in \`CHANGELOG.md\` via the shared changelog engine (release range: previous tag to this branch HEAD)" \
"" \
"## Checklist" \
"" \
"- [ ] Changelog section looks correct" \
"- [ ] Version bump is correct" \
> /tmp/pr-body.txt
gh pr create \
--head "${BRANCH}" \
--base main \
--title "${PR_TITLE}" \
--body-file /tmp/pr-body.txt \
--label no-changelog
- name: Report outcome
run: |
TARGET_VERSION="${{ steps.version.outputs.TARGET_VERSION }}"
BRANCH="${{ steps.meta.outputs.BRANCH }}"
PR_EXISTS="${{ steps.pr-check.outputs.EXISTS }}"
PR_URL="${{ steps.pr-check.outputs.PR_URL }}"
echo "Release preparation complete"
echo " Target version : ${TARGET_VERSION}"
echo " Branch : ${BRANCH}"
echo " Compare range : ${{ steps.changelog_engine.outputs.compare_range }}"
echo " Section header : ${{ steps.changelog_engine.outputs.section_header }}"
echo " Has PRs in range: ${{ steps.changelog_engine.outputs.has_prs }}"
echo " User-facing changelog entries: ${{ steps.changelog_engine.outputs.has_user_facing_changes }}"
if [ "$PR_EXISTS" = "true" ]; then
echo " PR : ${PR_URL} (reused)"
else
echo " PR : created"
fi