blob: 8c34d571d9b2c8f3959f4cebace40437273f8b86 [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# branch to compare against, defaults to master
29GERRIT_BRANCH=${GERRIT_BRANCH:-opencord/master}
30
31# directory to compare against, doesn't need to be present
32OLD_REPO_DIR="${OLD_REPO_DIR:-cord-charts-repo}"
33NEW_REPO_DIR="${NEW_REPO_DIR:-chart_repo}"
Luca Prete1b823d62018-12-13 17:33:47 -080034
35GERRIT_BRANCH="${GERRIT_BRANCH:-$(git symbolic-ref --short HEAD)}"
Luca Prete138c7762018-12-14 14:16:14 -080036PUBLISH_URL="${PUBLISH_URL:-charts.opencord.org}"
Luca Prete1b823d62018-12-13 17:33:47 -080037
Zack Williams48542de2018-12-19 17:26:41 -070038# create and clean NEW_REPO_DIR
39mkdir -p "${NEW_REPO_DIR}"
40rm -f "${NEW_REPO_DIR}"/*
Luca Prete94d95192018-12-14 09:56:00 -080041
Zack Williams48542de2018-12-19 17:26:41 -070042# if OLD_REPO_DIR doesn't exist, generate packages and index in NEW_REPO_DIR
43if [ ! -d "${OLD_REPO_DIR}" ]
44then
45 echo "Creating new helm repo: ${NEW_REPO_DIR}"
Luca Prete1b823d62018-12-13 17:33:47 -080046
Zack Williams48542de2018-12-19 17:26:41 -070047 while IFS= read -r -d '' chart
48 do
Zack Williams66dea082019-01-02 16:10:53 -070049 chartdir=$(dirname "${chart#${WORKSPACE}/}")
Zack Williams48542de2018-12-19 17:26:41 -070050 helm package --dependency-update --destination "${NEW_REPO_DIR}" "${chartdir}"
Luca Prete1b823d62018-12-13 17:33:47 -080051
Zack Williams48542de2018-12-19 17:26:41 -070052 done < <(find "${WORKSPACE}" -name Chart.yaml -print0)
Luca Prete1b823d62018-12-13 17:33:47 -080053
Zack Williams48542de2018-12-19 17:26:41 -070054 helm repo index "${NEW_REPO_DIR}" --url https://"${PUBLISH_URL}"
55 echo "# helmrepo.sh Success! Generated new repo index in ${NEW_REPO_DIR}"
Luca Prete1b823d62018-12-13 17:33:47 -080056
Zack Williams48542de2018-12-19 17:26:41 -070057else
58 # OLD_REPO_DIR exists, check for new charts and update only with changes
59 echo "Found existing helm repo: ${OLD_REPO_DIR}, attempting update"
Luca Prete1b823d62018-12-13 17:33:47 -080060
Zack Williams48542de2018-12-19 17:26:41 -070061 # Loop and create chart packages, only if changed
62 while IFS= read -r -d '' chart
63 do
Zack Williams66dea082019-01-02 16:10:53 -070064 chartdir=$(dirname "${chart#${WORKSPACE}/}")
Luca Prete1b823d62018-12-13 17:33:47 -080065
Zack Williams48542de2018-12-19 17:26:41 -070066 # See if chart version changed from previous HEAD commit
67 chart_yaml_diff=$(git diff -p HEAD^ "${chartdir}/Chart.yaml")
Luca Prete05ba35b2018-12-14 11:08:12 -080068
Zack Williams48542de2018-12-19 17:26:41 -070069 if [ -n "$chart_yaml_diff" ]
70 then
71 # assumes that helmlint.sh and chart_version_check.sh have been run
72 # pre-merge, which ensures that all charts are valid and have their
73 # version updated in Chart.yaml
74 new_version_string=$(echo "$chart_yaml_diff" | grep -E '\+version:\s*\d+\.\d+\.\d+$')
75 echo "New version of chart ${chartdir}, creating package: ${new_version_string//+version:/}"
76 helm package --dependency-update --destination "${NEW_REPO_DIR}" "${chartdir}"
77 else
78 echo "Chart unchanged, not packaging: '${chartdir}'"
79 fi
Luca Prete1b823d62018-12-13 17:33:47 -080080
Zack Williams48542de2018-12-19 17:26:41 -070081 done < <(find "${WORKSPACE}" -name Chart.yaml -print0)
Luca Prete1b823d62018-12-13 17:33:47 -080082
Zack Williams48542de2018-12-19 17:26:41 -070083 # Check for collisions between old/new packages
84 while IFS= read -r -d '' package_path
85 do
86 package=$(basename "${package_path}")
87
88 if [ -f "${OLD_REPO_DIR}/${package}" ]
89 then
90 echo "# helmrepo.sh Failure! Package: ${package} with same version already exists in ${OLD_REPO_DIR}"
91 exit 1
92 fi
93 done < <(find "${NEW_REPO_DIR}" -name '*.tgz' -print0)
94
95 # Create updated index.yaml (new version created in NEW_REPO_DIR)
96 helm repo index --url "https://${PUBLISH_URL}" --merge "${OLD_REPO_DIR}/index.yaml" "${NEW_REPO_DIR}"
97
98 # move over packages and index.yaml
99 mv "${NEW_REPO_DIR}"/*.tgz "${OLD_REPO_DIR}/"
100 mv "${NEW_REPO_DIR}/index.yaml" "${OLD_REPO_DIR}/index.yaml"
101
102 echo "# helmrepo.sh Success! Updated existing repo index in ${OLD_REPO_DIR}"
103fi
104
105exit 0