blob: 913792f64449458b00025bd8f980a0f55900e8e8 [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
146 return
147}
148trap sigtrap EXIT
149
150## -----------------------------------------------------------------------
Joey Armstronge8560da2023-04-26 15:44:45 -0400151## Intent: Return a random release version string.
152## -----------------------------------------------------------------------
153## Note: Do not use this function in production. get_version() is
154## intended for local use or debugging $0 from within a jenkins
155## job.
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400156## -----------------------------------------------------------------------
157function get_version()
158{
Joey Armstrong1cc90292024-04-02 09:27:33 -0400159 local -n ref=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400160
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400161 declare -a rev=()
162 rev+=("$(( RANDOM % 10 + 1 ))")
163 rev+=("$(( RANDOM % 256 + 1 ))")
Joey Armstrongeef8c2c2023-03-27 17:27:43 -0400164 rev+=("$(( RANDOM % 10000 + 1 ))")
Joey Armstrongfc20ed52023-04-03 19:37:58 -0400165 local ver="v${rev[0]}.${rev[1]}.${rev[2]}"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400166
Joey Armstrong4d612a92024-04-24 15:30:49 -0400167 func_echo "GENERATED VERSION STRING: $ver"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400168 ref="$ver"
169 return
170}
171
172## -----------------------------------------------------------------------
173## Intent: Provide defaults for environment variables
174## -----------------------------------------------------------------------
175function initEnvVars()
176{
177 # when not running under Jenkins, use current dir as workspace and a blank
178 # project name
179 declare -g WORKSPACE=${WORKSPACE:-.}
180 declare -g GERRIT_PROJECT=${GERRIT_PROJECT:-}
Joey Armstrong76026b72023-03-29 12:19:17 -0400181
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400182 # Github organization (or user) this project is published on. Project name should
183 # be the same on both Gerrit and GitHub
Joey Armstrong76026b72023-03-29 12:19:17 -0400184 declare -g GITHUB_ORGANIZATION=${GITHUB_ORGANIZATION:-}
185
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400186 # glob pattern relative to project dir matching release artifacts
187 # ARTIFACT_GLOB=${ARTIFACT_GLOB:-"release/*"} # stat -- release/* not found, literal string (?)
188 declare -g ARTIFACT_GLOB=${ARTIFACT_GLOB:-"release/."}
Joey Armstrong76026b72023-03-29 12:19:17 -0400189
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400190 # Use "release" as the default makefile target, can be a space separated list
191 declare -g RELEASE_TARGETS=${RELEASE_TARGETS:-release}
Joey Armstrong76026b72023-03-29 12:19:17 -0400192
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400193 # Set and handle GOPATH and PATH
194 export GOPATH=${GOPATH:-$WORKSPACE/go}
195 export PATH=$PATH:/usr/lib/go-1.12/bin:/usr/local/go/bin:$GOPATH/bin
196 return
197}
198
199## -----------------------------------------------------------------------
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500200## Intent: Create a scratch area for downloads and transient tools
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400201## temp directory will be automatically removed upon exit.
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500202## -----------------------------------------------------------------------
203function init()
204{
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500205 local pkgbase="${0##*/}" # basename
206 local pkgname="${pkgbase%.*}"
207
Joey Armstrong4d612a92024-04-24 15:30:49 -0400208 bannerEL 'ENTER'
209
Joey Armstrongad7b1e02023-03-27 11:55:48 -0400210 # initEnvVars # moved to full_banner()
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400211
212 ## Create a temp directory for auto-cleanup
213 declare -g scratch
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500214 scratch="$(mktemp -d -t "${pkgname}.XXXXXXXXXX")"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400215 readonly scratch
216 declare -p scratch
217
Joey Armstrong43c48422023-04-03 10:17:32 -0400218 ## prime the stream: cache answers
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400219 local work
220 get_release_dir work
221 declare -p work
Joey Armstrong43c48422023-04-03 10:17:32 -0400222
Joey Armstronge8560da2023-04-26 15:44:45 -0400223 local git_version
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400224 getGitVersion git_version
225 func_echo "$(declare -p git_version)"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400226 return
227}
228
229## -----------------------------------------------------------------------
230## Intent: Verbose output for logging
231## -----------------------------------------------------------------------
232function banner()
233{
234 local iam="${0##*/}"
235 cat <<EOB
236
237** -----------------------------------------------------------------------
238** ${iam}::${FUNCNAME[1]}: $*
239** -----------------------------------------------------------------------
240EOB
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500241}
242
243## -----------------------------------------------------------------------
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500244## Intent: Output a log banner to identify the running script/version.
245## -----------------------------------------------------------------------
246## TODO:
247## o SCRIPT_VERSION => git changeset for repo:ci-managment
248## o echo "library version: ${env."library.libName.version"}"
249# -----------------------------------------------------------------------
250# 14:18:38 > git fetch --no-tags --progress -- https://gerrit.opencord.org/ci-management.git +refs/heads/*:refs/remotes/origin/* # timeout=10
251# 14:18:39 Checking out Revision 50f6e0b97f449b32d32ec0e02d59642000351847 (master)
252# -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400253function full_banner()
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500254{
Joey Armstrongad7b1e02023-03-27 11:55:48 -0400255 local iam="${0##*/}"
256
257 initEnvVars # set defaults
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500258
Joey Armstrong4d612a92024-04-24 15:30:49 -0400259 cat <<EOH
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500260
261** -----------------------------------------------------------------------
262** IAM: ${iam} :: ${FUNCNAME[0]}
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400263** ARGV: ${ARGV[@]}
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500264** PWD: $(/bin/pwd)
265** NOW: $(date '+%Y/%m/%d %H:%M:%S')
266** VER: ${SCRIPT_VERSION:-'unknown'}
267** -----------------------------------------------------------------------
Joey Armstrongad7b1e02023-03-27 11:55:48 -0400268** GERRIT_PROJECT: $(declare -p GERRIT_PROJECT)
269** GITHUB_ORGANIZATION: $(declare -p GITHUB_ORGANIZATION)
270** RELEASE_TARGETS: $(declare -p RELEASE_TARGETS)
271** GOPATH: $(declare -p GOPATH)
272** -----------------------------------------------------------------------
273** PATH += /usr/lib/go-1.12/bin:/usr/local/go/bin:GOPATH/bin
274** -----------------------------------------------------------------------
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500275EOH
276
277 return
278}
279
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500280## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400281## Intent: Display a message with 'iam' identifier for logging
Joey Armstrong50f6e0b2023-01-24 14:14:08 -0500282## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400283function func_echo()
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500284{
285 local iam="${0##*/}"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400286 echo "** ${iam} :: ${FUNCNAME[1]}: $*"
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500287 return
288}
289
290## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400291## Intent: Display an error message then exit with status.
292## -----------------------------------------------------------------------
293function error()
294{
295 local iam="${0##*/}"
Joey Armstrong4d612a92024-04-24 15:30:49 -0400296 local func="${FUNCNAME[1]}"
297 local line="${BASH_LINENO[1]}"
298 printf "\nERROR %s :: %s (LINENO:%s): %s" "$iam" "$func" "$line" "$*"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400299 exit 1
300}
301
302## -----------------------------------------------------------------------
303## Intent: Verify sandbox/build is versioned for release.
304## -----------------------------------------------------------------------
305function getGitVersion()
306{
Joey Armstrong1cc90292024-04-02 09:27:33 -0400307 local -n varname=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400308
Joey Armstrong4d612a92024-04-24 15:30:49 -0400309 bannerEL 'ENTER'
310
Joey Armstrong43c48422023-04-03 10:17:32 -0400311 local __ver # use prefix('__') to protect callers variable name
312 if [[ -v cached_getGitVersion ]]; then
313 __ver="$cached_getGitVersion"
314 varname="$__ver"
315 return
Joey Armstrong39fac652023-03-27 18:50:43 -0400316
Joey Armstrong43c48422023-04-03 10:17:32 -0400317 elif [[ -v argv_gen_version ]]; then
318 get_version __ver
319
320 elif [[ -v WORKSPACE ]] && [[ -v GITHUB_TOKEN ]]; then # i_am_jenkins
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400321 local path="$GERRIT_PROJECT"
Joey Armstrong4d612a92024-04-24 15:30:49 -0400322 pushd "$path" || { error "pushd GERRIT_PROJECT= failed $(declare -p path)"; }
Joey Armstrong43c48422023-04-03 10:17:32 -0400323 __ver="$(git tag -l --points-at HEAD)"
Joey Armstrong4d612a92024-04-24 15:30:49 -0400324 popd || { error "popd GERRIT_PROJECT= failed"; }
Joey Armstrong43c48422023-04-03 10:17:32 -0400325
326 elif [[ -v argv_version_file ]]; then # local debug
327 [[ ! -f VERSION ]] && error "./VERSION file not found"
328 readarray -t tmp < <(sed -e 's/[[:blank:]]//g' 'VERSION')
329 __ver="v${tmp[0]}"
330
Joey Armstrong4d612a92024-04-24 15:30:49 -0400331 elif [[ ${#GERRIT_PROJECT} -gt 0 ]]; then
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400332 cd ..
333 local path="$GERRIT_PROJECT"
Joey Armstrong4d612a92024-04-24 15:30:49 -0400334 declare -p path
335 pushd "$path" || { error "pushd GERRIT_PROJECT= failed $(declare -p path)"; }
Joey Armstrong43c48422023-04-03 10:17:32 -0400336 __ver="$(git tag -l --points-at HEAD)"
Joey Armstrong4d612a92024-04-24 15:30:49 -0400337 popd || { error "popd GERRIT_PROJECT= failed"; }
338
339 else # pwd==sandbox
340
341 local line="${BASH_LINENO[0]}"
342 local message="
343GERRIT_PROJECT= is not defined:
344 o One of the following are required:
345 - Define GERRIT_PROJECT= (~jenkins).
346 - Argument --verison-file.
347"
348 error "$message"
Joey Armstrong43c48422023-04-03 10:17:32 -0400349 fi
Joey Armstrong76026b72023-03-29 12:19:17 -0400350
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400351 # ------------------------------------------------------
352 # match bare versions or v-prefixed golang style version
353 # Critical failure for new/yet-to-be-released repo ?
354 # ------------------------------------------------------
Joey Armstrong43c48422023-04-03 10:17:32 -0400355 if [[ "$__ver" =~ ^v?([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then
Joey Armstrongfc20ed52023-04-03 19:37:58 -0400356 echo "git has a SemVer released version tag: '$__ver'"
Joey Armstrong43c48422023-04-03 10:17:32 -0400357 echo "Building artifacts for GitHub release."
358
359 elif [[ "$__ver" =~ ^v?([0-9]+)\.([0-9]+)\.([0-9]+)-dev([0-9]+)$ ]]; then
Joey Armstrong4d612a92024-04-24 15:30:49 -0400360 # v1.2.3-dev (*-dev*) is an implicit draft release.
Joey Armstrong43c48422023-04-03 10:17:32 -0400361 declare -i -g draft_release=1
362 echo "Detected --draft SemVer release version tag [$__ver]"
363 echo "Building artifacts for GitHub release."
364
Joey Armstrong4d612a92024-04-24 15:30:49 -0400365 # Should also accept: X.Y.Z-{alpha,beta,...}
Joey Armstrong4f87de82023-08-25 12:42:39 -0400366
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400367 else
Joey Armstrong4f87de82023-08-25 12:42:39 -0400368 echo "Version string contains: [${__ver}]"
Joey Armstrong43c48422023-04-03 10:17:32 -0400369 error "No SemVer released version tag found, exiting..."
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400370 fi
371
Joey Armstrong43c48422023-04-03 10:17:32 -0400372 ## Cache value for subsequent calls.
373 ## readonly is a guarantee assignment happens once.
374 declare -g cached_getGitVersion="$__ver"
375 readonly cached_getGitVersion;
376
377 varname="$__ver"
378 func_echo "Version is [$__ver] from $(/bin/pwd)"
Joey Armstrong4d612a92024-04-24 15:30:49 -0400379
380 bannerEL 'LEAVE'
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400381 return
382}
383
384## -----------------------------------------------------------------------
385## Intent:
386## Note: Release description is sanitized version of the log message
387## -----------------------------------------------------------------------
388function getReleaseDescription()
389{
Joey Armstrong1cc90292024-04-02 09:27:33 -0400390 local -n varname=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400391
392 local msg
393 msg="$(git log -1 --pretty=%B)"
394
395 local val
396 val="$(tr -dc "[:alnum:]\n\r\.\[\]\:\-\\\/\`\' " <<< "$msg")"
397
398 [[ ${#val} -eq 0 ]] && error "Release Description is empty ($msg)"
399 varname="$val"
400 return
401}
402
403## -----------------------------------------------------------------------
404## Intent: Retrieve value of the release temp directory.
405## -----------------------------------------------------------------------
406## Note: Limit use of globals so logic can be isolated in function calls.
407## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400408function get_release_dir()
409{
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400410 local -n varname=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400411
412 # Temporary staging directory to copy artifacts to
413 varname="$scratch/release"
414 mkdir -p "$varname"
415 return
416}
417
418## -----------------------------------------------------------------------
419## Intent: Retrieve github server hostname
420## -----------------------------------------------------------------------
421function get_gh_hostname()
422{
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400423 local -n varname=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400424 varname+=('--hostname' "${__githost}")
425 return
426}
427
428## -----------------------------------------------------------------------
429## Intent: Retrieve repository organizaiton name
430## -----------------------------------------------------------------------
431function get_gh_repo_org()
432{
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400433 local -n ref=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400434 declare -g __repo_org
Joey Armstrong76026b72023-03-29 12:19:17 -0400435
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400436 local org
437 if [[ -v __repo_org ]]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400438 org="$__repo_org"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400439 elif [[ ! -v GITHUB_ORGANIZATION ]]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400440 error "--repo-org or GITHUB_ORGANIZATION= are required"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400441 else
Joey Armstrong43c48422023-04-03 10:17:32 -0400442 org="${GITHUB_ORGANIZATION}"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400443 fi
444
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400445 ref="$org"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400446 return
447}
448
449## -----------------------------------------------------------------------
Eric Balla8ed45a2025-01-08 12:45:45 -0800450## Intent: Retrieve repository name
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400451## -----------------------------------------------------------------------
452function get_gh_repo_name()
453{
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400454 local -n ref=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400455 declare -g __repo_name
Joey Armstrong76026b72023-03-29 12:19:17 -0400456
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400457 local name
458 if [[ -v __repo_name ]]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400459 name="$__repo_name"
Joey Armstrong39fac652023-03-27 18:50:43 -0400460 elif [[ ! -v GERRIT_PROJECT ]]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400461 error "--repo-name or GERRIT_PROJECT= are required"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400462 else
Joey Armstrong43c48422023-04-03 10:17:32 -0400463 name="${GERRIT_PROJECT}"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400464 fi
465
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400466 ref="$name"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400467 return
468}
469
470## -----------------------------------------------------------------------
471## Intent: Return path for the gh release query
472## -----------------------------------------------------------------------
473function get_gh_releases()
474{
Joey Armstrong1cc90292024-04-02 09:27:33 -0400475 local -n ref=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400476
477 local repo_org
478 get_gh_repo_org repo_org
479
480 local repo_name
481 get_gh_repo_name repo_name
482
483 ref="repos/${repo_org}/${repo_name}/releases"
Joey Armstrong4d612a92024-04-24 15:30:49 -0400484 func_echo "releases_uri=$ref"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400485 return
486}
487
488## -----------------------------------------------------------------------
Eric Balla8ed45a2025-01-08 12:45:45 -0800489## Intent: Return whether the repo has discussions enabled
490## -----------------------------------------------------------------------
491function get_discussions()
492{
493 declare -g HAS_DISCUSSIONS
494
495 local repo_org
496 get_gh_repo_org repo_org
497
498 local repo_name
499 get_gh_repo_name repo_name
500
501 local repo_uri
502 repo_uri="repos/${repo_org}/${repo_name}"
503
504 func_echo "RUNNING: $gh_cmd api $repo_uri | jq '.has_discussions'"
505 HAS_DISCUSSIONS=$($gh_cmd api $repo_uri | jq '.has_discussions')
506 return
507}
508
509## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400510## Intent: Retrieve repository path argument
511## -----------------------------------------------------------------------
512function get_argv_repo()
513{
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400514 local -n ref=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400515
516 local repo_org
517 get_gh_repo_org repo_org
Joey Armstrong76026b72023-03-29 12:19:17 -0400518
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400519 local repo_name
Joey Armstrong76026b72023-03-29 12:19:17 -0400520 get_gh_repo_name repo_name
521
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400522 ref="${repo_org}/${repo_name}"
523 # func_echo "ref=$(declare -p ref)"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400524 return
525}
526
527## -----------------------------------------------------------------------
528## Intent: Retrieve release name string "project - version"
529## -----------------------------------------------------------------------
530function get_argv_name()
531{
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400532 local -n ref=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400533
534 local repo_name
535 get_gh_repo_name repo_name
Joey Armstrong43c48422023-04-03 10:17:32 -0400536
537 local repo_ver
538 getGitVersion repo_ver
539
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400540 # ref="${repo_name} - $GIT_VERSION"
541 ref="${repo_name} - ${repo_ver}"
542 func_echo "ref=$(declare -p ref)"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400543 return
544}
545
546## -----------------------------------------------------------------------
547## Intent: Retrieve tag version
548## -----------------------------------------------------------------------
549function get_argv_tag()
550{
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400551 local -n ref=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400552
553 # cached_argv_tag='v3.41.3204'
554 if [[ ! -v cached_argv_tag ]]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400555 declare -g cached_argv_tag
556 if [[ -v GIT_VERSION ]]; then # hello jenkins
557 cached_argv_tag="$GIT_VERSION"
558 fi
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400559 fi
560
561 [[ ${#cached_argv_tag} -eq 0 ]] && error "Unable to determine GIT_VERSION="
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400562 ref="$cached_argv_tag"
563 func_echo "ref=$(declare -p ref)"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400564 return
565}
566
567## -----------------------------------------------------------------------
568## Intent:
569## -----------------------------------------------------------------------
570# To support golang projects that require GOPATH to be set and code checked out there
571# If $DEST_GOPATH is not an empty string:
572# - create GOPATH within WORKSPACE, and destination directory within
573# - set PATH to include $GOPATH/bin and the system go binaries
574# - move project from $WORKSPACE/$GERRIT_PROJECT to new location in $GOPATH
575# - start release process within that directory
576## -----------------------------------------------------------------------
577function get_release_path()
578{
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400579 local -n ref=$1; shift
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400580 local varpath="$ref"
Joey Armstrongdaa1f0a2024-04-03 18:07:59 -0400581
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400582 DEST_GOPATH=${DEST_GOPATH:-}
583 if [ -n "$DEST_GOPATH" ]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400584 mkdir -p "$GOPATH/src/$DEST_GOPATH"
Joey Armstronge8560da2023-04-26 15:44:45 -0400585 varpath="$GOPATH/src/$DEST_GOPATH/$GERRIT_PROJECT"
586 mv "$WORKSPACE/$GERRIT_PROJECT" "$varpath"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400587 else
Joey Armstronge8560da2023-04-26 15:44:45 -0400588 varpath="$WORKSPACE/$GERRIT_PROJECT"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400589 fi
590
Joey Armstrong43c48422023-04-03 10:17:32 -0400591 ## Verify pwd is OK
592 for path in \
Joey Armstronge8560da2023-04-26 15:44:45 -0400593 "${varpath}/Makefile"\
Joey Armstrong4d612a92024-04-24 15:30:49 -0400594 "${varpath}/makefile"\
595 "__ERROR__"\
596 ; do
Joey Armstrong43c48422023-04-03 10:17:32 -0400597 case "$path" in
Joey Armstronge8560da2023-04-26 15:44:45 -0400598 __ERROR__) error "Makefile not found at ${varpath} !" ;;
Joey Armstrong43c48422023-04-03 10:17:32 -0400599 *) [[ -f "$path" ]] && break ;;
600 esac
601 done
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400602
Mahir Gunyel2c9e8502024-03-29 13:05:38 -0700603 ref="$varpath"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400604}
605
606## -----------------------------------------------------------------------
607## Intent: Display future enhancements
608## -----------------------------------------------------------------------
609function todo()
610{
611 local iam="${0##*/}"
612
Joey Armstrong4d612a92024-04-24 15:30:49 -0400613 cat <<EOT
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400614
615** -----------------------------------------------------------------------
616** IAM: ${iam} :: ${FUNCNAME[0]}
617** -----------------------------------------------------------------------
618 o get_release_path()
619 - refactor redundant paths into local vars.
620 - see comments, do we have a one-off failure condition ?
Joey Armstrongad7b1e02023-03-27 11:55:48 -0400621 o PATH += golang appended 3 times, release needs a single, reliable answer.
Eric Balla8ed45a2025-01-08 12:45:45 -0800622 o do_login and api calls do not use the my_gh wrapper:
Joey Armstrongad7b1e02023-03-27 11:55:48 -0400623 - Add a lookup function to cache and retrieve path to downloaded gh command.
Joey Armstrong76026b72023-03-29 12:19:17 -0400624
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400625EOT
626
627 return
628}
629
630## -----------------------------------------------------------------------
Joey Armstrong76026b72023-03-29 12:19:17 -0400631## Intent: Verify a directory contains content for release.
632## -----------------------------------------------------------------------
633## Given:
634## scalar Path to release/ directory
635## ref Results returned through this indirect var.
636## -----------------------------------------------------------------------
637## Usage:
638## declare -a artifacts=()
639## get_artifacts '/foo/bar/tans' artifacts
640## declare -p artifacts
641## -----------------------------------------------------------------------
642function get_artifacts()
643{
644 local dir="$1" ; shift
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400645 local -n refA=$1 ; shift
Joey Armstrong76026b72023-03-29 12:19:17 -0400646
Joey Armstrong55fe80e2023-04-05 18:32:14 -0400647 # Glob available files, exclude checksums
648 readarray -t __artifacts < <(find "$dir" -mindepth 1 ! -type d \
Joey Armstrong4d612a92024-04-24 15:30:49 -0400649 | grep -iv -e 'sum256' -e 'checksum')
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400650 # func_echo "$(declare -p __artifacts)"
Joey Armstrong76026b72023-03-29 12:19:17 -0400651
652 # -----------------------------------------------------------------------
653 # Verify count>0 to inhibit source-only release
654 # Problem children:
655 # o build or make release failures.
656 # o docker container filesystem mount problem (~volume)
657 # -----------------------------------------------------------------------
658 [[ ${#__artifacts[@]} -eq 0 ]] \
Joey Armstrong4d612a92024-04-24 15:30:49 -0400659 && error "Artifact dir is empty: $(declare -p dir)"
Joey Armstrong76026b72023-03-29 12:19:17 -0400660
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400661 refA=("${__artifacts[@]}")
Joey Armstrong76026b72023-03-29 12:19:17 -0400662 return
663}
664
665## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400666## Intent: Copy files from the build directory into the release staging
667## directory for publishing to github releases/ endpoint.
Joey Armstrong26707f02023-01-26 12:41:12 -0500668## -----------------------------------------------------------------------
669function copyToRelease()
670{
Joey Armstrong76026b72023-03-29 12:19:17 -0400671 banner ''
Joey Armstrong26707f02023-01-26 12:41:12 -0500672
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400673 local artifact_glob="${ARTIFACT_GLOB%/*}"
674 func_echo "$(declare -p artifact_glob)"
675
676 local work
677 get_release_dir work
Joey Armstrong76026b72023-03-29 12:19:17 -0400678 func_echo "Artifact dir: $(declare -p work)"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400679
Joey Armstrong76026b72023-03-29 12:19:17 -0400680 ## Verify release content is available
681 declare -a artifacts=()
Joey Armstrong43c48422023-04-03 10:17:32 -0400682 # get_artifacts "$work" artifacts
683 get_artifacts "$artifact_glob" artifacts
684 func_echo "Artifacts in copy_from: $(declare -p artifacts)"
Joey Armstrong472bfba2023-03-27 18:12:28 -0400685
Joey Armstrong26707f02023-01-26 12:41:12 -0500686 # Copy artifacts into the release temp dir
687 # shellcheck disable=SC2086
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400688 echo "rsync -rv --checksum \"$artifact_glob/.\" \"$work/.\""
689 rsync -rv --checksum "$artifact_glob/." "$work/."
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500690
Joey Armstrong43c48422023-04-03 10:17:32 -0400691 get_artifacts "$work" artifacts
692 func_echo "Artifacts in copy_to: $(declare -p artifacts)"
693
Joey Armstrong26707f02023-01-26 12:41:12 -0500694 return
695}
696
697## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400698## https://docs.github.com/en/rest/releases?apiVersion=2022-11-28
Joey Armstrong4d612a92024-04-24 15:30:49 -0400699# https://cli.github.com/manual/gh_release_create
700# --target <branch> or commit SHA
701# --title
702# --generate-notes
703# --release-notes (notes file)
704# --release
705# release create dist/*.tgz
706# --discussion-category "General"
Joey Armstrongf085d872023-01-28 17:52:29 -0500707## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400708# https://cli.github.com/manual/gh_release_create
709## -----------------------------------------------------------------------
710function gh_release_create()
Joey Armstrong38bfeea2023-02-06 18:01:29 -0500711{
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400712 banner ''
Joey Armstrong38bfeea2023-02-06 18:01:29 -0500713
Joey Armstrong43c48422023-04-03 10:17:32 -0400714 local version
715 getGitVersion version
Joey Armstrong38bfeea2023-02-06 18:01:29 -0500716
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400717 local work
718 get_release_dir work
719
Joey Armstrong43c48422023-04-03 10:17:32 -0400720 declare -a args=()
721 args+=('--host-repo')
722 args+=('--title')
Eric Balla8ed45a2025-01-08 12:45:45 -0800723
724 # Check if repo has discussions enabled
725 get_discussions
726
Joey Armstrong43c48422023-04-03 10:17:32 -0400727 if [[ -v draft_release ]]; then
728 args+=('--draft')
Eric Balla8ed45a2025-01-08 12:45:45 -0800729 elif [[ $HAS_DISCUSSIONS == "true" ]]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400730 args+=('--discussion-category' 'Announcements')
731 fi
732
Joey Armstrong55fe80e2023-04-05 18:32:14 -0400733 if [[ -v release_notes ]]; then
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400734 args+=('--notes-file' "$release_notes")
Joey Armstrong55fe80e2023-04-05 18:32:14 -0400735 fi
Joey Armstrong43c48422023-04-03 10:17:32 -0400736
Joey Armstrong78cecc52023-04-03 11:39:11 -0400737 pushd "$work/.." >/dev/null
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400738 # func_echo "WORK=$work"
Joey Armstrong78cecc52023-04-03 11:39:11 -0400739 readarray -t payload < <(find 'release' -maxdepth 4 ! -type d -print)
Joey Armstrong76026b72023-03-29 12:19:17 -0400740
Joey Armstronge8560da2023-04-26 15:44:45 -0400741 func_echo "$gh_cmd release create ${version} ${args[*]} ${payload[*]}"
Joey Armstrong43c48422023-04-03 10:17:32 -0400742
743 if [[ -v dry_run ]]; then
744 echo "[SKIP] dry run"
745 else
Joey Armstronge8560da2023-04-26 15:44:45 -0400746 func_echo "my_gh release create '$version' ${args[*]} ${payload[*]}"
Joey Armstrong4dbe7002023-04-04 12:47:38 -0400747 my_gh 'release' 'create' "$version" "${args[@]}" "${payload[@]}"
Joey Armstrong43c48422023-04-03 10:17:32 -0400748 fi
Joey Armstrong76026b72023-03-29 12:19:17 -0400749 popd >/dev/null
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400750
Joey Armstrong38bfeea2023-02-06 18:01:29 -0500751 return
752}
753
754## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400755## Intent: Authenticate credentials for a github gh session
Joey Armstrong38bfeea2023-02-06 18:01:29 -0500756## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400757## NOTE: my_gh currently unused due to --with-token < "$pac"
758## -----------------------------------------------------------------------
759function do_login()
Joey Armstrongf085d872023-01-28 17:52:29 -0500760{
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400761 # shellcheck disable=SC2120
762 # shellcheck disable=SC2034
Joey Armstrong4d612a92024-04-24 15:30:49 -0400763 [[ $# -gt 0 ]] && { local unused="$1"; shift; }
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400764
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400765 declare -g pac
Joey Armstrong485081f2023-03-27 13:34:08 -0400766 declare -a login_args=()
767 [[ $# -gt 0 ]] && login_args+=("$@")
Joey Armstrongf085d872023-01-28 17:52:29 -0500768
Joey Armstrong485081f2023-03-27 13:34:08 -0400769 # https://github.com/cli/cli/issues/2922#issuecomment-775027762
Joey Armstrong75a0d932023-03-28 08:59:54 -0400770 # (sigh) why not quietly return VS forcing a special logic path
Joey Armstronge6e18eb2023-03-27 14:19:21 -0400771 # [[ -v WORKSPACE ]] && [[ -v GITHUB_TOKEN ]] && return
772
Joey Armstrong4d612a92024-04-24 15:30:49 -0400773 # 12:58:36 ** -----------------------------------------------------------------------
774 # 12:58:36 ** jenkins582353203049151829.sh::do_login: --hostname github.com
775 # 12:58:36 ** --------------------------------------------------------------------# ---
776 # 12:58:36 ** jenkins582353203049151829.sh :: do_login: Detected ENV{GITHUB_TOKEN}=
777 # 12:58:36 The value of the GITHUB_TOKEN environment variable is being used for authentication.
778 # 12:58:36 To have GitHub CLI store credentials instead, first clear the value from the environment.
779 # -----------------------------------------------------------------------
Joey Armstrong76026b72023-03-29 12:19:17 -0400780
Joey Armstrong485081f2023-03-27 13:34:08 -0400781 get_gh_hostname login_args
Joey Armstrongf085d872023-01-28 17:52:29 -0500782
Joey Armstrong485081f2023-03-27 13:34:08 -0400783 banner "${login_args[@]}"
Joey Armstrong78cecc52023-04-03 11:39:11 -0400784 func_echo "$(declare -p WORKSPACE)"
Joey Armstrongf085d872023-01-28 17:52:29 -0500785
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400786 ## Read from disk is safer than export GITHUB_TOKEN=
787 if [[ -v pac ]] && [[ ${#pac} -gt 0 ]]; then # interactive/debugging
Joey Armstrong43c48422023-04-03 10:17:32 -0400788 [ ! -f "$pac" ] && error "PAC token file $pac does not exist"
Joey Armstronge8560da2023-04-26 15:44:45 -0400789 func_echo "$gh_cmd auth login ${login_args[*]} --with-token < $pac"
Joey Armstrong485081f2023-03-27 13:34:08 -0400790 "$gh_cmd" auth login "${login_args[@]}" --with-token < "$pac"
Joey Armstrongf085d872023-01-28 17:52:29 -0500791
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400792 elif [[ ! -v GITHUB_TOKEN ]]; then
793 error "--token [t] or GITHUB_TOKEN= are required"
Joey Armstrong41923cc2023-01-30 14:38:16 -0500794
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400795 else # jenkins
Joey Armstronge8560da2023-04-26 15:44:45 -0400796 func_echo "$gh_cmd auth login ${login_args[*]} (ie: jenkins)"
Joey Armstrong733ea9f2023-04-03 21:19:46 -0400797
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400798 # https://github.com/cli/cli/issues/2922#issuecomment-775027762
799 # When using GITHUB_TOKEN, there is no need to even run gh auth login
Joey Armstrong733ea9f2023-04-03 21:19:46 -0400800 # "$gh_cmd" auth login "${login_args[@]}"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400801 fi
Joey Armstrongf085d872023-01-28 17:52:29 -0500802
Joey Armstrong43c48422023-04-03 10:17:32 -0400803 declare -i -g active_login=1 # signal logout needed
Joey Armstrongf085d872023-01-28 17:52:29 -0500804
Joey Armstrongf085d872023-01-28 17:52:29 -0500805 return
806}
807
808## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400809## Intent: Query for repository version strings
810## -----------------------------------------------------------------------
811function get_releases()
812{
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400813 # shellcheck disable=SC2178
Joey Armstrong1cc90292024-04-02 09:27:33 -0400814 local -n refA=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400815
816 banner ""
817 pushd "$scratch" >/dev/null
Joey Armstrong76026b72023-03-29 12:19:17 -0400818
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400819 # gh api repos/{owner}/{repo}/releases
820 local releases_uri
821 get_gh_releases releases_uri
Joey Armstrong4d612a92024-04-24 15:30:49 -0400822
823 func_echo "RUNNING: $gh_cmd api $releases_uri | jq . > release.raw"
Joey Armstrong76026b72023-03-29 12:19:17 -0400824
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400825 refA=()
Joey Armstronge8560da2023-04-26 15:44:45 -0400826 "$gh_cmd" api "$releases_uri" | jq . > 'release.raw'
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400827 readarray -t __tmp < <(jq '.[] | "\(.tag_name)"' 'release.raw')
828
829 local release
830 for release in "${__tmp[@]}";
831 do
Joey Armstrong43c48422023-04-03 10:17:32 -0400832 release="${release//\"/}"
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400833 refA+=("$release")
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400834 done
835
836 popd >/dev/null
837 return
838}
839
840## -----------------------------------------------------------------------
841## Intent: Display repository query strings.
842## Indirect: verify authentication and API
843## -----------------------------------------------------------------------
844function showReleases()
845{
Joey Armstrong43c48422023-04-03 10:17:32 -0400846 declare -a raw=()
847 get_releases raw
848
849 ## Sort for display, we may need to prune volume later on
Joey Armstronge8560da2023-04-26 15:44:45 -0400850 readarray -t releases < <(sort -nr <<<"${raw[*]}")
851 # IFS=$'\n' releases=($(sort -nr <<<"${raw[*]}"))
852 # unset IFS
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400853
854 local release
855 for release in "${releases[@]}";
856 do
Joey Armstrong43c48422023-04-03 10:17:32 -0400857 func_echo "$release"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400858 done
859 return
860}
861
862## -----------------------------------------------------------------------
863## Intent: Install the gh command line tool locally
Joey Armstrong26707f02023-01-26 12:41:12 -0500864## -----------------------------------------------------------------------
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500865function install_gh_binary()
Joey Armstrong26707f02023-01-26 12:41:12 -0500866{
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400867 banner
Joey Armstrong26707f02023-01-26 12:41:12 -0500868
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500869 pushd "$scratch"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400870 func_echo "Retrieve latest gh download URLs"
Joey Armstrongf085d872023-01-28 17:52:29 -0500871
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500872 local latest="https://github.com/cli/cli/releases/latest"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400873 local tarball="gh.tar.tgz"
874
Joey Armstrong76026b72023-03-29 12:19:17 -0400875 readarray -t latest < <(\
Joey Armstrong4d612a92024-04-24 15:30:49 -0400876 curl --silent -qI "$latest" \
877 | awk -F '/' '/^location/ {print substr($NF, 1, length($NF)-1)}')
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400878 declare -p latest
879 if [ ${#latest[@]} -ne 1 ]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400880 error "Unable to determine latest gh package version"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400881 fi
Joey Armstrong76026b72023-03-29 12:19:17 -0400882
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400883 local VER="${latest[0]}"
884
885 func_echo "Download latest gh binary"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500886 local url="https://github.com/cli/cli/releases/download/${VER}/gh_${VER#v}_linux_amd64.tar.gz"
Joey Armstrong43c48422023-04-03 10:17:32 -0400887 func_echo "wget --quiet --output-document='$tarball' '$url'"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500888 wget --quiet --output-document="$tarball" "$url"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400889
890 func_echo "Unpack tarball"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500891 tar zxf "$tarball"
892
Joey Armstrong78cecc52023-04-03 11:39:11 -0400893 declare -g gh_cmd
894 gh_cmd="$(find "${scratch}" -name 'gh' -print)"
895 #declare -g gh_cmd='/usr/bin/gh'
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500896 readonly gh_cmd
897
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400898 func_echo "Command: ${gh_cmd}"
899 func_echo "Version: $("$gh_cmd" --version)"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500900 popd
901
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400902 return
903}
904
905## -----------------------------------------------------------------------
906## Intent: Danger Will Robinson
907## -----------------------------------------------------------------------
908function releaseDelete()
909{
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400910 # shellcheck disable=SC2178
911 local -n refA=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400912 local version="$1"; shift
Joey Armstrong76026b72023-03-29 12:19:17 -0400913
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400914 banner "${refA[@]}"
915 # declare -a refA=()
916 refA+=('--host-repo')
917 refA+=('--yes')
918 # refA+=('--cleanup-tag')
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400919
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400920 echo
921 echo "==========================================================================="
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400922 my_gh 'release' 'delete' "$version" "${refA[@]}"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400923 echo "==========================================================================="
924 echo
925
926 showReleases
927 return
928}
929
930## -----------------------------------------------------------------------
931## Intent: Copy binaries into temp/release, checksum then publish
932## -----------------------------------------------------------------------
933function release_staging()
934{
935 local release_temp
936 get_release_dir release_temp
937
938 banner ''
939 func_echo "Packaging release files"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400940
Joey Armstrong76026b72023-03-29 12:19:17 -0400941 pushd "$release_temp" >/dev/null \
Joey Armstrong4d612a92024-04-24 15:30:49 -0400942 || { error "pushd failed: dir is [$release_temp]"; }
Joey Armstrong76026b72023-03-29 12:19:17 -0400943
944 declare -a to_release=()
945 get_artifacts '.' to_release
946
Joey Armstrong55fe80e2023-04-05 18:32:14 -0400947 if false; then
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400948 for fyl in "${to_release[@]}";
949 do
950 func_echo "sha256sum $fyl > ${fyl}.sha256"
951 sha256sum "$fyl" > "${fyl}.sha256"
952 done
Joey Armstrong55fe80e2023-04-05 18:32:14 -0400953 fi
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400954
955 # Generate and check checksums
Joey Armstrong55fe80e2023-04-05 18:32:14 -0400956 sha256sum -- * | grep -iv -e 'checksum' -e 'sha256' > checksum.SHA256
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400957 sha256sum -c < checksum.SHA256
958
959 echo
960 func_echo "Checksums(checksum.SHA256):"
961 cat checksum.SHA256
962
Joey Armstrong43c48422023-04-03 10:17:32 -0400963 if false; then
Joey Armstrong78cecc52023-04-03 11:39:11 -0400964 # Careful with credentials display
Joey Armstrong43c48422023-04-03 10:17:32 -0400965 get_gh_hostname login_args
Joey Armstronge8560da2023-04-26 15:44:45 -0400966 banner "gh auth status ${login_args[*]}"
Joey Armstrong43c48422023-04-03 10:17:32 -0400967 gh auth status "${login_args[@]}"
968 fi
969
Joey Armstrong76026b72023-03-29 12:19:17 -0400970 gh_release_create # publish
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400971
Joey Armstrong4d612a92024-04-24 15:30:49 -0400972 popd >/dev/null || { error "pushd failed: dir is [$release_temp]"; }
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400973
974 return
975}
976
977## -----------------------------------------------------------------------
Joey Armstrong43c48422023-04-03 10:17:32 -0400978## Intent: Normalize common arguments and access to the gh command.
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400979## o Cache path to the gh command
980## o Construct a gh command line from given args
981## o Command wrapper can provide defaults (--hostname github.com)
982## -----------------------------------------------------------------------
983## Given:
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400984## scalar Array variable name (local -n is a reference)
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400985## Return:
986## ref gh command line passed back to caller
987## Switches:
988## --host Pass default github hostname
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400989## --verbose Enable verbose mode
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400990## --version Display command version
991## @array Remaining arguments passed as command switches.
992## -----------------------------------------------------------------------
993## See Also:
994## o https://cli.github.com/manual
995## -----------------------------------------------------------------------
996function my_gh()
997{
Joey Armstrong4d612a92024-04-24 15:30:49 -0400998 bannerEL 'ENTER'
Joey Armstrongdaa1f0a2024-04-03 18:07:59 -0400999
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001000 declare -a cmd=()
1001 cmd+=("$gh_cmd")
1002
1003 ## ----------------------
1004 ## Construct command line
1005 ## ----------------------
1006 # shellcheck disable=SC2034
1007 declare -A action=() # pending operations
1008 declare -a args=() # common gh command line args
1009
1010 while [ $# -gt 0 ]; do
1011 local arg="$1"; shift
Joey Armstrong4d612a92024-04-24 15:30:49 -04001012 func_echo "function arg is [$arg]"
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001013 case "$arg" in
1014
Joey Armstrong43c48422023-04-03 10:17:32 -04001015 # Modes
Joey Armstrongdaa1f0a2024-04-03 18:07:59 -04001016 -*debug)
Joey Armstrong4d612a92024-04-24 15:30:49 -04001017 # shellcheck disable=SC2034
1018 declare -i -g debug=1
1019 ;;
Joey Armstronge8560da2023-04-26 15:44:45 -04001020 -*verbose) args+=('--verbose') ;;
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001021
Joey Armstrong43c48422023-04-03 10:17:32 -04001022 -*hostname)
1023 get_gh_hostname in_args
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001024 ;;
1025
Joey Armstrong43c48422023-04-03 10:17:32 -04001026 --host-repo)
1027 local val
1028 get_argv_repo val
Joey Armstrong76026b72023-03-29 12:19:17 -04001029
Joey Armstrong43c48422023-04-03 10:17:32 -04001030 # --repo <[HOST/]OWNER/REPO>
Joey Armstrong78cecc52023-04-03 11:39:11 -04001031 args+=('--repo' "${__githost}/${val}")
Joey Armstrong43c48422023-04-03 10:17:32 -04001032 ;;
Joey Armstrong76026b72023-03-29 12:19:17 -04001033
Joey Armstrong4d612a92024-04-24 15:30:49 -04001034 # args+=('--repo' 'github.com/opencord/bbsim')
Joey Armstrongf9a0a882023-04-17 11:53:57 -04001035
Joey Armstrong43c48422023-04-03 10:17:32 -04001036 --repo)
1037 local val
1038 get_argv_repo val
1039 args+=('--repo' "$val")
1040 ;;
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001041
Joey Armstrong43c48422023-04-03 10:17:32 -04001042 --tag)
1043 local val
Joey Armstrong4d612a92024-04-24 15:30:49 -04001044 get_argv_tag val
Joey Armstrong4dbe7002023-04-04 12:47:38 -04001045 args+=("$val") # No switch, pass inline
Joey Armstrong43c48422023-04-03 10:17:32 -04001046 ;;
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001047
Joey Armstrong43c48422023-04-03 10:17:32 -04001048 --title)
1049 local val
1050 get_argv_name val
Joey Armstrongf9a0a882023-04-17 11:53:57 -04001051 args+=('--title' "'$val'")
Joey Armstrong43c48422023-04-03 10:17:32 -04001052 ;;
Joey Armstrong76026b72023-03-29 12:19:17 -04001053
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001054 *) args+=("$arg") ;;
1055 esac
1056 done
Joey Armstrong76026b72023-03-29 12:19:17 -04001057
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001058 cmd+=("${args[@]}")
Joey Armstrong73bb2f62023-04-17 19:31:50 -04001059
1060 echo
1061 declare -p cmd
1062
1063 echo
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001064 echo "** Running: ${cmd[*]}"
1065 "${cmd[@]}"
1066 local status=$?
1067
Joey Armstrong4d612a92024-04-24 15:30:49 -04001068 bannerEL 'LEAVE'
Joey Armstrong73bb2f62023-04-17 19:31:50 -04001069
Joey Armstronge8560da2023-04-26 15:44:45 -04001070 if [[ $status -eq 0 ]]; then
Joey Armstrong4d612a92024-04-24 15:30:49 -04001071 true
Joey Armstronge8560da2023-04-26 15:44:45 -04001072 else
Joey Armstrong4d612a92024-04-24 15:30:49 -04001073 false
Joey Armstronge8560da2023-04-26 15:44:45 -04001074 fi
1075
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001076 return
1077}
1078
Joey Armstrongaf577ab2022-12-15 14:43:33 -05001079##----------------##
1080##---] MAIN [---##
1081##----------------##
Joey Armstrong4d612a92024-04-24 15:30:49 -04001082bannerEL 'ENTER'
Joey Armstrong1962bcf2023-01-27 13:53:18 -05001083iam="${0##*/}"
1084
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001085full_banner
Eric Ball863379b2024-10-10 10:34:55 -07001086if [[ $(type -t "parse_args" 2>/dev/null) == "function" ]]; then
1087 parse_args "$@"
1088fi
Joey Armstrongd99e3d22023-01-29 12:35:43 -05001089init
1090install_gh_binary
Joey Armstrong7f382ef2023-01-25 12:00:08 -05001091
Joey Armstrong01e4edb2023-08-18 12:50:32 -04001092do_login "$*"
Zack Williams27cd3e52018-09-18 16:44:50 -07001093
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001094release_path='/dev/null'
1095get_release_path release_path
1096declare -p release_path
Zack Williams27cd3e52018-09-18 16:44:50 -07001097
Joey Armstrong4d612a92024-04-24 15:30:49 -04001098pushd "$release_path" || { error "pushd failed: dir is [$release_path]"; }
Zack Williams27cd3e52018-09-18 16:44:50 -07001099
Joey Armstrong4d612a92024-04-24 15:30:49 -04001100# legacy: getGitVersion "$GERRIT_PROJECT" GIT_VERSION
1101getGitVersion GIT_VERSION
1102getReleaseDescription RELEASE_DESCRIPTION
1103if [[ ! -v release_notes ]]; then
1104 func_echo "Generating release notes from RELEASE_DESCRIPTION"
1105 declare -g release_notes="$scratch/release.notes"
1106 echo "$RELEASE_DESCRIPTION" > "$release_notes"
1107fi
1108cat "$release_notes"
Zack Williams27cd3e52018-09-18 16:44:50 -07001109
Joey Armstrong4d612a92024-04-24 15:30:49 -04001110cat <<EOM
Joey Armstrongeef8c2c2023-03-27 17:27:43 -04001111
1112** -----------------------------------------------------------------------
1113** GIT VERSION: $(declare -p GIT_VERSION)
1114** RELEASE_DESCRIPTION: $(declare -p RELEASE_DESCRIPTION)
1115** RELEASE_TARGETS: $(declare -p RELEASE_TARGETS)
Joey Armstrong43c48422023-04-03 10:17:32 -04001116** -----------------------------------------------------------------------
Joey Armstrong4d612a92024-04-24 15:30:49 -04001117** URL: https://github.com/opencord/{repo}/releases
Joey Armstronge8560da2023-04-26 15:44:45 -04001118** -----------------------------------------------------------------------
Joey Armstrong43c48422023-04-03 10:17:32 -04001119** Running: make ${RELEASE_TARGETS}
Joey Armstrongeef8c2c2023-03-27 17:27:43 -04001120** -----------------------------------------------------------------------
1121EOM
Joey Armstrong50f6e0b2023-01-24 14:14:08 -05001122
Joey Armstrong4d612a92024-04-24 15:30:49 -04001123# build the release, can be multiple space separated targets
1124# -----------------------------------------------------------------------
1125# % go build command-line-arguments:
1126# copying /tmp/go-build4212845548/b001/exe/a.out:
1127# open release/voltctl-1.8.25-linux-amd64: permission denied
1128# missing: docker run mkdir
1129# -----------------------------------------------------------------------
1130# shellcheck disable=SC2086
1131make "$RELEASE_TARGETS"
1132copyToRelease
1133
1134cat <<EOM
Joey Armstrong1962bcf2023-01-27 13:53:18 -05001135
1136** -----------------------------------------------------------------------
1137** Create the release:
1138** 1) Create initial github release with download area.
1139** 2) Generate checksum.SHA256 for all released files.
1140** 3) Upload files to complete the release.
Joey Armstrong75a0d932023-03-28 08:59:54 -04001141** 4) Display released info from github
Joey Armstrong1962bcf2023-01-27 13:53:18 -05001142** -----------------------------------------------------------------------
Joey Armstrong26707f02023-01-26 12:41:12 -05001143EOM
1144
Joey Armstrong4d612a92024-04-24 15:30:49 -04001145showReleases
1146release_staging
Joey Armstronge8560da2023-04-26 15:44:45 -04001147
Joey Armstrong4d612a92024-04-24 15:30:49 -04001148# Useful to display but --draft images use a non-standard subdir identifier.
1149# showReleaseUrl
Joey Armstronge8560da2023-04-26 15:44:45 -04001150
Joey Armstrong4d612a92024-04-24 15:30:49 -04001151popd || { error "pushd failed: dir is [$release_path]"; }
Joey Armstrong28eddda2023-01-10 03:09:34 -05001152
1153# [SEE ALSO]
1154# -----------------------------------------------------------------------
1155# https://www.shellcheck.net/wiki/SC2236
Joey Armstrong26707f02023-01-26 12:41:12 -05001156# https://docs.github.com/en/rest/releases/releases?apiVersion=2022-11-28#create-a-release
Joey Armstrong28eddda2023-01-10 03:09:34 -05001157# -----------------------------------------------------------------------
Joey Armstrongf085d872023-01-28 17:52:29 -05001158# https://cli.github.com/manual/gh_help_reference
1159# https://cli.github.com/manual/gh_release
1160# -----------------------------------------------------------------------
Joey Armstrong28eddda2023-01-10 03:09:34 -05001161
1162# [EOF]