Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 1 | #!/usr/bin/env bash |
Joey Armstrong | 28eddda | 2023-01-10 03:09:34 -0500 | [diff] [blame] | 2 | # ----------------------------------------------------------------------- |
| 3 | # Copyright 2018-2023 Open Networking Foundation (ONF) and the ONF Contributors |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [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. |
Joey Armstrong | 28eddda | 2023-01-10 03:09:34 -0500 | [diff] [blame] | 16 | # |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 17 | # github-release.sh |
| 18 | # builds (with make) and uploads release artifacts (binaries, etc.) to github |
| 19 | # given a tag also create checksums files and release notes from the commit |
| 20 | # message |
Joey Armstrong | 28eddda | 2023-01-10 03:09:34 -0500 | [diff] [blame] | 21 | # ----------------------------------------------------------------------- |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 22 | |
Joey Armstrong | 7f382ef | 2023-01-25 12:00:08 -0500 | [diff] [blame] | 23 | ##-------------------## |
| 24 | ##---] GLOBALS [---## |
| 25 | ##-------------------## |
| 26 | declare -g SCRIPT_VERSION='1.0' # git changeset needed |
| 27 | declare -g TRACE=1 # uncomment to set -x |
| 28 | declare -g ARGV="$*" # archive for display |
| 29 | |
| 30 | ##--------------------## |
| 31 | ##---] INCLUDES [---## |
| 32 | ##--------------------## |
| 33 | declare -g pgmdir="${0%/*}" # dirname($script) |
Joey Armstrong | bd54b57 | 2023-01-25 14:28:10 -0500 | [diff] [blame] | 34 | echo "** ${0}: PWD=$(/bin/pwd)" |
| 35 | find . -maxdepth 1 -ls |
| 36 | |
Joey Armstrong | 7f382ef | 2023-01-25 12:00:08 -0500 | [diff] [blame] | 37 | declare -a common_args=() |
| 38 | common_args+=('--common-args-begin--') |
| 39 | common_args+=('--traputils') |
| 40 | common_args+=('--stacktrace') |
| 41 | # common_args+=('--tempdir') |
| 42 | |
| 43 | # shellcheck disable=SC1091 |
| 44 | source "${pgmdir}/common/common.sh" "${common_args[@]}" |
| 45 | |
| 46 | ## ----------------------------------------------------------------------- |
| 47 | ## Intent: Output a log banner to identify the running script/version. |
| 48 | ## ----------------------------------------------------------------------- |
| 49 | ## TODO: |
| 50 | ## o SCRIPT_VERSION => git changeset for repo:ci-managment |
| 51 | ## o echo "library version: ${env."library.libName.version"}" |
| 52 | # ----------------------------------------------------------------------- |
| 53 | # 14:18:38 > git fetch --no-tags --progress -- https://gerrit.opencord.org/ci-management.git +refs/heads/*:refs/remotes/origin/* # timeout=10 |
| 54 | # 14:18:39 Checking out Revision 50f6e0b97f449b32d32ec0e02d59642000351847 (master) |
| 55 | # ----------------------------------------------------------------------- |
| 56 | function banner() |
| 57 | { |
| 58 | local iam="${0##*/}" |
| 59 | |
| 60 | cat <<EOH |
| 61 | |
| 62 | ** ----------------------------------------------------------------------- |
| 63 | ** IAM: ${iam} :: ${FUNCNAME[0]} |
| 64 | ** ARGV: ${ARGV} |
| 65 | ** PWD: $(/bin/pwd) |
| 66 | ** NOW: $(date '+%Y/%m/%d %H:%M:%S') |
| 67 | ** VER: ${SCRIPT_VERSION:-'unknown'} |
| 68 | ** ----------------------------------------------------------------------- |
| 69 | EOH |
| 70 | |
| 71 | return |
| 72 | } |
| 73 | |
Joey Armstrong | af577ab | 2022-12-15 14:43:33 -0500 | [diff] [blame] | 74 | ## ----------------------------------------------------------------------- |
Joey Armstrong | 50f6e0b | 2023-01-24 14:14:08 -0500 | [diff] [blame] | 75 | ## Intent: |
| 76 | ## Display available command versions |
| 77 | ## A github release job is failing due to a command being mia. |
| 78 | ## ----------------------------------------------------------------------- |
| 79 | function displayCommands() |
| 80 | { |
| 81 | # which git || /bin/true |
| 82 | # git --version || /bin/true |
| 83 | # go version || /bin/true |
| 84 | # helm version || /bin/true |
| 85 | return |
| 86 | } |
| 87 | |
| 88 | ## ----------------------------------------------------------------------- |
Joey Armstrong | af577ab | 2022-12-15 14:43:33 -0500 | [diff] [blame] | 89 | ## ----------------------------------------------------------------------- |
Joey Armstrong | 1b443bf | 2023-01-24 10:22:18 -0500 | [diff] [blame] | 90 | function doDebug() |
Joey Armstrong | af577ab | 2022-12-15 14:43:33 -0500 | [diff] [blame] | 91 | { |
Joey Armstrong | 1b443bf | 2023-01-24 10:22:18 -0500 | [diff] [blame] | 92 | echo "** ${FUNCNAME[0]}: ENTER" |
Joey Armstrong | 28eddda | 2023-01-10 03:09:34 -0500 | [diff] [blame] | 93 | |
Joey Armstrong | af577ab | 2022-12-15 14:43:33 -0500 | [diff] [blame] | 94 | echo |
Joey Armstrong | 1b443bf | 2023-01-24 10:22:18 -0500 | [diff] [blame] | 95 | echo "** PWD: $(/bin/pwd)" |
| 96 | echo "** make-pre: $(/bin/ls -l)" |
| 97 | echo |
| 98 | |
| 99 | declare -p ARTIFACT_GLOB |
| 100 | declare -p RELEASE_TEMP |
Joey Armstrong | af577ab | 2022-12-15 14:43:33 -0500 | [diff] [blame] | 101 | |
| 102 | echo |
Joey Armstrong | 1b443bf | 2023-01-24 10:22:18 -0500 | [diff] [blame] | 103 | echo "** ${FUNCNAME[0]}: ARTIFACT_GLOB=${ARTIFACT_GLOB}" |
| 104 | local artifact_glob="${ARTIFACT_GLOB%/*}" |
| 105 | declare -p artifact_glob |
Joey Armstrong | bd54b57 | 2023-01-25 14:28:10 -0500 | [diff] [blame] | 106 | |
| 107 | echo "** ${FUNCNAME[0]}: PWD=$(/bin/pwd)" |
Joey Armstrong | 1b443bf | 2023-01-24 10:22:18 -0500 | [diff] [blame] | 108 | find "$artifact_glob" -print || /bin/true |
Joey Armstrong | af577ab | 2022-12-15 14:43:33 -0500 | [diff] [blame] | 109 | |
Joey Armstrong | 1b443bf | 2023-01-24 10:22:18 -0500 | [diff] [blame] | 110 | # Copy artifacts into the release temp dir |
| 111 | # shellcheck disable=SC2086 |
Joey Armstrong | 7f382ef | 2023-01-25 12:00:08 -0500 | [diff] [blame] | 112 | # cp -v "$ARTIFACT_GLOB" "$RELEASE_TEMP" |
Joey Armstrong | bd54b57 | 2023-01-25 14:28:10 -0500 | [diff] [blame] | 113 | echo "rsync -rv --checksum \"$artifact_glob\" \"$RELEASE_TEMP/.\"" |
| 114 | rsync -rv --checksum "$artifact_glob" "$RELEASE_TEMP/." |
Joey Armstrong | 1b443bf | 2023-01-24 10:22:18 -0500 | [diff] [blame] | 115 | |
Joey Armstrong | af577ab | 2022-12-15 14:43:33 -0500 | [diff] [blame] | 116 | echo |
Joey Armstrong | 1b443bf | 2023-01-24 10:22:18 -0500 | [diff] [blame] | 117 | echo "** ${FUNCNAME[0]}: RELEASE_TEMP=${RELEASE_TEMP}" |
| 118 | find "$RELEASE_TEMP" -print || /bin/true |
Joey Armstrong | af577ab | 2022-12-15 14:43:33 -0500 | [diff] [blame] | 119 | |
Joey Armstrong | 1b443bf | 2023-01-24 10:22:18 -0500 | [diff] [blame] | 120 | echo "** ${FUNCNAME[0]}: LEAVE" |
| 121 | echo |
Joey Armstrong | af577ab | 2022-12-15 14:43:33 -0500 | [diff] [blame] | 122 | return |
| 123 | } |
| 124 | |
| 125 | ##----------------## |
| 126 | ##---] MAIN [---## |
| 127 | ##----------------## |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 128 | set -eu -o pipefail |
| 129 | |
Joey Armstrong | 7f382ef | 2023-01-25 12:00:08 -0500 | [diff] [blame] | 130 | banner |
| 131 | |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 132 | # when not running under Jenkins, use current dir as workspace and a blank |
| 133 | # project name |
| 134 | WORKSPACE=${WORKSPACE:-.} |
| 135 | GERRIT_PROJECT=${GERRIT_PROJECT:-} |
| 136 | |
| 137 | # Github organization (or user) this project is published on. Project name should |
| 138 | # be the same on both Gerrit and GitHub |
| 139 | GITHUB_ORGANIZATION=${GITHUB_ORGANIZATION:-} |
| 140 | |
| 141 | # glob pattern relative to project dir matching release artifacts |
Joey Armstrong | 7f382ef | 2023-01-25 12:00:08 -0500 | [diff] [blame] | 142 | # ARTIFACT_GLOB=${ARTIFACT_GLOB:-"release/*"} # stat -- release/* not found, literal string (?) |
Joey Armstrong | 50f6e0b | 2023-01-24 14:14:08 -0500 | [diff] [blame] | 143 | ARTIFACT_GLOB=${ARTIFACT_GLOB:-"release/."} |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 144 | |
| 145 | # Temporary staging directory to copy artifacts to |
| 146 | RELEASE_TEMP="$WORKSPACE/release" |
Zack Williams | 2f8e847 | 2019-05-21 16:29:06 -0700 | [diff] [blame] | 147 | mkdir -p "$RELEASE_TEMP" |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 148 | |
| 149 | # Use "release" as the default makefile target, can be a space separated list |
| 150 | RELEASE_TARGETS=${RELEASE_TARGETS:-release} |
| 151 | |
Zack Williams | 0f39849 | 2019-10-23 16:02:24 -0700 | [diff] [blame] | 152 | |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 153 | # check that we're on a semver released version, or exit |
| 154 | pushd "$GERRIT_PROJECT" |
| 155 | GIT_VERSION=$(git tag -l --points-at HEAD) |
| 156 | |
Zack Williams | 6e070f5 | 2019-10-04 11:08:59 -0700 | [diff] [blame] | 157 | # match bare versions or v-prefixed golang style version |
| 158 | if [[ "$GIT_VERSION" =~ ^v?([0-9]+)\.([0-9]+)\.([0-9]+)$ ]] |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 159 | then |
| 160 | echo "git has a SemVer released version tag: '$GIT_VERSION'" |
| 161 | echo "Building artifacts for GitHub release." |
| 162 | else |
| 163 | echo "No SemVer released version tag found, exiting..." |
| 164 | exit 0 |
| 165 | fi |
| 166 | popd |
| 167 | |
Zack Williams | 0f39849 | 2019-10-23 16:02:24 -0700 | [diff] [blame] | 168 | # Set and handle GOPATH and PATH |
| 169 | export GOPATH=${GOPATH:-$WORKSPACE/go} |
| 170 | export PATH=$PATH:/usr/lib/go-1.12/bin:/usr/local/go/bin:$GOPATH/bin |
| 171 | |
| 172 | # To support golang projects that require GOPATH to be set and code checked out there |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 173 | # If $DEST_GOPATH is not an empty string: |
Zack Williams | 76356cb | 2019-08-30 10:44:42 -0700 | [diff] [blame] | 174 | # - create GOPATH within WORKSPACE, and destination directory within |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 175 | # - set PATH to include $GOPATH/bin and the system go binaries |
Zack Williams | 76356cb | 2019-08-30 10:44:42 -0700 | [diff] [blame] | 176 | # - move project from $WORKSPACE/$GERRIT_PROJECT to new location in $GOPATH |
| 177 | # - start release process within that directory |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 178 | |
| 179 | DEST_GOPATH=${DEST_GOPATH:-} |
Joey Armstrong | 28eddda | 2023-01-10 03:09:34 -0500 | [diff] [blame] | 180 | if [ -n "$DEST_GOPATH" ]; then |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 181 | mkdir -p "$GOPATH/src/$DEST_GOPATH" |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 182 | release_path="$GOPATH/src/$DEST_GOPATH/$GERRIT_PROJECT" |
Zack Williams | 76356cb | 2019-08-30 10:44:42 -0700 | [diff] [blame] | 183 | mv "$WORKSPACE/$GERRIT_PROJECT" "$release_path" |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 184 | else |
| 185 | release_path="$WORKSPACE/$GERRIT_PROJECT" |
| 186 | fi |
| 187 | |
| 188 | if [ ! -f "$release_path/Makefile" ]; then |
| 189 | echo "Makefile not found at $release_path!" |
| 190 | exit 1 |
| 191 | else |
Joey Armstrong | af577ab | 2022-12-15 14:43:33 -0500 | [diff] [blame] | 192 | |
Joey Armstrong | 7f382ef | 2023-01-25 12:00:08 -0500 | [diff] [blame] | 193 | declare -p release_path |
| 194 | |
| 195 | # shellcheck disable=SC2015 |
| 196 | [[ -v TRACE ]] && { set -x; } || { set +x; } # SC2015 (shellcheck -x) |
| 197 | |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 198 | pushd "$release_path" |
| 199 | |
| 200 | # Release description is sanitized version of the log message |
Joey Armstrong | af577ab | 2022-12-15 14:43:33 -0500 | [diff] [blame] | 201 | RELEASE_DESCRIPTION="$(git log -1 --pretty=%B | tr -dc "[:alnum:]\n\r\.\[\]\:\-\\\/\`\' ")" |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 202 | |
| 203 | # build the release, can be multiple space separated targets |
| 204 | # shellcheck disable=SC2086 |
Joey Armstrong | af577ab | 2022-12-15 14:43:33 -0500 | [diff] [blame] | 205 | make "$RELEASE_TARGETS" |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 206 | |
Joey Armstrong | 1b443bf | 2023-01-24 10:22:18 -0500 | [diff] [blame] | 207 | doDebug # deterine why ARTIFACT_GLOB is empty |
Joey Armstrong | 50f6e0b | 2023-01-24 14:14:08 -0500 | [diff] [blame] | 208 | |
| 209 | # Are we failing on a literal string "release/*" ? |
Joey Armstrong | 1b443bf | 2023-01-24 10:22:18 -0500 | [diff] [blame] | 210 | # cp -v "$ARTIFACT_GLOB" "$RELEASE_TEMP" |
Joey Armstrong | 7f382ef | 2023-01-25 12:00:08 -0500 | [diff] [blame] | 211 | # echo "rsync -rv --checksum \"$ARTIFACT_GLOB\" \"$RELEASE_TEMP/.\"" |
| 212 | # rsync -rv --checksum "$ARTIFACT_GLOB" "$RELEASE_TEMP/." |
Joey Armstrong | 50f6e0b | 2023-01-24 14:14:08 -0500 | [diff] [blame] | 213 | |
| 214 | echo |
Joey Armstrong | 7f382ef | 2023-01-25 12:00:08 -0500 | [diff] [blame] | 215 | echo "RELEASE_TEMP(${RELEASE_TEMP}) contains:" |
Joey Armstrong | 50f6e0b | 2023-01-24 14:14:08 -0500 | [diff] [blame] | 216 | find "$RELEASE_TEMP" -ls |
| 217 | |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 218 | # create release |
Zack Williams | 0d93d84 | 2019-05-21 17:02:49 -0700 | [diff] [blame] | 219 | echo "Creating Release: $GERRIT_PROJECT - $GIT_VERSION" |
Zack Williams | 0ba8a9b | 2020-04-30 22:25:52 -0700 | [diff] [blame] | 220 | github-release release \ |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 221 | --user "$GITHUB_ORGANIZATION" \ |
| 222 | --repo "$GERRIT_PROJECT" \ |
| 223 | --tag "$GIT_VERSION" \ |
| 224 | --name "$GERRIT_PROJECT - $GIT_VERSION" \ |
| 225 | --description "$RELEASE_DESCRIPTION" |
| 226 | |
| 227 | # handle release files |
| 228 | pushd "$RELEASE_TEMP" |
| 229 | |
| 230 | # Generate and check checksums |
Zack Williams | 2f8e847 | 2019-05-21 16:29:06 -0700 | [diff] [blame] | 231 | sha256sum -- * > checksum.SHA256 |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 232 | sha256sum -c < checksum.SHA256 |
| 233 | |
Zack Williams | 0d93d84 | 2019-05-21 17:02:49 -0700 | [diff] [blame] | 234 | echo "Checksums:" |
| 235 | cat checksum.SHA256 |
| 236 | |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 237 | # upload all files to the release |
Zack Williams | 2f8e847 | 2019-05-21 16:29:06 -0700 | [diff] [blame] | 238 | for rel_file in * |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 239 | do |
Zack Williams | 0d93d84 | 2019-05-21 17:02:49 -0700 | [diff] [blame] | 240 | echo "Uploading file: $rel_file" |
Zack Williams | 0ba8a9b | 2020-04-30 22:25:52 -0700 | [diff] [blame] | 241 | github-release upload \ |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 242 | --user "$GITHUB_ORGANIZATION" \ |
| 243 | --repo "$GERRIT_PROJECT" \ |
| 244 | --tag "$GIT_VERSION" \ |
| 245 | --name "$rel_file" \ |
| 246 | --file "$rel_file" |
| 247 | done |
Joey Armstrong | af577ab | 2022-12-15 14:43:33 -0500 | [diff] [blame] | 248 | set +x |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 249 | popd |
| 250 | |
| 251 | popd |
| 252 | fi |
Joey Armstrong | 28eddda | 2023-01-10 03:09:34 -0500 | [diff] [blame] | 253 | |
| 254 | # [SEE ALSO] |
| 255 | # ----------------------------------------------------------------------- |
| 256 | # https://www.shellcheck.net/wiki/SC2236 |
| 257 | # ----------------------------------------------------------------------- |
| 258 | |
| 259 | # [EOF] |