Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
Joey Armstrong | aebf185 | 2022-12-09 08:34:13 -0500 | [diff] [blame^] | 3 | # Copyright 2018-2023 Open Networking Foundation (ONF) and the ONF Contributors |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 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 | # directory to compare against, doesn't need to be present |
| 29 | OLD_REPO_DIR="${OLD_REPO_DIR:-cord-charts-repo}" |
| 30 | NEW_REPO_DIR="${NEW_REPO_DIR:-chart_repo}" |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 31 | |
Luca Prete | 138c776 | 2018-12-14 14:16:14 -0800 | [diff] [blame] | 32 | PUBLISH_URL="${PUBLISH_URL:-charts.opencord.org}" |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 33 | |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 34 | # create and clean NEW_REPO_DIR |
| 35 | mkdir -p "${NEW_REPO_DIR}" |
| 36 | rm -f "${NEW_REPO_DIR}"/* |
Luca Prete | 94d9519 | 2018-12-14 09:56:00 -0800 | [diff] [blame] | 37 | |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 38 | # if OLD_REPO_DIR doesn't exist, generate packages and index in NEW_REPO_DIR |
| 39 | if [ ! -d "${OLD_REPO_DIR}" ] |
| 40 | then |
| 41 | echo "Creating new helm repo: ${NEW_REPO_DIR}" |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 42 | |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 43 | while IFS= read -r -d '' chart |
| 44 | do |
Zack Williams | 66dea08 | 2019-01-02 16:10:53 -0700 | [diff] [blame] | 45 | chartdir=$(dirname "${chart#${WORKSPACE}/}") |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 46 | helm package --dependency-update --destination "${NEW_REPO_DIR}" "${chartdir}" |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 47 | |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 48 | done < <(find "${WORKSPACE}" -name Chart.yaml -print0) |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 49 | |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 50 | helm repo index "${NEW_REPO_DIR}" --url https://"${PUBLISH_URL}" |
| 51 | echo "# helmrepo.sh Success! Generated new repo index in ${NEW_REPO_DIR}" |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 52 | |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 53 | else |
| 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 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 | # Loop and create chart packages, only if changed |
| 58 | while IFS= read -r -d '' chart |
| 59 | do |
Zack Williams | 66dea08 | 2019-01-02 16:10:53 -0700 | [diff] [blame] | 60 | chartdir=$(dirname "${chart#${WORKSPACE}/}") |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 61 | |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 62 | # See if chart version changed from previous HEAD commit |
Zack Williams | 6a1c64a | 2019-01-23 12:08:33 -0700 | [diff] [blame] | 63 | chart_yaml_diff=$(git diff -p HEAD^ -- "${chartdir}/Chart.yaml") |
Luca Prete | 05ba35b | 2018-12-14 11:08:12 -0800 | [diff] [blame] | 64 | |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 65 | 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 Williams | a358609 | 2019-01-02 17:11:56 -0700 | [diff] [blame] | 70 | new_version_string=$(echo "$chart_yaml_diff" | awk '/^\+version:/ { print $2 }') |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 71 | 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 Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 76 | |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 77 | done < <(find "${WORKSPACE}" -name Chart.yaml -print0) |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 78 | |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 79 | # 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 Williams | 2f075ea | 2019-01-03 14:20:29 -0700 | [diff] [blame] | 91 | # 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 Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 96 | |
Zack Williams | 2f075ea | 2019-01-03 14:20:29 -0700 | [diff] [blame] | 97 | # 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 Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 100 | |
Zack Williams | 2f075ea | 2019-01-03 14:20:29 -0700 | [diff] [blame] | 101 | 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 Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 106 | fi |
| 107 | |
| 108 | exit 0 |