|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# See README.adoc |
| 4 | +# |
| 5 | +set -euo pipefail |
| 6 | +set -x |
| 7 | +#----------------------------------------------------------- |
| 8 | +# tags should be semver-compatible e.g. 23.1.1 not 23.01.1 |
| 9 | +# this is needed for cargo commands to work properly |
| 10 | +#----------------------------------------------------------- |
| 11 | +TAG_REGEX="^[0-9][0-9]\.([1-9]|[1][0-2])\.[0-9]+$" |
| 12 | +REPOSITORY="origin" |
| 13 | + |
| 14 | +tag_products() { |
| 15 | + cd "$TEMP_RELEASE_FOLDER/$DOCKER_IMAGES_REPO" |
| 16 | + git switch "$RELEASE_BRANCH" |
| 17 | + git tag "$RELEASE_TAG" |
| 18 | + push_branch |
| 19 | +} |
| 20 | + |
| 21 | +tag_operators() { |
| 22 | + while IFS="" read -r operator || [ -n "$operator" ] |
| 23 | + do |
| 24 | + cd "${TEMP_RELEASE_FOLDER}/${operator}" |
| 25 | + git switch "$RELEASE_BRANCH" |
| 26 | + |
| 27 | + # Update git submodules if needed |
| 28 | + if [ -f .gitmodules ]; then |
| 29 | + git submodule update --recursive --init |
| 30 | + fi |
| 31 | + |
| 32 | + git tag "$RELEASE_TAG" |
| 33 | + push_branch |
| 34 | + done < <(yq '... comments="" | .operators[] ' "$INITIAL_DIR"/release/config.yaml) |
| 35 | +} |
| 36 | + |
| 37 | +tag_repos() { |
| 38 | + if [ "products" == "$WHAT" ] || [ "both" == "$WHAT" ]; then |
| 39 | + tag_products |
| 40 | + fi |
| 41 | + if [ "operators" == "$WHAT" ] || [ "both" == "$WHAT" ]; then |
| 42 | + tag_operators |
| 43 | + fi |
| 44 | +} |
| 45 | + |
| 46 | +check_products() { |
| 47 | + #------------------------------------------------------------- |
| 48 | + # $TEMP_RELEASE_FOLDER has been created in the calling routine |
| 49 | + #------------------------------------------------------------- |
| 50 | + git clone "git@github.com:stackabletech/${DOCKER_IMAGES_REPO}.git" "$TEMP_RELEASE_FOLDER/$DOCKER_IMAGES_REPO" |
| 51 | + cd "$TEMP_RELEASE_FOLDER/$DOCKER_IMAGES_REPO" |
| 52 | + #--------------------------------------- |
| 53 | + # switch to the release branch |
| 54 | + # N.B. look for exact match |
| 55 | + #--------------------------------------- |
| 56 | + BRANCH_EXISTS=$(git branch -r | grep -E "$RELEASE_BRANCH$") |
| 57 | + |
| 58 | + if [ -z "${BRANCH_EXISTS}" ]; then |
| 59 | + echo "Expected release branch is missing: $RELEASE_BRANCH" |
| 60 | + exit 1 |
| 61 | + fi |
| 62 | + |
| 63 | + git switch "${RELEASE_BRANCH}" |
| 64 | + git fetch --tags |
| 65 | + #--------------------------------------- |
| 66 | + # check tags: N.B. look for exact match |
| 67 | + #--------------------------------------- |
| 68 | + TAG_EXISTS=$(git tag -l | grep -E "$RELEASE_TAG$") |
| 69 | + if [ -n "$TAG_EXISTS" ]; then |
| 70 | + echo "Tag $RELEASE_TAG already exists in $DOCKER_IMAGES_REPO" |
| 71 | + exit 1 |
| 72 | + fi |
| 73 | +} |
| 74 | + |
| 75 | +check_operators() { |
| 76 | + #------------------------------------------------------------- |
| 77 | + # $TEMP_RELEASE_FOLDER has been created in the calling routine |
| 78 | + #------------------------------------------------------------- |
| 79 | + while IFS="" read -r operator || [ -n "$operator" ] |
| 80 | + do |
| 81 | + echo "Operator: $operator" |
| 82 | + git clone "git@github.com:stackabletech/${operator}.git" "$TEMP_RELEASE_FOLDER/${operator}" |
| 83 | + cd "$TEMP_RELEASE_FOLDER/${operator}" |
| 84 | + |
| 85 | + BRANCH_EXISTS=$(git branch -r | grep -E "$RELEASE_BRANCH$") |
| 86 | + |
| 87 | + if [ -z "${BRANCH_EXISTS}" ]; then |
| 88 | + echo "Expected release branch is missing: ${operator}/$RELEASE_BRANCH" |
| 89 | + exit 1 |
| 90 | + fi |
| 91 | + git switch "${RELEASE_BRANCH}" |
| 92 | + |
| 93 | + git fetch --tags |
| 94 | + TAG_EXISTS=$(git tag -l | grep -E "$RELEASE_TAG$") |
| 95 | + if [ -n "${TAG_EXISTS}" ]; then |
| 96 | + echo "Tag $RELEASE_TAG already exists in ${operator}" |
| 97 | + exit 1 |
| 98 | + fi |
| 99 | + done < <(yq '... comments="" | .operators[] ' "$INITIAL_DIR"/release/config.yaml) |
| 100 | +} |
| 101 | + |
| 102 | +checks() { |
| 103 | + if [ "products" == "$WHAT" ] || [ "both" == "$WHAT" ]; then |
| 104 | + check_products |
| 105 | + fi |
| 106 | + if [ "operators" == "$WHAT" ] || [ "both" == "$WHAT" ]; then |
| 107 | + check_operators |
| 108 | + fi |
| 109 | +} |
| 110 | + |
| 111 | + |
| 112 | +push_branch() { |
| 113 | + if $PUSH; then |
| 114 | + echo "Pushing changes..." |
| 115 | + git push "${REPOSITORY}" "${RELEASE_BRANCH}" |
| 116 | + git push "${REPOSITORY}" "${RELEASE_TAG}" |
| 117 | + git switch main |
| 118 | + else |
| 119 | + echo "(Dry-run: not pushing...)" |
| 120 | + fi |
| 121 | +} |
| 122 | + |
| 123 | +cleanup() { |
| 124 | + if $CLEANUP; then |
| 125 | + echo "Cleaning up..." |
| 126 | + rm -rf "$TEMP_RELEASE_FOLDER" |
| 127 | + fi |
| 128 | +} |
| 129 | + |
| 130 | +parse_inputs() { |
| 131 | + RELEASE_TAG="" |
| 132 | + PUSH=false |
| 133 | + CLEANUP=false |
| 134 | + WHAT="" |
| 135 | + |
| 136 | + while [[ "$#" -gt 0 ]]; do |
| 137 | + case $1 in |
| 138 | + -t|--tag) RELEASE_TAG="$2"; shift ;; |
| 139 | + -w|--what) WHAT="$2"; shift ;; |
| 140 | + -p|--push) PUSH=true ;; |
| 141 | + -c|--cleanup) CLEANUP=true ;; |
| 142 | + *) echo "Unknown parameter passed: $1"; exit 1 ;; |
| 143 | + esac |
| 144 | + shift |
| 145 | + done |
| 146 | + #----------------------------------------------------------- |
| 147 | + # remove leading and trailing quotes |
| 148 | + #----------------------------------------------------------- |
| 149 | + RELEASE_TAG="${RELEASE_TAG%\"}" |
| 150 | + RELEASE_TAG="${RELEASE_TAG#\"}" |
| 151 | + #---------------------------------------------------------------------------------------------------- |
| 152 | + # for a tag of e.g. 23.1.1, the release branch (already created) will be 23.1 |
| 153 | + #---------------------------------------------------------------------------------------------------- |
| 154 | + RELEASE="$(cut -d'.' -f1,2 <<< "$RELEASE_TAG")" |
| 155 | + RELEASE_BRANCH="release-$RELEASE" |
| 156 | + |
| 157 | + INITIAL_DIR="$PWD" |
| 158 | + DOCKER_IMAGES_REPO=$(yq '... comments="" | .images-repo ' "$INITIAL_DIR"/release/config.yaml) |
| 159 | + TEMP_RELEASE_FOLDER="/tmp/stackable-$RELEASE_BRANCH" |
| 160 | + |
| 161 | + echo "Settings: ${RELEASE_BRANCH}: Push: $PUSH: Cleanup: $CLEANUP" |
| 162 | +} |
| 163 | + |
| 164 | +main() { |
| 165 | + parse_inputs "$@" |
| 166 | + #----------------------------------------------------------- |
| 167 | + # check if tag argument provided |
| 168 | + #----------------------------------------------------------- |
| 169 | + if [ -z "${RELEASE_TAG}" ]; then |
| 170 | + echo "Usage: create-release-tag.sh -t <tag>" |
| 171 | + exit 1 |
| 172 | + fi |
| 173 | + #----------------------------------------------------------- |
| 174 | + # check if argument matches our tag regex |
| 175 | + #----------------------------------------------------------- |
| 176 | + if [[ ! $RELEASE_TAG =~ $TAG_REGEX ]]; then |
| 177 | + echo "Provided tag [$RELEASE_TAG] does not match the required tag regex pattern [$TAG_REGEX]" |
| 178 | + exit 1 |
| 179 | + fi |
| 180 | + |
| 181 | + if [ -d "$TEMP_RELEASE_FOLDER" ]; then |
| 182 | + echo "Folder already exists, please delete it!: $TEMP_RELEASE_FOLDER" |
| 183 | + exit 1 |
| 184 | + fi |
| 185 | + |
| 186 | + echo "Creating folder for cloning docker images and operators: [$TEMP_RELEASE_FOLDER]" |
| 187 | + mkdir -p "$TEMP_RELEASE_FOLDER" |
| 188 | + |
| 189 | + # sanity checks before we start: folder, branches etc. |
| 190 | + # deactivate -e so that piped commands can be used |
| 191 | + set +e |
| 192 | + checks |
| 193 | + set -e |
| 194 | + |
| 195 | + tag_repos |
| 196 | + cleanup |
| 197 | +} |
| 198 | + |
| 199 | +main "$@" |
0 commit comments