Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 1 | #!/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 Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame^] | 18 | # 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 Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 20 | |
| 21 | set -eu -o pipefail |
| 22 | |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame^] | 23 | echo "# helmrepo.sh, using helm: $(helm version -c) #" |
| 24 | |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 25 | # when not running under Jenkins, use current dir as workspace |
| 26 | WORKSPACE=${WORKSPACE:-.} |
| 27 | |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame^] | 28 | # branch to compare against, defaults to master |
| 29 | GERRIT_BRANCH=${GERRIT_BRANCH:-opencord/master} |
| 30 | |
| 31 | # directory to compare against, doesn't need to be present |
| 32 | OLD_REPO_DIR="${OLD_REPO_DIR:-cord-charts-repo}" |
| 33 | NEW_REPO_DIR="${NEW_REPO_DIR:-chart_repo}" |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 34 | |
| 35 | GERRIT_BRANCH="${GERRIT_BRANCH:-$(git symbolic-ref --short HEAD)}" |
Luca Prete | 138c776 | 2018-12-14 14:16:14 -0800 | [diff] [blame] | 36 | PUBLISH_URL="${PUBLISH_URL:-charts.opencord.org}" |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 37 | |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame^] | 38 | # create and clean NEW_REPO_DIR |
| 39 | mkdir -p "${NEW_REPO_DIR}" |
| 40 | rm -f "${NEW_REPO_DIR}"/* |
Luca Prete | 94d9519 | 2018-12-14 09:56:00 -0800 | [diff] [blame] | 41 | |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame^] | 42 | # if OLD_REPO_DIR doesn't exist, generate packages and index in NEW_REPO_DIR |
| 43 | if [ ! -d "${OLD_REPO_DIR}" ] |
| 44 | then |
| 45 | echo "Creating new helm repo: ${NEW_REPO_DIR}" |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 46 | |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame^] | 47 | while IFS= read -r -d '' chart |
| 48 | do |
| 49 | chartdir=$(dirname "${chart#./}") |
| 50 | helm package --dependency-update --destination "${NEW_REPO_DIR}" "${chartdir}" |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 51 | |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame^] | 52 | done < <(find "${WORKSPACE}" -name Chart.yaml -print0) |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 53 | |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame^] | 54 | helm repo index "${NEW_REPO_DIR}" --url https://"${PUBLISH_URL}" |
| 55 | echo "# helmrepo.sh Success! Generated new repo index in ${NEW_REPO_DIR}" |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 56 | |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame^] | 57 | else |
| 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 Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 60 | |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame^] | 61 | # Loop and create chart packages, only if changed |
| 62 | while IFS= read -r -d '' chart |
| 63 | do |
| 64 | chartdir=$(dirname "${chart#./}") |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 65 | |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame^] | 66 | # See if chart version changed from previous HEAD commit |
| 67 | chart_yaml_diff=$(git diff -p HEAD^ "${chartdir}/Chart.yaml") |
Luca Prete | 05ba35b | 2018-12-14 11:08:12 -0800 | [diff] [blame] | 68 | |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame^] | 69 | 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 Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 80 | |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame^] | 81 | done < <(find "${WORKSPACE}" -name Chart.yaml -print0) |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 82 | |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame^] | 83 | # 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}" |
| 103 | fi |
| 104 | |
| 105 | exit 0 |