Skip to content

Commit 4138776

Browse files
authored
Refactor helm artifacts publish flow (openshift-service-mesh#459)
Refactor the helm artifacts publish flow to continuously (cron job) check for the published helm artifacts according to latest release assets. If missing, update and add a release to the "index.yaml" file under "gh-pages" branch. If exists, do not perform any change. Signed-off-by: Maxim Babushkin <[email protected]>
1 parent 881fff7 commit 4138776

File tree

3 files changed

+50
-44
lines changed

3 files changed

+50
-44
lines changed

.github/workflows/helm.yaml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
name: Publish Helm artifact
22

33
on:
4+
schedule:
5+
- cron: "0 1 * * 1,5" # Every Mon and Fri at 1AM UTC
46
workflow_dispatch:
5-
inputs:
6-
release_version:
7-
description: "Release version"
8-
required: true
97

108
run-name: Publish Helm artifact ${{ inputs.release_version }}
119

1210
env:
1311
GIT_USER: ${{ secrets.GIT_USER }}
1412
GITHUB_TOKEN: ${{ secrets.GIT_TOKEN }}
15-
VERSION: ${{ inputs.release_version }}
1613

1714
jobs:
1815
helm:
@@ -26,7 +23,6 @@ jobs:
2623
make helm-artifacts-publish \
2724
-e GIT_CONFIG_USER_NAME="$GIT_CONFIG_USER_NAME" \
2825
-e GIT_CONFIG_USER_EMAIL="$GIT_CONFIG_USER_EMAIL" \
29-
-e OPERATOR_VERSION=$VERSION
3026
env:
3127
GIT_CONFIG_USER_NAME: "${{ github.actor }}"
3228
GIT_CONFIG_USER_EMAIL: "${{ github.actor_id }}+${{ github.actor }}@users.noreply.github.com"

Makefile.core.mk

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,6 @@ bundle-publish-nightly: bundle-nightly bundle-publish
604604
helm-artifacts-publish: helm ## Publish Helm artifacts to be available for "Helm repo add"
605605
@export GIT_USER=$(GITHUB_USER); \
606606
export GITHUB_TOKEN=$(GITHUB_TOKEN); \
607-
export OPERATOR_VERSION=$(OPERATOR_VERSION); \
608607
./hack/helm-artifacts.sh
609608

610609
.PHONY: opm $(OPM)

hack/helm-artifacts.sh

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,35 +21,22 @@ CUR_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
2121
source "${CUR_DIR}"/validate_semver.sh
2222

2323
GITHUB_TOKEN="${GITHUB_TOKEN:-}"
24-
2524
GIT_CONFIG_USER_NAME="${GIT_CONFIG_USER_NAME:-}"
2625
GIT_CONFIG_USER_EMAIL="${GIT_CONFIG_USER_EMAIL:-}"
27-
2826
UPSTREAM_OPERATOR_NAME="${UPSTREAM_OPERATOR_NAME:-"sail-operator"}"
2927
OWNER="${OWNER:-"istio-ecosystem"}"
3028
HUB_REPO_URL="${HUB_REPO_URL:-github.com/${OWNER}/${UPSTREAM_OPERATOR_NAME}}"
3129
HUB_HELM_BRANCH="${HUB_HIVE_BRANCH:-"gh-pages"}"
32-
HUB_HELM_ARTIFACT_URL="https://${HUB_REPO_URL}/releases/download/${OPERATOR_VERSION}"/
33-
34-
: "${OPERATOR_VERSION:?"Missing OPERATOR_VERSION variable"}"
3530

36-
show_help() {
31+
function show_help() {
3732
cat <<EOF
38-
3933
Publish Sail Operator helm artifacts to gh-pages branch
40-
It will allow to install Sail Operator via "helm add repo"
41-
42-
./publish-helm-artifacts.sh [options]
43-
34+
Usage: ./publish-helm-artifacts.sh [options]
4435
EOF
4536
}
4637

47-
function update_repo_index() {
48-
echo "Update Helm repo index with new artifact"
49-
}
50-
5138
function prepare_repo() {
52-
echo "Prepare repository"
39+
echo "Preparing repository..."
5340

5441
if [ -z "${GITHUB_TOKEN}" ]; then
5542
die "Please provide GITHUB_TOKEN"
@@ -61,48 +48,70 @@ function prepare_repo() {
6148
TMP_DIR=$(mktemp -d)
6249
trap 'rm -rf "${TMP_DIR}"' EXIT
6350

64-
git clone --single-branch --depth=1 --branch "${HUB_HELM_BRANCH}" "https://${GIT_USER}:${GITHUB_TOKEN}@${HUB_REPO_URL}" "${TMP_DIR}/${UPSTREAM_OPERATOR_NAME}"
51+
git clone --single-branch --depth=1 --branch "${HUB_HELM_BRANCH}" "https://${GITHUB_TOKEN}@${HUB_REPO_URL}" "${TMP_DIR}/${UPSTREAM_OPERATOR_NAME}"
6552
cd "${TMP_DIR}/${UPSTREAM_OPERATOR_NAME}"
6653

6754
if ! git config user.name; then
6855
git config user.name "${GIT_CONFIG_USER_NAME}"
6956
fi
70-
7157
if ! git config user.email; then
7258
git config user.email "${GIT_CONFIG_USER_EMAIL}"
7359
fi
7460
}
7561

76-
function fetch_released_artifact() {
77-
echo "Fetch released helm artifact"
62+
function get_latest_release_version() {
63+
echo "Fetching latest release version from GitHub API..."
7864

79-
wget "${HUB_HELM_ARTIFACT_URL}/${UPSTREAM_OPERATOR_NAME}-${OPERATOR_VERSION}.tgz"
65+
latest_version=$(curl -s \
66+
"https://api.github.com/repos/${OWNER}/${UPSTREAM_OPERATOR_NAME}/releases/latest" | \
67+
jq -r .tag_name)
68+
echo "Latest release version: ${latest_version}"
8069
}
8170

82-
function update_helm_repo_index() {
83-
echo "Update index of Helm repo"
84-
local helm_branch="update_helm_artifact_${OPERATOR_VERSION}"
71+
function check_version_in_index() {
72+
echo "Checking if version ${latest_version} exists in index.yaml..."
8573

86-
git checkout -b "$helm_branch"
87-
helm repo index --merge index.yaml . --url "${HUB_HELM_ARTIFACT_URL}"
74+
if grep -q "version: ${latest_version}" index.yaml; then
75+
echo "Version ${latest_version} is already in index.yaml. Skipping update."
76+
return 0
77+
else
78+
echo "Version ${latest_version} not found in index.yaml. Updating index."
79+
return 1
80+
fi
81+
}
82+
83+
function add_version_to_index() {
84+
echo "Adding new version to Helm repo index..."
85+
86+
wget "https://${HUB_REPO_URL}/releases/download/${latest_version}/${UPSTREAM_OPERATOR_NAME}-${latest_version}.tgz"
87+
helm repo index --merge index.yaml . --url "https://${HUB_REPO_URL}/releases/download/${latest_version}/"
88+
89+
git checkout -b "update_helm_artifact_${latest_version}"
8890
git add index.yaml
89-
git commit -m "Add new sail-operator chart release - ${OPERATOR_VERSION}"
90-
git push origin "$helm_branch"
91+
git commit -m "Add new sail-operator chart release - ${latest_version}"
92+
git push origin "update_helm_artifact_${latest_version}"
93+
94+
create_pull_request
95+
}
9196

97+
function create_pull_request {
98+
echo "Creating pull request..."
9299
PAYLOAD="${TMP_DIR}/PAYLOAD"
93100

94101
jq -c -n \
95-
--arg msg "Add new sail-operator chart release - ${OPERATOR_VERSION}" \
96-
--arg head "${OWNER}:${helm_branch}" \
102+
--arg msg "Add new sail-operator chart release - ${latest_version}" \
103+
--arg head "${OWNER}:update_helm_artifact_${latest_version}" \
97104
--arg base "${HUB_HELM_BRANCH}" \
98-
--arg title "Helm artifact ${OPERATOR_VERSION}" \
105+
--arg title "Helm artifact ${latest_version}" \
99106
'{head: $head, base: $base, title: $title, body: $msg }' > "${PAYLOAD}"
100107

101-
curl --fail-with-body -X POST \
108+
curl -X POST \
102109
-H "Authorization: token ${GITHUB_TOKEN}" \
103110
-H "Accept: application/vnd.github.v3+json" \
104-
https://api.github.com/repos/"${OWNER}/${UPSTREAM_OPERATOR_NAME}"/pulls \
105-
--data-binary "@${PAYLOAD}"
111+
-d @"${PAYLOAD}" \
112+
"https://api.github.com/repos/${OWNER}/${UPSTREAM_OPERATOR_NAME}/pulls"
113+
114+
rm "${PAYLOAD}"
106115
}
107116

108117
while test $# -gt 0; do
@@ -112,12 +121,14 @@ while test $# -gt 0; do
112121
exit 0
113122
;;
114123
*)
115-
echo "Unknown param $1"
124+
echo "Unknown parameter $1"
116125
exit 1
117126
;;
118127
esac
119128
done
120129

121130
prepare_repo
122-
fetch_released_artifact
123-
update_helm_repo_index
131+
get_latest_release_version
132+
if ! check_version_in_index; then
133+
add_version_to_index
134+
fi

0 commit comments

Comments
 (0)