blob: fe206acc48a0ca54c7eafb0355a52ef283cc9b98 [file] [log] [blame]
Zack Williams27cd3e52018-09-18 16:44:50 -07001#!/usr/bin/env bash
Joey Armstrong28eddda2023-01-10 03:09:34 -05002# -----------------------------------------------------------------------
Joey Armstrongeb84a632024-03-21 13:36:32 -04003# Copyright 2018-2024 Open Networking Foundation Contributors
Zack Williams27cd3e52018-09-18 16:44:50 -07004#
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#
Joey Armstrong4d612a92024-04-24 15:30:49 -04009# http://www.apache.org/licenses/LICENSE-2.0
Zack Williams27cd3e52018-09-18 16:44:50 -070010#
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 Armstrongeb84a632024-03-21 13:36:32 -040016# -----------------------------------------------------------------------
17# SPDX-FileCopyrightText: 2018-2024 Open Networking Foundation Contributors
18# SPDX-License-Identifier: Apache-2.0
19# -----------------------------------------------------------------------
Joey Armstrongdb43bde2024-04-01 14:54:35 -040020# Intent:
Zack Williams27cd3e52018-09-18 16:44:50 -070021# builds (with make) and uploads release artifacts (binaries, etc.) to github
22# given a tag also create checksums files and release notes from the commit
23# message
Joey Armstrong28eddda2023-01-10 03:09:34 -050024# -----------------------------------------------------------------------
Zack Williams27cd3e52018-09-18 16:44:50 -070025
Joey Armstrong2097d3e2023-03-26 10:32:03 -040026set -euo pipefail
27
28##-------------------##
29##---] GLOBALS [---##
30##-------------------##
31declare -g WORKSPACE
32declare -g GERRIT_PROJECT
Joey Armstrong78cecc52023-04-03 11:39:11 -040033declare -g __githost=github.com
Joey Armstrong43c48422023-04-03 10:17:32 -040034# export DEBUG=1
Joey Armstrong2097d3e2023-03-26 10:32:03 -040035
Joey Armstrong4d612a92024-04-24 15:30:49 -040036declare -g pgm
37pgm="$(readlink --canonicalize-existing "$0")"
38readonly pgm
39declare libdir="${pgm:0:-3}"
40readonly libdir
41
42##--------------------##
43##---] INCLUDES [---##
44##--------------------##
45source "$libdir/help.sh"
46source "$libdir/parse-args.sh"
47
Joey Armstrong2097d3e2023-03-26 10:32:03 -040048## -----------------------------------------------------------------------
49## Uncomment to activate
50## -----------------------------------------------------------------------
Joey Armstrong4d612a92024-04-24 15:30:49 -040051# declare -g -i debug
52
Joey Armstronge8560da2023-04-26 15:44:45 -040053# Debug arguments
54# declare -i -g argv_gen_version=1
55# declare -i -g draft_release=1
Joey Armstrong41923cc2023-01-30 14:38:16 -050056
Joey Armstrong2097d3e2023-03-26 10:32:03 -040057declare -a -g ARGV=() # Capture args to minimize globals and arg passing
58[[ $# -gt 0 ]] && ARGV=("$@")
Joey Armstrongd99e3d22023-01-29 12:35:43 -050059
60declare -g scratch # temp workspace for downloads
Joey Armstrongd99e3d22023-01-29 12:35:43 -050061
Joey Armstrong4d612a92024-04-24 15:30:49 -040062declare -g SCRIPT_VERSION='1.5' # git changeset needed
Joey Armstrong26707f02023-01-26 12:41:12 -050063
Joey Armstrong7f382ef2023-01-25 12:00:08 -050064##--------------------##
65##---] INCLUDES [---##
Joey Armstrong1962bcf2023-01-27 13:53:18 -050066##--------------------#
Joey Armstrong2097d3e2023-03-26 10:32:03 -040067
68## -----------------------------------------------------------------------
69## Intent: Register an interrupt handler to display a stack trace on error
70## -----------------------------------------------------------------------
Joey Armstrong4d612a92024-04-24 15:30:49 -040071function bannerEL()
72{
73 # echo " $i: ${BASH_SOURCE[$i+1]}:${BASH_LINENO[$i]} ${FUNCNAME[$i]}(...)"
74
75 # iam="${0##*/}"
76 if [[ -v debug ]]; then
77 if [[ $# -gt 0 ]]; then
78 local type="$1"; shift
79 else
80 local type='LEAVE'
81 fi
82
83 local func="${FUNCNAME[1]}"
84 local line="${BASH_LINENO[1]}"
85 case "$type" in
86 LEAVE) ;;
87 *) type='ENTER' ;;
88 esac
89
90 printf "** %s %s (LINENO:%s)\n" "$func" "$type" "$line"
91 fi
92
93 return
94}
95
96## -----------------------------------------------------------------------
97## Intent: Register an interrupt handler to display a stack trace on error
98## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -040099function errexit()
100{
101 local err=$?
102 set +o xtrace
103 local code="${1:-1}"
104
105 local prefix="${BASH_SOURCE[1]}:${BASH_LINENO[0]}"
106 echo -e "\nOFFENDER: ${prefix}"
107 if [ $# -gt 0 ] && [ "$1" == '--stacktrace-quiet' ]; then
108 code=1
109 else
110 echo "ERROR: '${BASH_COMMAND}' exited with status $err"
111 fi
112
113 # Print out the stack trace described by $function_stack
114 if [ ${#FUNCNAME[@]} -gt 2 ]
115 then
Joey Armstrong4d612a92024-04-24 15:30:49 -0400116 echo "Call tree:"
117 for ((i=1;i<${#FUNCNAME[@]}-1;i++))
118 do
119 echo " $i: ${BASH_SOURCE[$i+1]}:${BASH_LINENO[$i]} ${FUNCNAME[$i]}(...)"
120 done
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400121 fi
122
123 echo "Exiting with status ${code}"
124 echo
125 exit "${code}"
126 # return
127}
128
129# trap ERR to provide an error handler whenever a command exits nonzero
130# this is a more verbose version of set -o errexit
131trap errexit ERR
132
133# setting errtrace allows our ERR trap handler to be propagated to functions,
134# expansions and subshells
135set -o errtrace
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500136
137## -----------------------------------------------------------------------
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500138## Intent: Cleanup scratch area on exit
139## -----------------------------------------------------------------------
140function sigtrap()
141{
142 ## Prevent mishaps
143 local is_read_only
144 is_read_only="$(declare -p scratch)"
145 if [[ $is_read_only != *"declare -r scratch="* ]]; then
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400146 echo "ERROR: variable scratch is not read-only, cleanup skipped"
147 exit 1
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500148 fi
149
150 if [ -d "$scratch" ]; then
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400151 /bin/rm -fr "$scratch"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500152 fi
153
Joey Armstrong4d612a92024-04-24 15:30:49 -0400154 # shellcheck disable=SC2119
155 do_logout
156
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500157 return
158}
159trap sigtrap EXIT
160
161## -----------------------------------------------------------------------
Joey Armstronge8560da2023-04-26 15:44:45 -0400162## Intent: Return a random release version string.
163## -----------------------------------------------------------------------
164## Note: Do not use this function in production. get_version() is
165## intended for local use or debugging $0 from within a jenkins
166## job.
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400167## -----------------------------------------------------------------------
168function get_version()
169{
Joey Armstrong1cc90292024-04-02 09:27:33 -0400170 local -n ref=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400171
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400172 declare -a rev=()
173 rev+=("$(( RANDOM % 10 + 1 ))")
174 rev+=("$(( RANDOM % 256 + 1 ))")
Joey Armstrongeef8c2c2023-03-27 17:27:43 -0400175 rev+=("$(( RANDOM % 10000 + 1 ))")
Joey Armstrongfc20ed52023-04-03 19:37:58 -0400176 local ver="v${rev[0]}.${rev[1]}.${rev[2]}"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400177
Joey Armstrong4d612a92024-04-24 15:30:49 -0400178 func_echo "GENERATED VERSION STRING: $ver"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400179 ref="$ver"
180 return
181}
182
183## -----------------------------------------------------------------------
184## Intent: Provide defaults for environment variables
185## -----------------------------------------------------------------------
186function initEnvVars()
187{
188 # when not running under Jenkins, use current dir as workspace and a blank
189 # project name
190 declare -g WORKSPACE=${WORKSPACE:-.}
191 declare -g GERRIT_PROJECT=${GERRIT_PROJECT:-}
Joey Armstrong76026b72023-03-29 12:19:17 -0400192
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400193 # Github organization (or user) this project is published on. Project name should
194 # be the same on both Gerrit and GitHub
Joey Armstrong76026b72023-03-29 12:19:17 -0400195 declare -g GITHUB_ORGANIZATION=${GITHUB_ORGANIZATION:-}
196
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400197 # glob pattern relative to project dir matching release artifacts
198 # ARTIFACT_GLOB=${ARTIFACT_GLOB:-"release/*"} # stat -- release/* not found, literal string (?)
199 declare -g ARTIFACT_GLOB=${ARTIFACT_GLOB:-"release/."}
Joey Armstrong76026b72023-03-29 12:19:17 -0400200
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400201 # Use "release" as the default makefile target, can be a space separated list
202 declare -g RELEASE_TARGETS=${RELEASE_TARGETS:-release}
Joey Armstrong76026b72023-03-29 12:19:17 -0400203
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400204 # Set and handle GOPATH and PATH
205 export GOPATH=${GOPATH:-$WORKSPACE/go}
206 export PATH=$PATH:/usr/lib/go-1.12/bin:/usr/local/go/bin:$GOPATH/bin
207 return
208}
209
210## -----------------------------------------------------------------------
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500211## Intent: Create a scratch area for downloads and transient tools
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400212## temp directory will be automatically removed upon exit.
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500213## -----------------------------------------------------------------------
214function init()
215{
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500216 local pkgbase="${0##*/}" # basename
217 local pkgname="${pkgbase%.*}"
218
Joey Armstrong4d612a92024-04-24 15:30:49 -0400219 bannerEL 'ENTER'
220
Joey Armstrongad7b1e02023-03-27 11:55:48 -0400221 # initEnvVars # moved to full_banner()
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400222
223 ## Create a temp directory for auto-cleanup
224 declare -g scratch
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500225 scratch="$(mktemp -d -t "${pkgname}.XXXXXXXXXX")"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400226 readonly scratch
227 declare -p scratch
228
Joey Armstrong43c48422023-04-03 10:17:32 -0400229 ## prime the stream: cache answers
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400230 local work
231 get_release_dir work
232 declare -p work
Joey Armstrong43c48422023-04-03 10:17:32 -0400233
Joey Armstronge8560da2023-04-26 15:44:45 -0400234 local git_version
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400235 getGitVersion git_version
236 func_echo "$(declare -p git_version)"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400237 return
238}
239
240## -----------------------------------------------------------------------
241## Intent: Verbose output for logging
242## -----------------------------------------------------------------------
243function banner()
244{
245 local iam="${0##*/}"
246 cat <<EOB
247
248** -----------------------------------------------------------------------
249** ${iam}::${FUNCNAME[1]}: $*
250** -----------------------------------------------------------------------
251EOB
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500252}
253
254## -----------------------------------------------------------------------
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500255## Intent: Output a log banner to identify the running script/version.
256## -----------------------------------------------------------------------
257## TODO:
258## o SCRIPT_VERSION => git changeset for repo:ci-managment
259## o echo "library version: ${env."library.libName.version"}"
260# -----------------------------------------------------------------------
261# 14:18:38 > git fetch --no-tags --progress -- https://gerrit.opencord.org/ci-management.git +refs/heads/*:refs/remotes/origin/* # timeout=10
262# 14:18:39 Checking out Revision 50f6e0b97f449b32d32ec0e02d59642000351847 (master)
263# -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400264function full_banner()
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500265{
Joey Armstrongad7b1e02023-03-27 11:55:48 -0400266 local iam="${0##*/}"
267
268 initEnvVars # set defaults
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500269
Joey Armstrong4d612a92024-04-24 15:30:49 -0400270 cat <<EOH
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500271
272** -----------------------------------------------------------------------
273** IAM: ${iam} :: ${FUNCNAME[0]}
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400274** ARGV: ${ARGV[@]}
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500275** PWD: $(/bin/pwd)
276** NOW: $(date '+%Y/%m/%d %H:%M:%S')
277** VER: ${SCRIPT_VERSION:-'unknown'}
278** -----------------------------------------------------------------------
Joey Armstrongad7b1e02023-03-27 11:55:48 -0400279** GERRIT_PROJECT: $(declare -p GERRIT_PROJECT)
280** GITHUB_ORGANIZATION: $(declare -p GITHUB_ORGANIZATION)
281** RELEASE_TARGETS: $(declare -p RELEASE_TARGETS)
282** GOPATH: $(declare -p GOPATH)
283** -----------------------------------------------------------------------
284** PATH += /usr/lib/go-1.12/bin:/usr/local/go/bin:GOPATH/bin
285** -----------------------------------------------------------------------
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500286EOH
287
288 return
289}
290
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500291## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400292## Intent: Display a message with 'iam' identifier for logging
Joey Armstrong50f6e0b2023-01-24 14:14:08 -0500293## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400294function func_echo()
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500295{
296 local iam="${0##*/}"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400297 echo "** ${iam} :: ${FUNCNAME[1]}: $*"
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500298 return
299}
300
301## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400302## Intent: Display an error message then exit with status.
303## -----------------------------------------------------------------------
304function error()
305{
306 local iam="${0##*/}"
Joey Armstrong4d612a92024-04-24 15:30:49 -0400307 local func="${FUNCNAME[1]}"
308 local line="${BASH_LINENO[1]}"
309 printf "\nERROR %s :: %s (LINENO:%s): %s" "$iam" "$func" "$line" "$*"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400310 exit 1
311}
312
313## -----------------------------------------------------------------------
Joey Armstrong4d612a92024-04-24 15:30:49 -0400314## Intent: Script self checking, direct API access for troubleshooting
315## -----------------------------------------------------------------------
316function do_api_validation()
317{
318 # if --verify credentials
319 do_login
320 # shellcheck disable=SC2119
321 do_logout
322 return
323}
324
325## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400326## Intent: Verify sandbox/build is versioned for release.
327## -----------------------------------------------------------------------
328function getGitVersion()
329{
Joey Armstrong1cc90292024-04-02 09:27:33 -0400330 local -n varname=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400331
Joey Armstrong4d612a92024-04-24 15:30:49 -0400332 bannerEL 'ENTER'
333
Joey Armstrong43c48422023-04-03 10:17:32 -0400334 local __ver # use prefix('__') to protect callers variable name
335 if [[ -v cached_getGitVersion ]]; then
336 __ver="$cached_getGitVersion"
337 varname="$__ver"
338 return
Joey Armstrong39fac652023-03-27 18:50:43 -0400339
Joey Armstrong43c48422023-04-03 10:17:32 -0400340 elif [[ -v argv_gen_version ]]; then
341 get_version __ver
342
343 elif [[ -v WORKSPACE ]] && [[ -v GITHUB_TOKEN ]]; then # i_am_jenkins
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400344 local path="$GERRIT_PROJECT"
Joey Armstrong4d612a92024-04-24 15:30:49 -0400345 pushd "$path" || { error "pushd GERRIT_PROJECT= failed $(declare -p path)"; }
Joey Armstrong43c48422023-04-03 10:17:32 -0400346 __ver="$(git tag -l --points-at HEAD)"
Joey Armstrong4d612a92024-04-24 15:30:49 -0400347 popd || { error "popd GERRIT_PROJECT= failed"; }
Joey Armstrong43c48422023-04-03 10:17:32 -0400348
349 elif [[ -v argv_version_file ]]; then # local debug
350 [[ ! -f VERSION ]] && error "./VERSION file not found"
351 readarray -t tmp < <(sed -e 's/[[:blank:]]//g' 'VERSION')
352 __ver="v${tmp[0]}"
353
Joey Armstrong4d612a92024-04-24 15:30:49 -0400354 elif [[ ${#GERRIT_PROJECT} -gt 0 ]]; then
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400355 cd ..
356 local path="$GERRIT_PROJECT"
Joey Armstrong4d612a92024-04-24 15:30:49 -0400357 declare -p path
358 pushd "$path" || { error "pushd GERRIT_PROJECT= failed $(declare -p path)"; }
Joey Armstrong43c48422023-04-03 10:17:32 -0400359 __ver="$(git tag -l --points-at HEAD)"
Joey Armstrong4d612a92024-04-24 15:30:49 -0400360 popd || { error "popd GERRIT_PROJECT= failed"; }
361
362 else # pwd==sandbox
363
364 local line="${BASH_LINENO[0]}"
365 local message="
366GERRIT_PROJECT= is not defined:
367 o One of the following are required:
368 - Define GERRIT_PROJECT= (~jenkins).
369 - Argument --verison-file.
370"
371 error "$message"
Joey Armstrong43c48422023-04-03 10:17:32 -0400372 fi
Joey Armstrong76026b72023-03-29 12:19:17 -0400373
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400374 # ------------------------------------------------------
375 # match bare versions or v-prefixed golang style version
376 # Critical failure for new/yet-to-be-released repo ?
377 # ------------------------------------------------------
Joey Armstrong43c48422023-04-03 10:17:32 -0400378 if [[ "$__ver" =~ ^v?([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then
Joey Armstrongfc20ed52023-04-03 19:37:58 -0400379 echo "git has a SemVer released version tag: '$__ver'"
Joey Armstrong43c48422023-04-03 10:17:32 -0400380 echo "Building artifacts for GitHub release."
381
382 elif [[ "$__ver" =~ ^v?([0-9]+)\.([0-9]+)\.([0-9]+)-dev([0-9]+)$ ]]; then
Joey Armstrong4d612a92024-04-24 15:30:49 -0400383 # v1.2.3-dev (*-dev*) is an implicit draft release.
Joey Armstrong43c48422023-04-03 10:17:32 -0400384 declare -i -g draft_release=1
385 echo "Detected --draft SemVer release version tag [$__ver]"
386 echo "Building artifacts for GitHub release."
387
Joey Armstrong4d612a92024-04-24 15:30:49 -0400388 # Should also accept: X.Y.Z-{alpha,beta,...}
Joey Armstrong4f87de82023-08-25 12:42:39 -0400389
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400390 else
Joey Armstrong4f87de82023-08-25 12:42:39 -0400391 echo "Version string contains: [${__ver}]"
Joey Armstrong43c48422023-04-03 10:17:32 -0400392 error "No SemVer released version tag found, exiting..."
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400393 fi
394
Joey Armstrong43c48422023-04-03 10:17:32 -0400395 ## Cache value for subsequent calls.
396 ## readonly is a guarantee assignment happens once.
397 declare -g cached_getGitVersion="$__ver"
398 readonly cached_getGitVersion;
399
400 varname="$__ver"
401 func_echo "Version is [$__ver] from $(/bin/pwd)"
Joey Armstrong4d612a92024-04-24 15:30:49 -0400402
403 bannerEL 'LEAVE'
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400404 return
405}
406
407## -----------------------------------------------------------------------
408## Intent:
409## Note: Release description is sanitized version of the log message
410## -----------------------------------------------------------------------
411function getReleaseDescription()
412{
Joey Armstrong1cc90292024-04-02 09:27:33 -0400413 local -n varname=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400414
415 local msg
416 msg="$(git log -1 --pretty=%B)"
417
418 local val
419 val="$(tr -dc "[:alnum:]\n\r\.\[\]\:\-\\\/\`\' " <<< "$msg")"
420
421 [[ ${#val} -eq 0 ]] && error "Release Description is empty ($msg)"
422 varname="$val"
423 return
424}
425
426## -----------------------------------------------------------------------
427## Intent: Retrieve value of the release temp directory.
428## -----------------------------------------------------------------------
429## Note: Limit use of globals so logic can be isolated in function calls.
430## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400431function get_release_dir()
432{
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400433 local -n varname=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400434
435 # Temporary staging directory to copy artifacts to
436 varname="$scratch/release"
437 mkdir -p "$varname"
438 return
439}
440
441## -----------------------------------------------------------------------
442## Intent: Retrieve github server hostname
443## -----------------------------------------------------------------------
444function get_gh_hostname()
445{
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400446 local -n varname=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400447 varname+=('--hostname' "${__githost}")
448 return
449}
450
451## -----------------------------------------------------------------------
452## Intent: Retrieve repository organizaiton name
453## -----------------------------------------------------------------------
454function get_gh_repo_org()
455{
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400456 local -n ref=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400457 declare -g __repo_org
Joey Armstrong76026b72023-03-29 12:19:17 -0400458
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400459 local org
460 if [[ -v __repo_org ]]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400461 org="$__repo_org"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400462 elif [[ ! -v GITHUB_ORGANIZATION ]]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400463 error "--repo-org or GITHUB_ORGANIZATION= are required"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400464 else
Joey Armstrong43c48422023-04-03 10:17:32 -0400465 org="${GITHUB_ORGANIZATION}"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400466 fi
467
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400468 ref="$org"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400469 return
470}
471
472## -----------------------------------------------------------------------
473## Intent: Retrieve repository organizaiton name
474## -----------------------------------------------------------------------
475function get_gh_repo_name()
476{
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400477 local -n ref=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400478 declare -g __repo_name
Joey Armstrong76026b72023-03-29 12:19:17 -0400479
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400480 local name
481 if [[ -v __repo_name ]]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400482 name="$__repo_name"
Joey Armstrong39fac652023-03-27 18:50:43 -0400483 elif [[ ! -v GERRIT_PROJECT ]]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400484 error "--repo-name or GERRIT_PROJECT= are required"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400485 else
Joey Armstrong43c48422023-04-03 10:17:32 -0400486 name="${GERRIT_PROJECT}"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400487 fi
488
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400489 ref="$name"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400490 return
491}
492
493## -----------------------------------------------------------------------
494## Intent: Return path for the gh release query
495## -----------------------------------------------------------------------
496function get_gh_releases()
497{
Joey Armstrong1cc90292024-04-02 09:27:33 -0400498 local -n ref=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400499
500 local repo_org
501 get_gh_repo_org repo_org
502
503 local repo_name
504 get_gh_repo_name repo_name
505
506 ref="repos/${repo_org}/${repo_name}/releases"
Joey Armstrong4d612a92024-04-24 15:30:49 -0400507 func_echo "releases_uri=$ref"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400508 return
509}
510
511## -----------------------------------------------------------------------
512## Intent: Retrieve repository path argument
513## -----------------------------------------------------------------------
514function get_argv_repo()
515{
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400516 local -n ref=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400517
518 local repo_org
519 get_gh_repo_org repo_org
Joey Armstrong76026b72023-03-29 12:19:17 -0400520
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400521 local repo_name
Joey Armstrong76026b72023-03-29 12:19:17 -0400522 get_gh_repo_name repo_name
523
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400524 ref="${repo_org}/${repo_name}"
525 # func_echo "ref=$(declare -p ref)"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400526 return
527}
528
529## -----------------------------------------------------------------------
530## Intent: Retrieve release name string "project - version"
531## -----------------------------------------------------------------------
532function get_argv_name()
533{
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400534 local -n ref=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400535
536 local repo_name
537 get_gh_repo_name repo_name
Joey Armstrong43c48422023-04-03 10:17:32 -0400538
539 local repo_ver
540 getGitVersion repo_ver
541
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400542 # ref="${repo_name} - $GIT_VERSION"
543 ref="${repo_name} - ${repo_ver}"
544 func_echo "ref=$(declare -p ref)"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400545 return
546}
547
548## -----------------------------------------------------------------------
549## Intent: Retrieve tag version
550## -----------------------------------------------------------------------
551function get_argv_tag()
552{
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400553 local -n ref=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400554
555 # cached_argv_tag='v3.41.3204'
556 if [[ ! -v cached_argv_tag ]]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400557 declare -g cached_argv_tag
558 if [[ -v GIT_VERSION ]]; then # hello jenkins
559 cached_argv_tag="$GIT_VERSION"
560 fi
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400561 fi
562
563 [[ ${#cached_argv_tag} -eq 0 ]] && error "Unable to determine GIT_VERSION="
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400564 ref="$cached_argv_tag"
565 func_echo "ref=$(declare -p ref)"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400566 return
567}
568
569## -----------------------------------------------------------------------
570## Intent:
571## -----------------------------------------------------------------------
572# To support golang projects that require GOPATH to be set and code checked out there
573# If $DEST_GOPATH is not an empty string:
574# - create GOPATH within WORKSPACE, and destination directory within
575# - set PATH to include $GOPATH/bin and the system go binaries
576# - move project from $WORKSPACE/$GERRIT_PROJECT to new location in $GOPATH
577# - start release process within that directory
578## -----------------------------------------------------------------------
579function get_release_path()
580{
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400581 local -n ref=$1; shift
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400582 local varpath="$ref"
Joey Armstrongdaa1f0a2024-04-03 18:07:59 -0400583
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400584 DEST_GOPATH=${DEST_GOPATH:-}
585 if [ -n "$DEST_GOPATH" ]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400586 mkdir -p "$GOPATH/src/$DEST_GOPATH"
Joey Armstronge8560da2023-04-26 15:44:45 -0400587 varpath="$GOPATH/src/$DEST_GOPATH/$GERRIT_PROJECT"
588 mv "$WORKSPACE/$GERRIT_PROJECT" "$varpath"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400589 else
Joey Armstronge8560da2023-04-26 15:44:45 -0400590 varpath="$WORKSPACE/$GERRIT_PROJECT"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400591 fi
592
Joey Armstrong43c48422023-04-03 10:17:32 -0400593 ## Verify pwd is OK
594 for path in \
Joey Armstronge8560da2023-04-26 15:44:45 -0400595 "${varpath}/Makefile"\
Joey Armstrong4d612a92024-04-24 15:30:49 -0400596 "${varpath}/makefile"\
597 "__ERROR__"\
598 ; do
Joey Armstrong43c48422023-04-03 10:17:32 -0400599 case "$path" in
Joey Armstronge8560da2023-04-26 15:44:45 -0400600 __ERROR__) error "Makefile not found at ${varpath} !" ;;
Joey Armstrong43c48422023-04-03 10:17:32 -0400601 *) [[ -f "$path" ]] && break ;;
602 esac
603 done
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400604
Mahir Gunyel2c9e8502024-03-29 13:05:38 -0700605 ref="$varpath"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400606}
607
608## -----------------------------------------------------------------------
609## Intent: Display future enhancements
610## -----------------------------------------------------------------------
611function todo()
612{
613 local iam="${0##*/}"
614
Joey Armstrong4d612a92024-04-24 15:30:49 -0400615 cat <<EOT
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400616
617** -----------------------------------------------------------------------
618** IAM: ${iam} :: ${FUNCNAME[0]}
619** -----------------------------------------------------------------------
620 o get_release_path()
621 - refactor redundant paths into local vars.
622 - see comments, do we have a one-off failure condition ?
Joey Armstrongad7b1e02023-03-27 11:55:48 -0400623 o PATH += golang appended 3 times, release needs a single, reliable answer.
624 o do_login, do_logout and api calls do not use the my_gh wrapper:
625 - Add a lookup function to cache and retrieve path to downloaded gh command.
Joey Armstrong76026b72023-03-29 12:19:17 -0400626
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400627EOT
628
629 return
630}
631
632## -----------------------------------------------------------------------
Joey Armstrong76026b72023-03-29 12:19:17 -0400633## Intent: Verify a directory contains content for release.
634## -----------------------------------------------------------------------
635## Given:
636## scalar Path to release/ directory
637## ref Results returned through this indirect var.
638## -----------------------------------------------------------------------
639## Usage:
640## declare -a artifacts=()
641## get_artifacts '/foo/bar/tans' artifacts
642## declare -p artifacts
643## -----------------------------------------------------------------------
644function get_artifacts()
645{
646 local dir="$1" ; shift
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400647 local -n refA=$1 ; shift
Joey Armstrong76026b72023-03-29 12:19:17 -0400648
Joey Armstrong55fe80e2023-04-05 18:32:14 -0400649 # Glob available files, exclude checksums
650 readarray -t __artifacts < <(find "$dir" -mindepth 1 ! -type d \
Joey Armstrong4d612a92024-04-24 15:30:49 -0400651 | grep -iv -e 'sum256' -e 'checksum')
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400652 # func_echo "$(declare -p __artifacts)"
Joey Armstrong76026b72023-03-29 12:19:17 -0400653
654 # -----------------------------------------------------------------------
655 # Verify count>0 to inhibit source-only release
656 # Problem children:
657 # o build or make release failures.
658 # o docker container filesystem mount problem (~volume)
659 # -----------------------------------------------------------------------
660 [[ ${#__artifacts[@]} -eq 0 ]] \
Joey Armstrong4d612a92024-04-24 15:30:49 -0400661 && error "Artifact dir is empty: $(declare -p dir)"
Joey Armstrong76026b72023-03-29 12:19:17 -0400662
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400663 refA=("${__artifacts[@]}")
Joey Armstrong76026b72023-03-29 12:19:17 -0400664 return
665}
666
667## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400668## Intent: Copy files from the build directory into the release staging
669## directory for publishing to github releases/ endpoint.
Joey Armstrong26707f02023-01-26 12:41:12 -0500670## -----------------------------------------------------------------------
671function copyToRelease()
672{
Joey Armstrong76026b72023-03-29 12:19:17 -0400673 banner ''
Joey Armstrong26707f02023-01-26 12:41:12 -0500674
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400675 local artifact_glob="${ARTIFACT_GLOB%/*}"
676 func_echo "$(declare -p artifact_glob)"
677
678 local work
679 get_release_dir work
Joey Armstrong76026b72023-03-29 12:19:17 -0400680 func_echo "Artifact dir: $(declare -p work)"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400681
Joey Armstrong76026b72023-03-29 12:19:17 -0400682 ## Verify release content is available
683 declare -a artifacts=()
Joey Armstrong43c48422023-04-03 10:17:32 -0400684 # get_artifacts "$work" artifacts
685 get_artifacts "$artifact_glob" artifacts
686 func_echo "Artifacts in copy_from: $(declare -p artifacts)"
Joey Armstrong472bfba2023-03-27 18:12:28 -0400687
Joey Armstrong26707f02023-01-26 12:41:12 -0500688 # Copy artifacts into the release temp dir
689 # shellcheck disable=SC2086
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400690 echo "rsync -rv --checksum \"$artifact_glob/.\" \"$work/.\""
691 rsync -rv --checksum "$artifact_glob/." "$work/."
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500692
Joey Armstrong43c48422023-04-03 10:17:32 -0400693 get_artifacts "$work" artifacts
694 func_echo "Artifacts in copy_to: $(declare -p artifacts)"
695
Joey Armstrong26707f02023-01-26 12:41:12 -0500696 return
697}
698
699## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400700## https://docs.github.com/en/rest/releases?apiVersion=2022-11-28
Joey Armstrong4d612a92024-04-24 15:30:49 -0400701# https://cli.github.com/manual/gh_release_create
702# --target <branch> or commit SHA
703# --title
704# --generate-notes
705# --release-notes (notes file)
706# --release
707# release create dist/*.tgz
708# --discussion-category "General"
Joey Armstrongf085d872023-01-28 17:52:29 -0500709## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400710# https://cli.github.com/manual/gh_release_create
711## -----------------------------------------------------------------------
712function gh_release_create()
Joey Armstrong38bfeea2023-02-06 18:01:29 -0500713{
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400714 banner ''
Joey Armstrong38bfeea2023-02-06 18:01:29 -0500715
Joey Armstrong43c48422023-04-03 10:17:32 -0400716 local version
717 getGitVersion version
Joey Armstrong38bfeea2023-02-06 18:01:29 -0500718
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400719 local work
720 get_release_dir work
721
Joey Armstrong43c48422023-04-03 10:17:32 -0400722 declare -a args=()
723 args+=('--host-repo')
724 args+=('--title')
725 if [[ -v draft_release ]]; then
726 args+=('--draft')
727 else
728 args+=('--discussion-category' 'Announcements')
729 fi
730
Joey Armstrong55fe80e2023-04-05 18:32:14 -0400731 if [[ -v release_notes ]]; then
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400732 args+=('--notes-file' "$release_notes")
Joey Armstrong55fe80e2023-04-05 18:32:14 -0400733 fi
Joey Armstrong43c48422023-04-03 10:17:32 -0400734
Joey Armstrong78cecc52023-04-03 11:39:11 -0400735 pushd "$work/.." >/dev/null
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400736 # func_echo "WORK=$work"
Joey Armstrong78cecc52023-04-03 11:39:11 -0400737 readarray -t payload < <(find 'release' -maxdepth 4 ! -type d -print)
Joey Armstrong76026b72023-03-29 12:19:17 -0400738
Joey Armstronge8560da2023-04-26 15:44:45 -0400739 func_echo "$gh_cmd release create ${version} ${args[*]} ${payload[*]}"
Joey Armstrong43c48422023-04-03 10:17:32 -0400740
741 if [[ -v dry_run ]]; then
742 echo "[SKIP] dry run"
743 else
Joey Armstronge8560da2023-04-26 15:44:45 -0400744 func_echo "my_gh release create '$version' ${args[*]} ${payload[*]}"
Joey Armstrong4dbe7002023-04-04 12:47:38 -0400745 my_gh 'release' 'create' "$version" "${args[@]}" "${payload[@]}"
Joey Armstrong43c48422023-04-03 10:17:32 -0400746 fi
Joey Armstrong76026b72023-03-29 12:19:17 -0400747 popd >/dev/null
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400748
Joey Armstrong38bfeea2023-02-06 18:01:29 -0500749 return
750}
751
752## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400753## Intent: Authenticate credentials for a github gh session
Joey Armstrong38bfeea2023-02-06 18:01:29 -0500754## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400755## NOTE: my_gh currently unused due to --with-token < "$pac"
756## -----------------------------------------------------------------------
757function do_login()
Joey Armstrongf085d872023-01-28 17:52:29 -0500758{
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400759 # shellcheck disable=SC2120
760 # shellcheck disable=SC2034
Joey Armstrong4d612a92024-04-24 15:30:49 -0400761 [[ $# -gt 0 ]] && { local unused="$1"; shift; }
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400762
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400763 declare -g pac
Joey Armstrong485081f2023-03-27 13:34:08 -0400764 declare -a login_args=()
765 [[ $# -gt 0 ]] && login_args+=("$@")
Joey Armstrongf085d872023-01-28 17:52:29 -0500766
Joey Armstrong485081f2023-03-27 13:34:08 -0400767 # https://github.com/cli/cli/issues/2922#issuecomment-775027762
Joey Armstrong75a0d932023-03-28 08:59:54 -0400768 # (sigh) why not quietly return VS forcing a special logic path
Joey Armstronge6e18eb2023-03-27 14:19:21 -0400769 # [[ -v WORKSPACE ]] && [[ -v GITHUB_TOKEN ]] && return
770
Joey Armstrong4d612a92024-04-24 15:30:49 -0400771 # 12:58:36 ** -----------------------------------------------------------------------
772 # 12:58:36 ** jenkins582353203049151829.sh::do_login: --hostname github.com
773 # 12:58:36 ** --------------------------------------------------------------------# ---
774 # 12:58:36 ** jenkins582353203049151829.sh :: do_login: Detected ENV{GITHUB_TOKEN}=
775 # 12:58:36 The value of the GITHUB_TOKEN environment variable is being used for authentication.
776 # 12:58:36 To have GitHub CLI store credentials instead, first clear the value from the environment.
777 # -----------------------------------------------------------------------
Joey Armstrong76026b72023-03-29 12:19:17 -0400778
Joey Armstrong485081f2023-03-27 13:34:08 -0400779 get_gh_hostname login_args
Joey Armstrongf085d872023-01-28 17:52:29 -0500780
Joey Armstrong485081f2023-03-27 13:34:08 -0400781 banner "${login_args[@]}"
Joey Armstrong78cecc52023-04-03 11:39:11 -0400782 func_echo "$(declare -p WORKSPACE)"
Joey Armstrongf085d872023-01-28 17:52:29 -0500783
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400784 ## Read from disk is safer than export GITHUB_TOKEN=
785 if [[ -v pac ]] && [[ ${#pac} -gt 0 ]]; then # interactive/debugging
Joey Armstrong43c48422023-04-03 10:17:32 -0400786 [ ! -f "$pac" ] && error "PAC token file $pac does not exist"
Joey Armstronge8560da2023-04-26 15:44:45 -0400787 func_echo "$gh_cmd auth login ${login_args[*]} --with-token < $pac"
Joey Armstrong485081f2023-03-27 13:34:08 -0400788 "$gh_cmd" auth login "${login_args[@]}" --with-token < "$pac"
Joey Armstrongf085d872023-01-28 17:52:29 -0500789
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400790 elif [[ ! -v GITHUB_TOKEN ]]; then
791 error "--token [t] or GITHUB_TOKEN= are required"
Joey Armstrong41923cc2023-01-30 14:38:16 -0500792
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400793 else # jenkins
Joey Armstronge8560da2023-04-26 15:44:45 -0400794 func_echo "$gh_cmd auth login ${login_args[*]} (ie: jenkins)"
Joey Armstrong733ea9f2023-04-03 21:19:46 -0400795
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400796 # https://github.com/cli/cli/issues/2922#issuecomment-775027762
797 # When using GITHUB_TOKEN, there is no need to even run gh auth login
Joey Armstrong733ea9f2023-04-03 21:19:46 -0400798 # "$gh_cmd" auth login "${login_args[@]}"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400799 fi
Joey Armstrongf085d872023-01-28 17:52:29 -0500800
Joey Armstrong43c48422023-04-03 10:17:32 -0400801 declare -i -g active_login=1 # signal logout needed
Joey Armstrongf085d872023-01-28 17:52:29 -0500802
Joey Armstrongf085d872023-01-28 17:52:29 -0500803 return
804}
805
806## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400807## Intent: Destroy credentials/gh session authenticated by do_login
808## -----------------------------------------------------------------------
809## NOTE: my_gh currently unused due to "<<< 'y'"
810## -----------------------------------------------------------------------
811function do_logout()
812{
813 declare -i -g active_login
814 [[ ! -v active_login ]] && return
815
Joey Armstrong78cecc52023-04-03 11:39:11 -0400816 declare -a logout_args=()
817 [[ $# -gt 0 ]] && logout_args+=("$@")
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400818
Joey Armstrong78cecc52023-04-03 11:39:11 -0400819 get_gh_hostname logout_args
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400820
Joey Armstrong78cecc52023-04-03 11:39:11 -0400821 banner "${logout_args[@]}"
Joey Armstronge8560da2023-04-26 15:44:45 -0400822 func_echo "$gh_cmd auth logout ${logout_args[*]} <<< 'Y'"
Joey Armstrong78cecc52023-04-03 11:39:11 -0400823 "$gh_cmd" auth logout "${logout_args[@]}" <<< 'Y'
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400824 unset active_login
825 return
826}
827
828## -----------------------------------------------------------------------
829## Intent: Query for repository version strings
830## -----------------------------------------------------------------------
831function get_releases()
832{
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400833 # shellcheck disable=SC2178
Joey Armstrong1cc90292024-04-02 09:27:33 -0400834 local -n refA=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400835
836 banner ""
837 pushd "$scratch" >/dev/null
Joey Armstrong76026b72023-03-29 12:19:17 -0400838
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400839 # gh api repos/{owner}/{repo}/releases
840 local releases_uri
841 get_gh_releases releases_uri
Joey Armstrong4d612a92024-04-24 15:30:49 -0400842
843 func_echo "RUNNING: $gh_cmd api $releases_uri | jq . > release.raw"
Joey Armstrong76026b72023-03-29 12:19:17 -0400844
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400845 refA=()
Joey Armstronge8560da2023-04-26 15:44:45 -0400846 "$gh_cmd" api "$releases_uri" | jq . > 'release.raw'
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400847 readarray -t __tmp < <(jq '.[] | "\(.tag_name)"' 'release.raw')
848
849 local release
850 for release in "${__tmp[@]}";
851 do
Joey Armstrong43c48422023-04-03 10:17:32 -0400852 release="${release//\"/}"
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400853 refA+=("$release")
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400854 done
855
856 popd >/dev/null
857 return
858}
859
860## -----------------------------------------------------------------------
861## Intent: Display repository query strings.
862## Indirect: verify authentication and API
863## -----------------------------------------------------------------------
864function showReleases()
865{
Joey Armstrong43c48422023-04-03 10:17:32 -0400866 declare -a raw=()
867 get_releases raw
868
869 ## Sort for display, we may need to prune volume later on
Joey Armstronge8560da2023-04-26 15:44:45 -0400870 readarray -t releases < <(sort -nr <<<"${raw[*]}")
871 # IFS=$'\n' releases=($(sort -nr <<<"${raw[*]}"))
872 # unset IFS
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400873
874 local release
875 for release in "${releases[@]}";
876 do
Joey Armstrong43c48422023-04-03 10:17:32 -0400877 func_echo "$release"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400878 done
879 return
880}
881
882## -----------------------------------------------------------------------
883## Intent: Install the gh command line tool locally
Joey Armstrong26707f02023-01-26 12:41:12 -0500884## -----------------------------------------------------------------------
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500885function install_gh_binary()
Joey Armstrong26707f02023-01-26 12:41:12 -0500886{
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400887 banner
Joey Armstrong26707f02023-01-26 12:41:12 -0500888
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500889 pushd "$scratch"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400890 func_echo "Retrieve latest gh download URLs"
Joey Armstrongf085d872023-01-28 17:52:29 -0500891
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500892 local latest="https://github.com/cli/cli/releases/latest"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400893 local tarball="gh.tar.tgz"
894
Joey Armstrong76026b72023-03-29 12:19:17 -0400895 readarray -t latest < <(\
Joey Armstrong4d612a92024-04-24 15:30:49 -0400896 curl --silent -qI "$latest" \
897 | awk -F '/' '/^location/ {print substr($NF, 1, length($NF)-1)}')
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400898 declare -p latest
899 if [ ${#latest[@]} -ne 1 ]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400900 error "Unable to determine latest gh package version"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400901 fi
Joey Armstrong76026b72023-03-29 12:19:17 -0400902
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400903 local VER="${latest[0]}"
904
905 func_echo "Download latest gh binary"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500906 local url="https://github.com/cli/cli/releases/download/${VER}/gh_${VER#v}_linux_amd64.tar.gz"
Joey Armstrong43c48422023-04-03 10:17:32 -0400907 func_echo "wget --quiet --output-document='$tarball' '$url'"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500908 wget --quiet --output-document="$tarball" "$url"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400909
910 func_echo "Unpack tarball"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500911 tar zxf "$tarball"
912
Joey Armstrong78cecc52023-04-03 11:39:11 -0400913 declare -g gh_cmd
914 gh_cmd="$(find "${scratch}" -name 'gh' -print)"
915 #declare -g gh_cmd='/usr/bin/gh'
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500916 readonly gh_cmd
917
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400918 func_echo "Command: ${gh_cmd}"
919 func_echo "Version: $("$gh_cmd" --version)"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500920 popd
921
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400922 return
923}
924
925## -----------------------------------------------------------------------
926## Intent: Danger Will Robinson
927## -----------------------------------------------------------------------
928function releaseDelete()
929{
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400930 # shellcheck disable=SC2178
931 local -n refA=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400932 local version="$1"; shift
Joey Armstrong76026b72023-03-29 12:19:17 -0400933
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400934 banner "${refA[@]}"
935 # declare -a refA=()
936 refA+=('--host-repo')
937 refA+=('--yes')
938 # refA+=('--cleanup-tag')
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400939
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400940 echo
941 echo "==========================================================================="
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400942 my_gh 'release' 'delete' "$version" "${refA[@]}"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400943 echo "==========================================================================="
944 echo
945
946 showReleases
947 return
948}
949
950## -----------------------------------------------------------------------
951## Intent: Copy binaries into temp/release, checksum then publish
952## -----------------------------------------------------------------------
953function release_staging()
954{
955 local release_temp
956 get_release_dir release_temp
957
958 banner ''
959 func_echo "Packaging release files"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400960
Joey Armstrong76026b72023-03-29 12:19:17 -0400961 pushd "$release_temp" >/dev/null \
Joey Armstrong4d612a92024-04-24 15:30:49 -0400962 || { error "pushd failed: dir is [$release_temp]"; }
Joey Armstrong76026b72023-03-29 12:19:17 -0400963
964 declare -a to_release=()
965 get_artifacts '.' to_release
966
Joey Armstrong55fe80e2023-04-05 18:32:14 -0400967 if false; then
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400968 for fyl in "${to_release[@]}";
969 do
970 func_echo "sha256sum $fyl > ${fyl}.sha256"
971 sha256sum "$fyl" > "${fyl}.sha256"
972 done
Joey Armstrong55fe80e2023-04-05 18:32:14 -0400973 fi
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400974
975 # Generate and check checksums
Joey Armstrong55fe80e2023-04-05 18:32:14 -0400976 sha256sum -- * | grep -iv -e 'checksum' -e 'sha256' > checksum.SHA256
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400977 sha256sum -c < checksum.SHA256
978
979 echo
980 func_echo "Checksums(checksum.SHA256):"
981 cat checksum.SHA256
982
Joey Armstrong43c48422023-04-03 10:17:32 -0400983 if false; then
Joey Armstrong78cecc52023-04-03 11:39:11 -0400984 # Careful with credentials display
Joey Armstrong43c48422023-04-03 10:17:32 -0400985 get_gh_hostname login_args
Joey Armstronge8560da2023-04-26 15:44:45 -0400986 banner "gh auth status ${login_args[*]}"
Joey Armstrong43c48422023-04-03 10:17:32 -0400987 gh auth status "${login_args[@]}"
988 fi
989
Joey Armstrong76026b72023-03-29 12:19:17 -0400990 gh_release_create # publish
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400991
Joey Armstrong4d612a92024-04-24 15:30:49 -0400992 popd >/dev/null || { error "pushd failed: dir is [$release_temp]"; }
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400993
994 return
995}
996
997## -----------------------------------------------------------------------
Joey Armstrong43c48422023-04-03 10:17:32 -0400998## Intent: Normalize common arguments and access to the gh command.
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400999## o Cache path to the gh command
1000## o Construct a gh command line from given args
1001## o Command wrapper can provide defaults (--hostname github.com)
1002## -----------------------------------------------------------------------
1003## Given:
Joey Armstrongdb43bde2024-04-01 14:54:35 -04001004## scalar Array variable name (local -n is a reference)
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001005## Return:
1006## ref gh command line passed back to caller
1007## Switches:
1008## --host Pass default github hostname
Joey Armstrong01e4edb2023-08-18 12:50:32 -04001009## --verbose Enable verbose mode
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001010## --version Display command version
1011## @array Remaining arguments passed as command switches.
1012## -----------------------------------------------------------------------
1013## See Also:
1014## o https://cli.github.com/manual
1015## -----------------------------------------------------------------------
1016function my_gh()
1017{
Joey Armstrong4d612a92024-04-24 15:30:49 -04001018 bannerEL 'ENTER'
Joey Armstrongdaa1f0a2024-04-03 18:07:59 -04001019
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001020 declare -a cmd=()
1021 cmd+=("$gh_cmd")
1022
1023 ## ----------------------
1024 ## Construct command line
1025 ## ----------------------
1026 # shellcheck disable=SC2034
1027 declare -A action=() # pending operations
1028 declare -a args=() # common gh command line args
1029
1030 while [ $# -gt 0 ]; do
1031 local arg="$1"; shift
Joey Armstrong4d612a92024-04-24 15:30:49 -04001032 func_echo "function arg is [$arg]"
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001033 case "$arg" in
1034
Joey Armstrong43c48422023-04-03 10:17:32 -04001035 # Modes
Joey Armstrongdaa1f0a2024-04-03 18:07:59 -04001036 -*debug)
Joey Armstrong4d612a92024-04-24 15:30:49 -04001037 # shellcheck disable=SC2034
1038 declare -i -g debug=1
1039 ;;
Joey Armstronge8560da2023-04-26 15:44:45 -04001040 -*verbose) args+=('--verbose') ;;
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001041
Joey Armstrong43c48422023-04-03 10:17:32 -04001042 -*hostname)
1043 get_gh_hostname in_args
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001044 ;;
1045
Joey Armstrong43c48422023-04-03 10:17:32 -04001046 --host-repo)
1047 local val
1048 get_argv_repo val
Joey Armstrong76026b72023-03-29 12:19:17 -04001049
Joey Armstrong43c48422023-04-03 10:17:32 -04001050 # --repo <[HOST/]OWNER/REPO>
Joey Armstrong78cecc52023-04-03 11:39:11 -04001051 args+=('--repo' "${__githost}/${val}")
Joey Armstrong43c48422023-04-03 10:17:32 -04001052 ;;
Joey Armstrong76026b72023-03-29 12:19:17 -04001053
Joey Armstrong4d612a92024-04-24 15:30:49 -04001054 # args+=('--repo' 'github.com/opencord/bbsim')
Joey Armstrongf9a0a882023-04-17 11:53:57 -04001055
Joey Armstrong43c48422023-04-03 10:17:32 -04001056 --repo)
1057 local val
1058 get_argv_repo val
1059 args+=('--repo' "$val")
1060 ;;
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001061
Joey Armstrong43c48422023-04-03 10:17:32 -04001062 --tag)
1063 local val
Joey Armstrong4d612a92024-04-24 15:30:49 -04001064 get_argv_tag val
Joey Armstrong4dbe7002023-04-04 12:47:38 -04001065 args+=("$val") # No switch, pass inline
Joey Armstrong43c48422023-04-03 10:17:32 -04001066 ;;
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001067
Joey Armstrong43c48422023-04-03 10:17:32 -04001068 --title)
1069 local val
1070 get_argv_name val
Joey Armstrongf9a0a882023-04-17 11:53:57 -04001071 args+=('--title' "'$val'")
Joey Armstrong43c48422023-04-03 10:17:32 -04001072 ;;
Joey Armstrong76026b72023-03-29 12:19:17 -04001073
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001074 *) args+=("$arg") ;;
1075 esac
1076 done
Joey Armstrong76026b72023-03-29 12:19:17 -04001077
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001078 cmd+=("${args[@]}")
Joey Armstrong73bb2f62023-04-17 19:31:50 -04001079
1080 echo
1081 declare -p cmd
1082
1083 echo
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001084 echo "** Running: ${cmd[*]}"
1085 "${cmd[@]}"
1086 local status=$?
1087
Joey Armstrong4d612a92024-04-24 15:30:49 -04001088 bannerEL 'LEAVE'
Joey Armstrong73bb2f62023-04-17 19:31:50 -04001089
Joey Armstronge8560da2023-04-26 15:44:45 -04001090 if [[ $status -eq 0 ]]; then
Joey Armstrong4d612a92024-04-24 15:30:49 -04001091 true
Joey Armstronge8560da2023-04-26 15:44:45 -04001092 else
Joey Armstrong4d612a92024-04-24 15:30:49 -04001093 false
Joey Armstronge8560da2023-04-26 15:44:45 -04001094 fi
1095
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001096 return
1097}
1098
Joey Armstrongaf577ab2022-12-15 14:43:33 -05001099##----------------##
1100##---] MAIN [---##
1101##----------------##
Joey Armstrong4d612a92024-04-24 15:30:49 -04001102bannerEL 'ENTER'
Joey Armstrong1962bcf2023-01-27 13:53:18 -05001103iam="${0##*/}"
1104
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001105full_banner
Joey Armstronge8560da2023-04-26 15:44:45 -04001106parse_args "$@"
Joey Armstrongd99e3d22023-01-29 12:35:43 -05001107init
1108install_gh_binary
Joey Armstrong7f382ef2023-01-25 12:00:08 -05001109
Joey Armstrong4d612a92024-04-24 15:30:49 -04001110[[ -v argv_self_check ]] && { do_api_validation; exit 0; }
1111
Joey Armstrong01e4edb2023-08-18 12:50:32 -04001112do_login "$*"
Zack Williams27cd3e52018-09-18 16:44:50 -07001113
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001114release_path='/dev/null'
1115get_release_path release_path
1116declare -p release_path
Zack Williams27cd3e52018-09-18 16:44:50 -07001117
Joey Armstrong4d612a92024-04-24 15:30:49 -04001118pushd "$release_path" || { error "pushd failed: dir is [$release_path]"; }
Zack Williams27cd3e52018-09-18 16:44:50 -07001119
Joey Armstrong4d612a92024-04-24 15:30:49 -04001120# legacy: getGitVersion "$GERRIT_PROJECT" GIT_VERSION
1121getGitVersion GIT_VERSION
1122getReleaseDescription RELEASE_DESCRIPTION
1123if [[ ! -v release_notes ]]; then
1124 func_echo "Generating release notes from RELEASE_DESCRIPTION"
1125 declare -g release_notes="$scratch/release.notes"
1126 echo "$RELEASE_DESCRIPTION" > "$release_notes"
1127fi
1128cat "$release_notes"
Zack Williams27cd3e52018-09-18 16:44:50 -07001129
Joey Armstrong4d612a92024-04-24 15:30:49 -04001130cat <<EOM
Joey Armstrongeef8c2c2023-03-27 17:27:43 -04001131
1132** -----------------------------------------------------------------------
1133** GIT VERSION: $(declare -p GIT_VERSION)
1134** RELEASE_DESCRIPTION: $(declare -p RELEASE_DESCRIPTION)
1135** RELEASE_TARGETS: $(declare -p RELEASE_TARGETS)
Joey Armstrong43c48422023-04-03 10:17:32 -04001136** -----------------------------------------------------------------------
Joey Armstrong4d612a92024-04-24 15:30:49 -04001137** URL: https://github.com/opencord/{repo}/releases
Joey Armstronge8560da2023-04-26 15:44:45 -04001138** -----------------------------------------------------------------------
Joey Armstrong43c48422023-04-03 10:17:32 -04001139** Running: make ${RELEASE_TARGETS}
Joey Armstrongeef8c2c2023-03-27 17:27:43 -04001140** -----------------------------------------------------------------------
1141EOM
Joey Armstrong50f6e0b2023-01-24 14:14:08 -05001142
Joey Armstrong4d612a92024-04-24 15:30:49 -04001143# build the release, can be multiple space separated targets
1144# -----------------------------------------------------------------------
1145# % go build command-line-arguments:
1146# copying /tmp/go-build4212845548/b001/exe/a.out:
1147# open release/voltctl-1.8.25-linux-amd64: permission denied
1148# missing: docker run mkdir
1149# -----------------------------------------------------------------------
1150# shellcheck disable=SC2086
1151make "$RELEASE_TARGETS"
1152copyToRelease
1153
1154cat <<EOM
Joey Armstrong1962bcf2023-01-27 13:53:18 -05001155
1156** -----------------------------------------------------------------------
1157** Create the release:
1158** 1) Create initial github release with download area.
1159** 2) Generate checksum.SHA256 for all released files.
1160** 3) Upload files to complete the release.
Joey Armstrong75a0d932023-03-28 08:59:54 -04001161** 4) Display released info from github
Joey Armstrong1962bcf2023-01-27 13:53:18 -05001162** -----------------------------------------------------------------------
Joey Armstrong26707f02023-01-26 12:41:12 -05001163EOM
1164
Joey Armstrong4d612a92024-04-24 15:30:49 -04001165showReleases
1166release_staging
Joey Armstronge8560da2023-04-26 15:44:45 -04001167
Joey Armstrong4d612a92024-04-24 15:30:49 -04001168# Useful to display but --draft images use a non-standard subdir identifier.
1169# showReleaseUrl
Joey Armstronge8560da2023-04-26 15:44:45 -04001170
Joey Armstrong4d612a92024-04-24 15:30:49 -04001171popd || { error "pushd failed: dir is [$release_path]"; }
Joey Armstrong28eddda2023-01-10 03:09:34 -05001172
Joey Armstrong4d612a92024-04-24 15:30:49 -04001173# shellcheck disable=SC2119
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001174do_logout
1175
Joey Armstrong28eddda2023-01-10 03:09:34 -05001176# [SEE ALSO]
1177# -----------------------------------------------------------------------
1178# https://www.shellcheck.net/wiki/SC2236
Joey Armstrong26707f02023-01-26 12:41:12 -05001179# https://docs.github.com/en/rest/releases/releases?apiVersion=2022-11-28#create-a-release
Joey Armstrong28eddda2023-01-10 03:09:34 -05001180# -----------------------------------------------------------------------
Joey Armstrongf085d872023-01-28 17:52:29 -05001181# https://cli.github.com/manual/gh_help_reference
1182# https://cli.github.com/manual/gh_release
1183# -----------------------------------------------------------------------
Joey Armstrong28eddda2023-01-10 03:09:34 -05001184
1185# [EOF]