blob: 0fdb3ec8c09caa7a473e4ccc834fd60190be102d [file] [log] [blame]
Luca Prete1b823d62018-12-13 17:33:47 -08001#!/usr/bin/env bash
2
3# Copyright 2018-present Open Networking Foundation
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# helmrepo.sh
Zack Williams48542de2018-12-19 17:26:41 -070018# creates or updates a helm repo for publishing on the guide website
19# Reference: https://github.com/helm/charts/blob/master/test/repo-sync.sh
Luca Prete1b823d62018-12-13 17:33:47 -080020
21set -eu -o pipefail
22
Zack Williams48542de2018-12-19 17:26:41 -070023echo "# helmrepo.sh, using helm: $(helm version -c) #"
24
Luca Prete1b823d62018-12-13 17:33:47 -080025# when not running under Jenkins, use current dir as workspace
26WORKSPACE=${WORKSPACE:-.}
27
Zack Williams48542de2018-12-19 17:26:41 -070028# directory to compare against, doesn't need to be present
29OLD_REPO_DIR="${OLD_REPO_DIR:-cord-charts-repo}"
30NEW_REPO_DIR="${NEW_REPO_DIR:-chart_repo}"
Luca Prete1b823d62018-12-13 17:33:47 -080031
Luca Prete138c7762018-12-14 14:16:14 -080032PUBLISH_URL="${PUBLISH_URL:-charts.opencord.org}"
Luca Prete1b823d62018-12-13 17:33:47 -080033
Zack Williams48542de2018-12-19 17:26:41 -070034# create and clean NEW_REPO_DIR
35mkdir -p "${NEW_REPO_DIR}"
36rm -f "${NEW_REPO_DIR}"/*
Luca Prete94d95192018-12-14 09:56:00 -080037
Zack Williams48542de2018-12-19 17:26:41 -070038# if OLD_REPO_DIR doesn't exist, generate packages and index in NEW_REPO_DIR
39if [ ! -d "${OLD_REPO_DIR}" ]
40then
41 echo "Creating new helm repo: ${NEW_REPO_DIR}"
Luca Prete1b823d62018-12-13 17:33:47 -080042
Zack Williams48542de2018-12-19 17:26:41 -070043 while IFS= read -r -d '' chart
44 do
Zack Williams66dea082019-01-02 16:10:53 -070045 chartdir=$(dirname "${chart#${WORKSPACE}/}")
Zack Williams48542de2018-12-19 17:26:41 -070046 helm package --dependency-update --destination "${NEW_REPO_DIR}" "${chartdir}"
Luca Prete1b823d62018-12-13 17:33:47 -080047
Zack Williams48542de2018-12-19 17:26:41 -070048 done < <(find "${WORKSPACE}" -name Chart.yaml -print0)
Luca Prete1b823d62018-12-13 17:33:47 -080049
Zack Williams48542de2018-12-19 17:26:41 -070050 helm repo index "${NEW_REPO_DIR}" --url https://"${PUBLISH_URL}"
51 echo "# helmrepo.sh Success! Generated new repo index in ${NEW_REPO_DIR}"
Luca Prete1b823d62018-12-13 17:33:47 -080052
Zack Williams48542de2018-12-19 17:26:41 -070053else
54 # OLD_REPO_DIR exists, check for new charts and update only with changes
55 echo "Found existing helm repo: ${OLD_REPO_DIR}, attempting update"
Luca Prete1b823d62018-12-13 17:33:47 -080056
Zack Williams48542de2018-12-19 17:26:41 -070057 # Loop and create chart packages, only if changed
58 while IFS= read -r -d '' chart
59 do
Zack Williams66dea082019-01-02 16:10:53 -070060 chartdir=$(dirname "${chart#${WORKSPACE}/}")
Luca Prete1b823d62018-12-13 17:33:47 -080061
Zack Williams48542de2018-12-19 17:26:41 -070062 # See if chart version changed from previous HEAD commit
Zack Williams6a1c64a2019-01-23 12:08:33 -070063 chart_yaml_diff=$(git diff -p HEAD^ -- "${chartdir}/Chart.yaml")
Luca Prete05ba35b2018-12-14 11:08:12 -080064
Zack Williams48542de2018-12-19 17:26:41 -070065 if [ -n "$chart_yaml_diff" ]
66 then
67 # assumes that helmlint.sh and chart_version_check.sh have been run
68 # pre-merge, which ensures that all charts are valid and have their
69 # version updated in Chart.yaml
Zack Williamsa3586092019-01-02 17:11:56 -070070 new_version_string=$(echo "$chart_yaml_diff" | awk '/^\+version:/ { print $2 }')
Zack Williams48542de2018-12-19 17:26:41 -070071 echo "New version of chart ${chartdir}, creating package: ${new_version_string//+version:/}"
72 helm package --dependency-update --destination "${NEW_REPO_DIR}" "${chartdir}"
73 else
74 echo "Chart unchanged, not packaging: '${chartdir}'"
75 fi
Luca Prete1b823d62018-12-13 17:33:47 -080076
Zack Williams48542de2018-12-19 17:26:41 -070077 done < <(find "${WORKSPACE}" -name Chart.yaml -print0)
Luca Prete1b823d62018-12-13 17:33:47 -080078
Zack Williams48542de2018-12-19 17:26:41 -070079 # Check for collisions between old/new packages
80 while IFS= read -r -d '' package_path
81 do
82 package=$(basename "${package_path}")
83
84 if [ -f "${OLD_REPO_DIR}/${package}" ]
85 then
86 echo "# helmrepo.sh Failure! Package: ${package} with same version already exists in ${OLD_REPO_DIR}"
87 exit 1
88 fi
89 done < <(find "${NEW_REPO_DIR}" -name '*.tgz' -print0)
90
Zack Williams2f075ea2019-01-03 14:20:29 -070091 # only update index if new charts are added
92 if ls "${NEW_REPO_DIR}"/*.tgz > /dev/null 2>&1;
93 then
94 # Create updated index.yaml (new version created in NEW_REPO_DIR)
95 helm repo index --url "https://${PUBLISH_URL}" --merge "${OLD_REPO_DIR}/index.yaml" "${NEW_REPO_DIR}"
Zack Williams48542de2018-12-19 17:26:41 -070096
Zack Williams2f075ea2019-01-03 14:20:29 -070097 # move over packages and index.yaml
98 mv "${NEW_REPO_DIR}"/*.tgz "${OLD_REPO_DIR}/"
99 mv "${NEW_REPO_DIR}/index.yaml" "${OLD_REPO_DIR}/index.yaml"
Zack Williams48542de2018-12-19 17:26:41 -0700100
Zack Williams2f075ea2019-01-03 14:20:29 -0700101 echo "# helmrepo.sh Success! Updated existing repo index in ${OLD_REPO_DIR}"
102
103 else
104 echo "# helmrepo.sh Success! No new charts added."
105 fi
Zack Williams48542de2018-12-19 17:26:41 -0700106fi
107
108exit 0