pulsar-helm-chart/.ci/release.sh
Sijie Guo 0338d17b89
Publish chart index to gh-pages branch (#3)
*Motivation*

Release helm chart when new tags are created
2020-04-21 02:44:58 -07:00

123 lines
3.5 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
#
BINDIR=`dirname "$0"`
CHARTS_HOME=`cd ${BINDIR}/..;pwd`
CHARTS_PKGS=${CHARTS_HOME}/.chart-packages
CHARTS_INDEX=${CHARTS_HOME}/.chart-index
CHARTS_REPO=${CHARTS_REPO:-"https://pulsar.apache.org/charts/"}
OWNER=${OWNER:-apache}
REPO=${REPO:-pulsar-helm-chart}
PUBLISH_CHARTS=${PUBLISH_CHARTS:-"false"}
# hack/common.sh need this variable to be set
PULSAR_CHART_HOME=${CHARTS_HOME}
source ${CHARTS_HOME}/hack/common.sh
source ${CHARTS_HOME}/.ci/git.sh
# allow overwriting cr binary
CR="docker run -v ${CHARTS_HOME}:/cr quay.io/helmpack/chart-releaser:v${CR_VERSION} cr"
function release::ensure_dir() {
local dir=$1
if [[ -d ${dir} ]]; then
rm -rf ${dir}
fi
mkdir -p ${dir}
}
function release::find_changed_charts() {
local charts_dir=$1
echo $(git diff --find-renames --name-only "$latest_tag_rev" -- ${charts_dir} | cut -d '/' -f 2 | uniq)
}
function release::package_chart() {
local chart=$1
echo "Packaging chart '$chart'..."
helm package ${CHARTS_HOME}/charts/$chart --destination ${CHARTS_PKGS}
}
function release::upload_packages() {
${CR} upload --owner ${OWNER} --git-repo ${REPO} -t ${GITHUB_TOKEN} --package-path /cr/.chart-packages
}
function release::update_chart_index() {
${CR} index -o ${OWNER} -r ${REPO} -t "${GITHUB_TOKEN}" -c ${CHARTS_REPO} --index-path /cr/.chart-index --package-path /cr/.chart-packages
}
function release::git_setup() {
cat <<- EOF > $HOME/.netrc
machine github.com
login $GITHUB_ACTOR
password $GITHUB_TOKEN
machine api.github.com
login $GITHUB_ACTOR
password $GITHUB_TOKEN
EOF
chmod 600 $HOME/.netrc
git config --global user.email "$GITHUB_ACTOR@users.noreply.github.com"
git config --global user.name "$GITHUB_ACTOR"
}
function release::publish_charts() {
release::git_setup
git remote update
git fetch --all
git checkout gh-pages
cp --force ${CHARTS_INDEX}/index.yaml index.yaml
git add index.yaml
git commit --message="Publish new charts to ${CHARTS_REPO}" --signoff
git push --set-upstream origin gh-pages
}
# install cr
# hack::ensure_cr
docker pull quay.io/helmpack/chart-releaser:v${CR_VERSION}
latest_tag=$(git::find_latest_tag)
echo "Latest tag: $latest_tag"
latest_tag_rev=$(git::get_revision "$latest_tag")
echo "$latest_tag_rev $latest_tag (latest tag)"
head_rev=$(git::get_revision HEAD)
echo "$head_rev HEAD"
if [[ "$latest_tag_rev" == "$head_rev" ]]; then
echo "Do nothing. Exiting ..."
exit
fi
release::ensure_dir ${CHARTS_PKGS}
release::ensure_dir ${CHARTS_INDEX}
for chart in $(release::find_changed_charts charts); do
release::package_chart ${chart}
done
release::upload_packages
release::update_chart_index
if [[ "x${PUBLISH_CHARTS}" == "xtrue" ]]; then
release::publish_charts
fi