From fd0707c148ee5a330033c8b571f8b4158e58125e Mon Sep 17 00:00:00 2001 From: Jeff Billimek Date: Sun, 4 Aug 2019 20:40:59 -0400 Subject: [PATCH] initial circleci import --- .circleci/config.yml | 33 ++++++++++++ .circleci/install_tools.sh | 21 ++++++++ .circleci/release.sh | 107 +++++++++++++++++++++++++++++++++++++ 3 files changed, 161 insertions(+) create mode 100644 .circleci/config.yml create mode 100755 .circleci/install_tools.sh create mode 100755 .circleci/release.sh diff --git a/.circleci/config.yml b/.circleci/config.yml new file mode 100644 index 00000000..b6b64f8d --- /dev/null +++ b/.circleci/config.yml @@ -0,0 +1,33 @@ +version: 2 +jobs: + lint-scripts: + docker: + - image: koalaman/shellcheck-alpine + steps: + - checkout + - run: + name: lint + command: shellcheck -x .circleci/*.sh + release-charts: + machine: true + steps: + - checkout + - run: + command: | + echo "export GIT_REPOSITORY_URL=$CIRCLE_REPOSITORY_URL" >> $BASH_ENV + echo "export GIT_USERNAME=$CIRCLE_PROJECT_USERNAME" >> $BASH_ENV + echo "export GIT_REPOSITORY_NAME=$CIRCLE_PROJECT_REPONAME" >> $BASH_ENV + .circleci/install_tools.sh + .circleci/release.sh + +workflows: + version: 2 + release: + jobs: + - lint-scripts + - release-charts: + filters: + tags: + ignore: /.*/ + branches: + only: master \ No newline at end of file diff --git a/.circleci/install_tools.sh b/.circleci/install_tools.sh new file mode 100755 index 00000000..7f886a9d --- /dev/null +++ b/.circleci/install_tools.sh @@ -0,0 +1,21 @@ +#!/usr/bin/env bash + +set -o errexit + +readonly HELM_VERSION=2.13.1 +readonly CHART_RELEASER_VERSION=0.1.4 + +echo "Installing Helm..." +curl -LO "https://kubernetes-helm.storage.googleapis.com/helm-v$HELM_VERSION-linux-amd64.tar.gz" +sudo mkdir -p "/usr/local/helm-v$HELM_VERSION" +sudo tar -xzf "helm-v$HELM_VERSION-linux-amd64.tar.gz" -C "/usr/local/helm-v$HELM_VERSION" +sudo ln -s "/usr/local/helm-v$HELM_VERSION/linux-amd64/helm" /usr/local/bin/helm +rm -f "helm-v$HELM_VERSION-linux-amd64.tar.gz" +helm init --client-only + +echo "Installing chart-releaser..." +curl -LO "https://github.com/helm/chart-releaser/releases/download/v${CHART_RELEASER_VERSION}/chart-releaser_${CHART_RELEASER_VERSION}_Linux_x86_64.tar.gz" +sudo mkdir -p "/usr/local/chart-releaser-v$CHART_RELEASER_VERSION" +sudo tar -xzf "chart-releaser_${CHART_RELEASER_VERSION}_Linux_x86_64.tar.gz" -C "/usr/local/chart-releaser-v$CHART_RELEASER_VERSION" +sudo ln -s "/usr/local/chart-releaser-v$CHART_RELEASER_VERSION/chart-releaser" /usr/local/bin/chart-releaser +rm -f "chart-releaser_${CHART_RELEASER_VERSION}_Linux_x86_64.tar.gz" diff --git a/.circleci/release.sh b/.circleci/release.sh new file mode 100755 index 00000000..fb40bc57 --- /dev/null +++ b/.circleci/release.sh @@ -0,0 +1,107 @@ +#!/usr/bin/env bash + +set -o errexit +set -o nounset +set -o pipefail + +: "${CH_TOKEN:?Environment variable CH_TOKEN must be set}" +: "${GIT_REPOSITORY_URL:?Environment variable GIT_REPO_URL must be set}" +: "${GIT_USERNAME:?Environment variable GIT_USERNAME must be set}" +: "${GIT_EMAIL:?Environment variable GIT_EMAIL must be set}" +: "${GIT_REPOSITORY_NAME:?Environment variable GIT_REPOSITORY_NAME must be set}" + +readonly REPO_ROOT="${REPO_ROOT:-$(git rev-parse --show-toplevel)}" + +main() { + pushd "$REPO_ROOT" > /dev/null + + echo "Fetching tags..." + git fetch --tags + + local latest_tag + latest_tag=$(find_latest_tag) + + local latest_tag_rev + latest_tag_rev=$(git rev-parse --verify "$latest_tag") + echo "$latest_tag_rev $latest_tag (latest tag)" + + local head_rev + head_rev=$(git rev-parse --verify HEAD) + echo "$head_rev HEAD" + + if [[ "$latest_tag_rev" == "$head_rev" ]]; then + echo "No code changes. Nothing to release." + exit + fi + + rm -rf .deploy + mkdir -p .deploy + + echo "Identifying changed charts since tag '$latest_tag'..." + + local changed_charts=() + readarray -t changed_charts <<< "$(git diff --find-renames --name-only "$latest_tag_rev" -- charts | cut -d '/' -f 2 | uniq)" + + if [[ -n "${changed_charts[*]}" ]]; then + for chart in "${changed_charts[@]}"; do + echo "Packaging chart '$chart'..." + package_chart "charts/$chart" + done + + release_charts + sleep 5 + update_index + else + echo "Nothing to do. No chart changes detected." + fi + + popd > /dev/null +} + +find_latest_tag() { + if ! git describe --tags --abbrev=0 2> /dev/null; then + git rev-list --max-parents=0 --first-parent HEAD + fi +} + +package_chart() { + local chart="$1" + Helm dependency build "$chart" + Helm package "$chart" --destination .deploy +} + +release_charts() { + chart-releaser upload -o "$GIT_USERNAME" -r "$GIT_REPOSITORY_NAME" -p .deploy +} + +update_index() { + chart-releaser index -o "$GIT_USERNAME" -r "$GIT_REPOSITORY_NAME" -p .deploy/index.yaml + + git config user.email "$GIT_EMAIL" + git config user.name "$GIT_USERNAME" + + for file in charts/*/*.md; do + if [[ -e $file ]]; then + mkdir -p ".deploy/docs/$(dirname "$file")" + cp --force "$file" ".deploy/docs/$(dirname "$file")" + fi + done + + git checkout gh-pages + cp --force .deploy/index.yaml index.yaml + + if [[ -e ".deploy/docs/charts" ]]; then + mkdir -p charts + cp --force --recursive .deploy/docs/charts/* charts/ + fi + + git checkout master -- README.md + + if ! git diff --quiet; then + git add . + git commit --message="Update index.yaml" --signoff + git push "$GIT_REPOSITORY_URL" gh-pages + fi +} + +main