blob: a8d9f4c3cbf7c4d1d83a85b1c05efd80074c1371 [file] [log] [blame]
Zack Williams27cd3e52018-09-18 16:44:50 -07001#!/usr/bin/env bash
Joey Armstrong28eddda2023-01-10 03:09:34 -05002# -----------------------------------------------------------------------
3# Copyright 2018-2023 Open Networking Foundation (ONF) and the ONF 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#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
Joey Armstrong28eddda2023-01-10 03:09:34 -050016#
Zack Williams27cd3e52018-09-18 16:44:50 -070017# github-release.sh
18# builds (with make) and uploads release artifacts (binaries, etc.) to github
19# given a tag also create checksums files and release notes from the commit
20# message
Joey Armstrong28eddda2023-01-10 03:09:34 -050021# -----------------------------------------------------------------------
Zack Williams27cd3e52018-09-18 16:44:50 -070022
Joey Armstrong2097d3e2023-03-26 10:32:03 -040023set -euo pipefail
24
25##-------------------##
26##---] GLOBALS [---##
27##-------------------##
28declare -g WORKSPACE
29declare -g GERRIT_PROJECT
Joey Armstrong78cecc52023-04-03 11:39:11 -040030declare -g __githost=github.com
Joey Armstrong43c48422023-04-03 10:17:32 -040031# export DEBUG=1
Joey Armstrong2097d3e2023-03-26 10:32:03 -040032
33## -----------------------------------------------------------------------
34## Uncomment to activate
35## -----------------------------------------------------------------------
Joey Armstronge8560da2023-04-26 15:44:45 -040036# Debug arguments
37# declare -i -g argv_gen_version=1
38# declare -i -g draft_release=1
Joey Armstrong41923cc2023-01-30 14:38:16 -050039
Joey Armstrong2097d3e2023-03-26 10:32:03 -040040declare -a -g ARGV=() # Capture args to minimize globals and arg passing
41[[ $# -gt 0 ]] && ARGV=("$@")
Joey Armstrongd99e3d22023-01-29 12:35:43 -050042
43declare -g scratch # temp workspace for downloads
Joey Armstrongd99e3d22023-01-29 12:35:43 -050044
Joey Armstronge8560da2023-04-26 15:44:45 -040045declare -g SCRIPT_VERSION='1.4' # git changeset needed
Joey Armstrong26707f02023-01-26 12:41:12 -050046
Joey Armstrong7f382ef2023-01-25 12:00:08 -050047##--------------------##
48##---] INCLUDES [---##
Joey Armstrong1962bcf2023-01-27 13:53:18 -050049##--------------------#
Joey Armstrong2097d3e2023-03-26 10:32:03 -040050
51## -----------------------------------------------------------------------
52## Intent: Register an interrupt handler to display a stack trace on error
53## -----------------------------------------------------------------------
54function errexit()
55{
56 local err=$?
57 set +o xtrace
58 local code="${1:-1}"
59
60 local prefix="${BASH_SOURCE[1]}:${BASH_LINENO[0]}"
61 echo -e "\nOFFENDER: ${prefix}"
62 if [ $# -gt 0 ] && [ "$1" == '--stacktrace-quiet' ]; then
63 code=1
64 else
65 echo "ERROR: '${BASH_COMMAND}' exited with status $err"
66 fi
67
68 # Print out the stack trace described by $function_stack
69 if [ ${#FUNCNAME[@]} -gt 2 ]
70 then
Joey Armstrong43c48422023-04-03 10:17:32 -040071 echo "Call tree:"
72 for ((i=1;i<${#FUNCNAME[@]}-1;i++))
73 do
74 echo " $i: ${BASH_SOURCE[$i+1]}:${BASH_LINENO[$i]} ${FUNCNAME[$i]}(...)"
75 done
Joey Armstrong2097d3e2023-03-26 10:32:03 -040076 fi
77
78 echo "Exiting with status ${code}"
79 echo
80 exit "${code}"
81 # return
82}
83
84# trap ERR to provide an error handler whenever a command exits nonzero
85# this is a more verbose version of set -o errexit
86trap errexit ERR
87
88# setting errtrace allows our ERR trap handler to be propagated to functions,
89# expansions and subshells
90set -o errtrace
Joey Armstrong7f382ef2023-01-25 12:00:08 -050091
92## -----------------------------------------------------------------------
Joey Armstrongd99e3d22023-01-29 12:35:43 -050093## Intent: Cleanup scratch area on exit
94## -----------------------------------------------------------------------
95function sigtrap()
96{
97 ## Prevent mishaps
98 local is_read_only
99 is_read_only="$(declare -p scratch)"
100 if [[ $is_read_only != *"declare -r scratch="* ]]; then
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400101 echo "ERROR: variable scratch is not read-only, cleanup skipped"
102 exit 1
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500103 fi
104
105 if [ -d "$scratch" ]; then
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400106 /bin/rm -fr "$scratch"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500107 fi
108
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400109 do_logout 'shellcheck-SC2119'
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500110 return
111}
112trap sigtrap EXIT
113
114## -----------------------------------------------------------------------
Joey Armstronge8560da2023-04-26 15:44:45 -0400115## Intent: Return a random release version string.
116## -----------------------------------------------------------------------
117## Note: Do not use this function in production. get_version() is
118## intended for local use or debugging $0 from within a jenkins
119## job.
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400120## -----------------------------------------------------------------------
121function get_version()
122{
123 declare -n ref="$1"
124
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400125 declare -a rev=()
126 rev+=("$(( RANDOM % 10 + 1 ))")
127 rev+=("$(( RANDOM % 256 + 1 ))")
Joey Armstrongeef8c2c2023-03-27 17:27:43 -0400128 rev+=("$(( RANDOM % 10000 + 1 ))")
Joey Armstrongfc20ed52023-04-03 19:37:58 -0400129 local ver="v${rev[0]}.${rev[1]}.${rev[2]}"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400130
Joey Armstrong43c48422023-04-03 10:17:32 -0400131 func_echo "VERSION: $ver"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400132 ref="$ver"
133 return
134}
135
136## -----------------------------------------------------------------------
137## Intent: Provide defaults for environment variables
138## -----------------------------------------------------------------------
139function initEnvVars()
140{
141 # when not running under Jenkins, use current dir as workspace and a blank
142 # project name
143 declare -g WORKSPACE=${WORKSPACE:-.}
144 declare -g GERRIT_PROJECT=${GERRIT_PROJECT:-}
Joey Armstrong76026b72023-03-29 12:19:17 -0400145
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400146 # Github organization (or user) this project is published on. Project name should
147 # be the same on both Gerrit and GitHub
Joey Armstrong76026b72023-03-29 12:19:17 -0400148 declare -g GITHUB_ORGANIZATION=${GITHUB_ORGANIZATION:-}
149
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400150 # glob pattern relative to project dir matching release artifacts
151 # ARTIFACT_GLOB=${ARTIFACT_GLOB:-"release/*"} # stat -- release/* not found, literal string (?)
152 declare -g ARTIFACT_GLOB=${ARTIFACT_GLOB:-"release/."}
Joey Armstrong76026b72023-03-29 12:19:17 -0400153
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400154 # Use "release" as the default makefile target, can be a space separated list
155 declare -g RELEASE_TARGETS=${RELEASE_TARGETS:-release}
Joey Armstrong76026b72023-03-29 12:19:17 -0400156
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400157 # Set and handle GOPATH and PATH
158 export GOPATH=${GOPATH:-$WORKSPACE/go}
159 export PATH=$PATH:/usr/lib/go-1.12/bin:/usr/local/go/bin:$GOPATH/bin
160 return
161}
162
163## -----------------------------------------------------------------------
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500164## Intent: Create a scratch area for downloads and transient tools
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400165## temp directory will be automatically removed upon exit.
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500166## -----------------------------------------------------------------------
167function init()
168{
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500169 local pkgbase="${0##*/}" # basename
170 local pkgname="${pkgbase%.*}"
171
Joey Armstrongad7b1e02023-03-27 11:55:48 -0400172 # initEnvVars # moved to full_banner()
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400173
174 ## Create a temp directory for auto-cleanup
175 declare -g scratch
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500176 scratch="$(mktemp -d -t "${pkgname}.XXXXXXXXXX")"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400177 readonly scratch
178 declare -p scratch
179
Joey Armstrong43c48422023-04-03 10:17:32 -0400180 ## prime the stream: cache answers
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400181 local work
182 get_release_dir work
183 declare -p work
Joey Armstrong43c48422023-04-03 10:17:32 -0400184
Joey Armstronge8560da2023-04-26 15:44:45 -0400185 local git_version
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400186 getGitVersion git_version
187 func_echo "$(declare -p git_version)"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400188 return
189}
190
191## -----------------------------------------------------------------------
192## Intent: Verbose output for logging
193## -----------------------------------------------------------------------
194function banner()
195{
196 local iam="${0##*/}"
197 cat <<EOB
198
199** -----------------------------------------------------------------------
200** ${iam}::${FUNCNAME[1]}: $*
201** -----------------------------------------------------------------------
202EOB
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500203}
204
205## -----------------------------------------------------------------------
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500206## Intent: Output a log banner to identify the running script/version.
207## -----------------------------------------------------------------------
208## TODO:
209## o SCRIPT_VERSION => git changeset for repo:ci-managment
210## o echo "library version: ${env."library.libName.version"}"
211# -----------------------------------------------------------------------
212# 14:18:38 > git fetch --no-tags --progress -- https://gerrit.opencord.org/ci-management.git +refs/heads/*:refs/remotes/origin/* # timeout=10
213# 14:18:39 Checking out Revision 50f6e0b97f449b32d32ec0e02d59642000351847 (master)
214# -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400215function full_banner()
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500216{
Joey Armstrongad7b1e02023-03-27 11:55:48 -0400217 local iam="${0##*/}"
218
219 initEnvVars # set defaults
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500220
221cat <<EOH
222
223** -----------------------------------------------------------------------
224** IAM: ${iam} :: ${FUNCNAME[0]}
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400225** ARGV: ${ARGV[@]}
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500226** PWD: $(/bin/pwd)
227** NOW: $(date '+%Y/%m/%d %H:%M:%S')
228** VER: ${SCRIPT_VERSION:-'unknown'}
229** -----------------------------------------------------------------------
Joey Armstrongad7b1e02023-03-27 11:55:48 -0400230** GERRIT_PROJECT: $(declare -p GERRIT_PROJECT)
231** GITHUB_ORGANIZATION: $(declare -p GITHUB_ORGANIZATION)
232** RELEASE_TARGETS: $(declare -p RELEASE_TARGETS)
233** GOPATH: $(declare -p GOPATH)
234** -----------------------------------------------------------------------
235** PATH += /usr/lib/go-1.12/bin:/usr/local/go/bin:GOPATH/bin
236** -----------------------------------------------------------------------
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500237EOH
238
239 return
240}
241
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500242## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400243## Intent: Display a message with 'iam' identifier for logging
Joey Armstrong50f6e0b2023-01-24 14:14:08 -0500244## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400245function func_echo()
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500246{
247 local iam="${0##*/}"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400248 echo "** ${iam} :: ${FUNCNAME[1]}: $*"
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500249 return
250}
251
252## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400253## Intent: Display an error message then exit with status.
254## -----------------------------------------------------------------------
255function error()
256{
257 local iam="${0##*/}"
258 echo "ERROR ${iam} :: ${FUNCNAME[1]}: $*"
259 exit 1
260}
261
262## -----------------------------------------------------------------------
263## Intent: Verify sandbox/build is versioned for release.
264## -----------------------------------------------------------------------
265function getGitVersion()
266{
267 declare -n varname="$1"; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400268
Joey Armstrong43c48422023-04-03 10:17:32 -0400269 local __ver # use prefix('__') to protect callers variable name
270 if [[ -v cached_getGitVersion ]]; then
271 __ver="$cached_getGitVersion"
272 varname="$__ver"
273 return
Joey Armstrong39fac652023-03-27 18:50:43 -0400274
Joey Armstrong43c48422023-04-03 10:17:32 -0400275 elif [[ -v argv_gen_version ]]; then
276 get_version __ver
277
278 elif [[ -v WORKSPACE ]] && [[ -v GITHUB_TOKEN ]]; then # i_am_jenkins
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400279 local path="$GERRIT_PROJECT"
280 pushd "$path" || error "pushd GERRIT_PROJECT= failed $(declare -p path)"
Joey Armstrong43c48422023-04-03 10:17:32 -0400281 __ver="$(git tag -l --points-at HEAD)"
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400282 popd || error "popd GERRIT_PROJECT= failed"
Joey Armstrong43c48422023-04-03 10:17:32 -0400283
284 elif [[ -v argv_version_file ]]; then # local debug
285 [[ ! -f VERSION ]] && error "./VERSION file not found"
286 readarray -t tmp < <(sed -e 's/[[:blank:]]//g' 'VERSION')
287 __ver="v${tmp[0]}"
288
289 else
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400290 cd ..
291 local path="$GERRIT_PROJECT"
292 pushd "$path" || error "pushd GERRIT_PROJECT= failed $(declare -p path)"
Joey Armstrong43c48422023-04-03 10:17:32 -0400293 __ver="$(git tag -l --points-at HEAD)"
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400294 popd || error "popd GERRIT_PROJECT= failed"
Joey Armstrong43c48422023-04-03 10:17:32 -0400295 fi
Joey Armstrong76026b72023-03-29 12:19:17 -0400296
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400297 # ------------------------------------------------------
298 # match bare versions or v-prefixed golang style version
299 # Critical failure for new/yet-to-be-released repo ?
300 # ------------------------------------------------------
Joey Armstrong43c48422023-04-03 10:17:32 -0400301 if [[ "$__ver" =~ ^v?([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then
Joey Armstrongfc20ed52023-04-03 19:37:58 -0400302 echo "git has a SemVer released version tag: '$__ver'"
Joey Armstrong43c48422023-04-03 10:17:32 -0400303 echo "Building artifacts for GitHub release."
304
305 elif [[ "$__ver" =~ ^v?([0-9]+)\.([0-9]+)\.([0-9]+)-dev([0-9]+)$ ]]; then
Joey Armstronge8560da2023-04-26 15:44:45 -0400306 # v1.2.3-dev (*-dev*) is an implicit draft release.
Joey Armstrong43c48422023-04-03 10:17:32 -0400307 declare -i -g draft_release=1
308 echo "Detected --draft SemVer release version tag [$__ver]"
309 echo "Building artifacts for GitHub release."
310
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400311 else
Joey Armstrong43c48422023-04-03 10:17:32 -0400312 error "No SemVer released version tag found, exiting..."
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400313 fi
314
Joey Armstrong43c48422023-04-03 10:17:32 -0400315 ## Cache value for subsequent calls.
316 ## readonly is a guarantee assignment happens once.
317 declare -g cached_getGitVersion="$__ver"
318 readonly cached_getGitVersion;
319
320 varname="$__ver"
321 func_echo "Version is [$__ver] from $(/bin/pwd)"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400322 return
323}
324
325## -----------------------------------------------------------------------
326## Intent:
327## Note: Release description is sanitized version of the log message
328## -----------------------------------------------------------------------
329function getReleaseDescription()
330{
331 declare -n varname="$1"; shift
332
333 local msg
334 msg="$(git log -1 --pretty=%B)"
335
336 local val
337 val="$(tr -dc "[:alnum:]\n\r\.\[\]\:\-\\\/\`\' " <<< "$msg")"
338
339 [[ ${#val} -eq 0 ]] && error "Release Description is empty ($msg)"
340 varname="$val"
341 return
342}
343
344## -----------------------------------------------------------------------
345## Intent: Retrieve value of the release temp directory.
346## -----------------------------------------------------------------------
347## Note: Limit use of globals so logic can be isolated in function calls.
348## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400349function get_release_dir()
350{
351 declare -n varname=$1; shift
352
353 # Temporary staging directory to copy artifacts to
354 varname="$scratch/release"
355 mkdir -p "$varname"
356 return
357}
358
359## -----------------------------------------------------------------------
360## Intent: Retrieve github server hostname
361## -----------------------------------------------------------------------
362function get_gh_hostname()
363{
364 declare -n varname=$1; shift
365 varname+=('--hostname' "${__githost}")
366 return
367}
368
369## -----------------------------------------------------------------------
370## Intent: Retrieve repository organizaiton name
371## -----------------------------------------------------------------------
372function get_gh_repo_org()
373{
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400374 declare -n ref=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400375 declare -g __repo_org
Joey Armstrong76026b72023-03-29 12:19:17 -0400376
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400377 local org
378 if [[ -v __repo_org ]]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400379 org="$__repo_org"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400380 elif [[ ! -v GITHUB_ORGANIZATION ]]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400381 error "--repo-org or GITHUB_ORGANIZATION= are required"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400382 else
Joey Armstrong43c48422023-04-03 10:17:32 -0400383 org="${GITHUB_ORGANIZATION}"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400384 fi
385
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400386 ref="$org"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400387 return
388}
389
390## -----------------------------------------------------------------------
391## Intent: Retrieve repository organizaiton name
392## -----------------------------------------------------------------------
393function get_gh_repo_name()
394{
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400395 declare -n ref=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400396 declare -g __repo_name
Joey Armstrong76026b72023-03-29 12:19:17 -0400397
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400398 local name
399 if [[ -v __repo_name ]]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400400 name="$__repo_name"
Joey Armstrong39fac652023-03-27 18:50:43 -0400401 elif [[ ! -v GERRIT_PROJECT ]]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400402 error "--repo-name or GERRIT_PROJECT= are required"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400403 else
Joey Armstrong43c48422023-04-03 10:17:32 -0400404 name="${GERRIT_PROJECT}"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400405 fi
406
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400407 ref="$name"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400408 return
409}
410
411## -----------------------------------------------------------------------
412## Intent: Return path for the gh release query
413## -----------------------------------------------------------------------
414function get_gh_releases()
415{
416 declare -n ref="$1"
417
418 local repo_org
419 get_gh_repo_org repo_org
420
421 local repo_name
422 get_gh_repo_name repo_name
423
424 ref="repos/${repo_org}/${repo_name}/releases"
425 func_echo "ref=$ref"
426 return
427}
428
429## -----------------------------------------------------------------------
430## Intent: Retrieve repository path argument
431## -----------------------------------------------------------------------
432function get_argv_repo()
433{
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400434 declare -n ref=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400435
436 local repo_org
437 get_gh_repo_org repo_org
Joey Armstrong76026b72023-03-29 12:19:17 -0400438
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400439 local repo_name
Joey Armstrong76026b72023-03-29 12:19:17 -0400440 get_gh_repo_name repo_name
441
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400442 ref="${repo_org}/${repo_name}"
443 # func_echo "ref=$(declare -p ref)"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400444 return
445}
446
447## -----------------------------------------------------------------------
448## Intent: Retrieve release name string "project - version"
449## -----------------------------------------------------------------------
450function get_argv_name()
451{
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400452 declare -n ref=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400453
454 local repo_name
455 get_gh_repo_name repo_name
Joey Armstrong43c48422023-04-03 10:17:32 -0400456
457 local repo_ver
458 getGitVersion repo_ver
459
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400460 # ref="${repo_name} - $GIT_VERSION"
461 ref="${repo_name} - ${repo_ver}"
462 func_echo "ref=$(declare -p ref)"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400463 return
464}
465
466## -----------------------------------------------------------------------
467## Intent: Retrieve tag version
468## -----------------------------------------------------------------------
469function get_argv_tag()
470{
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400471 declare -n ref=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400472
473 # cached_argv_tag='v3.41.3204'
474 if [[ ! -v cached_argv_tag ]]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400475 declare -g cached_argv_tag
476 if [[ -v GIT_VERSION ]]; then # hello jenkins
477 cached_argv_tag="$GIT_VERSION"
478 fi
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400479 fi
480
481 [[ ${#cached_argv_tag} -eq 0 ]] && error "Unable to determine GIT_VERSION="
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400482 ref="$cached_argv_tag"
483 func_echo "ref=$(declare -p ref)"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400484 return
485}
486
487## -----------------------------------------------------------------------
488## Intent:
489## -----------------------------------------------------------------------
490# To support golang projects that require GOPATH to be set and code checked out there
491# If $DEST_GOPATH is not an empty string:
492# - create GOPATH within WORKSPACE, and destination directory within
493# - set PATH to include $GOPATH/bin and the system go binaries
494# - move project from $WORKSPACE/$GERRIT_PROJECT to new location in $GOPATH
495# - start release process within that directory
496## -----------------------------------------------------------------------
497function get_release_path()
498{
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400499 declare -n ref=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400500
Joey Armstronge8560da2023-04-26 15:44:45 -0400501 # shellcheck disable=SC2128
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400502 local varpath="$ref"
Joey Armstronge8560da2023-04-26 15:44:45 -0400503
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400504 DEST_GOPATH=${DEST_GOPATH:-}
505 if [ -n "$DEST_GOPATH" ]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400506 mkdir -p "$GOPATH/src/$DEST_GOPATH"
Joey Armstronge8560da2023-04-26 15:44:45 -0400507 varpath="$GOPATH/src/$DEST_GOPATH/$GERRIT_PROJECT"
508 mv "$WORKSPACE/$GERRIT_PROJECT" "$varpath"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400509 else
Joey Armstronge8560da2023-04-26 15:44:45 -0400510 varpath="$WORKSPACE/$GERRIT_PROJECT"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400511 fi
512
Joey Armstrong43c48422023-04-03 10:17:32 -0400513 ## Verify pwd is OK
514 for path in \
Joey Armstronge8560da2023-04-26 15:44:45 -0400515 "${varpath}/Makefile"\
516 "${varpath}/makefile"\
Joey Armstrong43c48422023-04-03 10:17:32 -0400517 "__ERROR__"\
518 ; do
519 case "$path" in
Joey Armstronge8560da2023-04-26 15:44:45 -0400520 __ERROR__) error "Makefile not found at ${varpath} !" ;;
Joey Armstrong43c48422023-04-03 10:17:32 -0400521 *) [[ -f "$path" ]] && break ;;
522 esac
523 done
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400524
525 return
526}
527
528## -----------------------------------------------------------------------
529## Intent: Display future enhancements
530## -----------------------------------------------------------------------
531function todo()
532{
533 local iam="${0##*/}"
534
535cat <<EOT
536
537** -----------------------------------------------------------------------
538** IAM: ${iam} :: ${FUNCNAME[0]}
539** -----------------------------------------------------------------------
540 o get_release_path()
541 - refactor redundant paths into local vars.
542 - see comments, do we have a one-off failure condition ?
Joey Armstrongad7b1e02023-03-27 11:55:48 -0400543 o PATH += golang appended 3 times, release needs a single, reliable answer.
544 o do_login, do_logout and api calls do not use the my_gh wrapper:
545 - Add a lookup function to cache and retrieve path to downloaded gh command.
Joey Armstrong76026b72023-03-29 12:19:17 -0400546
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400547EOT
548
549 return
550}
551
552## -----------------------------------------------------------------------
Joey Armstrong76026b72023-03-29 12:19:17 -0400553## Intent: Verify a directory contains content for release.
554## -----------------------------------------------------------------------
555## Given:
556## scalar Path to release/ directory
557## ref Results returned through this indirect var.
558## -----------------------------------------------------------------------
559## Usage:
560## declare -a artifacts=()
561## get_artifacts '/foo/bar/tans' artifacts
562## declare -p artifacts
563## -----------------------------------------------------------------------
564function get_artifacts()
565{
566 local dir="$1" ; shift
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400567 declare -n refA=$1 ; shift
Joey Armstrong76026b72023-03-29 12:19:17 -0400568
Joey Armstrong55fe80e2023-04-05 18:32:14 -0400569 # Glob available files, exclude checksums
570 readarray -t __artifacts < <(find "$dir" -mindepth 1 ! -type d \
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400571 | grep -iv -e 'sum256' -e 'checksum')
572 # func_echo "$(declare -p __artifacts)"
Joey Armstrong76026b72023-03-29 12:19:17 -0400573
574 # -----------------------------------------------------------------------
575 # Verify count>0 to inhibit source-only release
576 # Problem children:
577 # o build or make release failures.
578 # o docker container filesystem mount problem (~volume)
579 # -----------------------------------------------------------------------
580 [[ ${#__artifacts[@]} -eq 0 ]] \
Joey Armstrong43c48422023-04-03 10:17:32 -0400581 && error "Artifact dir is empty: $(declare -p dir)"
Joey Armstrong76026b72023-03-29 12:19:17 -0400582
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400583 # shellcheck disable=SC2034
584 refA=("${__artifacts[@]}")
Joey Armstrong76026b72023-03-29 12:19:17 -0400585 return
586}
587
588## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400589## Intent: Copy files from the build directory into the release staging
590## directory for publishing to github releases/ endpoint.
Joey Armstrong26707f02023-01-26 12:41:12 -0500591## -----------------------------------------------------------------------
592function copyToRelease()
593{
Joey Armstrong76026b72023-03-29 12:19:17 -0400594 banner ''
Joey Armstrong26707f02023-01-26 12:41:12 -0500595
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400596 local artifact_glob="${ARTIFACT_GLOB%/*}"
597 func_echo "$(declare -p artifact_glob)"
598
599 local work
600 get_release_dir work
Joey Armstrong76026b72023-03-29 12:19:17 -0400601 func_echo "Artifact dir: $(declare -p work)"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400602
Joey Armstrong76026b72023-03-29 12:19:17 -0400603 ## Verify release content is available
604 declare -a artifacts=()
Joey Armstrong43c48422023-04-03 10:17:32 -0400605 # get_artifacts "$work" artifacts
606 get_artifacts "$artifact_glob" artifacts
607 func_echo "Artifacts in copy_from: $(declare -p artifacts)"
Joey Armstrong472bfba2023-03-27 18:12:28 -0400608
Joey Armstrong26707f02023-01-26 12:41:12 -0500609 # Copy artifacts into the release temp dir
610 # shellcheck disable=SC2086
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400611 echo "rsync -rv --checksum \"$artifact_glob/.\" \"$work/.\""
612 rsync -rv --checksum "$artifact_glob/." "$work/."
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500613
Joey Armstrong43c48422023-04-03 10:17:32 -0400614 get_artifacts "$work" artifacts
615 func_echo "Artifacts in copy_to: $(declare -p artifacts)"
616
Joey Armstrong26707f02023-01-26 12:41:12 -0500617 return
618}
619
620## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400621## https://docs.github.com/en/rest/releases?apiVersion=2022-11-28
622 # https://cli.github.com/manual/gh_release_create
623 # --target <branch> or commit SHA
624 # --title
625 # --generate-notes
626 # --release-notes (notes file)
627 # --release
628 # release create dist/*.tgz
629 # --discussion-category "General"
Joey Armstrongf085d872023-01-28 17:52:29 -0500630## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400631# https://cli.github.com/manual/gh_release_create
632## -----------------------------------------------------------------------
633function gh_release_create()
Joey Armstrong38bfeea2023-02-06 18:01:29 -0500634{
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400635 banner ''
Joey Armstrong38bfeea2023-02-06 18:01:29 -0500636
Joey Armstrong43c48422023-04-03 10:17:32 -0400637 local version
638 getGitVersion version
Joey Armstrong38bfeea2023-02-06 18:01:29 -0500639
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400640 local work
641 get_release_dir work
642
Joey Armstrong43c48422023-04-03 10:17:32 -0400643 declare -a args=()
644 args+=('--host-repo')
645 args+=('--title')
646 if [[ -v draft_release ]]; then
647 args+=('--draft')
648 else
649 args+=('--discussion-category' 'Announcements')
650 fi
651
Joey Armstrong55fe80e2023-04-05 18:32:14 -0400652 if [[ -v release_notes ]]; then
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400653 args+=('--notes-file' "$release_notes")
Joey Armstrong55fe80e2023-04-05 18:32:14 -0400654 fi
Joey Armstrong43c48422023-04-03 10:17:32 -0400655
Joey Armstrong78cecc52023-04-03 11:39:11 -0400656 pushd "$work/.." >/dev/null
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400657 # func_echo "WORK=$work"
Joey Armstrong78cecc52023-04-03 11:39:11 -0400658 readarray -t payload < <(find 'release' -maxdepth 4 ! -type d -print)
Joey Armstrong76026b72023-03-29 12:19:17 -0400659
Joey Armstronge8560da2023-04-26 15:44:45 -0400660 func_echo "$gh_cmd release create ${version} ${args[*]} ${payload[*]}"
Joey Armstrong43c48422023-04-03 10:17:32 -0400661
662 if [[ -v dry_run ]]; then
663 echo "[SKIP] dry run"
664 else
Joey Armstronge8560da2023-04-26 15:44:45 -0400665 func_echo "my_gh release create '$version' ${args[*]} ${payload[*]}"
Joey Armstrong4dbe7002023-04-04 12:47:38 -0400666 my_gh 'release' 'create' "$version" "${args[@]}" "${payload[@]}"
Joey Armstrong43c48422023-04-03 10:17:32 -0400667 fi
Joey Armstrong76026b72023-03-29 12:19:17 -0400668 popd >/dev/null
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400669
Joey Armstrong38bfeea2023-02-06 18:01:29 -0500670 return
671}
672
673## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400674## Intent: Authenticate credentials for a github gh session
Joey Armstrong38bfeea2023-02-06 18:01:29 -0500675## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400676## NOTE: my_gh currently unused due to --with-token < "$pac"
677## -----------------------------------------------------------------------
678function do_login()
Joey Armstrongf085d872023-01-28 17:52:29 -0500679{
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400680 # shellcheck disable=SC2120
681 # shellcheck disable=SC2034
682 local unused="$1"; shift
683
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400684 declare -g pac
Joey Armstrong485081f2023-03-27 13:34:08 -0400685 declare -a login_args=()
686 [[ $# -gt 0 ]] && login_args+=("$@")
Joey Armstrongf085d872023-01-28 17:52:29 -0500687
Joey Armstrong485081f2023-03-27 13:34:08 -0400688 # https://github.com/cli/cli/issues/2922#issuecomment-775027762
Joey Armstrong75a0d932023-03-28 08:59:54 -0400689 # (sigh) why not quietly return VS forcing a special logic path
Joey Armstronge6e18eb2023-03-27 14:19:21 -0400690 # [[ -v WORKSPACE ]] && [[ -v GITHUB_TOKEN ]] && return
691
692# 12:58:36 ** -----------------------------------------------------------------------
693# 12:58:36 ** jenkins582353203049151829.sh::do_login: --hostname github.com
694# 12:58:36 ** --------------------------------------------------------------------# ---
695# 12:58:36 ** jenkins582353203049151829.sh :: do_login: Detected ENV{GITHUB_TOKEN}=
696# 12:58:36 The value of the GITHUB_TOKEN environment variable is being used for authentication.
697# 12:58:36 To have GitHub CLI store credentials instead, first clear the value from the environment.
Joey Armstrong43c48422023-04-03 10:17:32 -0400698# -----------------------------------------------------------------------
Joey Armstrong76026b72023-03-29 12:19:17 -0400699
Joey Armstrong485081f2023-03-27 13:34:08 -0400700 get_gh_hostname login_args
Joey Armstrongf085d872023-01-28 17:52:29 -0500701
Joey Armstrong485081f2023-03-27 13:34:08 -0400702 banner "${login_args[@]}"
Joey Armstrong78cecc52023-04-03 11:39:11 -0400703 func_echo "$(declare -p WORKSPACE)"
Joey Armstrongf085d872023-01-28 17:52:29 -0500704
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400705 ## Read from disk is safer than export GITHUB_TOKEN=
706 if [[ -v pac ]] && [[ ${#pac} -gt 0 ]]; then # interactive/debugging
Joey Armstrong43c48422023-04-03 10:17:32 -0400707 [ ! -f "$pac" ] && error "PAC token file $pac does not exist"
Joey Armstronge8560da2023-04-26 15:44:45 -0400708 func_echo "$gh_cmd auth login ${login_args[*]} --with-token < $pac"
Joey Armstrong485081f2023-03-27 13:34:08 -0400709 "$gh_cmd" auth login "${login_args[@]}" --with-token < "$pac"
Joey Armstrongf085d872023-01-28 17:52:29 -0500710
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400711 elif [[ ! -v GITHUB_TOKEN ]]; then
712 error "--token [t] or GITHUB_TOKEN= are required"
Joey Armstrong41923cc2023-01-30 14:38:16 -0500713
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400714 else # jenkins
Joey Armstronge8560da2023-04-26 15:44:45 -0400715 func_echo "$gh_cmd auth login ${login_args[*]} (ie: jenkins)"
Joey Armstrong733ea9f2023-04-03 21:19:46 -0400716
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400717 # https://github.com/cli/cli/issues/2922#issuecomment-775027762
718 # When using GITHUB_TOKEN, there is no need to even run gh auth login
Joey Armstrong733ea9f2023-04-03 21:19:46 -0400719 # "$gh_cmd" auth login "${login_args[@]}"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400720 fi
Joey Armstrongf085d872023-01-28 17:52:29 -0500721
Joey Armstrong43c48422023-04-03 10:17:32 -0400722 declare -i -g active_login=1 # signal logout needed
Joey Armstrongf085d872023-01-28 17:52:29 -0500723
Joey Armstrongf085d872023-01-28 17:52:29 -0500724 return
725}
726
727## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400728## Intent: Destroy credentials/gh session authenticated by do_login
729## -----------------------------------------------------------------------
730## NOTE: my_gh currently unused due to "<<< 'y'"
731## -----------------------------------------------------------------------
732function do_logout()
733{
734 declare -i -g active_login
735 [[ ! -v active_login ]] && return
736
Joey Armstrong78cecc52023-04-03 11:39:11 -0400737 declare -a logout_args=()
738 [[ $# -gt 0 ]] && logout_args+=("$@")
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400739
Joey Armstrong78cecc52023-04-03 11:39:11 -0400740 get_gh_hostname logout_args
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400741
Joey Armstrong78cecc52023-04-03 11:39:11 -0400742 banner "${logout_args[@]}"
Joey Armstronge8560da2023-04-26 15:44:45 -0400743 func_echo "$gh_cmd auth logout ${logout_args[*]} <<< 'Y'"
Joey Armstrong78cecc52023-04-03 11:39:11 -0400744 "$gh_cmd" auth logout "${logout_args[@]}" <<< 'Y'
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400745
746 unset active_login
747 return
748}
749
750## -----------------------------------------------------------------------
751## Intent: Query for repository version strings
752## -----------------------------------------------------------------------
753function get_releases()
754{
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400755 declare -n refA="$1"; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400756
757 banner ""
758 pushd "$scratch" >/dev/null
Joey Armstrong76026b72023-03-29 12:19:17 -0400759
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400760 # gh api repos/{owner}/{repo}/releases
761 local releases_uri
762 get_gh_releases releases_uri
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400763 # declare -p releases_uri
Joey Armstrong76026b72023-03-29 12:19:17 -0400764
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400765 refA=()
Joey Armstronge8560da2023-04-26 15:44:45 -0400766 "$gh_cmd" api "$releases_uri" | jq . > 'release.raw'
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400767 readarray -t __tmp < <(jq '.[] | "\(.tag_name)"' 'release.raw')
768
769 local release
770 for release in "${__tmp[@]}";
771 do
Joey Armstrong43c48422023-04-03 10:17:32 -0400772 release="${release//\"/}"
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400773 refA+=("$release")
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400774 done
775
776 popd >/dev/null
777 return
778}
779
780## -----------------------------------------------------------------------
781## Intent: Display repository query strings.
782## Indirect: verify authentication and API
783## -----------------------------------------------------------------------
784function showReleases()
785{
Joey Armstrong43c48422023-04-03 10:17:32 -0400786 declare -a raw=()
787 get_releases raw
788
789 ## Sort for display, we may need to prune volume later on
Joey Armstronge8560da2023-04-26 15:44:45 -0400790 readarray -t releases < <(sort -nr <<<"${raw[*]}")
791 # IFS=$'\n' releases=($(sort -nr <<<"${raw[*]}"))
792 # unset IFS
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400793
794 local release
795 for release in "${releases[@]}";
796 do
Joey Armstrong43c48422023-04-03 10:17:32 -0400797 func_echo "$release"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400798 done
799 return
800}
801
802## -----------------------------------------------------------------------
803## Intent: Install the gh command line tool locally
Joey Armstrong26707f02023-01-26 12:41:12 -0500804## -----------------------------------------------------------------------
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500805function install_gh_binary()
Joey Armstrong26707f02023-01-26 12:41:12 -0500806{
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400807 banner
Joey Armstrong26707f02023-01-26 12:41:12 -0500808
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500809 pushd "$scratch"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400810 func_echo "Retrieve latest gh download URLs"
Joey Armstrongf085d872023-01-28 17:52:29 -0500811
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500812 local latest="https://github.com/cli/cli/releases/latest"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400813 local tarball="gh.tar.tgz"
814
Joey Armstrong76026b72023-03-29 12:19:17 -0400815 readarray -t latest < <(\
816 curl --silent -qI "$latest" \
Joey Armstrong43c48422023-04-03 10:17:32 -0400817 | awk -F '/' '/^location/ {print substr($NF, 1, length($NF)-1)}')
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400818 declare -p latest
819 if [ ${#latest[@]} -ne 1 ]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400820 error "Unable to determine latest gh package version"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400821 fi
Joey Armstrong76026b72023-03-29 12:19:17 -0400822
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400823 local VER="${latest[0]}"
824
825 func_echo "Download latest gh binary"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500826 local url="https://github.com/cli/cli/releases/download/${VER}/gh_${VER#v}_linux_amd64.tar.gz"
Joey Armstrong43c48422023-04-03 10:17:32 -0400827 func_echo "wget --quiet --output-document='$tarball' '$url'"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500828 wget --quiet --output-document="$tarball" "$url"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400829
830 func_echo "Unpack tarball"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500831 tar zxf "$tarball"
832
Joey Armstrong78cecc52023-04-03 11:39:11 -0400833 declare -g gh_cmd
834 gh_cmd="$(find "${scratch}" -name 'gh' -print)"
835 #declare -g gh_cmd='/usr/bin/gh'
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500836 readonly gh_cmd
837
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400838 func_echo "Command: ${gh_cmd}"
839 func_echo "Version: $("$gh_cmd" --version)"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500840 popd
841
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400842 return
843}
844
845## -----------------------------------------------------------------------
846## Intent: Danger Will Robinson
847## -----------------------------------------------------------------------
848function releaseDelete()
849{
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400850 declare -n refA=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400851 local version="$1"; shift
Joey Armstrong76026b72023-03-29 12:19:17 -0400852
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400853 banner "${refA[@]}"
854 # declare -a refA=()
855 refA+=('--host-repo')
856 refA+=('--yes')
857 # refA+=('--cleanup-tag')
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400858
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400859 echo
860 echo "==========================================================================="
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400861 my_gh 'release' 'delete' "$version" "${refA[@]}"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400862 echo "==========================================================================="
863 echo
864
865 showReleases
866 return
867}
868
869## -----------------------------------------------------------------------
870## Intent: Copy binaries into temp/release, checksum then publish
871## -----------------------------------------------------------------------
872function release_staging()
873{
874 local release_temp
875 get_release_dir release_temp
876
877 banner ''
878 func_echo "Packaging release files"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400879
Joey Armstrong76026b72023-03-29 12:19:17 -0400880 pushd "$release_temp" >/dev/null \
Joey Armstrong43c48422023-04-03 10:17:32 -0400881 || error "pushd failed: dir is [$release_temp]"
Joey Armstrong76026b72023-03-29 12:19:17 -0400882
883 declare -a to_release=()
884 get_artifacts '.' to_release
885
Joey Armstrong55fe80e2023-04-05 18:32:14 -0400886 if false; then
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400887 for fyl in "${to_release[@]}";
888 do
889 func_echo "sha256sum $fyl > ${fyl}.sha256"
890 sha256sum "$fyl" > "${fyl}.sha256"
891 done
Joey Armstrong55fe80e2023-04-05 18:32:14 -0400892 fi
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400893
894 # Generate and check checksums
Joey Armstrong55fe80e2023-04-05 18:32:14 -0400895 sha256sum -- * | grep -iv -e 'checksum' -e 'sha256' > checksum.SHA256
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400896 sha256sum -c < checksum.SHA256
897
898 echo
899 func_echo "Checksums(checksum.SHA256):"
900 cat checksum.SHA256
901
Joey Armstrong43c48422023-04-03 10:17:32 -0400902 if false; then
Joey Armstrong78cecc52023-04-03 11:39:11 -0400903 # Careful with credentials display
Joey Armstrong43c48422023-04-03 10:17:32 -0400904 get_gh_hostname login_args
Joey Armstronge8560da2023-04-26 15:44:45 -0400905 banner "gh auth status ${login_args[*]}"
Joey Armstrong43c48422023-04-03 10:17:32 -0400906 gh auth status "${login_args[@]}"
907 fi
908
Joey Armstrong76026b72023-03-29 12:19:17 -0400909 gh_release_create # publish
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400910
911 popd >/dev/null || error "pushd failed: dir is [$release_temp]"
912
913 return
914}
915
916## -----------------------------------------------------------------------
Joey Armstrong43c48422023-04-03 10:17:32 -0400917## Intent: Normalize common arguments and access to the gh command.
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400918## o Cache path to the gh command
919## o Construct a gh command line from given args
920## o Command wrapper can provide defaults (--hostname github.com)
921## -----------------------------------------------------------------------
922## Given:
923## scalar Array variable name (declare -n is a reference)
924## Return:
925## ref gh command line passed back to caller
926## Switches:
927## --host Pass default github hostname
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400928## --verbose Enable verbose mode
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400929## --version Display command version
930## @array Remaining arguments passed as command switches.
931## -----------------------------------------------------------------------
932## See Also:
933## o https://cli.github.com/manual
934## -----------------------------------------------------------------------
935function my_gh()
936{
Joey Armstrong73bb2f62023-04-17 19:31:50 -0400937 func_echo "ENTER"
938 set -x
939
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400940 declare -a cmd=()
941 cmd+=("$gh_cmd")
942
943 ## ----------------------
944 ## Construct command line
945 ## ----------------------
946 # shellcheck disable=SC2034
947 declare -A action=() # pending operations
948 declare -a args=() # common gh command line args
949
950 while [ $# -gt 0 ]; do
951 local arg="$1"; shift
Joey Armstrong73bb2f62023-04-17 19:31:50 -0400952 func_echo "function arg is [$arg]"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400953 case "$arg" in
954
Joey Armstrong43c48422023-04-03 10:17:32 -0400955 # Modes
Joey Armstronge8560da2023-04-26 15:44:45 -0400956 -*debug)
957 # shellcheck disable=SC2034
958 declare -i -g debug=1
959 ;;
960 -*verbose) args+=('--verbose') ;;
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400961
Joey Armstrong43c48422023-04-03 10:17:32 -0400962 -*hostname)
963 get_gh_hostname in_args
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400964 ;;
965
Joey Armstrong43c48422023-04-03 10:17:32 -0400966 --host-repo)
967 local val
968 get_argv_repo val
Joey Armstrong76026b72023-03-29 12:19:17 -0400969
Joey Armstrong43c48422023-04-03 10:17:32 -0400970 # --repo <[HOST/]OWNER/REPO>
Joey Armstrong78cecc52023-04-03 11:39:11 -0400971 args+=('--repo' "${__githost}/${val}")
Joey Armstrong43c48422023-04-03 10:17:32 -0400972 ;;
Joey Armstrong76026b72023-03-29 12:19:17 -0400973
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400974# args+=('--repo' 'github.com/opencord/bbsim')
975
Joey Armstrong43c48422023-04-03 10:17:32 -0400976 --repo)
977 local val
978 get_argv_repo val
979 args+=('--repo' "$val")
980 ;;
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400981
Joey Armstrong43c48422023-04-03 10:17:32 -0400982 --tag)
983 local val
984 get_argv_tag val
Joey Armstrong4dbe7002023-04-04 12:47:38 -0400985 args+=("$val") # No switch, pass inline
Joey Armstrong43c48422023-04-03 10:17:32 -0400986 ;;
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400987
Joey Armstrong43c48422023-04-03 10:17:32 -0400988 --title)
989 local val
990 get_argv_name val
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400991 args+=('--title' "'$val'")
Joey Armstrong43c48422023-04-03 10:17:32 -0400992 ;;
Joey Armstrong76026b72023-03-29 12:19:17 -0400993
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400994 *) args+=("$arg") ;;
995 esac
996 done
Joey Armstrong76026b72023-03-29 12:19:17 -0400997
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400998 cmd+=("${args[@]}")
Joey Armstrong73bb2f62023-04-17 19:31:50 -0400999
1000 echo
1001 declare -p cmd
1002
1003 echo
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001004 echo "** Running: ${cmd[*]}"
1005 "${cmd[@]}"
1006 local status=$?
1007
Joey Armstrong73bb2f62023-04-17 19:31:50 -04001008 set +x
1009 func_echo "LEAVE"
1010
Joey Armstronge8560da2023-04-26 15:44:45 -04001011 if [[ $status -eq 0 ]]; then
1012 true
1013 else
1014 false
1015 fi
1016
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001017 return
1018}
1019
1020## ---------------------------------------------------------------------------
1021## Intent:
1022## ---------------------------------------------------------------------------
1023function usage()
1024{
1025 cat <<EOH
Joey Armstrong76026b72023-03-29 12:19:17 -04001026
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001027Usage: $0
1028Usage: make [options] [target] ...
1029 --help This mesage
Joey Armstrong55fe80e2023-04-05 18:32:14 -04001030 --pac Personal Access Token (path to containing file or a string)
1031 --repo-name ex: voltctl
1032 --repo-org ex: opencord
Joey Armstronge8560da2023-04-26 15:44:45 -04001033 --release-notes [f] Release notes are passed by file argument
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001034
1035[DEBUG]
1036 --gen-version Generate a random release version string.
1037 --git-hostname Git server hostname (default=github.com)
Joey Armstrong55fe80e2023-04-05 18:32:14 -04001038 --version-file Read version string from local version file (vs env var)
Joey Armstrongf9a0a882023-04-17 11:53:57 -04001039
1040[MODES]
Joey Armstrong55fe80e2023-04-05 18:32:14 -04001041 --debug Enable script debug mode
1042 --draft Create a draft release (vs published)
Joey Armstrong43c48422023-04-03 10:17:32 -04001043 --dry-run Simulation mode
Joey Armstrong55fe80e2023-04-05 18:32:14 -04001044 --todo Display future enhancement list
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001045
Joey Armstronge8560da2023-04-26 15:44:45 -04001046All other arguments are pass-through to the gh command.
1047
Joey Armstrong55fe80e2023-04-05 18:32:14 -04001048Usage: $0 --draft --repo-org opencord --repo-name voltctl --git-hostname github.com --pac ~/access.pac
Joey Armstronge8560da2023-04-26 15:44:45 -04001049
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001050EOH
Joey Armstronge8560da2023-04-26 15:44:45 -04001051
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001052 return
1053}
1054
1055## ---------------------------------------------------------------------------
1056## Intent: Parse script command line arguments
1057## ---------------------------------------------------------------------------
1058function parse_args()
1059{
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001060 [[ -v DEBUG ]] && func_echo "ENTER"
1061
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001062 while [ $# -gt 0 ]; do
Joey Armstrong43c48422023-04-03 10:17:32 -04001063 local arg="$1"; shift
1064 func_echo "ARGV: $arg"
Joey Armstrong01e4edb2023-08-18 12:50:32 -04001065
1066 # shellcheck disable=SC2034
Joey Armstrong43c48422023-04-03 10:17:32 -04001067 case "$arg" in
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001068
Joey Armstrong43c48422023-04-03 10:17:32 -04001069 -*debug) declare -i -g debug=1 ;;
1070 --draft) declare -i -g draft_release=1 ;;
1071 --dry-run) declare -i -g dry_run=1 ;;
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001072
Joey Armstrong43c48422023-04-03 10:17:32 -04001073 --version-file)
1074 declare -i -g argv_version_file=1
1075 ;;
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001076
Joey Armstrong43c48422023-04-03 10:17:32 -04001077 -*gen-version)
1078 declare -g -i argv_gen_version=1
1079 ;;
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001080
Joey Armstrong43c48422023-04-03 10:17:32 -04001081 -*git-hostname)
1082 __githost="$1"; shift
1083 ;;
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001084
Joey Armstrong55fe80e2023-04-05 18:32:14 -04001085 -*release-notes)
Joey Armstronge8560da2023-04-26 15:44:45 -04001086 [[ ! -f "$1" ]] && error "--release-notes: file path required (arg=\"$arg\")"
1087 declare -g release_notes="$1"; shift
Joey Armstrongf9a0a882023-04-17 11:53:57 -04001088 ;;
Joey Armstrong55fe80e2023-04-05 18:32:14 -04001089
Joey Armstrong43c48422023-04-03 10:17:32 -04001090 -*repo-name)
1091 __repo_name="$1"; shift
1092 ;;
1093
1094 -*repo-org)
1095 __repo_org="$1"; shift
1096 ;;
1097
1098 -*pac)
1099 declare -g pac="$1"; shift
1100 readonly pac
1101 [[ ! -f "$pac" ]] && error "--token= does not exist ($pac)"
1102 : # nop/true
1103 ;;
1104
1105 -*todo) todo ;;
1106
1107 -*help)
1108 usage
1109 exit 0
1110 ;;
1111
1112 *) error "Detected unknown argument $arg" ;;
1113 esac
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001114 done
1115
Joey Armstrong26707f02023-01-26 12:41:12 -05001116 return
1117}
1118
Joey Armstrongaf577ab2022-12-15 14:43:33 -05001119##----------------##
1120##---] MAIN [---##
1121##----------------##
Joey Armstrong1962bcf2023-01-27 13:53:18 -05001122iam="${0##*/}"
1123
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001124full_banner
Joey Armstronge8560da2023-04-26 15:44:45 -04001125parse_args "$@"
Joey Armstrongd99e3d22023-01-29 12:35:43 -05001126init
1127install_gh_binary
Joey Armstrong7f382ef2023-01-25 12:00:08 -05001128
Joey Armstrong01e4edb2023-08-18 12:50:32 -04001129do_login "$*"
Zack Williams27cd3e52018-09-18 16:44:50 -07001130
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001131release_path='/dev/null'
1132get_release_path release_path
1133declare -p release_path
Zack Williams27cd3e52018-09-18 16:44:50 -07001134
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001135pushd "$release_path" || error "pushd failed: dir is [$release_path]"
Zack Williams27cd3e52018-09-18 16:44:50 -07001136
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001137 # legacy: getGitVersion "$GERRIT_PROJECT" GIT_VERSION
1138 getGitVersion GIT_VERSION
1139 getReleaseDescription RELEASE_DESCRIPTION
Joey Armstrong55fe80e2023-04-05 18:32:14 -04001140 if [[ ! -v release_notes ]]; then
Joey Armstronge8560da2023-04-26 15:44:45 -04001141 func_echo "Generating release notes from RELEASE_DESCRIPTION"
Joey Armstrongf9a0a882023-04-17 11:53:57 -04001142 declare -g release_notes="$scratch/release.notes"
1143 echo "$RELEASE_DESCRIPTION" > "$release_notes"
Joey Armstrong55fe80e2023-04-05 18:32:14 -04001144 fi
Joey Armstronge8560da2023-04-26 15:44:45 -04001145 cat "$release_notes"
Zack Williams27cd3e52018-09-18 16:44:50 -07001146
Joey Armstrongeef8c2c2023-03-27 17:27:43 -04001147 cat <<EOM
1148
1149** -----------------------------------------------------------------------
1150** GIT VERSION: $(declare -p GIT_VERSION)
1151** RELEASE_DESCRIPTION: $(declare -p RELEASE_DESCRIPTION)
1152** RELEASE_TARGETS: $(declare -p RELEASE_TARGETS)
Joey Armstrong43c48422023-04-03 10:17:32 -04001153** -----------------------------------------------------------------------
Joey Armstronge8560da2023-04-26 15:44:45 -04001154** URL: https://github.com/opencord/bbsim/releases
1155** -----------------------------------------------------------------------
Joey Armstrong43c48422023-04-03 10:17:32 -04001156** Running: make ${RELEASE_TARGETS}
Joey Armstrongeef8c2c2023-03-27 17:27:43 -04001157** -----------------------------------------------------------------------
1158EOM
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001159 # build the release, can be multiple space separated targets
1160 # -----------------------------------------------------------------------
1161 # % go build command-line-arguments:
1162 # copying /tmp/go-build4212845548/b001/exe/a.out:
1163 # open release/voltctl-1.8.25-linux-amd64: permission denied
1164 # missing: docker run mkdir
1165 # -----------------------------------------------------------------------
1166 # shellcheck disable=SC2086
Joey Armstrongeef8c2c2023-03-27 17:27:43 -04001167 make "$RELEASE_TARGETS"
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001168 copyToRelease
Joey Armstrong50f6e0b2023-01-24 14:14:08 -05001169
Joey Armstrong26707f02023-01-26 12:41:12 -05001170 cat <<EOM
Joey Armstrong1962bcf2023-01-27 13:53:18 -05001171
1172** -----------------------------------------------------------------------
1173** Create the release:
1174** 1) Create initial github release with download area.
1175** 2) Generate checksum.SHA256 for all released files.
1176** 3) Upload files to complete the release.
Joey Armstrong75a0d932023-03-28 08:59:54 -04001177** 4) Display released info from github
Joey Armstrong1962bcf2023-01-27 13:53:18 -05001178** -----------------------------------------------------------------------
Joey Armstrong26707f02023-01-26 12:41:12 -05001179EOM
1180
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001181 showReleases
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001182 release_staging
Joey Armstronge8560da2023-04-26 15:44:45 -04001183
1184 # Useful to display but --draft images use a non-standard subdir identifier.
1185 # showReleaseUrl
1186
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001187 popd || error "pushd failed: dir is [$release_path]"
Joey Armstrong28eddda2023-01-10 03:09:34 -05001188
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001189do_logout
1190
Joey Armstrong28eddda2023-01-10 03:09:34 -05001191# [SEE ALSO]
1192# -----------------------------------------------------------------------
1193# https://www.shellcheck.net/wiki/SC2236
Joey Armstrong26707f02023-01-26 12:41:12 -05001194# https://docs.github.com/en/rest/releases/releases?apiVersion=2022-11-28#create-a-release
Joey Armstrong28eddda2023-01-10 03:09:34 -05001195# -----------------------------------------------------------------------
Joey Armstrongf085d872023-01-28 17:52:29 -05001196# https://cli.github.com/manual/gh_help_reference
1197# https://cli.github.com/manual/gh_release
1198# -----------------------------------------------------------------------
Joey Armstrong28eddda2023-01-10 03:09:34 -05001199
1200# [EOF]