-
Notifications
You must be signed in to change notification settings - Fork 0
300 lines (271 loc) · 11.9 KB
/
Copy pathupdate-helm-chart.yml
File metadata and controls
300 lines (271 loc) · 11.9 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
name: Update chart
on:
workflow_call:
inputs:
RUN_MODE:
description: Mode the workflow is running. Use 'caller' to trigger the workflow in the chart repository, 'called' to update the chart in the current repository
required: true
type: string
WORKFLOW_NAME:
description: Name of the workflow to trigger in the chart repository (eg. 'update-chart.yml')
required: false
type: string
default: update-app-version.yml
CHART_REPO:
description: Name of the chart repository (eg. 'this-is-tobi/helm-charts')
required: false
type: string
CHART_DIR:
description: Name of the folder containing the chart (in CHART_REPO)
required: false
type: string
default: charts
CHART_NAME:
description: Name of the chart to update (in CHART_DIR)
required: true
type: string
APP_VERSION:
description: The app version to inject in Chart.yaml
required: true
type: string
UPGRADE_TYPE:
description: Should update 'major', 'minor', 'patch' or 'prerelease'
required: false
type: string
default: patch
PRERELEASE_IDENTIFIER:
description: Prerelease identifier (only used if UPGRADE_TYPE is 'prerelease')
required: false
type: string
default: rc
AUTOMERGE_PRERELEASE:
description: Automatically merge the PR when UPGRADE_TYPE is 'prerelease' (requires GH_PAT)
required: false
type: boolean
default: false
AUTOMERGE_RELEASE:
description: Automatically merge the PR when UPGRADE_TYPE is not 'prerelease' (requires GH_PAT)
required: false
type: boolean
default: false
RUNS_ON:
description: Runner labels as JSON array (e.g., '["ubuntu-24.04"]' or '["self-hosted", "linux"]')
required: false
type: string
default: '["ubuntu-24.04"]'
secrets:
GH_PAT:
description: GitHub Personal Access Token (needed for automerge)
required: false
jobs:
caller:
name: Update chart
runs-on: ${{ fromJSON(inputs.RUNS_ON) }}
permissions: {}
if: ${{ inputs.RUN_MODE == 'caller' }}
steps:
- name: Trigger helm-charts update
env:
GH_TOKEN: ${{ secrets.GH_PAT }}
CHART_DIR: ${{ inputs.CHART_DIR }}
CHART_REPO: ${{ inputs.CHART_REPO }}
WORKFLOW_NAME: ${{ inputs.WORKFLOW_NAME }}
CHART_NAME: ${{ inputs.CHART_NAME }}
APP_VERSION: ${{ inputs.APP_VERSION }}
UPGRADE_TYPE: ${{ inputs.UPGRADE_TYPE }}
PRERELEASE_IDENTIFIER: ${{ inputs.PRERELEASE_IDENTIFIER }}
AUTOMERGE_PRERELEASE: ${{ inputs.AUTOMERGE_PRERELEASE }}
AUTOMERGE_RELEASE: ${{ inputs.AUTOMERGE_RELEASE }}
run: |
# Remove trailing slashes
CHART_DIR=$(echo "$CHART_DIR" | sed 's:/*$::')
if [ -z "$CHART_REPO" ]; then
echo "CHART_REPO input is required when using 'caller' mode"
exit 1
fi
echo "Trigger workflow '$WORKFLOW_NAME' in repository '$CHART_REPO' to update chart '$CHART_DIR/$CHART_NAME' with app version '$APP_VERSION'"
gh workflow --repo "$CHART_REPO" run "$WORKFLOW_NAME" \
-f "APP_VERSION=$APP_VERSION" \
-f "CHART_NAME=$CHART_NAME" \
-f "CHART_DIR=$CHART_DIR" \
-f "UPGRADE_TYPE=$UPGRADE_TYPE" \
-f "PRERELEASE_IDENTIFIER=$PRERELEASE_IDENTIFIER" \
-f "AUTOMERGE_PRERELEASE=$AUTOMERGE_PRERELEASE" \
-f "AUTOMERGE_RELEASE=$AUTOMERGE_RELEASE" \
-f RUN_MODE=called
called:
name: Update chart
runs-on: ${{ fromJSON(inputs.RUNS_ON) }}
permissions:
contents: write
pull-requests: write
if: ${{ inputs.RUN_MODE == 'called' || inputs.RUN_MODE == 'local' }}
steps:
- name: Checks-out repository
uses: actions/checkout@9f698171ed81b15d1823a05fc7211befd50c8ae0 # v6.0.4
- name: update chart version
id: update-chart
env:
CHART_DIR: ${{ inputs.CHART_DIR }}
CHART_NAME: ${{ inputs.CHART_NAME }}
APP_VERSION: ${{ inputs.APP_VERSION }}
UPGRADE_TYPE: ${{ inputs.UPGRADE_TYPE }}
PRERELEASE_IDENTIFIER: ${{ inputs.PRERELEASE_IDENTIFIER }}
run: |
# Remove trailing slashes
CHART_DIR=$(echo "$CHART_DIR" | sed 's:/*$::')
if [ ! -f "$CHART_DIR/$CHART_NAME/Chart.yaml" ]; then
echo "Chart.yaml not found in $CHART_DIR/$CHART_NAME"
exit 1
fi
echo "Update chart '$CHART_NAME' in $CHART_DIR/$CHART_NAME with app version '$APP_VERSION'"
CURRENT_VERSION=$(yq '.version' "$CHART_DIR/$CHART_NAME/Chart.yaml")
# Bump version using Release Please compatible logic
if [[ "$UPGRADE_TYPE" == "prerelease" ]]; then
# Parse version: extract base version and prerelease part
if [[ $CURRENT_VERSION =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)(-(.+))?$ ]]; then
MAJOR="${BASH_REMATCH[1]}"
MINOR="${BASH_REMATCH[2]}"
PATCH="${BASH_REMATCH[3]}"
PRERELEASE="${BASH_REMATCH[5]}"
if [[ -z "$PRERELEASE" ]]; then
# No prerelease: bump patch then add prerelease identifier
# This prepares the next version (e.g., 0.2.0 -> 0.2.1-rc)
NEXT_VERSION="${MAJOR}.${MINOR}.$((PATCH + 1))-$PRERELEASE_IDENTIFIER"
else
# Has prerelease: extract current identifier
if [[ "$PRERELEASE" =~ ^([a-z]+)(\.([0-9]+))?$ ]]; then
CURRENT_IDENTIFIER="${BASH_REMATCH[1]}"
CURRENT_NUM="${BASH_REMATCH[3]}"
if [[ "$CURRENT_IDENTIFIER" != "$PRERELEASE_IDENTIFIER" ]]; then
# Different identifier: switch to new identifier without number
# e.g., 1.0.0-alpha.2 -> 1.0.0-beta
NEXT_VERSION="${MAJOR}.${MINOR}.${PATCH}-$PRERELEASE_IDENTIFIER"
elif [[ -z "$CURRENT_NUM" ]]; then
# Same identifier without number: add .1
# e.g., 1.0.0-rc -> 1.0.0-rc.1
NEXT_VERSION="${MAJOR}.${MINOR}.${PATCH}-$PRERELEASE_IDENTIFIER.1"
else
# Same identifier with number: increment
# e.g., 1.0.0-rc.1 -> 1.0.0-rc.2
NEXT_NUM=$((CURRENT_NUM + 1))
NEXT_VERSION="${MAJOR}.${MINOR}.${PATCH}-$PRERELEASE_IDENTIFIER.${NEXT_NUM}"
fi
else
echo "Unexpected prerelease format: $PRERELEASE"
exit 1
fi
fi
else
echo "Invalid version format: $CURRENT_VERSION"
exit 1
fi
else
# Standard version bump (major, minor, patch)
if [[ $CURRENT_VERSION =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)(-(.+))?$ ]]; then
MAJOR="${BASH_REMATCH[1]}"
MINOR="${BASH_REMATCH[2]}"
PATCH="${BASH_REMATCH[3]}"
PRERELEASE="${BASH_REMATCH[5]}"
if [[ -n "$PRERELEASE" ]]; then
# Has prerelease: release the official version without prerelease
NEXT_VERSION="${MAJOR}.${MINOR}.${PATCH}"
else
# No prerelease: perform standard version bump
case "$UPGRADE_TYPE" in
major)
NEXT_VERSION="$((MAJOR + 1)).0.0"
;;
minor)
NEXT_VERSION="${MAJOR}.$((MINOR + 1)).0"
;;
patch)
NEXT_VERSION="${MAJOR}.${MINOR}.$((PATCH + 1))"
;;
*)
echo "Invalid upgrade type: $UPGRADE_TYPE"
exit 1
;;
esac
fi
else
echo "Invalid version format: $CURRENT_VERSION"
exit 1
fi
fi
echo "Update chart from version '$CURRENT_VERSION' to '$NEXT_VERSION'"
echo "Update chart files..."
sed -i "s/^appVersion: .*/appVersion: $APP_VERSION/" "$CHART_DIR/$CHART_NAME/Chart.yaml"
sed -i "s/^version: .*/version: $NEXT_VERSION/" "$CHART_DIR/$CHART_NAME/Chart.yaml"
echo "Generate chart documentation..."
docker run \
--rm \
--volume "$(pwd)/$CHART_DIR/$CHART_NAME:/helm-docs" \
-u $(id -u) \
docker.io/jnorwood/helm-docs:latest
echo "CURRENT_VERSION=$CURRENT_VERSION" >> $GITHUB_OUTPUT
echo "NEXT_VERSION=$NEXT_VERSION" >> $GITHUB_OUTPUT
echo "BRANCH_NAME=$CHART_NAME-v$NEXT_VERSION" >> $GITHUB_OUTPUT
- name: Create pull request
id: create-pr
uses: peter-evans/create-pull-request@c0f553fe549906ede9cf27b5156039d195d2ece0 # v8.1.0
if: ${{ inputs.RUN_MODE == 'called' }}
with:
branch: ${{ steps.update-chart.outputs.BRANCH_NAME }}
base: main
title: Update chart ${{ inputs.CHART_NAME }} app to v${{ inputs.APP_VERSION }}
add-paths: |
**/**
commit-message: "chore: update chart ${{ inputs.CHART_NAME }} app to v${{ inputs.APP_VERSION }}"
draft: false
body: |
🤖 This PR has been automatically generated, please check updates before merging.
- Chart version has been updated from `${{ steps.update-chart.outputs.CURRENT_VERSION }}` to `${{ steps.update-chart.outputs.NEXT_VERSION }}`
- App version has been updated to `${{ inputs.APP_VERSION }}`
- Chart documentation has been regenerated
- name: Commit and push helm updates
if: ${{ inputs.RUN_MODE == 'local' }}
env:
CHART_DIR: ${{ inputs.CHART_DIR }}
CHART_NAME: ${{ inputs.CHART_NAME }}
run: |
git config user.name github-actions[bot]
git config user.email github-actions[bot]@users.noreply.github.com
git fetch --all --prune
pr_branch=$(git branch -r | grep "release-please--branches--${{ github.ref_name }}" | sed 's|origin/||' | xargs)
if [ -n "$pr_branch" ]; then
echo "Commit and push helm updates to release-please branch '$pr_branch' for chart '$CHART_DIR/$CHART_NAME'"
git fetch origin $pr_branch
git checkout $pr_branch
git add $CHART_DIR/$CHART_NAME/Chart.yaml $CHART_DIR/$CHART_NAME/README.md
git commit --amend --no-edit
git push --force-with-lease --force-if-includes
echo "Helm updates pushed to release-please branch '$pr_branch' for chart '$CHART_DIR/$CHART_NAME'"
else
echo "No release-please branch found for branch '${{ github.ref_name }}', skipping synchronization"
fi
- name: Automerge chart update PR
if: ${{ inputs.RUN_MODE == 'called' && ((inputs.AUTOMERGE_PRERELEASE && inputs.UPGRADE_TYPE == 'prerelease') || (inputs.AUTOMERGE_RELEASE && inputs.UPGRADE_TYPE != 'prerelease')) && steps.create-pr.outputs.pull-request-number }}
env:
GH_TOKEN: ${{ secrets.GH_PAT }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_REPOSITORY: ${{ github.repository }}
PR_NUMBER: ${{ steps.create-pr.outputs.pull-request-number }}
run: |
if [ -n "$GH_TOKEN" ]; then
echo "Using provided GitHub PAT for automerge"
AUTOMERGE_ALLOWED=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Accept: application/vnd.github+json" \
"https://api.github.com/repos/$GITHUB_REPOSITORY" | jq '.allow_auto_merge')
if [ "$AUTOMERGE_ALLOWED" == "true" ]; then
echo "Auto-merge is enabled for repo '$GITHUB_REPOSITORY', setting up automerge gh flag"
GH_EXTRA_ARGS="--auto"
else
echo "Auto-merge is not enabled for repo '$GITHUB_REPOSITORY', setting up admin gh flag"
GH_EXTRA_ARGS="--admin"
fi
echo "Merging PR #$PR_NUMBER"
gh pr merge $PR_NUMBER --rebase $GH_EXTRA_ARGS
else
echo "No GitHub PAT provided, skipping automerge"
fi