Skip to content

Commit 668f9d6

Browse files
committed
auto create pr to update helm index when release chart
Signed-off-by: chaosi-zju <[email protected]>
1 parent cf5def1 commit 668f9d6

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

.github/workflows/release.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ jobs:
121121
release-charts:
122122
permissions:
123123
contents: write # for softprops/action-gh-release to create GitHub release
124+
pull-requests: write
124125
name: Release charts
125126
outputs:
126127
hashes: ${{ steps.hash.outputs.hashes }}
@@ -145,6 +146,14 @@ jobs:
145146
run: |
146147
cd _output/charts
147148
echo "hashes=$(sha256sum *.tgz|base64 -w0)" >> "$GITHUB_OUTPUT"
149+
- name: update chart index
150+
env:
151+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
152+
run: |
153+
git config user.name "$GITHUB_ACTOR"
154+
git config user.email "[email protected]"
155+
export tag=${{ github.ref_name }}
156+
hack/update-chart-index.sh
148157
charts-provenance:
149158
needs: [release-charts]
150159
permissions:

hack/update-chart-index.sh

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#!/usr/bin/env bash
2+
# Copyright 2024 The Karmada Authors.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
set -o errexit
17+
set -o nounset
18+
set -o pipefail
19+
20+
# This script used to update helm index to specific release version and automatically submit a pr to remote repo.
21+
# Usage:
22+
# export CURRENT_REPO_ORG=karmada-io CURRENT_REPO_NAME=karmada tag=v1.12.0
23+
# hack/update-helm-index.sh
24+
25+
REPO_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
26+
cd ${REPO_ROOT}
27+
28+
CURRENT_REMOTE=${CURRENT_REMOTE:-origin}
29+
CURRENT_REPO_ORG=${CURRENT_REPO_ORG:-$(git remote get-url "$CURRENT_REMOTE" | awk '{gsub(/http[s]:\/\/|git@/,"")}1' | awk -F'[@:./]' 'NR==1{print $3}')}
30+
CURRENT_REPO_NAME=${CURRENT_REPO_NAME:-$(git remote get-url "$CURRENT_REMOTE" | awk '{gsub(/http[s]:\/\/|git@/,"")}1' | awk -F'[@:./]' 'NR==1{print $4}')}
31+
32+
get_latest_release_tag() {
33+
curl --silent "https://api.github.com/repos/$1/releases/latest" |
34+
grep '"tag_name":' |
35+
sed -E 's/.*"([^"]+)".*/\1/'
36+
}
37+
38+
# step1: get tag, defaults to latest release tag
39+
tag=${tag:-"$(get_latest_release_tag "${CURRENT_REPO_ORG}/${CURRENT_REPO_NAME}")"}
40+
if [ $(grep -c "version: ${tag}" charts/index.yaml) -ge '2' ]; then
41+
echo "the tag already in helm index!"
42+
exit 0
43+
fi
44+
45+
# step2: checkout a new branch
46+
# Normally return if remote branch already exist, which means you can re-execute the script without throwing errors.
47+
NEWBRANCH="auto-helm-index-${tag}"
48+
git fetch -q
49+
if [ $(git branch -r | grep -c "origin/${NEWBRANCH}") -gt '0' ]; then
50+
echo 'remote branch '${NEWBRANCH}' already exist!'
51+
exit 0
52+
fi
53+
git checkout -b ${NEWBRANCH}
54+
55+
# step3: update index for karmada-chart
56+
wget https://github.com/${CURRENT_REPO_ORG}/${CURRENT_REPO_NAME}/releases/download/${tag}/karmada-chart-${tag}.tgz -P charts/karmada/
57+
helm repo index charts/karmada --url https://github.com/${CURRENT_REPO_ORG}/${CURRENT_REPO_NAME}/releases/download/${tag} --merge charts/index.yaml
58+
mv charts/karmada/index.yaml charts/index.yaml
59+
60+
# step4: update index for karmada-operator-chart
61+
wget https://github.com/${CURRENT_REPO_ORG}/${CURRENT_REPO_NAME}/releases/download/${tag}/karmada-operator-chart-${tag}.tgz -P charts/karmada-operator/
62+
helm repo index charts/karmada-operator --url https://github.com/${CURRENT_REPO_ORG}/${CURRENT_REPO_NAME}/releases/download/${tag} --merge charts/index.yaml
63+
mv charts/karmada-operator/index.yaml charts/index.yaml
64+
65+
# step5: the `helm repo index` command also generates index for dependencies(common-2.x.x) by default,
66+
# which is undesirable; therefore, the contents of the `common` field should be manaually removed.
67+
# this `sed` command deletes lines between the line `entries:` and the line `karmada:`.
68+
sed -i'' '/entries:/,/karmada:/{//!d}' charts/index.yaml
69+
echo "Successfully generated helm index."
70+
71+
# step6: commit the modification
72+
git add charts/index.yaml
73+
git commit -s -m "Bump upgrade helm chart index to ${tag}"
74+
git push origin ${NEWBRANCH}
75+
echo "Successfully pushed the commit."
76+
77+
# step6: create pull request
78+
prtext=$(
79+
cat <<EOF
80+
**What type of PR is this?**
81+
82+
/kind cleanup
83+
84+
**What this PR does / why we need it**:
85+
86+
Bump upgrade helm chart index to ${tag}
87+
88+
**Which issue(s) this PR fixes**:
89+
90+
Fixes
91+
92+
**Does this PR introduce a user-facing change?**:
93+
\`\`\`release-note
94+
upgrade helm chart index to ${tag}.
95+
\`\`\`
96+
EOF
97+
)
98+
gh pr create --title "Bump upgrade helm chart index to ${tag}" --body "${prtext}" --base master --head "${NEWBRANCH}" --repo="${CURRENT_REPO_ORG}/${CURRENT_REPO_NAME}"
99+
echo "Successfully created the pr."

0 commit comments

Comments
 (0)