From a95cb968bcee57e50ab27c8b55ad4a0b7c4cb36a Mon Sep 17 00:00:00 2001 From: Aliaksei Kozich Date: Mon, 18 Jan 2021 10:48:06 +0300 Subject: [PATCH 1/2] Add release steps to serve Helm Charts Repository on Github Pages --- build.sbt | 17 ++++++++- scripts/build_charts_index.sh | 24 ++++++++++++ scripts/package_chart.sh | 9 +++-- scripts/publish_charts_index.sh | 67 +++++++++++++++++++++++++++++++++ 4 files changed, 113 insertions(+), 4 deletions(-) create mode 100755 scripts/build_charts_index.sh create mode 100755 scripts/publish_charts_index.sh diff --git a/build.sbt b/build.sbt index 9b7aa277..cdf08666 100644 --- a/build.sbt +++ b/build.sbt @@ -75,6 +75,7 @@ lazy val kafkaLagExporter = updateHelmChartRelease, // Update the Helm Chart publishDockerImage, // Publish the Docker images used by the chart packageChart, // Package the Helm Chart + buildChartsIndex, // Build Helm Charts index packageJavaApp, // Package the standalone Java App commitReleaseVersion, updateReadmeRelease, // Update the README.md with this version @@ -82,6 +83,7 @@ lazy val kafkaLagExporter = commitChartThisVersion, // Commit the Helm Chart tagRelease, githubReleaseDraft, // Create a GitHub release draft + publishChartsIndex, // Publish Helm Charts index setNextVersion, updateHelmChartNextVersion, // Update the Helm Chart with the next snapshot version commitChartNextVersion, // Commit the Helm Chart @@ -191,10 +193,23 @@ lazy val packageChart = ReleaseStep(action = st => { st }) +lazy val buildChartsIndex = ReleaseStep(action = st => { + val (releaseVersion, _) = st.get(versions).getOrElse(sys.error("No versions are set! Was this release part executed before inquireVersions?")) + exec( + s"./scripts/build_charts_index.sh https://github.com/lightbend/kafka-lag-exporter/releases/download/v$releaseVersion/ https://lightbend.github.io/kafka-lag-exporter/index.yaml", + "Error while building Helm Charts index") + st +}) + +lazy val publishChartsIndex = ReleaseStep(action = st => { + exec("./scripts/publish_charts_index.sh", "Error while publishing Helm Charts index") + st +}) + lazy val githubReleaseDraft = ReleaseStep(action = st => { val (releaseVersion, _) = st.get(versions).getOrElse(sys.error("No versions are set! Was this release part executed before inquireVersions?")) exec( - s"./scripts/github_release.sh lightbend/kafka-lag-exporter v$releaseVersion -- kafka-lag-exporter-$releaseVersion.tgz ./target/universal/kafka-lag-exporter-$releaseVersion.zip", + s"./scripts/github_release.sh lightbend/kafka-lag-exporter v$releaseVersion -- .helm-release-packages/kafka-lag-exporter-$releaseVersion.tgz ./target/universal/kafka-lag-exporter-$releaseVersion.zip", "Error while publishing GitHub release draft") st }) diff --git a/scripts/build_charts_index.sh b/scripts/build_charts_index.sh new file mode 100755 index 00000000..612b1fb9 --- /dev/null +++ b/scripts/build_charts_index.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash + +set -e + +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )" + +cd $DIR/.. + +REMOTE_URL_BASE=$1 +REMOTE_INDEX_URL=$2 +CHARTS_DIR=".helm-release-packages" + +EXISTING_INDEX=$(mktemp /tmp/index.yaml.XXXXXX) + +REMOTE_CODE=$(curl -s -L -w "%{http_code}" $REMOTE_INDEX_URL -o $EXISTING_INDEX) +if [ $REMOTE_CODE -eq 200 ]; then + echo Adding new packages to existing index + helm repo index --merge $EXISTING_INDEX --url $REMOTE_URL_BASE CHARTS_DIR +else + echo Creating new index + helm repo index --url $REMOTE_URL_BASE $CHARTS_DIR +fi + +rm "${EXISTING_INDEX}" \ No newline at end of file diff --git a/scripts/package_chart.sh b/scripts/package_chart.sh index f6f87678..c60206f9 100755 --- a/scripts/package_chart.sh +++ b/scripts/package_chart.sh @@ -6,8 +6,11 @@ DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )" cd $DIR/.. +CHARTS_DIR=".helm-release-packages" + # Bundle Kafka Lag Exporter Helm Chart into a tarball artifact. The `helm package` command will output the artifact -# in the CWD it is executed from. +# in the CHARTS_DIR. echo Package helm chart -rm -f ./kafka-lag-exporter-*.tgz -helm package ./charts/kafka-lag-exporter \ No newline at end of file +mkdir -p $CHARTS_DIR +rm -f $CHARTS_DIR/kafka-lag-exporter-*.tgz +helm package ./charts/kafka-lag-exporter -d $CHARTS_DIR \ No newline at end of file diff --git a/scripts/publish_charts_index.sh b/scripts/publish_charts_index.sh new file mode 100755 index 00000000..ed7b6123 --- /dev/null +++ b/scripts/publish_charts_index.sh @@ -0,0 +1,67 @@ +#!/usr/bin/env bash + +set -e + +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )" + +cd $DIR/.. + +CHARTS_DIR=".helm-release-packages" +GITHUB_PAGES_BRANCH="gh-pages" + +function pages_branch_missing() { + local existed_in_remote=$(git ls-remote --heads origin $GITHUB_PAGES_BRANCH) + + [ -z "${existed_in_remote}" ] +} + +function create_pages_branch() { + echo "Creating pages branch ${GITHUB_PAGES_BRANCH}" + git checkout --quiet --orphan $GITHUB_PAGES_BRANCH + git rm --quiet -rf . + git commit --quiet --allow-empty -m "Pages initial commit" + git push --quiet origin HEAD:refs/heads/$GITHUB_PAGES_BRANCH + git checkout --detach --quiet + git branch --quiet -d $GITHUB_PAGES_BRANCH +} + +function add_pages_worktree() { + local worktree_dir=${1} + + if pages_branch_missing; then + git worktree add --quiet --detach ${worktree_dir} + pushd ${worktree_dir} > /dev/null + create_pages_branch + popd > /dev/null + else + git worktree add --quiet ${worktree_dir} "origin/${GITHUB_PAGES_BRANCH}" + fi +} + +function remove_pages_worktree() { + local worktree_dir=${1} + git worktree remove --force ${worktree_dir} + rm -rf ${worktree_dir} +} + +function push_index() { + if [[ ! -f "${CHARTS_DIR}/index.yaml" ]]; then + return -1 + fi + + local worktree=$(mktemp -d /tmp/worktree.XXXXXX) + add_pages_worktree ${worktree} + + cp ${CHARTS_DIR}/index.yaml ${worktree}/ + pushd ${worktree} > /dev/null + git add index.yaml + git commit --quiet --message "Update index.yaml" + git push --quiet origin HEAD:refs/heads/$GITHUB_PAGES_BRANCH + popd > /dev/null + + remove_pages_worktree ${worktree} +} + +push_index + + From ed2b78288870649e29afb7225e8b27d429b85e42 Mon Sep 17 00:00:00 2001 From: Aliaksei Kozich Date: Tue, 19 Jan 2021 15:31:44 +0300 Subject: [PATCH 2/2] Restart build