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