Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

auto create pr to update helm index when release chart #5911

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ jobs:
release-charts:
permissions:
contents: write # for softprops/action-gh-release to create GitHub release
pull-requests: write
name: Release charts
outputs:
hashes: ${{ steps.hash.outputs.hashes }}
Expand All @@ -145,6 +146,14 @@ jobs:
run: |
cd _output/charts
echo "hashes=$(sha256sum *.tgz|base64 -w0)" >> "$GITHUB_OUTPUT"
- name: update chart index
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
git config user.name "$GITHUB_ACTOR"
git config user.email "[email protected]"
export tag=${{ github.ref_name }}
hack/update-chart-index.sh
charts-provenance:
needs: [release-charts]
permissions:
Expand Down
99 changes: 99 additions & 0 deletions hack/update-chart-index.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#!/usr/bin/env bash
# Copyright 2024 The Karmada Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -o errexit
set -o nounset
set -o pipefail

# This script used to update helm index to specific release version and automatically submit a pr to remote repo.
# Usage:
# export CURRENT_REPO_ORG=karmada-io CURRENT_REPO_NAME=karmada tag=v1.12.0
# hack/update-helm-index.sh
chaosi-zju marked this conversation as resolved.
Show resolved Hide resolved

REPO_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
cd ${REPO_ROOT}

CURRENT_REMOTE=${CURRENT_REMOTE:-origin}
CURRENT_REPO_ORG=${CURRENT_REPO_ORG:-$(git remote get-url "$CURRENT_REMOTE" | awk '{gsub(/http[s]:\/\/|git@/,"")}1' | awk -F'[@:./]' 'NR==1{print $3}')}
CURRENT_REPO_NAME=${CURRENT_REPO_NAME:-$(git remote get-url "$CURRENT_REMOTE" | awk '{gsub(/http[s]:\/\/|git@/,"")}1' | awk -F'[@:./]' 'NR==1{print $4}')}

get_latest_release_tag() {
curl --silent "https://api.github.com/repos/$1/releases/latest" |
grep '"tag_name":' |
sed -E 's/.*"([^"]+)".*/\1/'
}

# step1: get tag, defaults to latest release tag
tag=${tag:-"$(get_latest_release_tag "${CURRENT_REPO_ORG}/${CURRENT_REPO_NAME}")"}
if [ $(grep -c "version: ${tag}" charts/index.yaml) -ge '2' ]; then
echo "the tag already in helm index!"
exit 0
fi

# step2: checkout a new branch
# Normally return if remote branch already exist, which means you can re-execute the script without throwing errors.
NEWBRANCH="auto-helm-index-${tag}"
git fetch -q
if [ $(git branch -r | grep -c "origin/${NEWBRANCH}") -gt '0' ]; then
echo 'remote branch '${NEWBRANCH}' already exist!'
exit 0
fi
git checkout -b ${NEWBRANCH}

# step3: update index for karmada-chart
wget https://github.com/${CURRENT_REPO_ORG}/${CURRENT_REPO_NAME}/releases/download/${tag}/karmada-chart-${tag}.tgz -P charts/karmada/
helm repo index charts/karmada --url https://github.com/${CURRENT_REPO_ORG}/${CURRENT_REPO_NAME}/releases/download/${tag} --merge charts/index.yaml
mv charts/karmada/index.yaml charts/index.yaml

# step4: update index for karmada-operator-chart
wget https://github.com/${CURRENT_REPO_ORG}/${CURRENT_REPO_NAME}/releases/download/${tag}/karmada-operator-chart-${tag}.tgz -P charts/karmada-operator/
helm repo index charts/karmada-operator --url https://github.com/${CURRENT_REPO_ORG}/${CURRENT_REPO_NAME}/releases/download/${tag} --merge charts/index.yaml
mv charts/karmada-operator/index.yaml charts/index.yaml

# step5: the `helm repo index` command also generates index for dependencies(common-2.x.x) by default,
# which is undesirable; therefore, the contents of the `common` field should be manaually removed.
# this `sed` command deletes lines between the line `entries:` and the line `karmada:`.
sed -i'' '/entries:/,/karmada:/{//!d}' charts/index.yaml
echo "Successfully generated helm index."

# step6: commit the modification
git add charts/index.yaml
git commit -s -m "Bump upgrade helm chart index to ${tag}"
git push origin ${NEWBRANCH}
echo "Successfully pushed the commit."

# step6: create pull request
prtext=$(
cat <<EOF
**What type of PR is this?**

/kind cleanup

**What this PR does / why we need it**:

Bump upgrade helm chart index to ${tag}

**Which issue(s) this PR fixes**:

Fixes

**Does this PR introduce a user-facing change?**:
\`\`\`release-note
upgrade helm chart index to ${tag}.
\`\`\`
EOF
)
gh pr create --title "Bump upgrade helm chart index to ${tag}" --body "${prtext}" --base master --head "${NEWBRANCH}" --repo="${CURRENT_REPO_ORG}/${CURRENT_REPO_NAME}"
echo "Successfully created the pr."