blob: 8cbd0443527c951021dcb8f0ca1b2e77920a25d0 [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##--------------------##
Eric Ball863379b2024-10-10 10:34:55 -070045# If running locally, we can include help and parse-args. However, when run as
46# part of a CI build, these files are not included nor necessary.
47if [[ -d $libdir ]]; then
48 # source "$libdir/help.sh" "usage" function is never called
49 source "$libdir/parse-args.sh"
50fi
Joey Armstrongd99e3d22023-01-29 12:35:43 -050051
52declare -g scratch # temp workspace for downloads
Joey Armstrongd99e3d22023-01-29 12:35:43 -050053
Joey Armstrong4d612a92024-04-24 15:30:49 -040054declare -g SCRIPT_VERSION='1.5' # git changeset needed
Joey Armstrong26707f02023-01-26 12:41:12 -050055
Joey Armstrong7f382ef2023-01-25 12:00:08 -050056##--------------------##
57##---] INCLUDES [---##
Joey Armstrong1962bcf2023-01-27 13:53:18 -050058##--------------------#
Joey Armstrong2097d3e2023-03-26 10:32:03 -040059
60## -----------------------------------------------------------------------
61## Intent: Register an interrupt handler to display a stack trace on error
62## -----------------------------------------------------------------------
Joey Armstrong4d612a92024-04-24 15:30:49 -040063function bannerEL()
64{
65 # echo " $i: ${BASH_SOURCE[$i+1]}:${BASH_LINENO[$i]} ${FUNCNAME[$i]}(...)"
66
67 # iam="${0##*/}"
68 if [[ -v debug ]]; then
69 if [[ $# -gt 0 ]]; then
70 local type="$1"; shift
71 else
72 local type='LEAVE'
73 fi
74
75 local func="${FUNCNAME[1]}"
76 local line="${BASH_LINENO[1]}"
77 case "$type" in
78 LEAVE) ;;
79 *) type='ENTER' ;;
80 esac
81
82 printf "** %s %s (LINENO:%s)\n" "$func" "$type" "$line"
83 fi
84
85 return
86}
87
88## -----------------------------------------------------------------------
89## Intent: Register an interrupt handler to display a stack trace on error
90## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -040091function errexit()
92{
93 local err=$?
94 set +o xtrace
95 local code="${1:-1}"
96
97 local prefix="${BASH_SOURCE[1]}:${BASH_LINENO[0]}"
98 echo -e "\nOFFENDER: ${prefix}"
99 if [ $# -gt 0 ] && [ "$1" == '--stacktrace-quiet' ]; then
100 code=1
101 else
102 echo "ERROR: '${BASH_COMMAND}' exited with status $err"
103 fi
104
105 # Print out the stack trace described by $function_stack
106 if [ ${#FUNCNAME[@]} -gt 2 ]
107 then
Joey Armstrong4d612a92024-04-24 15:30:49 -0400108 echo "Call tree:"
109 for ((i=1;i<${#FUNCNAME[@]}-1;i++))
110 do
111 echo " $i: ${BASH_SOURCE[$i+1]}:${BASH_LINENO[$i]} ${FUNCNAME[$i]}(...)"
112 done
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400113 fi
114
115 echo "Exiting with status ${code}"
116 echo
117 exit "${code}"
118 # return
119}
120
121# trap ERR to provide an error handler whenever a command exits nonzero
122# this is a more verbose version of set -o errexit
123trap errexit ERR
124
125# setting errtrace allows our ERR trap handler to be propagated to functions,
126# expansions and subshells
127set -o errtrace
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500128
129## -----------------------------------------------------------------------
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500130## Intent: Cleanup scratch area on exit
131## -----------------------------------------------------------------------
132function sigtrap()
133{
134 ## Prevent mishaps
135 local is_read_only
136 is_read_only="$(declare -p scratch)"
137 if [[ $is_read_only != *"declare -r scratch="* ]]; then
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400138 echo "ERROR: variable scratch is not read-only, cleanup skipped"
139 exit 1
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500140 fi
141
142 if [ -d "$scratch" ]; then
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400143 /bin/rm -fr "$scratch"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500144 fi
145
Joey Armstrong4d612a92024-04-24 15:30:49 -0400146 # shellcheck disable=SC2119
147 do_logout
Eric Ball863379b2024-10-10 10:34:55 -0700148
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500149 return
150}
151trap sigtrap EXIT
152
153## -----------------------------------------------------------------------
Joey Armstronge8560da2023-04-26 15:44:45 -0400154## Intent: Return a random release version string.
155## -----------------------------------------------------------------------
156## Note: Do not use this function in production. get_version() is
157## intended for local use or debugging $0 from within a jenkins
158## job.
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400159## -----------------------------------------------------------------------
160function get_version()
161{
Joey Armstrong1cc90292024-04-02 09:27:33 -0400162 local -n ref=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400163
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400164 declare -a rev=()
165 rev+=("$(( RANDOM % 10 + 1 ))")
166 rev+=("$(( RANDOM % 256 + 1 ))")
Joey Armstrongeef8c2c2023-03-27 17:27:43 -0400167 rev+=("$(( RANDOM % 10000 + 1 ))")
Joey Armstrongfc20ed52023-04-03 19:37:58 -0400168 local ver="v${rev[0]}.${rev[1]}.${rev[2]}"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400169
Joey Armstrong4d612a92024-04-24 15:30:49 -0400170 func_echo "GENERATED VERSION STRING: $ver"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400171 ref="$ver"
172 return
173}
174
175## -----------------------------------------------------------------------
176## Intent: Provide defaults for environment variables
177## -----------------------------------------------------------------------
178function initEnvVars()
179{
180 # when not running under Jenkins, use current dir as workspace and a blank
181 # project name
182 declare -g WORKSPACE=${WORKSPACE:-.}
183 declare -g GERRIT_PROJECT=${GERRIT_PROJECT:-}
Joey Armstrong76026b72023-03-29 12:19:17 -0400184
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400185 # Github organization (or user) this project is published on. Project name should
186 # be the same on both Gerrit and GitHub
Joey Armstrong76026b72023-03-29 12:19:17 -0400187 declare -g GITHUB_ORGANIZATION=${GITHUB_ORGANIZATION:-}
188
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400189 # glob pattern relative to project dir matching release artifacts
190 # ARTIFACT_GLOB=${ARTIFACT_GLOB:-"release/*"} # stat -- release/* not found, literal string (?)
191 declare -g ARTIFACT_GLOB=${ARTIFACT_GLOB:-"release/."}
Joey Armstrong76026b72023-03-29 12:19:17 -0400192
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400193 # Use "release" as the default makefile target, can be a space separated list
194 declare -g RELEASE_TARGETS=${RELEASE_TARGETS:-release}
Joey Armstrong76026b72023-03-29 12:19:17 -0400195
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400196 # Set and handle GOPATH and PATH
197 export GOPATH=${GOPATH:-$WORKSPACE/go}
198 export PATH=$PATH:/usr/lib/go-1.12/bin:/usr/local/go/bin:$GOPATH/bin
199 return
200}
201
202## -----------------------------------------------------------------------
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500203## Intent: Create a scratch area for downloads and transient tools
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400204## temp directory will be automatically removed upon exit.
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500205## -----------------------------------------------------------------------
206function init()
207{
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500208 local pkgbase="${0##*/}" # basename
209 local pkgname="${pkgbase%.*}"
210
Joey Armstrong4d612a92024-04-24 15:30:49 -0400211 bannerEL 'ENTER'
212
Joey Armstrongad7b1e02023-03-27 11:55:48 -0400213 # initEnvVars # moved to full_banner()
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400214
215 ## Create a temp directory for auto-cleanup
216 declare -g scratch
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500217 scratch="$(mktemp -d -t "${pkgname}.XXXXXXXXXX")"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400218 readonly scratch
219 declare -p scratch
220
Joey Armstrong43c48422023-04-03 10:17:32 -0400221 ## prime the stream: cache answers
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400222 local work
223 get_release_dir work
224 declare -p work
Joey Armstrong43c48422023-04-03 10:17:32 -0400225
Joey Armstronge8560da2023-04-26 15:44:45 -0400226 local git_version
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400227 getGitVersion git_version
228 func_echo "$(declare -p git_version)"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400229 return
230}
231
232## -----------------------------------------------------------------------
233## Intent: Verbose output for logging
234## -----------------------------------------------------------------------
235function banner()
236{
237 local iam="${0##*/}"
238 cat <<EOB
239
240** -----------------------------------------------------------------------
241** ${iam}::${FUNCNAME[1]}: $*
242** -----------------------------------------------------------------------
243EOB
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500244}
245
246## -----------------------------------------------------------------------
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500247## Intent: Output a log banner to identify the running script/version.
248## -----------------------------------------------------------------------
249## TODO:
250## o SCRIPT_VERSION => git changeset for repo:ci-managment
251## o echo "library version: ${env."library.libName.version"}"
252# -----------------------------------------------------------------------
253# 14:18:38 > git fetch --no-tags --progress -- https://gerrit.opencord.org/ci-management.git +refs/heads/*:refs/remotes/origin/* # timeout=10
254# 14:18:39 Checking out Revision 50f6e0b97f449b32d32ec0e02d59642000351847 (master)
255# -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400256function full_banner()
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500257{
Joey Armstrongad7b1e02023-03-27 11:55:48 -0400258 local iam="${0##*/}"
259
260 initEnvVars # set defaults
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500261
Joey Armstrong4d612a92024-04-24 15:30:49 -0400262 cat <<EOH
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500263
264** -----------------------------------------------------------------------
265** IAM: ${iam} :: ${FUNCNAME[0]}
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400266** ARGV: ${ARGV[@]}
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500267** PWD: $(/bin/pwd)
268** NOW: $(date '+%Y/%m/%d %H:%M:%S')
269** VER: ${SCRIPT_VERSION:-'unknown'}
270** -----------------------------------------------------------------------
Joey Armstrongad7b1e02023-03-27 11:55:48 -0400271** GERRIT_PROJECT: $(declare -p GERRIT_PROJECT)
272** GITHUB_ORGANIZATION: $(declare -p GITHUB_ORGANIZATION)
273** RELEASE_TARGETS: $(declare -p RELEASE_TARGETS)
274** GOPATH: $(declare -p GOPATH)
275** -----------------------------------------------------------------------
276** PATH += /usr/lib/go-1.12/bin:/usr/local/go/bin:GOPATH/bin
277** -----------------------------------------------------------------------
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500278EOH
279
280 return
281}
282
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500283## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400284## Intent: Display a message with 'iam' identifier for logging
Joey Armstrong50f6e0b2023-01-24 14:14:08 -0500285## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400286function func_echo()
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500287{
288 local iam="${0##*/}"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400289 echo "** ${iam} :: ${FUNCNAME[1]}: $*"
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500290 return
291}
292
293## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400294## Intent: Display an error message then exit with status.
295## -----------------------------------------------------------------------
296function error()
297{
298 local iam="${0##*/}"
Joey Armstrong4d612a92024-04-24 15:30:49 -0400299 local func="${FUNCNAME[1]}"
300 local line="${BASH_LINENO[1]}"
301 printf "\nERROR %s :: %s (LINENO:%s): %s" "$iam" "$func" "$line" "$*"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400302 exit 1
303}
304
305## -----------------------------------------------------------------------
Joey Armstrong4d612a92024-04-24 15:30:49 -0400306## Intent: Script self checking, direct API access for troubleshooting
307## -----------------------------------------------------------------------
308function do_api_validation()
309{
310 # if --verify credentials
311 do_login
312 # shellcheck disable=SC2119
313 do_logout
314 return
315}
316
317## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400318## Intent: Verify sandbox/build is versioned for release.
319## -----------------------------------------------------------------------
320function getGitVersion()
321{
Joey Armstrong1cc90292024-04-02 09:27:33 -0400322 local -n varname=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400323
Joey Armstrong4d612a92024-04-24 15:30:49 -0400324 bannerEL 'ENTER'
325
Joey Armstrong43c48422023-04-03 10:17:32 -0400326 local __ver # use prefix('__') to protect callers variable name
327 if [[ -v cached_getGitVersion ]]; then
328 __ver="$cached_getGitVersion"
329 varname="$__ver"
330 return
Joey Armstrong39fac652023-03-27 18:50:43 -0400331
Joey Armstrong43c48422023-04-03 10:17:32 -0400332 elif [[ -v argv_gen_version ]]; then
333 get_version __ver
334
335 elif [[ -v WORKSPACE ]] && [[ -v GITHUB_TOKEN ]]; then # i_am_jenkins
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400336 local path="$GERRIT_PROJECT"
Joey Armstrong4d612a92024-04-24 15:30:49 -0400337 pushd "$path" || { error "pushd GERRIT_PROJECT= failed $(declare -p path)"; }
Joey Armstrong43c48422023-04-03 10:17:32 -0400338 __ver="$(git tag -l --points-at HEAD)"
Joey Armstrong4d612a92024-04-24 15:30:49 -0400339 popd || { error "popd GERRIT_PROJECT= failed"; }
Joey Armstrong43c48422023-04-03 10:17:32 -0400340
341 elif [[ -v argv_version_file ]]; then # local debug
342 [[ ! -f VERSION ]] && error "./VERSION file not found"
343 readarray -t tmp < <(sed -e 's/[[:blank:]]//g' 'VERSION')
344 __ver="v${tmp[0]}"
345
Joey Armstrong4d612a92024-04-24 15:30:49 -0400346 elif [[ ${#GERRIT_PROJECT} -gt 0 ]]; then
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400347 cd ..
348 local path="$GERRIT_PROJECT"
Joey Armstrong4d612a92024-04-24 15:30:49 -0400349 declare -p path
350 pushd "$path" || { error "pushd GERRIT_PROJECT= failed $(declare -p path)"; }
Joey Armstrong43c48422023-04-03 10:17:32 -0400351 __ver="$(git tag -l --points-at HEAD)"
Joey Armstrong4d612a92024-04-24 15:30:49 -0400352 popd || { error "popd GERRIT_PROJECT= failed"; }
353
354 else # pwd==sandbox
355
356 local line="${BASH_LINENO[0]}"
357 local message="
358GERRIT_PROJECT= is not defined:
359 o One of the following are required:
360 - Define GERRIT_PROJECT= (~jenkins).
361 - Argument --verison-file.
362"
363 error "$message"
Joey Armstrong43c48422023-04-03 10:17:32 -0400364 fi
Joey Armstrong76026b72023-03-29 12:19:17 -0400365
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400366 # ------------------------------------------------------
367 # match bare versions or v-prefixed golang style version
368 # Critical failure for new/yet-to-be-released repo ?
369 # ------------------------------------------------------
Joey Armstrong43c48422023-04-03 10:17:32 -0400370 if [[ "$__ver" =~ ^v?([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then
Joey Armstrongfc20ed52023-04-03 19:37:58 -0400371 echo "git has a SemVer released version tag: '$__ver'"
Joey Armstrong43c48422023-04-03 10:17:32 -0400372 echo "Building artifacts for GitHub release."
373
374 elif [[ "$__ver" =~ ^v?([0-9]+)\.([0-9]+)\.([0-9]+)-dev([0-9]+)$ ]]; then
Joey Armstrong4d612a92024-04-24 15:30:49 -0400375 # v1.2.3-dev (*-dev*) is an implicit draft release.
Joey Armstrong43c48422023-04-03 10:17:32 -0400376 declare -i -g draft_release=1
377 echo "Detected --draft SemVer release version tag [$__ver]"
378 echo "Building artifacts for GitHub release."
379
Joey Armstrong4d612a92024-04-24 15:30:49 -0400380 # Should also accept: X.Y.Z-{alpha,beta,...}
Joey Armstrong4f87de82023-08-25 12:42:39 -0400381
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400382 else
Joey Armstrong4f87de82023-08-25 12:42:39 -0400383 echo "Version string contains: [${__ver}]"
Joey Armstrong43c48422023-04-03 10:17:32 -0400384 error "No SemVer released version tag found, exiting..."
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400385 fi
386
Joey Armstrong43c48422023-04-03 10:17:32 -0400387 ## Cache value for subsequent calls.
388 ## readonly is a guarantee assignment happens once.
389 declare -g cached_getGitVersion="$__ver"
390 readonly cached_getGitVersion;
391
392 varname="$__ver"
393 func_echo "Version is [$__ver] from $(/bin/pwd)"
Joey Armstrong4d612a92024-04-24 15:30:49 -0400394
395 bannerEL 'LEAVE'
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400396 return
397}
398
399## -----------------------------------------------------------------------
400## Intent:
401## Note: Release description is sanitized version of the log message
402## -----------------------------------------------------------------------
403function getReleaseDescription()
404{
Joey Armstrong1cc90292024-04-02 09:27:33 -0400405 local -n varname=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400406
407 local msg
408 msg="$(git log -1 --pretty=%B)"
409
410 local val
411 val="$(tr -dc "[:alnum:]\n\r\.\[\]\:\-\\\/\`\' " <<< "$msg")"
412
413 [[ ${#val} -eq 0 ]] && error "Release Description is empty ($msg)"
414 varname="$val"
415 return
416}
417
418## -----------------------------------------------------------------------
419## Intent: Retrieve value of the release temp directory.
420## -----------------------------------------------------------------------
421## Note: Limit use of globals so logic can be isolated in function calls.
422## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400423function get_release_dir()
424{
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400425 local -n varname=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400426
427 # Temporary staging directory to copy artifacts to
428 varname="$scratch/release"
429 mkdir -p "$varname"
430 return
431}
432
433## -----------------------------------------------------------------------
434## Intent: Retrieve github server hostname
435## -----------------------------------------------------------------------
436function get_gh_hostname()
437{
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400438 local -n varname=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400439 varname+=('--hostname' "${__githost}")
440 return
441}
442
443## -----------------------------------------------------------------------
444## Intent: Retrieve repository organizaiton name
445## -----------------------------------------------------------------------
446function get_gh_repo_org()
447{
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400448 local -n ref=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400449 declare -g __repo_org
Joey Armstrong76026b72023-03-29 12:19:17 -0400450
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400451 local org
452 if [[ -v __repo_org ]]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400453 org="$__repo_org"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400454 elif [[ ! -v GITHUB_ORGANIZATION ]]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400455 error "--repo-org or GITHUB_ORGANIZATION= are required"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400456 else
Joey Armstrong43c48422023-04-03 10:17:32 -0400457 org="${GITHUB_ORGANIZATION}"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400458 fi
459
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400460 ref="$org"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400461 return
462}
463
464## -----------------------------------------------------------------------
465## Intent: Retrieve repository organizaiton name
466## -----------------------------------------------------------------------
467function get_gh_repo_name()
468{
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400469 local -n ref=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400470 declare -g __repo_name
Joey Armstrong76026b72023-03-29 12:19:17 -0400471
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400472 local name
473 if [[ -v __repo_name ]]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400474 name="$__repo_name"
Joey Armstrong39fac652023-03-27 18:50:43 -0400475 elif [[ ! -v GERRIT_PROJECT ]]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400476 error "--repo-name or GERRIT_PROJECT= are required"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400477 else
Joey Armstrong43c48422023-04-03 10:17:32 -0400478 name="${GERRIT_PROJECT}"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400479 fi
480
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400481 ref="$name"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400482 return
483}
484
485## -----------------------------------------------------------------------
486## Intent: Return path for the gh release query
487## -----------------------------------------------------------------------
488function get_gh_releases()
489{
Joey Armstrong1cc90292024-04-02 09:27:33 -0400490 local -n ref=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400491
492 local repo_org
493 get_gh_repo_org repo_org
494
495 local repo_name
496 get_gh_repo_name repo_name
497
498 ref="repos/${repo_org}/${repo_name}/releases"
Joey Armstrong4d612a92024-04-24 15:30:49 -0400499 func_echo "releases_uri=$ref"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400500 return
501}
502
503## -----------------------------------------------------------------------
504## Intent: Retrieve repository path argument
505## -----------------------------------------------------------------------
506function get_argv_repo()
507{
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400508 local -n ref=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400509
510 local repo_org
511 get_gh_repo_org repo_org
Joey Armstrong76026b72023-03-29 12:19:17 -0400512
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400513 local repo_name
Joey Armstrong76026b72023-03-29 12:19:17 -0400514 get_gh_repo_name repo_name
515
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400516 ref="${repo_org}/${repo_name}"
517 # func_echo "ref=$(declare -p ref)"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400518 return
519}
520
521## -----------------------------------------------------------------------
522## Intent: Retrieve release name string "project - version"
523## -----------------------------------------------------------------------
524function get_argv_name()
525{
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400526 local -n ref=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400527
528 local repo_name
529 get_gh_repo_name repo_name
Joey Armstrong43c48422023-04-03 10:17:32 -0400530
531 local repo_ver
532 getGitVersion repo_ver
533
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400534 # ref="${repo_name} - $GIT_VERSION"
535 ref="${repo_name} - ${repo_ver}"
536 func_echo "ref=$(declare -p ref)"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400537 return
538}
539
540## -----------------------------------------------------------------------
541## Intent: Retrieve tag version
542## -----------------------------------------------------------------------
543function get_argv_tag()
544{
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400545 local -n ref=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400546
547 # cached_argv_tag='v3.41.3204'
548 if [[ ! -v cached_argv_tag ]]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400549 declare -g cached_argv_tag
550 if [[ -v GIT_VERSION ]]; then # hello jenkins
551 cached_argv_tag="$GIT_VERSION"
552 fi
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400553 fi
554
555 [[ ${#cached_argv_tag} -eq 0 ]] && error "Unable to determine GIT_VERSION="
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400556 ref="$cached_argv_tag"
557 func_echo "ref=$(declare -p ref)"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400558 return
559}
560
561## -----------------------------------------------------------------------
562## Intent:
563## -----------------------------------------------------------------------
564# To support golang projects that require GOPATH to be set and code checked out there
565# If $DEST_GOPATH is not an empty string:
566# - create GOPATH within WORKSPACE, and destination directory within
567# - set PATH to include $GOPATH/bin and the system go binaries
568# - move project from $WORKSPACE/$GERRIT_PROJECT to new location in $GOPATH
569# - start release process within that directory
570## -----------------------------------------------------------------------
571function get_release_path()
572{
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400573 local -n ref=$1; shift
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400574 local varpath="$ref"
Joey Armstrongdaa1f0a2024-04-03 18:07:59 -0400575
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400576 DEST_GOPATH=${DEST_GOPATH:-}
577 if [ -n "$DEST_GOPATH" ]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400578 mkdir -p "$GOPATH/src/$DEST_GOPATH"
Joey Armstronge8560da2023-04-26 15:44:45 -0400579 varpath="$GOPATH/src/$DEST_GOPATH/$GERRIT_PROJECT"
580 mv "$WORKSPACE/$GERRIT_PROJECT" "$varpath"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400581 else
Joey Armstronge8560da2023-04-26 15:44:45 -0400582 varpath="$WORKSPACE/$GERRIT_PROJECT"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400583 fi
584
Joey Armstrong43c48422023-04-03 10:17:32 -0400585 ## Verify pwd is OK
586 for path in \
Joey Armstronge8560da2023-04-26 15:44:45 -0400587 "${varpath}/Makefile"\
Joey Armstrong4d612a92024-04-24 15:30:49 -0400588 "${varpath}/makefile"\
589 "__ERROR__"\
590 ; do
Joey Armstrong43c48422023-04-03 10:17:32 -0400591 case "$path" in
Joey Armstronge8560da2023-04-26 15:44:45 -0400592 __ERROR__) error "Makefile not found at ${varpath} !" ;;
Joey Armstrong43c48422023-04-03 10:17:32 -0400593 *) [[ -f "$path" ]] && break ;;
594 esac
595 done
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400596
Mahir Gunyel2c9e8502024-03-29 13:05:38 -0700597 ref="$varpath"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400598}
599
600## -----------------------------------------------------------------------
601## Intent: Display future enhancements
602## -----------------------------------------------------------------------
603function todo()
604{
605 local iam="${0##*/}"
606
Joey Armstrong4d612a92024-04-24 15:30:49 -0400607 cat <<EOT
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400608
609** -----------------------------------------------------------------------
610** IAM: ${iam} :: ${FUNCNAME[0]}
611** -----------------------------------------------------------------------
612 o get_release_path()
613 - refactor redundant paths into local vars.
614 - see comments, do we have a one-off failure condition ?
Joey Armstrongad7b1e02023-03-27 11:55:48 -0400615 o PATH += golang appended 3 times, release needs a single, reliable answer.
616 o do_login, do_logout and api calls do not use the my_gh wrapper:
617 - Add a lookup function to cache and retrieve path to downloaded gh command.
Joey Armstrong76026b72023-03-29 12:19:17 -0400618
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400619EOT
620
621 return
622}
623
624## -----------------------------------------------------------------------
Joey Armstrong76026b72023-03-29 12:19:17 -0400625## Intent: Verify a directory contains content for release.
626## -----------------------------------------------------------------------
627## Given:
628## scalar Path to release/ directory
629## ref Results returned through this indirect var.
630## -----------------------------------------------------------------------
631## Usage:
632## declare -a artifacts=()
633## get_artifacts '/foo/bar/tans' artifacts
634## declare -p artifacts
635## -----------------------------------------------------------------------
636function get_artifacts()
637{
638 local dir="$1" ; shift
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400639 local -n refA=$1 ; shift
Joey Armstrong76026b72023-03-29 12:19:17 -0400640
Joey Armstrong55fe80e2023-04-05 18:32:14 -0400641 # Glob available files, exclude checksums
642 readarray -t __artifacts < <(find "$dir" -mindepth 1 ! -type d \
Joey Armstrong4d612a92024-04-24 15:30:49 -0400643 | grep -iv -e 'sum256' -e 'checksum')
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400644 # func_echo "$(declare -p __artifacts)"
Joey Armstrong76026b72023-03-29 12:19:17 -0400645
646 # -----------------------------------------------------------------------
647 # Verify count>0 to inhibit source-only release
648 # Problem children:
649 # o build or make release failures.
650 # o docker container filesystem mount problem (~volume)
651 # -----------------------------------------------------------------------
652 [[ ${#__artifacts[@]} -eq 0 ]] \
Joey Armstrong4d612a92024-04-24 15:30:49 -0400653 && error "Artifact dir is empty: $(declare -p dir)"
Joey Armstrong76026b72023-03-29 12:19:17 -0400654
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400655 refA=("${__artifacts[@]}")
Joey Armstrong76026b72023-03-29 12:19:17 -0400656 return
657}
658
659## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400660## Intent: Copy files from the build directory into the release staging
661## directory for publishing to github releases/ endpoint.
Joey Armstrong26707f02023-01-26 12:41:12 -0500662## -----------------------------------------------------------------------
663function copyToRelease()
664{
Joey Armstrong76026b72023-03-29 12:19:17 -0400665 banner ''
Joey Armstrong26707f02023-01-26 12:41:12 -0500666
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400667 local artifact_glob="${ARTIFACT_GLOB%/*}"
668 func_echo "$(declare -p artifact_glob)"
669
670 local work
671 get_release_dir work
Joey Armstrong76026b72023-03-29 12:19:17 -0400672 func_echo "Artifact dir: $(declare -p work)"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400673
Joey Armstrong76026b72023-03-29 12:19:17 -0400674 ## Verify release content is available
675 declare -a artifacts=()
Joey Armstrong43c48422023-04-03 10:17:32 -0400676 # get_artifacts "$work" artifacts
677 get_artifacts "$artifact_glob" artifacts
678 func_echo "Artifacts in copy_from: $(declare -p artifacts)"
Joey Armstrong472bfba2023-03-27 18:12:28 -0400679
Joey Armstrong26707f02023-01-26 12:41:12 -0500680 # Copy artifacts into the release temp dir
681 # shellcheck disable=SC2086
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400682 echo "rsync -rv --checksum \"$artifact_glob/.\" \"$work/.\""
683 rsync -rv --checksum "$artifact_glob/." "$work/."
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500684
Joey Armstrong43c48422023-04-03 10:17:32 -0400685 get_artifacts "$work" artifacts
686 func_echo "Artifacts in copy_to: $(declare -p artifacts)"
687
Joey Armstrong26707f02023-01-26 12:41:12 -0500688 return
689}
690
691## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400692## https://docs.github.com/en/rest/releases?apiVersion=2022-11-28
Joey Armstrong4d612a92024-04-24 15:30:49 -0400693# https://cli.github.com/manual/gh_release_create
694# --target <branch> or commit SHA
695# --title
696# --generate-notes
697# --release-notes (notes file)
698# --release
699# release create dist/*.tgz
700# --discussion-category "General"
Joey Armstrongf085d872023-01-28 17:52:29 -0500701## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400702# https://cli.github.com/manual/gh_release_create
703## -----------------------------------------------------------------------
704function gh_release_create()
Joey Armstrong38bfeea2023-02-06 18:01:29 -0500705{
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400706 banner ''
Joey Armstrong38bfeea2023-02-06 18:01:29 -0500707
Joey Armstrong43c48422023-04-03 10:17:32 -0400708 local version
709 getGitVersion version
Joey Armstrong38bfeea2023-02-06 18:01:29 -0500710
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400711 local work
712 get_release_dir work
713
Joey Armstrong43c48422023-04-03 10:17:32 -0400714 declare -a args=()
715 args+=('--host-repo')
716 args+=('--title')
717 if [[ -v draft_release ]]; then
718 args+=('--draft')
719 else
720 args+=('--discussion-category' 'Announcements')
721 fi
722
Joey Armstrong55fe80e2023-04-05 18:32:14 -0400723 if [[ -v release_notes ]]; then
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400724 args+=('--notes-file' "$release_notes")
Joey Armstrong55fe80e2023-04-05 18:32:14 -0400725 fi
Joey Armstrong43c48422023-04-03 10:17:32 -0400726
Joey Armstrong78cecc52023-04-03 11:39:11 -0400727 pushd "$work/.." >/dev/null
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400728 # func_echo "WORK=$work"
Joey Armstrong78cecc52023-04-03 11:39:11 -0400729 readarray -t payload < <(find 'release' -maxdepth 4 ! -type d -print)
Joey Armstrong76026b72023-03-29 12:19:17 -0400730
Joey Armstronge8560da2023-04-26 15:44:45 -0400731 func_echo "$gh_cmd release create ${version} ${args[*]} ${payload[*]}"
Joey Armstrong43c48422023-04-03 10:17:32 -0400732
733 if [[ -v dry_run ]]; then
734 echo "[SKIP] dry run"
735 else
Joey Armstronge8560da2023-04-26 15:44:45 -0400736 func_echo "my_gh release create '$version' ${args[*]} ${payload[*]}"
Joey Armstrong4dbe7002023-04-04 12:47:38 -0400737 my_gh 'release' 'create' "$version" "${args[@]}" "${payload[@]}"
Joey Armstrong43c48422023-04-03 10:17:32 -0400738 fi
Joey Armstrong76026b72023-03-29 12:19:17 -0400739 popd >/dev/null
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400740
Joey Armstrong38bfeea2023-02-06 18:01:29 -0500741 return
742}
743
744## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400745## Intent: Authenticate credentials for a github gh session
Joey Armstrong38bfeea2023-02-06 18:01:29 -0500746## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400747## NOTE: my_gh currently unused due to --with-token < "$pac"
748## -----------------------------------------------------------------------
749function do_login()
Joey Armstrongf085d872023-01-28 17:52:29 -0500750{
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400751 # shellcheck disable=SC2120
752 # shellcheck disable=SC2034
Joey Armstrong4d612a92024-04-24 15:30:49 -0400753 [[ $# -gt 0 ]] && { local unused="$1"; shift; }
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400754
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400755 declare -g pac
Joey Armstrong485081f2023-03-27 13:34:08 -0400756 declare -a login_args=()
757 [[ $# -gt 0 ]] && login_args+=("$@")
Joey Armstrongf085d872023-01-28 17:52:29 -0500758
Joey Armstrong485081f2023-03-27 13:34:08 -0400759 # https://github.com/cli/cli/issues/2922#issuecomment-775027762
Joey Armstrong75a0d932023-03-28 08:59:54 -0400760 # (sigh) why not quietly return VS forcing a special logic path
Joey Armstronge6e18eb2023-03-27 14:19:21 -0400761 # [[ -v WORKSPACE ]] && [[ -v GITHUB_TOKEN ]] && return
762
Joey Armstrong4d612a92024-04-24 15:30:49 -0400763 # 12:58:36 ** -----------------------------------------------------------------------
764 # 12:58:36 ** jenkins582353203049151829.sh::do_login: --hostname github.com
765 # 12:58:36 ** --------------------------------------------------------------------# ---
766 # 12:58:36 ** jenkins582353203049151829.sh :: do_login: Detected ENV{GITHUB_TOKEN}=
767 # 12:58:36 The value of the GITHUB_TOKEN environment variable is being used for authentication.
768 # 12:58:36 To have GitHub CLI store credentials instead, first clear the value from the environment.
769 # -----------------------------------------------------------------------
Joey Armstrong76026b72023-03-29 12:19:17 -0400770
Joey Armstrong485081f2023-03-27 13:34:08 -0400771 get_gh_hostname login_args
Joey Armstrongf085d872023-01-28 17:52:29 -0500772
Joey Armstrong485081f2023-03-27 13:34:08 -0400773 banner "${login_args[@]}"
Joey Armstrong78cecc52023-04-03 11:39:11 -0400774 func_echo "$(declare -p WORKSPACE)"
Joey Armstrongf085d872023-01-28 17:52:29 -0500775
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400776 ## Read from disk is safer than export GITHUB_TOKEN=
777 if [[ -v pac ]] && [[ ${#pac} -gt 0 ]]; then # interactive/debugging
Joey Armstrong43c48422023-04-03 10:17:32 -0400778 [ ! -f "$pac" ] && error "PAC token file $pac does not exist"
Joey Armstronge8560da2023-04-26 15:44:45 -0400779 func_echo "$gh_cmd auth login ${login_args[*]} --with-token < $pac"
Joey Armstrong485081f2023-03-27 13:34:08 -0400780 "$gh_cmd" auth login "${login_args[@]}" --with-token < "$pac"
Joey Armstrongf085d872023-01-28 17:52:29 -0500781
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400782 elif [[ ! -v GITHUB_TOKEN ]]; then
783 error "--token [t] or GITHUB_TOKEN= are required"
Joey Armstrong41923cc2023-01-30 14:38:16 -0500784
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400785 else # jenkins
Joey Armstronge8560da2023-04-26 15:44:45 -0400786 func_echo "$gh_cmd auth login ${login_args[*]} (ie: jenkins)"
Joey Armstrong733ea9f2023-04-03 21:19:46 -0400787
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400788 # https://github.com/cli/cli/issues/2922#issuecomment-775027762
789 # When using GITHUB_TOKEN, there is no need to even run gh auth login
Joey Armstrong733ea9f2023-04-03 21:19:46 -0400790 # "$gh_cmd" auth login "${login_args[@]}"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400791 fi
Joey Armstrongf085d872023-01-28 17:52:29 -0500792
Joey Armstrong43c48422023-04-03 10:17:32 -0400793 declare -i -g active_login=1 # signal logout needed
Joey Armstrongf085d872023-01-28 17:52:29 -0500794
Joey Armstrongf085d872023-01-28 17:52:29 -0500795 return
796}
797
798## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400799## Intent: Destroy credentials/gh session authenticated by do_login
800## -----------------------------------------------------------------------
801## NOTE: my_gh currently unused due to "<<< 'y'"
802## -----------------------------------------------------------------------
803function do_logout()
804{
805 declare -i -g active_login
806 [[ ! -v active_login ]] && return
807
Joey Armstrong78cecc52023-04-03 11:39:11 -0400808 declare -a logout_args=()
809 [[ $# -gt 0 ]] && logout_args+=("$@")
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400810
Joey Armstrong78cecc52023-04-03 11:39:11 -0400811 get_gh_hostname logout_args
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400812
Joey Armstrong78cecc52023-04-03 11:39:11 -0400813 banner "${logout_args[@]}"
Joey Armstronge8560da2023-04-26 15:44:45 -0400814 func_echo "$gh_cmd auth logout ${logout_args[*]} <<< 'Y'"
Joey Armstrong78cecc52023-04-03 11:39:11 -0400815 "$gh_cmd" auth logout "${logout_args[@]}" <<< 'Y'
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400816 unset active_login
817 return
818}
819
820## -----------------------------------------------------------------------
821## Intent: Query for repository version strings
822## -----------------------------------------------------------------------
823function get_releases()
824{
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400825 # shellcheck disable=SC2178
Joey Armstrong1cc90292024-04-02 09:27:33 -0400826 local -n refA=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400827
828 banner ""
829 pushd "$scratch" >/dev/null
Joey Armstrong76026b72023-03-29 12:19:17 -0400830
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400831 # gh api repos/{owner}/{repo}/releases
832 local releases_uri
833 get_gh_releases releases_uri
Joey Armstrong4d612a92024-04-24 15:30:49 -0400834
835 func_echo "RUNNING: $gh_cmd api $releases_uri | jq . > release.raw"
Joey Armstrong76026b72023-03-29 12:19:17 -0400836
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400837 refA=()
Joey Armstronge8560da2023-04-26 15:44:45 -0400838 "$gh_cmd" api "$releases_uri" | jq . > 'release.raw'
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400839 readarray -t __tmp < <(jq '.[] | "\(.tag_name)"' 'release.raw')
840
841 local release
842 for release in "${__tmp[@]}";
843 do
Joey Armstrong43c48422023-04-03 10:17:32 -0400844 release="${release//\"/}"
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400845 refA+=("$release")
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400846 done
847
848 popd >/dev/null
849 return
850}
851
852## -----------------------------------------------------------------------
853## Intent: Display repository query strings.
854## Indirect: verify authentication and API
855## -----------------------------------------------------------------------
856function showReleases()
857{
Joey Armstrong43c48422023-04-03 10:17:32 -0400858 declare -a raw=()
859 get_releases raw
860
861 ## Sort for display, we may need to prune volume later on
Joey Armstronge8560da2023-04-26 15:44:45 -0400862 readarray -t releases < <(sort -nr <<<"${raw[*]}")
863 # IFS=$'\n' releases=($(sort -nr <<<"${raw[*]}"))
864 # unset IFS
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400865
866 local release
867 for release in "${releases[@]}";
868 do
Joey Armstrong43c48422023-04-03 10:17:32 -0400869 func_echo "$release"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400870 done
871 return
872}
873
874## -----------------------------------------------------------------------
875## Intent: Install the gh command line tool locally
Joey Armstrong26707f02023-01-26 12:41:12 -0500876## -----------------------------------------------------------------------
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500877function install_gh_binary()
Joey Armstrong26707f02023-01-26 12:41:12 -0500878{
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400879 banner
Joey Armstrong26707f02023-01-26 12:41:12 -0500880
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500881 pushd "$scratch"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400882 func_echo "Retrieve latest gh download URLs"
Joey Armstrongf085d872023-01-28 17:52:29 -0500883
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500884 local latest="https://github.com/cli/cli/releases/latest"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400885 local tarball="gh.tar.tgz"
886
Joey Armstrong76026b72023-03-29 12:19:17 -0400887 readarray -t latest < <(\
Joey Armstrong4d612a92024-04-24 15:30:49 -0400888 curl --silent -qI "$latest" \
889 | awk -F '/' '/^location/ {print substr($NF, 1, length($NF)-1)}')
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400890 declare -p latest
891 if [ ${#latest[@]} -ne 1 ]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400892 error "Unable to determine latest gh package version"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400893 fi
Joey Armstrong76026b72023-03-29 12:19:17 -0400894
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400895 local VER="${latest[0]}"
896
897 func_echo "Download latest gh binary"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500898 local url="https://github.com/cli/cli/releases/download/${VER}/gh_${VER#v}_linux_amd64.tar.gz"
Joey Armstrong43c48422023-04-03 10:17:32 -0400899 func_echo "wget --quiet --output-document='$tarball' '$url'"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500900 wget --quiet --output-document="$tarball" "$url"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400901
902 func_echo "Unpack tarball"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500903 tar zxf "$tarball"
904
Joey Armstrong78cecc52023-04-03 11:39:11 -0400905 declare -g gh_cmd
906 gh_cmd="$(find "${scratch}" -name 'gh' -print)"
907 #declare -g gh_cmd='/usr/bin/gh'
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500908 readonly gh_cmd
909
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400910 func_echo "Command: ${gh_cmd}"
911 func_echo "Version: $("$gh_cmd" --version)"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500912 popd
913
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400914 return
915}
916
917## -----------------------------------------------------------------------
918## Intent: Danger Will Robinson
919## -----------------------------------------------------------------------
920function releaseDelete()
921{
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400922 # shellcheck disable=SC2178
923 local -n refA=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400924 local version="$1"; shift
Joey Armstrong76026b72023-03-29 12:19:17 -0400925
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400926 banner "${refA[@]}"
927 # declare -a refA=()
928 refA+=('--host-repo')
929 refA+=('--yes')
930 # refA+=('--cleanup-tag')
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400931
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400932 echo
933 echo "==========================================================================="
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400934 my_gh 'release' 'delete' "$version" "${refA[@]}"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400935 echo "==========================================================================="
936 echo
937
938 showReleases
939 return
940}
941
942## -----------------------------------------------------------------------
943## Intent: Copy binaries into temp/release, checksum then publish
944## -----------------------------------------------------------------------
945function release_staging()
946{
947 local release_temp
948 get_release_dir release_temp
949
950 banner ''
951 func_echo "Packaging release files"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400952
Joey Armstrong76026b72023-03-29 12:19:17 -0400953 pushd "$release_temp" >/dev/null \
Joey Armstrong4d612a92024-04-24 15:30:49 -0400954 || { error "pushd failed: dir is [$release_temp]"; }
Joey Armstrong76026b72023-03-29 12:19:17 -0400955
956 declare -a to_release=()
957 get_artifacts '.' to_release
958
Joey Armstrong55fe80e2023-04-05 18:32:14 -0400959 if false; then
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400960 for fyl in "${to_release[@]}";
961 do
962 func_echo "sha256sum $fyl > ${fyl}.sha256"
963 sha256sum "$fyl" > "${fyl}.sha256"
964 done
Joey Armstrong55fe80e2023-04-05 18:32:14 -0400965 fi
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400966
967 # Generate and check checksums
Joey Armstrong55fe80e2023-04-05 18:32:14 -0400968 sha256sum -- * | grep -iv -e 'checksum' -e 'sha256' > checksum.SHA256
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400969 sha256sum -c < checksum.SHA256
970
971 echo
972 func_echo "Checksums(checksum.SHA256):"
973 cat checksum.SHA256
974
Joey Armstrong43c48422023-04-03 10:17:32 -0400975 if false; then
Joey Armstrong78cecc52023-04-03 11:39:11 -0400976 # Careful with credentials display
Joey Armstrong43c48422023-04-03 10:17:32 -0400977 get_gh_hostname login_args
Joey Armstronge8560da2023-04-26 15:44:45 -0400978 banner "gh auth status ${login_args[*]}"
Joey Armstrong43c48422023-04-03 10:17:32 -0400979 gh auth status "${login_args[@]}"
980 fi
981
Joey Armstrong76026b72023-03-29 12:19:17 -0400982 gh_release_create # publish
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400983
Joey Armstrong4d612a92024-04-24 15:30:49 -0400984 popd >/dev/null || { error "pushd failed: dir is [$release_temp]"; }
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400985
986 return
987}
988
989## -----------------------------------------------------------------------
Joey Armstrong43c48422023-04-03 10:17:32 -0400990## Intent: Normalize common arguments and access to the gh command.
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400991## o Cache path to the gh command
992## o Construct a gh command line from given args
993## o Command wrapper can provide defaults (--hostname github.com)
994## -----------------------------------------------------------------------
995## Given:
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400996## scalar Array variable name (local -n is a reference)
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400997## Return:
998## ref gh command line passed back to caller
999## Switches:
1000## --host Pass default github hostname
Joey Armstrong01e4edb2023-08-18 12:50:32 -04001001## --verbose Enable verbose mode
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001002## --version Display command version
1003## @array Remaining arguments passed as command switches.
1004## -----------------------------------------------------------------------
1005## See Also:
1006## o https://cli.github.com/manual
1007## -----------------------------------------------------------------------
1008function my_gh()
1009{
Joey Armstrong4d612a92024-04-24 15:30:49 -04001010 bannerEL 'ENTER'
Joey Armstrongdaa1f0a2024-04-03 18:07:59 -04001011
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001012 declare -a cmd=()
1013 cmd+=("$gh_cmd")
1014
1015 ## ----------------------
1016 ## Construct command line
1017 ## ----------------------
1018 # shellcheck disable=SC2034
1019 declare -A action=() # pending operations
1020 declare -a args=() # common gh command line args
1021
1022 while [ $# -gt 0 ]; do
1023 local arg="$1"; shift
Joey Armstrong4d612a92024-04-24 15:30:49 -04001024 func_echo "function arg is [$arg]"
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001025 case "$arg" in
1026
Joey Armstrong43c48422023-04-03 10:17:32 -04001027 # Modes
Joey Armstrongdaa1f0a2024-04-03 18:07:59 -04001028 -*debug)
Joey Armstrong4d612a92024-04-24 15:30:49 -04001029 # shellcheck disable=SC2034
1030 declare -i -g debug=1
1031 ;;
Joey Armstronge8560da2023-04-26 15:44:45 -04001032 -*verbose) args+=('--verbose') ;;
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001033
Joey Armstrong43c48422023-04-03 10:17:32 -04001034 -*hostname)
1035 get_gh_hostname in_args
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001036 ;;
1037
Joey Armstrong43c48422023-04-03 10:17:32 -04001038 --host-repo)
1039 local val
1040 get_argv_repo val
Joey Armstrong76026b72023-03-29 12:19:17 -04001041
Joey Armstrong43c48422023-04-03 10:17:32 -04001042 # --repo <[HOST/]OWNER/REPO>
Joey Armstrong78cecc52023-04-03 11:39:11 -04001043 args+=('--repo' "${__githost}/${val}")
Joey Armstrong43c48422023-04-03 10:17:32 -04001044 ;;
Joey Armstrong76026b72023-03-29 12:19:17 -04001045
Joey Armstrong4d612a92024-04-24 15:30:49 -04001046 # args+=('--repo' 'github.com/opencord/bbsim')
Joey Armstrongf9a0a882023-04-17 11:53:57 -04001047
Joey Armstrong43c48422023-04-03 10:17:32 -04001048 --repo)
1049 local val
1050 get_argv_repo val
1051 args+=('--repo' "$val")
1052 ;;
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001053
Joey Armstrong43c48422023-04-03 10:17:32 -04001054 --tag)
1055 local val
Joey Armstrong4d612a92024-04-24 15:30:49 -04001056 get_argv_tag val
Joey Armstrong4dbe7002023-04-04 12:47:38 -04001057 args+=("$val") # No switch, pass inline
Joey Armstrong43c48422023-04-03 10:17:32 -04001058 ;;
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001059
Joey Armstrong43c48422023-04-03 10:17:32 -04001060 --title)
1061 local val
1062 get_argv_name val
Joey Armstrongf9a0a882023-04-17 11:53:57 -04001063 args+=('--title' "'$val'")
Joey Armstrong43c48422023-04-03 10:17:32 -04001064 ;;
Joey Armstrong76026b72023-03-29 12:19:17 -04001065
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001066 *) args+=("$arg") ;;
1067 esac
1068 done
Joey Armstrong76026b72023-03-29 12:19:17 -04001069
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001070 cmd+=("${args[@]}")
Joey Armstrong73bb2f62023-04-17 19:31:50 -04001071
1072 echo
1073 declare -p cmd
1074
1075 echo
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001076 echo "** Running: ${cmd[*]}"
1077 "${cmd[@]}"
1078 local status=$?
1079
Joey Armstrong4d612a92024-04-24 15:30:49 -04001080 bannerEL 'LEAVE'
Joey Armstrong73bb2f62023-04-17 19:31:50 -04001081
Joey Armstronge8560da2023-04-26 15:44:45 -04001082 if [[ $status -eq 0 ]]; then
Joey Armstrong4d612a92024-04-24 15:30:49 -04001083 true
Joey Armstronge8560da2023-04-26 15:44:45 -04001084 else
Joey Armstrong4d612a92024-04-24 15:30:49 -04001085 false
Joey Armstronge8560da2023-04-26 15:44:45 -04001086 fi
1087
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001088 return
1089}
1090
Joey Armstrongaf577ab2022-12-15 14:43:33 -05001091##----------------##
1092##---] MAIN [---##
1093##----------------##
Joey Armstrong4d612a92024-04-24 15:30:49 -04001094bannerEL 'ENTER'
Joey Armstrong1962bcf2023-01-27 13:53:18 -05001095iam="${0##*/}"
1096
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001097full_banner
Eric Ball863379b2024-10-10 10:34:55 -07001098if [[ $(type -t "parse_args" 2>/dev/null) == "function" ]]; then
1099 parse_args "$@"
1100fi
Joey Armstrongd99e3d22023-01-29 12:35:43 -05001101init
1102install_gh_binary
Joey Armstrong7f382ef2023-01-25 12:00:08 -05001103
Joey Armstrong4d612a92024-04-24 15:30:49 -04001104[[ -v argv_self_check ]] && { do_api_validation; exit 0; }
1105
Joey Armstrong01e4edb2023-08-18 12:50:32 -04001106do_login "$*"
Zack Williams27cd3e52018-09-18 16:44:50 -07001107
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001108release_path='/dev/null'
1109get_release_path release_path
1110declare -p release_path
Zack Williams27cd3e52018-09-18 16:44:50 -07001111
Joey Armstrong4d612a92024-04-24 15:30:49 -04001112pushd "$release_path" || { error "pushd failed: dir is [$release_path]"; }
Zack Williams27cd3e52018-09-18 16:44:50 -07001113
Joey Armstrong4d612a92024-04-24 15:30:49 -04001114# legacy: getGitVersion "$GERRIT_PROJECT" GIT_VERSION
1115getGitVersion GIT_VERSION
1116getReleaseDescription RELEASE_DESCRIPTION
1117if [[ ! -v release_notes ]]; then
1118 func_echo "Generating release notes from RELEASE_DESCRIPTION"
1119 declare -g release_notes="$scratch/release.notes"
1120 echo "$RELEASE_DESCRIPTION" > "$release_notes"
1121fi
1122cat "$release_notes"
Zack Williams27cd3e52018-09-18 16:44:50 -07001123
Joey Armstrong4d612a92024-04-24 15:30:49 -04001124cat <<EOM
Joey Armstrongeef8c2c2023-03-27 17:27:43 -04001125
1126** -----------------------------------------------------------------------
1127** GIT VERSION: $(declare -p GIT_VERSION)
1128** RELEASE_DESCRIPTION: $(declare -p RELEASE_DESCRIPTION)
1129** RELEASE_TARGETS: $(declare -p RELEASE_TARGETS)
Joey Armstrong43c48422023-04-03 10:17:32 -04001130** -----------------------------------------------------------------------
Joey Armstrong4d612a92024-04-24 15:30:49 -04001131** URL: https://github.com/opencord/{repo}/releases
Joey Armstronge8560da2023-04-26 15:44:45 -04001132** -----------------------------------------------------------------------
Joey Armstrong43c48422023-04-03 10:17:32 -04001133** Running: make ${RELEASE_TARGETS}
Joey Armstrongeef8c2c2023-03-27 17:27:43 -04001134** -----------------------------------------------------------------------
1135EOM
Joey Armstrong50f6e0b2023-01-24 14:14:08 -05001136
Joey Armstrong4d612a92024-04-24 15:30:49 -04001137# build the release, can be multiple space separated targets
1138# -----------------------------------------------------------------------
1139# % go build command-line-arguments:
1140# copying /tmp/go-build4212845548/b001/exe/a.out:
1141# open release/voltctl-1.8.25-linux-amd64: permission denied
1142# missing: docker run mkdir
1143# -----------------------------------------------------------------------
1144# shellcheck disable=SC2086
1145make "$RELEASE_TARGETS"
1146copyToRelease
1147
1148cat <<EOM
Joey Armstrong1962bcf2023-01-27 13:53:18 -05001149
1150** -----------------------------------------------------------------------
1151** Create the release:
1152** 1) Create initial github release with download area.
1153** 2) Generate checksum.SHA256 for all released files.
1154** 3) Upload files to complete the release.
Joey Armstrong75a0d932023-03-28 08:59:54 -04001155** 4) Display released info from github
Joey Armstrong1962bcf2023-01-27 13:53:18 -05001156** -----------------------------------------------------------------------
Joey Armstrong26707f02023-01-26 12:41:12 -05001157EOM
1158
Joey Armstrong4d612a92024-04-24 15:30:49 -04001159showReleases
1160release_staging
Joey Armstronge8560da2023-04-26 15:44:45 -04001161
Joey Armstrong4d612a92024-04-24 15:30:49 -04001162# Useful to display but --draft images use a non-standard subdir identifier.
1163# showReleaseUrl
Joey Armstronge8560da2023-04-26 15:44:45 -04001164
Joey Armstrong4d612a92024-04-24 15:30:49 -04001165popd || { error "pushd failed: dir is [$release_path]"; }
Joey Armstrong28eddda2023-01-10 03:09:34 -05001166
Joey Armstrong4d612a92024-04-24 15:30:49 -04001167# shellcheck disable=SC2119
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001168do_logout
1169
Joey Armstrong28eddda2023-01-10 03:09:34 -05001170# [SEE ALSO]
1171# -----------------------------------------------------------------------
1172# https://www.shellcheck.net/wiki/SC2236
Joey Armstrong26707f02023-01-26 12:41:12 -05001173# https://docs.github.com/en/rest/releases/releases?apiVersion=2022-11-28#create-a-release
Joey Armstrong28eddda2023-01-10 03:09:34 -05001174# -----------------------------------------------------------------------
Joey Armstrongf085d872023-01-28 17:52:29 -05001175# https://cli.github.com/manual/gh_help_reference
1176# https://cli.github.com/manual/gh_release
1177# -----------------------------------------------------------------------
Joey Armstrong28eddda2023-01-10 03:09:34 -05001178
1179# [EOF]