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 | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 23 | set -euo pipefail |
| 24 | |
| 25 | ##-------------------## |
| 26 | ##---] GLOBALS [---## |
| 27 | ##-------------------## |
| 28 | declare -g WORKSPACE |
| 29 | declare -g GERRIT_PROJECT |
Joey Armstrong | 78cecc5 | 2023-04-03 11:39:11 -0400 | [diff] [blame] | 30 | declare -g __githost=github.com |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 31 | # export DEBUG=1 |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 32 | |
| 33 | ## ----------------------------------------------------------------------- |
| 34 | ## Uncomment to activate |
| 35 | ## ----------------------------------------------------------------------- |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 36 | # declare -i -g gen_version=1 |
Joey Armstrong | 78cecc5 | 2023-04-03 11:39:11 -0400 | [diff] [blame] | 37 | # declare -i -g draft_release=1 |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 38 | |
Joey Armstrong | 41923cc | 2023-01-30 14:38:16 -0500 | [diff] [blame] | 39 | # declare -g TRACE=0 # uncomment to set -x |
| 40 | |
| 41 | # shellcheck disable=SC2015 |
| 42 | [[ -v TRACE ]] && { set -x; } || { set +x; } # SC2015 (shellcheck -x) |
| 43 | |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 44 | declare -a -g ARGV=() # Capture args to minimize globals and arg passing |
| 45 | [[ $# -gt 0 ]] && ARGV=("$@") |
Joey Armstrong | d99e3d2 | 2023-01-29 12:35:43 -0500 | [diff] [blame] | 46 | |
| 47 | declare -g scratch # temp workspace for downloads |
Joey Armstrong | d99e3d2 | 2023-01-29 12:35:43 -0500 | [diff] [blame] | 48 | |
Joey Armstrong | 78cecc5 | 2023-04-03 11:39:11 -0400 | [diff] [blame] | 49 | declare -g SCRIPT_VERSION='1.3' # git changeset needed |
Joey Armstrong | 26707f0 | 2023-01-26 12:41:12 -0500 | [diff] [blame] | 50 | |
Joey Armstrong | 7f382ef | 2023-01-25 12:00:08 -0500 | [diff] [blame] | 51 | ##--------------------## |
| 52 | ##---] INCLUDES [---## |
Joey Armstrong | 1962bcf | 2023-01-27 13:53:18 -0500 | [diff] [blame] | 53 | ##--------------------# |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 54 | |
| 55 | ## ----------------------------------------------------------------------- |
| 56 | ## Intent: Register an interrupt handler to display a stack trace on error |
| 57 | ## ----------------------------------------------------------------------- |
| 58 | function errexit() |
| 59 | { |
| 60 | local err=$? |
| 61 | set +o xtrace |
| 62 | local code="${1:-1}" |
| 63 | |
| 64 | local prefix="${BASH_SOURCE[1]}:${BASH_LINENO[0]}" |
| 65 | echo -e "\nOFFENDER: ${prefix}" |
| 66 | if [ $# -gt 0 ] && [ "$1" == '--stacktrace-quiet' ]; then |
| 67 | code=1 |
| 68 | else |
| 69 | echo "ERROR: '${BASH_COMMAND}' exited with status $err" |
| 70 | fi |
| 71 | |
| 72 | # Print out the stack trace described by $function_stack |
| 73 | if [ ${#FUNCNAME[@]} -gt 2 ] |
| 74 | then |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 75 | echo "Call tree:" |
| 76 | for ((i=1;i<${#FUNCNAME[@]}-1;i++)) |
| 77 | do |
| 78 | echo " $i: ${BASH_SOURCE[$i+1]}:${BASH_LINENO[$i]} ${FUNCNAME[$i]}(...)" |
| 79 | done |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 80 | fi |
| 81 | |
| 82 | echo "Exiting with status ${code}" |
| 83 | echo |
| 84 | exit "${code}" |
| 85 | # return |
| 86 | } |
| 87 | |
| 88 | # trap ERR to provide an error handler whenever a command exits nonzero |
| 89 | # this is a more verbose version of set -o errexit |
| 90 | trap errexit ERR |
| 91 | |
| 92 | # setting errtrace allows our ERR trap handler to be propagated to functions, |
| 93 | # expansions and subshells |
| 94 | set -o errtrace |
Joey Armstrong | 7f382ef | 2023-01-25 12:00:08 -0500 | [diff] [blame] | 95 | |
| 96 | ## ----------------------------------------------------------------------- |
Joey Armstrong | d99e3d2 | 2023-01-29 12:35:43 -0500 | [diff] [blame] | 97 | ## Intent: Cleanup scratch area on exit |
| 98 | ## ----------------------------------------------------------------------- |
| 99 | function sigtrap() |
| 100 | { |
| 101 | ## Prevent mishaps |
| 102 | local is_read_only |
| 103 | is_read_only="$(declare -p scratch)" |
| 104 | if [[ $is_read_only != *"declare -r scratch="* ]]; then |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 105 | echo "ERROR: variable scratch is not read-only, cleanup skipped" |
| 106 | exit 1 |
Joey Armstrong | d99e3d2 | 2023-01-29 12:35:43 -0500 | [diff] [blame] | 107 | fi |
| 108 | |
| 109 | if [ -d "$scratch" ]; then |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 110 | /bin/rm -fr "$scratch" |
Joey Armstrong | d99e3d2 | 2023-01-29 12:35:43 -0500 | [diff] [blame] | 111 | fi |
| 112 | |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 113 | do_logout |
Joey Armstrong | d99e3d2 | 2023-01-29 12:35:43 -0500 | [diff] [blame] | 114 | return |
| 115 | } |
| 116 | trap sigtrap EXIT |
| 117 | |
| 118 | ## ----------------------------------------------------------------------- |
Joey Armstrong | ad7b1e0 | 2023-03-27 11:55:48 -0400 | [diff] [blame] | 119 | ## Intent: Return a release version for queries |
| 120 | ## Note: Do not use in production, function is intended for interactive use |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 121 | ## ----------------------------------------------------------------------- |
| 122 | function get_version() |
| 123 | { |
| 124 | declare -n ref="$1" |
| 125 | |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 126 | declare -a rev=() |
| 127 | rev+=("$(( RANDOM % 10 + 1 ))") |
| 128 | rev+=("$(( RANDOM % 256 + 1 ))") |
Joey Armstrong | eef8c2c | 2023-03-27 17:27:43 -0400 | [diff] [blame] | 129 | rev+=("$(( RANDOM % 10000 + 1 ))") |
Joey Armstrong | fc20ed5 | 2023-04-03 19:37:58 -0400 | [diff] [blame] | 130 | local ver="v${rev[0]}.${rev[1]}.${rev[2]}" |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 131 | |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 132 | func_echo "VERSION: $ver" |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 133 | ref="$ver" |
| 134 | return |
| 135 | } |
| 136 | |
| 137 | ## ----------------------------------------------------------------------- |
| 138 | ## Intent: Provide defaults for environment variables |
| 139 | ## ----------------------------------------------------------------------- |
| 140 | function initEnvVars() |
| 141 | { |
| 142 | # when not running under Jenkins, use current dir as workspace and a blank |
| 143 | # project name |
| 144 | declare -g WORKSPACE=${WORKSPACE:-.} |
| 145 | declare -g GERRIT_PROJECT=${GERRIT_PROJECT:-} |
Joey Armstrong | 76026b7 | 2023-03-29 12:19:17 -0400 | [diff] [blame] | 146 | |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 147 | # Github organization (or user) this project is published on. Project name should |
| 148 | # be the same on both Gerrit and GitHub |
Joey Armstrong | 76026b7 | 2023-03-29 12:19:17 -0400 | [diff] [blame] | 149 | declare -g GITHUB_ORGANIZATION=${GITHUB_ORGANIZATION:-} |
| 150 | |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 151 | # glob pattern relative to project dir matching release artifacts |
| 152 | # ARTIFACT_GLOB=${ARTIFACT_GLOB:-"release/*"} # stat -- release/* not found, literal string (?) |
| 153 | declare -g ARTIFACT_GLOB=${ARTIFACT_GLOB:-"release/."} |
Joey Armstrong | 76026b7 | 2023-03-29 12:19:17 -0400 | [diff] [blame] | 154 | |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 155 | # Use "release" as the default makefile target, can be a space separated list |
| 156 | declare -g RELEASE_TARGETS=${RELEASE_TARGETS:-release} |
Joey Armstrong | 76026b7 | 2023-03-29 12:19:17 -0400 | [diff] [blame] | 157 | |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 158 | # Set and handle GOPATH and PATH |
| 159 | export GOPATH=${GOPATH:-$WORKSPACE/go} |
| 160 | export PATH=$PATH:/usr/lib/go-1.12/bin:/usr/local/go/bin:$GOPATH/bin |
| 161 | return |
| 162 | } |
| 163 | |
| 164 | ## ----------------------------------------------------------------------- |
Joey Armstrong | d99e3d2 | 2023-01-29 12:35:43 -0500 | [diff] [blame] | 165 | ## Intent: Create a scratch area for downloads and transient tools |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 166 | ## temp directory will be automatically removed upon exit. |
Joey Armstrong | d99e3d2 | 2023-01-29 12:35:43 -0500 | [diff] [blame] | 167 | ## ----------------------------------------------------------------------- |
| 168 | function init() |
| 169 | { |
Joey Armstrong | d99e3d2 | 2023-01-29 12:35:43 -0500 | [diff] [blame] | 170 | local pkgbase="${0##*/}" # basename |
| 171 | local pkgname="${pkgbase%.*}" |
| 172 | |
Joey Armstrong | ad7b1e0 | 2023-03-27 11:55:48 -0400 | [diff] [blame] | 173 | # initEnvVars # moved to full_banner() |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 174 | |
| 175 | ## Create a temp directory for auto-cleanup |
| 176 | declare -g scratch |
Joey Armstrong | d99e3d2 | 2023-01-29 12:35:43 -0500 | [diff] [blame] | 177 | scratch="$(mktemp -d -t "${pkgname}.XXXXXXXXXX")" |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 178 | readonly scratch |
| 179 | declare -p scratch |
| 180 | |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 181 | ## prime the stream: cache answers |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 182 | local work |
| 183 | get_release_dir work |
| 184 | declare -p work |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 185 | |
| 186 | local filler |
| 187 | getGitVersion filler |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 188 | return |
| 189 | } |
| 190 | |
| 191 | ## ----------------------------------------------------------------------- |
| 192 | ## Intent: Verbose output for logging |
| 193 | ## ----------------------------------------------------------------------- |
| 194 | function banner() |
| 195 | { |
| 196 | local iam="${0##*/}" |
| 197 | cat <<EOB |
| 198 | |
| 199 | ** ----------------------------------------------------------------------- |
| 200 | ** ${iam}::${FUNCNAME[1]}: $* |
| 201 | ** ----------------------------------------------------------------------- |
| 202 | EOB |
Joey Armstrong | d99e3d2 | 2023-01-29 12:35:43 -0500 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | ## ----------------------------------------------------------------------- |
Joey Armstrong | 7f382ef | 2023-01-25 12:00:08 -0500 | [diff] [blame] | 206 | ## Intent: Output a log banner to identify the running script/version. |
| 207 | ## ----------------------------------------------------------------------- |
| 208 | ## TODO: |
| 209 | ## o SCRIPT_VERSION => git changeset for repo:ci-managment |
| 210 | ## o echo "library version: ${env."library.libName.version"}" |
| 211 | # ----------------------------------------------------------------------- |
| 212 | # 14:18:38 > git fetch --no-tags --progress -- https://gerrit.opencord.org/ci-management.git +refs/heads/*:refs/remotes/origin/* # timeout=10 |
| 213 | # 14:18:39 Checking out Revision 50f6e0b97f449b32d32ec0e02d59642000351847 (master) |
| 214 | # ----------------------------------------------------------------------- |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 215 | function full_banner() |
Joey Armstrong | 7f382ef | 2023-01-25 12:00:08 -0500 | [diff] [blame] | 216 | { |
Joey Armstrong | ad7b1e0 | 2023-03-27 11:55:48 -0400 | [diff] [blame] | 217 | local iam="${0##*/}" |
| 218 | |
| 219 | initEnvVars # set defaults |
Joey Armstrong | 7f382ef | 2023-01-25 12:00:08 -0500 | [diff] [blame] | 220 | |
| 221 | cat <<EOH |
| 222 | |
| 223 | ** ----------------------------------------------------------------------- |
| 224 | ** IAM: ${iam} :: ${FUNCNAME[0]} |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 225 | ** ARGV: ${ARGV[@]} |
Joey Armstrong | 7f382ef | 2023-01-25 12:00:08 -0500 | [diff] [blame] | 226 | ** PWD: $(/bin/pwd) |
| 227 | ** NOW: $(date '+%Y/%m/%d %H:%M:%S') |
| 228 | ** VER: ${SCRIPT_VERSION:-'unknown'} |
| 229 | ** ----------------------------------------------------------------------- |
Joey Armstrong | ad7b1e0 | 2023-03-27 11:55:48 -0400 | [diff] [blame] | 230 | ** GERRIT_PROJECT: $(declare -p GERRIT_PROJECT) |
| 231 | ** GITHUB_ORGANIZATION: $(declare -p GITHUB_ORGANIZATION) |
| 232 | ** RELEASE_TARGETS: $(declare -p RELEASE_TARGETS) |
| 233 | ** GOPATH: $(declare -p GOPATH) |
| 234 | ** ----------------------------------------------------------------------- |
| 235 | ** PATH += /usr/lib/go-1.12/bin:/usr/local/go/bin:GOPATH/bin |
| 236 | ** ----------------------------------------------------------------------- |
Joey Armstrong | 7f382ef | 2023-01-25 12:00:08 -0500 | [diff] [blame] | 237 | EOH |
| 238 | |
| 239 | return |
| 240 | } |
| 241 | |
Joey Armstrong | af577ab | 2022-12-15 14:43:33 -0500 | [diff] [blame] | 242 | ## ----------------------------------------------------------------------- |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 243 | ## Intent: Display a message with 'iam' identifier for logging |
Joey Armstrong | 50f6e0b | 2023-01-24 14:14:08 -0500 | [diff] [blame] | 244 | ## ----------------------------------------------------------------------- |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 245 | function func_echo() |
Joey Armstrong | 1962bcf | 2023-01-27 13:53:18 -0500 | [diff] [blame] | 246 | { |
| 247 | local iam="${0##*/}" |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 248 | echo "** ${iam} :: ${FUNCNAME[1]}: $*" |
Joey Armstrong | 1962bcf | 2023-01-27 13:53:18 -0500 | [diff] [blame] | 249 | return |
| 250 | } |
| 251 | |
| 252 | ## ----------------------------------------------------------------------- |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 253 | ## Intent: Display an error message then exit with status. |
| 254 | ## ----------------------------------------------------------------------- |
| 255 | function error() |
| 256 | { |
| 257 | local iam="${0##*/}" |
| 258 | echo "ERROR ${iam} :: ${FUNCNAME[1]}: $*" |
| 259 | exit 1 |
| 260 | } |
| 261 | |
| 262 | ## ----------------------------------------------------------------------- |
| 263 | ## Intent: Verify sandbox/build is versioned for release. |
| 264 | ## ----------------------------------------------------------------------- |
| 265 | function getGitVersion() |
| 266 | { |
| 267 | declare -n varname="$1"; shift |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 268 | |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 269 | local __ver # use prefix('__') to protect callers variable name |
| 270 | if [[ -v cached_getGitVersion ]]; then |
| 271 | __ver="$cached_getGitVersion" |
| 272 | varname="$__ver" |
| 273 | return |
Joey Armstrong | 39fac65 | 2023-03-27 18:50:43 -0400 | [diff] [blame] | 274 | |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 275 | elif [[ -v argv_gen_version ]]; then |
| 276 | get_version __ver |
| 277 | |
| 278 | elif [[ -v WORKSPACE ]] && [[ -v GITHUB_TOKEN ]]; then # i_am_jenkins |
Joey Armstrong | ca557dc | 2023-04-03 17:12:32 -0400 | [diff] [blame] | 279 | local path="$GERRIT_PROJECT" |
| 280 | pushd "$path" || error "pushd GERRIT_PROJECT= failed $(declare -p path)" |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 281 | __ver="$(git tag -l --points-at HEAD)" |
Joey Armstrong | ca557dc | 2023-04-03 17:12:32 -0400 | [diff] [blame] | 282 | popd || error "popd GERRIT_PROJECT= failed" |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 283 | |
| 284 | elif [[ -v argv_version_file ]]; then # local debug |
| 285 | [[ ! -f VERSION ]] && error "./VERSION file not found" |
| 286 | readarray -t tmp < <(sed -e 's/[[:blank:]]//g' 'VERSION') |
| 287 | __ver="v${tmp[0]}" |
| 288 | |
| 289 | else |
Joey Armstrong | ca557dc | 2023-04-03 17:12:32 -0400 | [diff] [blame] | 290 | local path="$GERRIT_PROJECT" |
| 291 | pushd "$path" || error "pushd GERRIT_PROJECT= failed $(declare -p path)" |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 292 | __ver="$(git tag -l --points-at HEAD)" |
Joey Armstrong | ca557dc | 2023-04-03 17:12:32 -0400 | [diff] [blame] | 293 | popd || error "popd GERRIT_PROJECT= failed" |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 294 | fi |
Joey Armstrong | 76026b7 | 2023-03-29 12:19:17 -0400 | [diff] [blame] | 295 | |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 296 | # ------------------------------------------------------ |
| 297 | # match bare versions or v-prefixed golang style version |
| 298 | # Critical failure for new/yet-to-be-released repo ? |
| 299 | # ------------------------------------------------------ |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 300 | if [[ "$__ver" =~ ^v?([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then |
Joey Armstrong | fc20ed5 | 2023-04-03 19:37:58 -0400 | [diff] [blame] | 301 | echo "git has a SemVer released version tag: '$__ver'" |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 302 | echo "Building artifacts for GitHub release." |
| 303 | |
| 304 | elif [[ "$__ver" =~ ^v?([0-9]+)\.([0-9]+)\.([0-9]+)-dev([0-9]+)$ ]]; then |
| 305 | declare -i -g draft_release=1 |
| 306 | echo "Detected --draft SemVer release version tag [$__ver]" |
| 307 | echo "Building artifacts for GitHub release." |
| 308 | |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 309 | else |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 310 | error "No SemVer released version tag found, exiting..." |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 311 | fi |
| 312 | |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 313 | ## Cache value for subsequent calls. |
| 314 | ## readonly is a guarantee assignment happens once. |
| 315 | declare -g cached_getGitVersion="$__ver" |
| 316 | readonly cached_getGitVersion; |
| 317 | |
| 318 | varname="$__ver" |
| 319 | func_echo "Version is [$__ver] from $(/bin/pwd)" |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 320 | return |
| 321 | } |
| 322 | |
| 323 | ## ----------------------------------------------------------------------- |
| 324 | ## Intent: |
| 325 | ## Note: Release description is sanitized version of the log message |
| 326 | ## ----------------------------------------------------------------------- |
| 327 | function getReleaseDescription() |
| 328 | { |
| 329 | declare -n varname="$1"; shift |
| 330 | |
| 331 | local msg |
| 332 | msg="$(git log -1 --pretty=%B)" |
| 333 | |
| 334 | local val |
| 335 | val="$(tr -dc "[:alnum:]\n\r\.\[\]\:\-\\\/\`\' " <<< "$msg")" |
| 336 | |
| 337 | [[ ${#val} -eq 0 ]] && error "Release Description is empty ($msg)" |
| 338 | varname="$val" |
| 339 | return |
| 340 | } |
| 341 | |
| 342 | ## ----------------------------------------------------------------------- |
| 343 | ## Intent: Retrieve value of the release temp directory. |
| 344 | ## ----------------------------------------------------------------------- |
| 345 | ## Note: Limit use of globals so logic can be isolated in function calls. |
| 346 | ## ----------------------------------------------------------------------- |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 347 | function get_release_dir() |
| 348 | { |
| 349 | declare -n varname=$1; shift |
| 350 | |
| 351 | # Temporary staging directory to copy artifacts to |
| 352 | varname="$scratch/release" |
| 353 | mkdir -p "$varname" |
| 354 | return |
| 355 | } |
| 356 | |
| 357 | ## ----------------------------------------------------------------------- |
| 358 | ## Intent: Retrieve github server hostname |
| 359 | ## ----------------------------------------------------------------------- |
| 360 | function get_gh_hostname() |
| 361 | { |
| 362 | declare -n varname=$1; shift |
| 363 | varname+=('--hostname' "${__githost}") |
| 364 | return |
| 365 | } |
| 366 | |
| 367 | ## ----------------------------------------------------------------------- |
| 368 | ## Intent: Retrieve repository organizaiton name |
| 369 | ## ----------------------------------------------------------------------- |
| 370 | function get_gh_repo_org() |
| 371 | { |
| 372 | declare -n varname=$1; shift |
| 373 | declare -g __repo_org |
Joey Armstrong | 76026b7 | 2023-03-29 12:19:17 -0400 | [diff] [blame] | 374 | |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 375 | local org |
| 376 | if [[ -v __repo_org ]]; then |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 377 | org="$__repo_org" |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 378 | elif [[ ! -v GITHUB_ORGANIZATION ]]; then |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 379 | error "--repo-org or GITHUB_ORGANIZATION= are required" |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 380 | else |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 381 | org="${GITHUB_ORGANIZATION}" |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 382 | fi |
| 383 | |
| 384 | # shellcheck disable=SC2178 |
| 385 | varname="$org" |
| 386 | return |
| 387 | } |
| 388 | |
| 389 | ## ----------------------------------------------------------------------- |
| 390 | ## Intent: Retrieve repository organizaiton name |
| 391 | ## ----------------------------------------------------------------------- |
| 392 | function get_gh_repo_name() |
| 393 | { |
| 394 | declare -n varname=$1; shift |
| 395 | declare -g __repo_name |
Joey Armstrong | 76026b7 | 2023-03-29 12:19:17 -0400 | [diff] [blame] | 396 | |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 397 | local name |
| 398 | if [[ -v __repo_name ]]; then |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 399 | name="$__repo_name" |
Joey Armstrong | 39fac65 | 2023-03-27 18:50:43 -0400 | [diff] [blame] | 400 | elif [[ ! -v GERRIT_PROJECT ]]; then |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 401 | error "--repo-name or GERRIT_PROJECT= are required" |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 402 | else |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 403 | name="${GERRIT_PROJECT}" |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 404 | fi |
| 405 | |
| 406 | varname="$name" |
| 407 | return |
| 408 | } |
| 409 | |
| 410 | ## ----------------------------------------------------------------------- |
| 411 | ## Intent: Return path for the gh release query |
| 412 | ## ----------------------------------------------------------------------- |
| 413 | function get_gh_releases() |
| 414 | { |
| 415 | declare -n ref="$1" |
| 416 | |
| 417 | local repo_org |
| 418 | get_gh_repo_org repo_org |
| 419 | |
| 420 | local repo_name |
| 421 | get_gh_repo_name repo_name |
| 422 | |
| 423 | ref="repos/${repo_org}/${repo_name}/releases" |
| 424 | func_echo "ref=$ref" |
| 425 | return |
| 426 | } |
| 427 | |
| 428 | ## ----------------------------------------------------------------------- |
| 429 | ## Intent: Retrieve repository path argument |
| 430 | ## ----------------------------------------------------------------------- |
| 431 | function get_argv_repo() |
| 432 | { |
| 433 | declare -n varname=$1; shift |
| 434 | |
| 435 | local repo_org |
| 436 | get_gh_repo_org repo_org |
Joey Armstrong | 76026b7 | 2023-03-29 12:19:17 -0400 | [diff] [blame] | 437 | |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 438 | local repo_name |
Joey Armstrong | 76026b7 | 2023-03-29 12:19:17 -0400 | [diff] [blame] | 439 | get_gh_repo_name repo_name |
| 440 | |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 441 | varname="${repo_org}/${repo_name}" |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 442 | # func_echo "VARNAME=$varname" |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 443 | return |
| 444 | } |
| 445 | |
| 446 | ## ----------------------------------------------------------------------- |
| 447 | ## Intent: Retrieve release name string "project - version" |
| 448 | ## ----------------------------------------------------------------------- |
| 449 | function get_argv_name() |
| 450 | { |
| 451 | declare -n varname=$1; shift |
| 452 | |
| 453 | local repo_name |
| 454 | get_gh_repo_name repo_name |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 455 | |
| 456 | local repo_ver |
| 457 | getGitVersion repo_ver |
| 458 | |
| 459 | # varname="${repo_name} - $GIT_VERSION" |
| 460 | varname="${repo_name} - ${repo_ver}" |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 461 | func_echo "varname=$varname" |
| 462 | return |
| 463 | } |
| 464 | |
| 465 | ## ----------------------------------------------------------------------- |
| 466 | ## Intent: Retrieve tag version |
| 467 | ## ----------------------------------------------------------------------- |
| 468 | function get_argv_tag() |
| 469 | { |
| 470 | declare -n varname=$1; shift |
| 471 | |
| 472 | # cached_argv_tag='v3.41.3204' |
| 473 | if [[ ! -v cached_argv_tag ]]; then |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 474 | declare -g cached_argv_tag |
| 475 | if [[ -v GIT_VERSION ]]; then # hello jenkins |
| 476 | cached_argv_tag="$GIT_VERSION" |
| 477 | fi |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 478 | fi |
| 479 | |
| 480 | [[ ${#cached_argv_tag} -eq 0 ]] && error "Unable to determine GIT_VERSION=" |
| 481 | varname="$cached_argv_tag" |
| 482 | func_echo "varname=$varname" |
| 483 | return |
| 484 | } |
| 485 | |
| 486 | ## ----------------------------------------------------------------------- |
| 487 | ## Intent: |
| 488 | ## ----------------------------------------------------------------------- |
| 489 | # To support golang projects that require GOPATH to be set and code checked out there |
| 490 | # If $DEST_GOPATH is not an empty string: |
| 491 | # - create GOPATH within WORKSPACE, and destination directory within |
| 492 | # - set PATH to include $GOPATH/bin and the system go binaries |
| 493 | # - move project from $WORKSPACE/$GERRIT_PROJECT to new location in $GOPATH |
| 494 | # - start release process within that directory |
| 495 | ## ----------------------------------------------------------------------- |
| 496 | function get_release_path() |
| 497 | { |
| 498 | declare -n varname=$1; shift |
| 499 | |
| 500 | DEST_GOPATH=${DEST_GOPATH:-} |
| 501 | if [ -n "$DEST_GOPATH" ]; then |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 502 | mkdir -p "$GOPATH/src/$DEST_GOPATH" |
| 503 | varname="$GOPATH/src/$DEST_GOPATH/$GERRIT_PROJECT" |
| 504 | mv "$WORKSPACE/$GERRIT_PROJECT" "$varname" |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 505 | else |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 506 | varname="$WORKSPACE/$GERRIT_PROJECT" |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 507 | fi |
| 508 | |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 509 | ## Verify pwd is OK |
| 510 | for path in \ |
| 511 | "$varname/Makefile"\ |
| 512 | "$varname/makefile"\ |
| 513 | "__ERROR__"\ |
| 514 | ; do |
| 515 | case "$path" in |
| 516 | __ERROR__) error "Makefile not found at $varname!" ;; |
| 517 | *) [[ -f "$path" ]] && break ;; |
| 518 | esac |
| 519 | done |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 520 | |
| 521 | return |
| 522 | } |
| 523 | |
| 524 | ## ----------------------------------------------------------------------- |
| 525 | ## Intent: Display future enhancements |
| 526 | ## ----------------------------------------------------------------------- |
| 527 | function todo() |
| 528 | { |
| 529 | local iam="${0##*/}" |
| 530 | |
| 531 | cat <<EOT |
| 532 | |
| 533 | ** ----------------------------------------------------------------------- |
| 534 | ** IAM: ${iam} :: ${FUNCNAME[0]} |
| 535 | ** ----------------------------------------------------------------------- |
| 536 | o get_release_path() |
| 537 | - refactor redundant paths into local vars. |
| 538 | - see comments, do we have a one-off failure condition ? |
Joey Armstrong | ad7b1e0 | 2023-03-27 11:55:48 -0400 | [diff] [blame] | 539 | o PATH += golang appended 3 times, release needs a single, reliable answer. |
| 540 | o do_login, do_logout and api calls do not use the my_gh wrapper: |
| 541 | - Add a lookup function to cache and retrieve path to downloaded gh command. |
Joey Armstrong | 76026b7 | 2023-03-29 12:19:17 -0400 | [diff] [blame] | 542 | |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 543 | EOT |
| 544 | |
| 545 | return |
| 546 | } |
| 547 | |
| 548 | ## ----------------------------------------------------------------------- |
Joey Armstrong | 76026b7 | 2023-03-29 12:19:17 -0400 | [diff] [blame] | 549 | ## Intent: Verify a directory contains content for release. |
| 550 | ## ----------------------------------------------------------------------- |
| 551 | ## Given: |
| 552 | ## scalar Path to release/ directory |
| 553 | ## ref Results returned through this indirect var. |
| 554 | ## ----------------------------------------------------------------------- |
| 555 | ## Usage: |
| 556 | ## declare -a artifacts=() |
| 557 | ## get_artifacts '/foo/bar/tans' artifacts |
| 558 | ## declare -p artifacts |
| 559 | ## ----------------------------------------------------------------------- |
| 560 | function get_artifacts() |
| 561 | { |
| 562 | local dir="$1" ; shift |
| 563 | declare -n ref=$1 ; shift |
| 564 | |
| 565 | # Glob available files |
| 566 | readarray -t __artifacts < <(find "$dir" -mindepth 1 ! -type d) |
| 567 | func_echo "$(declare -p __artifacts)" |
| 568 | |
| 569 | # ----------------------------------------------------------------------- |
| 570 | # Verify count>0 to inhibit source-only release |
| 571 | # Problem children: |
| 572 | # o build or make release failures. |
| 573 | # o docker container filesystem mount problem (~volume) |
| 574 | # ----------------------------------------------------------------------- |
| 575 | [[ ${#__artifacts[@]} -eq 0 ]] \ |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 576 | && error "Artifact dir is empty: $(declare -p dir)" |
Joey Armstrong | 76026b7 | 2023-03-29 12:19:17 -0400 | [diff] [blame] | 577 | |
| 578 | ref=("${__artifacts[@]}") |
| 579 | return |
| 580 | } |
| 581 | |
| 582 | ## ----------------------------------------------------------------------- |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 583 | ## Intent: Copy files from the build directory into the release staging |
| 584 | ## directory for publishing to github releases/ endpoint. |
Joey Armstrong | 26707f0 | 2023-01-26 12:41:12 -0500 | [diff] [blame] | 585 | ## ----------------------------------------------------------------------- |
| 586 | function copyToRelease() |
| 587 | { |
Joey Armstrong | 76026b7 | 2023-03-29 12:19:17 -0400 | [diff] [blame] | 588 | banner '' |
Joey Armstrong | 26707f0 | 2023-01-26 12:41:12 -0500 | [diff] [blame] | 589 | |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 590 | local artifact_glob="${ARTIFACT_GLOB%/*}" |
| 591 | func_echo "$(declare -p artifact_glob)" |
| 592 | |
| 593 | local work |
| 594 | get_release_dir work |
Joey Armstrong | 76026b7 | 2023-03-29 12:19:17 -0400 | [diff] [blame] | 595 | func_echo "Artifact dir: $(declare -p work)" |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 596 | |
Joey Armstrong | 76026b7 | 2023-03-29 12:19:17 -0400 | [diff] [blame] | 597 | ## Verify release content is available |
| 598 | declare -a artifacts=() |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 599 | # get_artifacts "$work" artifacts |
| 600 | get_artifacts "$artifact_glob" artifacts |
| 601 | func_echo "Artifacts in copy_from: $(declare -p artifacts)" |
Joey Armstrong | 472bfba | 2023-03-27 18:12:28 -0400 | [diff] [blame] | 602 | |
Joey Armstrong | 26707f0 | 2023-01-26 12:41:12 -0500 | [diff] [blame] | 603 | # Copy artifacts into the release temp dir |
| 604 | # shellcheck disable=SC2086 |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 605 | echo "rsync -rv --checksum \"$artifact_glob/.\" \"$work/.\"" |
| 606 | rsync -rv --checksum "$artifact_glob/." "$work/." |
Joey Armstrong | 1962bcf | 2023-01-27 13:53:18 -0500 | [diff] [blame] | 607 | |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 608 | get_artifacts "$work" artifacts |
| 609 | func_echo "Artifacts in copy_to: $(declare -p artifacts)" |
| 610 | |
Joey Armstrong | 26707f0 | 2023-01-26 12:41:12 -0500 | [diff] [blame] | 611 | return |
| 612 | } |
| 613 | |
| 614 | ## ----------------------------------------------------------------------- |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 615 | ## https://docs.github.com/en/rest/releases?apiVersion=2022-11-28 |
| 616 | # https://cli.github.com/manual/gh_release_create |
| 617 | # --target <branch> or commit SHA |
| 618 | # --title |
| 619 | # --generate-notes |
| 620 | # --release-notes (notes file) |
| 621 | # --release |
| 622 | # release create dist/*.tgz |
| 623 | # --discussion-category "General" |
Joey Armstrong | f085d87 | 2023-01-28 17:52:29 -0500 | [diff] [blame] | 624 | ## ----------------------------------------------------------------------- |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 625 | # https://cli.github.com/manual/gh_release_create |
| 626 | ## ----------------------------------------------------------------------- |
| 627 | function gh_release_create() |
Joey Armstrong | 38bfeea | 2023-02-06 18:01:29 -0500 | [diff] [blame] | 628 | { |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 629 | banner '' |
Joey Armstrong | 38bfeea | 2023-02-06 18:01:29 -0500 | [diff] [blame] | 630 | |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 631 | local version |
| 632 | getGitVersion version |
Joey Armstrong | 38bfeea | 2023-02-06 18:01:29 -0500 | [diff] [blame] | 633 | |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 634 | local work |
| 635 | get_release_dir work |
| 636 | |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 637 | declare -a args=() |
| 638 | args+=('--host-repo') |
| 639 | args+=('--title') |
| 640 | if [[ -v draft_release ]]; then |
| 641 | args+=('--draft') |
| 642 | else |
| 643 | args+=('--discussion-category' 'Announcements') |
| 644 | fi |
| 645 | |
| 646 | # args+=('--notes' "'Testing release create -- ignore'") |
| 647 | |
Joey Armstrong | 78cecc5 | 2023-04-03 11:39:11 -0400 | [diff] [blame] | 648 | pushd "$work/.." >/dev/null |
| 649 | func_echo "WORK=$work" |
| 650 | # readarray -t payload < <(find '.' -maxdepth 4 ! -type d -print) |
| 651 | readarray -t payload < <(find 'release' -maxdepth 4 ! -type d -print) |
Joey Armstrong | 76026b7 | 2023-03-29 12:19:17 -0400 | [diff] [blame] | 652 | |
Joey Armstrong | ad7b1e0 | 2023-03-27 11:55:48 -0400 | [diff] [blame] | 653 | func_echo "$gh_cmd release create ${version} ${args[@]}" "${payload[@]}" |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 654 | |
| 655 | if [[ -v dry_run ]]; then |
| 656 | echo "[SKIP] dry run" |
| 657 | else |
Joey Armstrong | 78cecc5 | 2023-04-03 11:39:11 -0400 | [diff] [blame] | 658 | func_echo "my_gh release create '$version' ${args[@]} ${payload[@]}" |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 659 | my_gh 'release' 'create' "'$version'" "${args[@]}" "${payload[@]}" |
| 660 | set +x |
| 661 | fi |
Joey Armstrong | 76026b7 | 2023-03-29 12:19:17 -0400 | [diff] [blame] | 662 | popd >/dev/null |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 663 | |
Joey Armstrong | 38bfeea | 2023-02-06 18:01:29 -0500 | [diff] [blame] | 664 | return |
| 665 | } |
| 666 | |
| 667 | ## ----------------------------------------------------------------------- |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 668 | ## Intent: Authenticate credentials for a github gh session |
Joey Armstrong | 38bfeea | 2023-02-06 18:01:29 -0500 | [diff] [blame] | 669 | ## ----------------------------------------------------------------------- |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 670 | ## NOTE: my_gh currently unused due to --with-token < "$pac" |
| 671 | ## ----------------------------------------------------------------------- |
| 672 | function do_login() |
Joey Armstrong | f085d87 | 2023-01-28 17:52:29 -0500 | [diff] [blame] | 673 | { |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 674 | declare -g pac |
Joey Armstrong | 485081f | 2023-03-27 13:34:08 -0400 | [diff] [blame] | 675 | declare -a login_args=() |
| 676 | [[ $# -gt 0 ]] && login_args+=("$@") |
Joey Armstrong | f085d87 | 2023-01-28 17:52:29 -0500 | [diff] [blame] | 677 | |
Joey Armstrong | 485081f | 2023-03-27 13:34:08 -0400 | [diff] [blame] | 678 | # https://github.com/cli/cli/issues/2922#issuecomment-775027762 |
Joey Armstrong | 75a0d93 | 2023-03-28 08:59:54 -0400 | [diff] [blame] | 679 | # (sigh) why not quietly return VS forcing a special logic path |
Joey Armstrong | e6e18eb | 2023-03-27 14:19:21 -0400 | [diff] [blame] | 680 | # [[ -v WORKSPACE ]] && [[ -v GITHUB_TOKEN ]] && return |
| 681 | |
| 682 | # 12:58:36 ** ----------------------------------------------------------------------- |
| 683 | # 12:58:36 ** jenkins582353203049151829.sh::do_login: --hostname github.com |
| 684 | # 12:58:36 ** --------------------------------------------------------------------# --- |
| 685 | # 12:58:36 ** jenkins582353203049151829.sh :: do_login: Detected ENV{GITHUB_TOKEN}= |
| 686 | # 12:58:36 The value of the GITHUB_TOKEN environment variable is being used for authentication. |
| 687 | # 12:58:36 To have GitHub CLI store credentials instead, first clear the value from the environment. |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 688 | # ----------------------------------------------------------------------- |
Joey Armstrong | 76026b7 | 2023-03-29 12:19:17 -0400 | [diff] [blame] | 689 | |
Joey Armstrong | 485081f | 2023-03-27 13:34:08 -0400 | [diff] [blame] | 690 | get_gh_hostname login_args |
Joey Armstrong | f085d87 | 2023-01-28 17:52:29 -0500 | [diff] [blame] | 691 | |
Joey Armstrong | 485081f | 2023-03-27 13:34:08 -0400 | [diff] [blame] | 692 | banner "${login_args[@]}" |
Joey Armstrong | 78cecc5 | 2023-04-03 11:39:11 -0400 | [diff] [blame] | 693 | func_echo "$(declare -p WORKSPACE)" |
Joey Armstrong | f085d87 | 2023-01-28 17:52:29 -0500 | [diff] [blame] | 694 | |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 695 | ## Read from disk is safer than export GITHUB_TOKEN= |
| 696 | if [[ -v pac ]] && [[ ${#pac} -gt 0 ]]; then # interactive/debugging |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 697 | [ ! -f "$pac" ] && error "PAC token file $pac does not exist" |
Joey Armstrong | 78cecc5 | 2023-04-03 11:39:11 -0400 | [diff] [blame] | 698 | func_echo "$gh_cmd auth login ${login_args[@]} --with-token < $pac" |
Joey Armstrong | 485081f | 2023-03-27 13:34:08 -0400 | [diff] [blame] | 699 | "$gh_cmd" auth login "${login_args[@]}" --with-token < "$pac" |
Joey Armstrong | f085d87 | 2023-01-28 17:52:29 -0500 | [diff] [blame] | 700 | |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 701 | elif [[ ! -v GITHUB_TOKEN ]]; then |
| 702 | error "--token [t] or GITHUB_TOKEN= are required" |
Joey Armstrong | 41923cc | 2023-01-30 14:38:16 -0500 | [diff] [blame] | 703 | |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 704 | else # jenkins |
Joey Armstrong | 78cecc5 | 2023-04-03 11:39:11 -0400 | [diff] [blame] | 705 | func_echo "$gh_cmd auth login ${login_args[@]} (ie: jenkins)" |
Joey Armstrong | 733ea9f | 2023-04-03 21:19:46 -0400 | [diff] [blame] | 706 | |
| 707 | # https://github.com/cli/cli/issues/2922#issuecomment-775027762 |
| 708 | # When using GITHUB_TOKEN, there is no need to even run gh auth logi |
| 709 | # "$gh_cmd" auth login "${login_args[@]}" |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 710 | fi |
Joey Armstrong | f085d87 | 2023-01-28 17:52:29 -0500 | [diff] [blame] | 711 | |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 712 | declare -i -g active_login=1 # signal logout needed |
Joey Armstrong | f085d87 | 2023-01-28 17:52:29 -0500 | [diff] [blame] | 713 | |
Joey Armstrong | f085d87 | 2023-01-28 17:52:29 -0500 | [diff] [blame] | 714 | return |
| 715 | } |
| 716 | |
| 717 | ## ----------------------------------------------------------------------- |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 718 | ## Intent: Destroy credentials/gh session authenticated by do_login |
| 719 | ## ----------------------------------------------------------------------- |
| 720 | ## NOTE: my_gh currently unused due to "<<< 'y'" |
| 721 | ## ----------------------------------------------------------------------- |
| 722 | function do_logout() |
| 723 | { |
| 724 | declare -i -g active_login |
| 725 | [[ ! -v active_login ]] && return |
| 726 | |
Joey Armstrong | 78cecc5 | 2023-04-03 11:39:11 -0400 | [diff] [blame] | 727 | declare -a logout_args=() |
| 728 | [[ $# -gt 0 ]] && logout_args+=("$@") |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 729 | |
Joey Armstrong | 78cecc5 | 2023-04-03 11:39:11 -0400 | [diff] [blame] | 730 | get_gh_hostname logout_args |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 731 | |
Joey Armstrong | 78cecc5 | 2023-04-03 11:39:11 -0400 | [diff] [blame] | 732 | banner "${logout_args[@]}" |
| 733 | func_echo "$gh_cmd auth logout ${logout_args[@]} <<< 'Y'" |
| 734 | "$gh_cmd" auth logout "${logout_args[@]}" <<< 'Y' |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 735 | |
| 736 | unset active_login |
| 737 | return |
| 738 | } |
| 739 | |
| 740 | ## ----------------------------------------------------------------------- |
| 741 | ## Intent: Query for repository version strings |
| 742 | ## ----------------------------------------------------------------------- |
| 743 | function get_releases() |
| 744 | { |
| 745 | declare -n ref="$1"; shift |
| 746 | local func="${FUNCNAME[0]}" |
| 747 | |
| 748 | banner "" |
| 749 | pushd "$scratch" >/dev/null |
Joey Armstrong | 76026b7 | 2023-03-29 12:19:17 -0400 | [diff] [blame] | 750 | |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 751 | # gh api repos/{owner}/{repo}/releases |
| 752 | local releases_uri |
| 753 | get_gh_releases releases_uri |
| 754 | declare -p releases_uri |
Joey Armstrong | 76026b7 | 2023-03-29 12:19:17 -0400 | [diff] [blame] | 755 | |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 756 | ref=() |
Joey Armstrong | ad7b1e0 | 2023-03-27 11:55:48 -0400 | [diff] [blame] | 757 | "$gh_cmd" api "$releases_uri" "${common[@]}" | jq . > 'release.raw' |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 758 | readarray -t __tmp < <(jq '.[] | "\(.tag_name)"' 'release.raw') |
| 759 | |
| 760 | local release |
| 761 | for release in "${__tmp[@]}"; |
| 762 | do |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 763 | release="${release//\"/}" |
| 764 | ref+=("$release") |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 765 | done |
| 766 | |
| 767 | popd >/dev/null |
| 768 | return |
| 769 | } |
| 770 | |
| 771 | ## ----------------------------------------------------------------------- |
| 772 | ## Intent: Display repository query strings. |
| 773 | ## Indirect: verify authentication and API |
| 774 | ## ----------------------------------------------------------------------- |
| 775 | function showReleases() |
| 776 | { |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 777 | declare -a raw=() |
| 778 | get_releases raw |
| 779 | |
| 780 | ## Sort for display, we may need to prune volume later on |
| 781 | IFS=$'\n' releases=($(sort -nr <<<"${raw[*]}")) |
| 782 | unset IFS |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 783 | |
| 784 | local release |
| 785 | for release in "${releases[@]}"; |
| 786 | do |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 787 | func_echo "$release" |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 788 | done |
| 789 | return |
| 790 | } |
| 791 | |
| 792 | ## ----------------------------------------------------------------------- |
| 793 | ## Intent: Install the gh command line tool locally |
Joey Armstrong | 26707f0 | 2023-01-26 12:41:12 -0500 | [diff] [blame] | 794 | ## ----------------------------------------------------------------------- |
Joey Armstrong | d99e3d2 | 2023-01-29 12:35:43 -0500 | [diff] [blame] | 795 | function install_gh_binary() |
Joey Armstrong | 26707f0 | 2023-01-26 12:41:12 -0500 | [diff] [blame] | 796 | { |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 797 | banner |
Joey Armstrong | 26707f0 | 2023-01-26 12:41:12 -0500 | [diff] [blame] | 798 | |
Joey Armstrong | d99e3d2 | 2023-01-29 12:35:43 -0500 | [diff] [blame] | 799 | pushd "$scratch" |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 800 | func_echo "Retrieve latest gh download URLs" |
Joey Armstrong | f085d87 | 2023-01-28 17:52:29 -0500 | [diff] [blame] | 801 | |
Joey Armstrong | d99e3d2 | 2023-01-29 12:35:43 -0500 | [diff] [blame] | 802 | local latest="https://github.com/cli/cli/releases/latest" |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 803 | local tarball="gh.tar.tgz" |
| 804 | |
Joey Armstrong | 76026b7 | 2023-03-29 12:19:17 -0400 | [diff] [blame] | 805 | readarray -t latest < <(\ |
| 806 | curl --silent -qI "$latest" \ |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 807 | | awk -F '/' '/^location/ {print substr($NF, 1, length($NF)-1)}') |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 808 | declare -p latest |
| 809 | if [ ${#latest[@]} -ne 1 ]; then |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 810 | error "Unable to determine latest gh package version" |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 811 | fi |
Joey Armstrong | 76026b7 | 2023-03-29 12:19:17 -0400 | [diff] [blame] | 812 | |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 813 | local VER="${latest[0]}" |
| 814 | |
| 815 | func_echo "Download latest gh binary" |
Joey Armstrong | d99e3d2 | 2023-01-29 12:35:43 -0500 | [diff] [blame] | 816 | local url="https://github.com/cli/cli/releases/download/${VER}/gh_${VER#v}_linux_amd64.tar.gz" |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 817 | func_echo "wget --quiet --output-document='$tarball' '$url'" |
Joey Armstrong | d99e3d2 | 2023-01-29 12:35:43 -0500 | [diff] [blame] | 818 | wget --quiet --output-document="$tarball" "$url" |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 819 | |
| 820 | func_echo "Unpack tarball" |
Joey Armstrong | d99e3d2 | 2023-01-29 12:35:43 -0500 | [diff] [blame] | 821 | tar zxf "$tarball" |
| 822 | |
Joey Armstrong | 78cecc5 | 2023-04-03 11:39:11 -0400 | [diff] [blame] | 823 | declare -g gh_cmd |
| 824 | gh_cmd="$(find "${scratch}" -name 'gh' -print)" |
| 825 | #declare -g gh_cmd='/usr/bin/gh' |
Joey Armstrong | d99e3d2 | 2023-01-29 12:35:43 -0500 | [diff] [blame] | 826 | readonly gh_cmd |
| 827 | |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 828 | func_echo "Command: ${gh_cmd}" |
| 829 | func_echo "Version: $("$gh_cmd" --version)" |
Joey Armstrong | d99e3d2 | 2023-01-29 12:35:43 -0500 | [diff] [blame] | 830 | popd |
| 831 | |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 832 | return |
| 833 | } |
| 834 | |
| 835 | ## ----------------------------------------------------------------------- |
| 836 | ## Intent: Danger Will Robinson |
| 837 | ## ----------------------------------------------------------------------- |
| 838 | function releaseDelete() |
| 839 | { |
| 840 | local version="$1"; shift |
Joey Armstrong | 76026b7 | 2023-03-29 12:19:17 -0400 | [diff] [blame] | 841 | |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 842 | banner "${in_args[@]}" |
| 843 | declare -a args=() |
| 844 | args+=('--host-repo') |
| 845 | args+=('--yes') |
| 846 | # args+=('--cleanup-tag') |
| 847 | |
| 848 | # ** github-release.sh :: get_argv_repo: VARNAME=opencord/voltctl |
| 849 | #* Running: /tmp/github-release.R7geZo7Ywo/gh_2.25.1_linux_amd64/bin/gh release delete v4.175.710 --repo 'github.com/opencord/voltctl' --yes --cleanup-tag |
| 850 | #rror connecting to 'github.com |
| 851 | #heck your internet connection or https://githubstatus.com |
| 852 | |
| 853 | echo |
| 854 | echo "===========================================================================" |
| 855 | my_gh 'release' 'delete' "$version" "${args[@]}" |
| 856 | echo "===========================================================================" |
| 857 | echo |
| 858 | |
| 859 | showReleases |
| 860 | return |
| 861 | } |
| 862 | |
| 863 | ## ----------------------------------------------------------------------- |
| 864 | ## Intent: Copy binaries into temp/release, checksum then publish |
| 865 | ## ----------------------------------------------------------------------- |
| 866 | function release_staging() |
| 867 | { |
| 868 | local release_temp |
| 869 | get_release_dir release_temp |
| 870 | |
| 871 | banner '' |
| 872 | func_echo "Packaging release files" |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 873 | |
Joey Armstrong | 76026b7 | 2023-03-29 12:19:17 -0400 | [diff] [blame] | 874 | pushd "$release_temp" >/dev/null \ |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 875 | || error "pushd failed: dir is [$release_temp]" |
Joey Armstrong | 76026b7 | 2023-03-29 12:19:17 -0400 | [diff] [blame] | 876 | |
| 877 | declare -a to_release=() |
| 878 | get_artifacts '.' to_release |
| 879 | |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 880 | func_echo "Files to release: $(declare -p to_release)" |
| 881 | |
| 882 | # Generate and check checksums |
| 883 | sha256sum -- * > checksum.SHA256 |
| 884 | sha256sum -c < checksum.SHA256 |
| 885 | |
| 886 | echo |
| 887 | func_echo "Checksums(checksum.SHA256):" |
| 888 | cat checksum.SHA256 |
| 889 | |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 890 | if false; then |
Joey Armstrong | 78cecc5 | 2023-04-03 11:39:11 -0400 | [diff] [blame] | 891 | # Careful with credentials display |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 892 | get_gh_hostname login_args |
| 893 | banner "gh auth status ${login_args[@]}" |
| 894 | gh auth status "${login_args[@]}" |
| 895 | fi |
| 896 | |
Joey Armstrong | 76026b7 | 2023-03-29 12:19:17 -0400 | [diff] [blame] | 897 | gh_release_create # publish |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 898 | |
| 899 | popd >/dev/null || error "pushd failed: dir is [$release_temp]" |
| 900 | |
| 901 | return |
| 902 | } |
| 903 | |
| 904 | ## ----------------------------------------------------------------------- |
| 905 | ## Intent: Display program usage |
| 906 | ## ----------------------------------------------------------------------- |
| 907 | function usage() |
| 908 | { |
| 909 | [[ $# -gt 0 ]] && func_echo "$*" |
| 910 | |
| 911 | cat <<EOH |
| 912 | Usage: github-release.sh [options] [target] ... |
| 913 | |
| 914 | [Github CLI (gh) arguments] |
| 915 | --login Perform authentication using a PAC |
| 916 | --logout |
| 917 | --host [h] Specify github server for connection. |
| 918 | |
| 919 | [Options] |
| 920 | --token Login debugging, alternative to env var use. |
| 921 | |
| 922 | [Modes] |
| 923 | --debug Enable script debug mode |
| 924 | --verbose Enable script verbose mode |
| 925 | |
| 926 | All remaining arguments are passthrough to the gh command. |
| 927 | EOH |
| 928 | |
| 929 | exit 0 |
| 930 | } |
| 931 | |
| 932 | ## ----------------------------------------------------------------------- |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 933 | ## Intent: Normalize common arguments and access to the gh command. |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 934 | ## o Cache path to the gh command |
| 935 | ## o Construct a gh command line from given args |
| 936 | ## o Command wrapper can provide defaults (--hostname github.com) |
| 937 | ## ----------------------------------------------------------------------- |
| 938 | ## Given: |
| 939 | ## scalar Array variable name (declare -n is a reference) |
| 940 | ## Return: |
| 941 | ## ref gh command line passed back to caller |
| 942 | ## Switches: |
| 943 | ## --host Pass default github hostname |
| 944 | ## --verbose Enable verbose mode# |
| 945 | ## --version Display command version |
| 946 | ## @array Remaining arguments passed as command switches. |
| 947 | ## ----------------------------------------------------------------------- |
| 948 | ## See Also: |
| 949 | ## o https://cli.github.com/manual |
| 950 | ## ----------------------------------------------------------------------- |
| 951 | function my_gh() |
| 952 | { |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 953 | declare -a cmd=() |
| 954 | cmd+=("$gh_cmd") |
| 955 | |
| 956 | ## ---------------------- |
| 957 | ## Construct command line |
| 958 | ## ---------------------- |
| 959 | # shellcheck disable=SC2034 |
| 960 | declare -A action=() # pending operations |
| 961 | declare -a args=() # common gh command line args |
| 962 | |
| 963 | while [ $# -gt 0 ]; do |
| 964 | local arg="$1"; shift |
| 965 | case "$arg" in |
| 966 | |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 967 | # Modes |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 968 | -*debug) declare -i -g debug=1 ;; |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 969 | -*verbose) args+=('--verbose') ;; |
| 970 | |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 971 | -*hostname) |
| 972 | get_gh_hostname in_args |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 973 | ;; |
| 974 | |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 975 | --host-repo) |
| 976 | local val |
| 977 | get_argv_repo val |
Joey Armstrong | 76026b7 | 2023-03-29 12:19:17 -0400 | [diff] [blame] | 978 | |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 979 | # --repo <[HOST/]OWNER/REPO> |
Joey Armstrong | 78cecc5 | 2023-04-03 11:39:11 -0400 | [diff] [blame] | 980 | args+=('--repo' "${__githost}/${val}") |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 981 | ;; |
Joey Armstrong | 76026b7 | 2023-03-29 12:19:17 -0400 | [diff] [blame] | 982 | |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 983 | --repo) |
| 984 | local val |
| 985 | get_argv_repo val |
| 986 | args+=('--repo' "$val") |
| 987 | ;; |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 988 | |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 989 | --tag) |
| 990 | local val |
| 991 | get_argv_tag val |
| 992 | args+=("'$val'") # No switch, pass inline |
| 993 | ;; |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 994 | |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 995 | --title) |
| 996 | local val |
| 997 | get_argv_name val |
| 998 | args+=('--title' "'$val'") |
| 999 | ;; |
Joey Armstrong | 76026b7 | 2023-03-29 12:19:17 -0400 | [diff] [blame] | 1000 | |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 1001 | *) args+=("$arg") ;; |
| 1002 | esac |
| 1003 | done |
Joey Armstrong | 76026b7 | 2023-03-29 12:19:17 -0400 | [diff] [blame] | 1004 | |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 1005 | cmd+=("${args[@]}") |
| 1006 | echo "** Running: ${cmd[*]}" |
| 1007 | "${cmd[@]}" |
| 1008 | local status=$? |
| 1009 | |
| 1010 | [[ $status -eq 0 ]] && { true; } || { false; } |
| 1011 | return |
| 1012 | } |
| 1013 | |
| 1014 | ## --------------------------------------------------------------------------- |
| 1015 | ## Intent: |
| 1016 | ## --------------------------------------------------------------------------- |
| 1017 | function usage() |
| 1018 | { |
| 1019 | cat <<EOH |
Joey Armstrong | 76026b7 | 2023-03-29 12:19:17 -0400 | [diff] [blame] | 1020 | |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 1021 | Usage: $0 |
| 1022 | Usage: make [options] [target] ... |
| 1023 | --help This mesage |
| 1024 | --pac Personal Access Token (file) |
| 1025 | |
| 1026 | [DEBUG] |
| 1027 | --gen-version Generate a random release version string. |
| 1028 | --git-hostname Git server hostname (default=github.com) |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 1029 | --version-file Read version string from local version file |
| 1030 | |
| 1031 | --dry-run Simulation mode |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 1032 | |
| 1033 | EOH |
| 1034 | return |
| 1035 | } |
| 1036 | |
| 1037 | ## --------------------------------------------------------------------------- |
| 1038 | ## Intent: Parse script command line arguments |
| 1039 | ## --------------------------------------------------------------------------- |
| 1040 | function parse_args() |
| 1041 | { |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 1042 | [[ -v DEBUG ]] && func_echo "ENTER" |
| 1043 | |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 1044 | while [ $# -gt 0 ]; do |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 1045 | local arg="$1"; shift |
| 1046 | func_echo "ARGV: $arg" |
| 1047 | case "$arg" in |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 1048 | |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 1049 | -*debug) declare -i -g debug=1 ;; |
| 1050 | --draft) declare -i -g draft_release=1 ;; |
| 1051 | --dry-run) declare -i -g dry_run=1 ;; |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 1052 | |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 1053 | --version-file) |
| 1054 | declare -i -g argv_version_file=1 |
| 1055 | ;; |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 1056 | |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 1057 | -*gen-version) |
| 1058 | declare -g -i argv_gen_version=1 |
| 1059 | ;; |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 1060 | |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 1061 | -*git-hostname) |
| 1062 | __githost="$1"; shift |
| 1063 | ;; |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 1064 | |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 1065 | -*repo-name) |
| 1066 | __repo_name="$1"; shift |
| 1067 | ;; |
| 1068 | |
| 1069 | -*repo-org) |
| 1070 | __repo_org="$1"; shift |
| 1071 | ;; |
| 1072 | |
| 1073 | -*pac) |
| 1074 | declare -g pac="$1"; shift |
| 1075 | readonly pac |
| 1076 | [[ ! -f "$pac" ]] && error "--token= does not exist ($pac)" |
| 1077 | : # nop/true |
| 1078 | ;; |
| 1079 | |
| 1080 | -*todo) todo ;; |
| 1081 | |
| 1082 | -*help) |
| 1083 | usage |
| 1084 | exit 0 |
| 1085 | ;; |
| 1086 | |
| 1087 | *) error "Detected unknown argument $arg" ;; |
| 1088 | esac |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 1089 | done |
| 1090 | |
Joey Armstrong | 26707f0 | 2023-01-26 12:41:12 -0500 | [diff] [blame] | 1091 | return |
| 1092 | } |
| 1093 | |
Joey Armstrong | af577ab | 2022-12-15 14:43:33 -0500 | [diff] [blame] | 1094 | ##----------------## |
| 1095 | ##---] MAIN [---## |
| 1096 | ##----------------## |
Joey Armstrong | 1962bcf | 2023-01-27 13:53:18 -0500 | [diff] [blame] | 1097 | iam="${0##*/}" |
| 1098 | |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 1099 | full_banner |
| 1100 | parse_args $@ |
Joey Armstrong | d99e3d2 | 2023-01-29 12:35:43 -0500 | [diff] [blame] | 1101 | init |
| 1102 | install_gh_binary |
Joey Armstrong | 7f382ef | 2023-01-25 12:00:08 -0500 | [diff] [blame] | 1103 | |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 1104 | do_login |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 1105 | |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 1106 | release_path='/dev/null' |
| 1107 | get_release_path release_path |
| 1108 | declare -p release_path |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 1109 | |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 1110 | pushd "$release_path" || error "pushd failed: dir is [$release_path]" |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 1111 | |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 1112 | # legacy: getGitVersion "$GERRIT_PROJECT" GIT_VERSION |
| 1113 | getGitVersion GIT_VERSION |
| 1114 | getReleaseDescription RELEASE_DESCRIPTION |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 1115 | |
Joey Armstrong | eef8c2c | 2023-03-27 17:27:43 -0400 | [diff] [blame] | 1116 | cat <<EOM |
| 1117 | |
| 1118 | ** ----------------------------------------------------------------------- |
| 1119 | ** GIT VERSION: $(declare -p GIT_VERSION) |
| 1120 | ** RELEASE_DESCRIPTION: $(declare -p RELEASE_DESCRIPTION) |
| 1121 | ** RELEASE_TARGETS: $(declare -p RELEASE_TARGETS) |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 1122 | ** ----------------------------------------------------------------------- |
| 1123 | ** Running: make ${RELEASE_TARGETS} |
Joey Armstrong | eef8c2c | 2023-03-27 17:27:43 -0400 | [diff] [blame] | 1124 | ** ----------------------------------------------------------------------- |
| 1125 | EOM |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 1126 | # build the release, can be multiple space separated targets |
| 1127 | # ----------------------------------------------------------------------- |
| 1128 | # % go build command-line-arguments: |
| 1129 | # copying /tmp/go-build4212845548/b001/exe/a.out: |
| 1130 | # open release/voltctl-1.8.25-linux-amd64: permission denied |
| 1131 | # missing: docker run mkdir |
| 1132 | # ----------------------------------------------------------------------- |
| 1133 | # shellcheck disable=SC2086 |
Joey Armstrong | eef8c2c | 2023-03-27 17:27:43 -0400 | [diff] [blame] | 1134 | make "$RELEASE_TARGETS" |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 1135 | copyToRelease |
Joey Armstrong | 50f6e0b | 2023-01-24 14:14:08 -0500 | [diff] [blame] | 1136 | |
Joey Armstrong | 26707f0 | 2023-01-26 12:41:12 -0500 | [diff] [blame] | 1137 | cat <<EOM |
Joey Armstrong | 1962bcf | 2023-01-27 13:53:18 -0500 | [diff] [blame] | 1138 | |
| 1139 | ** ----------------------------------------------------------------------- |
| 1140 | ** Create the release: |
| 1141 | ** 1) Create initial github release with download area. |
| 1142 | ** 2) Generate checksum.SHA256 for all released files. |
| 1143 | ** 3) Upload files to complete the release. |
Joey Armstrong | 75a0d93 | 2023-03-28 08:59:54 -0400 | [diff] [blame] | 1144 | ** 4) Display released info from github |
Joey Armstrong | 1962bcf | 2023-01-27 13:53:18 -0500 | [diff] [blame] | 1145 | ** ----------------------------------------------------------------------- |
Joey Armstrong | 26707f0 | 2023-01-26 12:41:12 -0500 | [diff] [blame] | 1146 | EOM |
| 1147 | |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 1148 | showReleases |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 1149 | release_staging |
| 1150 | popd || error "pushd failed: dir is [$release_path]" |
Joey Armstrong | 43c4842 | 2023-04-03 10:17:32 -0400 | [diff] [blame] | 1151 | # fi |
Joey Armstrong | 28eddda | 2023-01-10 03:09:34 -0500 | [diff] [blame] | 1152 | |
Joey Armstrong | 2097d3e | 2023-03-26 10:32:03 -0400 | [diff] [blame] | 1153 | do_logout |
| 1154 | |
Joey Armstrong | 28eddda | 2023-01-10 03:09:34 -0500 | [diff] [blame] | 1155 | # [SEE ALSO] |
| 1156 | # ----------------------------------------------------------------------- |
| 1157 | # https://www.shellcheck.net/wiki/SC2236 |
Joey Armstrong | 26707f0 | 2023-01-26 12:41:12 -0500 | [diff] [blame] | 1158 | # https://docs.github.com/en/rest/releases/releases?apiVersion=2022-11-28#create-a-release |
Joey Armstrong | 28eddda | 2023-01-10 03:09:34 -0500 | [diff] [blame] | 1159 | # ----------------------------------------------------------------------- |
Joey Armstrong | f085d87 | 2023-01-28 17:52:29 -0500 | [diff] [blame] | 1160 | # https://cli.github.com/manual/gh_help_reference |
| 1161 | # https://cli.github.com/manual/gh_release |
| 1162 | # ----------------------------------------------------------------------- |
Joey Armstrong | 28eddda | 2023-01-10 03:09:34 -0500 | [diff] [blame] | 1163 | |
| 1164 | # [EOF] |