blob: daf7ac8b54dae678579c2e05a64c593fd18de832 [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 Armstrong55fe80e2023-04-05 18:32:14 -040036declare -i -g gen_version=1
37declare -i -g draft_release=1
Joey Armstrong43c48422023-04-03 10:17:32 -040038
Joey Armstrong41923cc2023-01-30 14:38:16 -050039# declare -g TRACE=0 # uncomment to set -x
40
41# shellcheck disable=SC2015
42[[ -v TRACE ]] && { set -x; } || { set +x; } # SC2015 (shellcheck -x)
43
Joey Armstrong2097d3e2023-03-26 10:32:03 -040044declare -a -g ARGV=() # Capture args to minimize globals and arg passing
45[[ $# -gt 0 ]] && ARGV=("$@")
Joey Armstrongd99e3d22023-01-29 12:35:43 -050046
47declare -g scratch # temp workspace for downloads
Joey Armstrongd99e3d22023-01-29 12:35:43 -050048
Joey Armstrong78cecc52023-04-03 11:39:11 -040049declare -g SCRIPT_VERSION='1.3' # git changeset needed
Joey Armstrong26707f02023-01-26 12:41:12 -050050
Joey Armstrong7f382ef2023-01-25 12:00:08 -050051##--------------------##
52##---] INCLUDES [---##
Joey Armstrong1962bcf2023-01-27 13:53:18 -050053##--------------------#
Joey Armstrong2097d3e2023-03-26 10:32:03 -040054
55## -----------------------------------------------------------------------
56## Intent: Register an interrupt handler to display a stack trace on error
57## -----------------------------------------------------------------------
58function errexit()
59{
60 local err=$?
61 set +o xtrace
62 local code="${1:-1}"
63
64 local prefix="${BASH_SOURCE[1]}:${BASH_LINENO[0]}"
65 echo -e "\nOFFENDER: ${prefix}"
66 if [ $# -gt 0 ] && [ "$1" == '--stacktrace-quiet' ]; then
67 code=1
68 else
69 echo "ERROR: '${BASH_COMMAND}' exited with status $err"
70 fi
71
72 # Print out the stack trace described by $function_stack
73 if [ ${#FUNCNAME[@]} -gt 2 ]
74 then
Joey Armstrong43c48422023-04-03 10:17:32 -040075 echo "Call tree:"
76 for ((i=1;i<${#FUNCNAME[@]}-1;i++))
77 do
78 echo " $i: ${BASH_SOURCE[$i+1]}:${BASH_LINENO[$i]} ${FUNCNAME[$i]}(...)"
79 done
Joey Armstrong2097d3e2023-03-26 10:32:03 -040080 fi
81
82 echo "Exiting with status ${code}"
83 echo
84 exit "${code}"
85 # return
86}
87
88# trap ERR to provide an error handler whenever a command exits nonzero
89# this is a more verbose version of set -o errexit
90trap errexit ERR
91
92# setting errtrace allows our ERR trap handler to be propagated to functions,
93# expansions and subshells
94set -o errtrace
Joey Armstrong7f382ef2023-01-25 12:00:08 -050095
96## -----------------------------------------------------------------------
Joey Armstrongd99e3d22023-01-29 12:35:43 -050097## Intent: Cleanup scratch area on exit
98## -----------------------------------------------------------------------
99function sigtrap()
100{
101 ## Prevent mishaps
102 local is_read_only
103 is_read_only="$(declare -p scratch)"
104 if [[ $is_read_only != *"declare -r scratch="* ]]; then
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400105 echo "ERROR: variable scratch is not read-only, cleanup skipped"
106 exit 1
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500107 fi
108
109 if [ -d "$scratch" ]; then
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400110 /bin/rm -fr "$scratch"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500111 fi
112
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400113 do_logout
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500114 return
115}
116trap sigtrap EXIT
117
118## -----------------------------------------------------------------------
Joey Armstrongad7b1e02023-03-27 11:55:48 -0400119## Intent: Return a release version for queries
120## Note: Do not use in production, function is intended for interactive use
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400121## -----------------------------------------------------------------------
122function get_version()
123{
124 declare -n ref="$1"
125
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400126 declare -a rev=()
127 rev+=("$(( RANDOM % 10 + 1 ))")
128 rev+=("$(( RANDOM % 256 + 1 ))")
Joey Armstrongeef8c2c2023-03-27 17:27:43 -0400129 rev+=("$(( RANDOM % 10000 + 1 ))")
Joey Armstrongfc20ed52023-04-03 19:37:58 -0400130 local ver="v${rev[0]}.${rev[1]}.${rev[2]}"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400131
Joey Armstrong43c48422023-04-03 10:17:32 -0400132 func_echo "VERSION: $ver"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400133 ref="$ver"
134 return
135}
136
137## -----------------------------------------------------------------------
138## Intent: Provide defaults for environment variables
139## -----------------------------------------------------------------------
140function initEnvVars()
141{
142 # when not running under Jenkins, use current dir as workspace and a blank
143 # project name
144 declare -g WORKSPACE=${WORKSPACE:-.}
145 declare -g GERRIT_PROJECT=${GERRIT_PROJECT:-}
Joey Armstrong76026b72023-03-29 12:19:17 -0400146
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400147 # Github organization (or user) this project is published on. Project name should
148 # be the same on both Gerrit and GitHub
Joey Armstrong76026b72023-03-29 12:19:17 -0400149 declare -g GITHUB_ORGANIZATION=${GITHUB_ORGANIZATION:-}
150
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400151 # glob pattern relative to project dir matching release artifacts
152 # ARTIFACT_GLOB=${ARTIFACT_GLOB:-"release/*"} # stat -- release/* not found, literal string (?)
153 declare -g ARTIFACT_GLOB=${ARTIFACT_GLOB:-"release/."}
Joey Armstrong76026b72023-03-29 12:19:17 -0400154
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400155 # Use "release" as the default makefile target, can be a space separated list
156 declare -g RELEASE_TARGETS=${RELEASE_TARGETS:-release}
Joey Armstrong76026b72023-03-29 12:19:17 -0400157
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400158 # Set and handle GOPATH and PATH
159 export GOPATH=${GOPATH:-$WORKSPACE/go}
160 export PATH=$PATH:/usr/lib/go-1.12/bin:/usr/local/go/bin:$GOPATH/bin
161 return
162}
163
164## -----------------------------------------------------------------------
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500165## Intent: Create a scratch area for downloads and transient tools
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400166## temp directory will be automatically removed upon exit.
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500167## -----------------------------------------------------------------------
168function init()
169{
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500170 local pkgbase="${0##*/}" # basename
171 local pkgname="${pkgbase%.*}"
172
Joey Armstrongad7b1e02023-03-27 11:55:48 -0400173 # initEnvVars # moved to full_banner()
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400174
175 ## Create a temp directory for auto-cleanup
176 declare -g scratch
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500177 scratch="$(mktemp -d -t "${pkgname}.XXXXXXXXXX")"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400178 readonly scratch
179 declare -p scratch
180
Joey Armstrong43c48422023-04-03 10:17:32 -0400181 ## prime the stream: cache answers
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400182 local work
183 get_release_dir work
184 declare -p work
Joey Armstrong43c48422023-04-03 10:17:32 -0400185
186 local filler
187 getGitVersion filler
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
306 declare -i -g draft_release=1
307 echo "Detected --draft SemVer release version tag [$__ver]"
308 echo "Building artifacts for GitHub release."
309
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400310 else
Joey Armstrong43c48422023-04-03 10:17:32 -0400311 error "No SemVer released version tag found, exiting..."
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400312 fi
313
Joey Armstrong43c48422023-04-03 10:17:32 -0400314 ## Cache value for subsequent calls.
315 ## readonly is a guarantee assignment happens once.
316 declare -g cached_getGitVersion="$__ver"
317 readonly cached_getGitVersion;
318
319 varname="$__ver"
320 func_echo "Version is [$__ver] from $(/bin/pwd)"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400321 return
322}
323
324## -----------------------------------------------------------------------
325## Intent:
326## Note: Release description is sanitized version of the log message
327## -----------------------------------------------------------------------
328function getReleaseDescription()
329{
330 declare -n varname="$1"; shift
331
332 local msg
333 msg="$(git log -1 --pretty=%B)"
334
335 local val
336 val="$(tr -dc "[:alnum:]\n\r\.\[\]\:\-\\\/\`\' " <<< "$msg")"
337
338 [[ ${#val} -eq 0 ]] && error "Release Description is empty ($msg)"
339 varname="$val"
340 return
341}
342
343## -----------------------------------------------------------------------
344## Intent: Retrieve value of the release temp directory.
345## -----------------------------------------------------------------------
346## Note: Limit use of globals so logic can be isolated in function calls.
347## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400348function get_release_dir()
349{
350 declare -n varname=$1; shift
351
352 # Temporary staging directory to copy artifacts to
353 varname="$scratch/release"
354 mkdir -p "$varname"
355 return
356}
357
358## -----------------------------------------------------------------------
359## Intent: Retrieve github server hostname
360## -----------------------------------------------------------------------
361function get_gh_hostname()
362{
363 declare -n varname=$1; shift
364 varname+=('--hostname' "${__githost}")
365 return
366}
367
368## -----------------------------------------------------------------------
369## Intent: Retrieve repository organizaiton name
370## -----------------------------------------------------------------------
371function get_gh_repo_org()
372{
373 declare -n varname=$1; shift
374 declare -g __repo_org
Joey Armstrong76026b72023-03-29 12:19:17 -0400375
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400376 local org
377 if [[ -v __repo_org ]]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400378 org="$__repo_org"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400379 elif [[ ! -v GITHUB_ORGANIZATION ]]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400380 error "--repo-org or GITHUB_ORGANIZATION= are required"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400381 else
Joey Armstrong43c48422023-04-03 10:17:32 -0400382 org="${GITHUB_ORGANIZATION}"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400383 fi
384
385 # shellcheck disable=SC2178
386 varname="$org"
387 return
388}
389
390## -----------------------------------------------------------------------
391## Intent: Retrieve repository organizaiton name
392## -----------------------------------------------------------------------
393function get_gh_repo_name()
394{
395 declare -n varname=$1; shift
396 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
407 varname="$name"
408 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{
434 declare -n varname=$1; shift
435
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 Armstrong2097d3e2023-03-26 10:32:03 -0400442 varname="${repo_org}/${repo_name}"
Joey Armstrong43c48422023-04-03 10:17:32 -0400443 # func_echo "VARNAME=$varname"
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{
452 declare -n varname=$1; shift
453
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
460 # varname="${repo_name} - $GIT_VERSION"
461 varname="${repo_name} - ${repo_ver}"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400462 func_echo "varname=$varname"
463 return
464}
465
466## -----------------------------------------------------------------------
467## Intent: Retrieve tag version
468## -----------------------------------------------------------------------
469function get_argv_tag()
470{
471 declare -n varname=$1; shift
472
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="
482 varname="$cached_argv_tag"
483 func_echo "varname=$varname"
484 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{
499 declare -n varname=$1; shift
500
501 DEST_GOPATH=${DEST_GOPATH:-}
502 if [ -n "$DEST_GOPATH" ]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400503 mkdir -p "$GOPATH/src/$DEST_GOPATH"
504 varname="$GOPATH/src/$DEST_GOPATH/$GERRIT_PROJECT"
505 mv "$WORKSPACE/$GERRIT_PROJECT" "$varname"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400506 else
Joey Armstrong43c48422023-04-03 10:17:32 -0400507 varname="$WORKSPACE/$GERRIT_PROJECT"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400508 fi
509
Joey Armstrong43c48422023-04-03 10:17:32 -0400510 ## Verify pwd is OK
511 for path in \
512 "$varname/Makefile"\
513 "$varname/makefile"\
514 "__ERROR__"\
515 ; do
516 case "$path" in
517 __ERROR__) error "Makefile not found at $varname!" ;;
518 *) [[ -f "$path" ]] && break ;;
519 esac
520 done
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400521
522 return
523}
524
525## -----------------------------------------------------------------------
526## Intent: Display future enhancements
527## -----------------------------------------------------------------------
528function todo()
529{
530 local iam="${0##*/}"
531
532cat <<EOT
533
534** -----------------------------------------------------------------------
535** IAM: ${iam} :: ${FUNCNAME[0]}
536** -----------------------------------------------------------------------
537 o get_release_path()
538 - refactor redundant paths into local vars.
539 - see comments, do we have a one-off failure condition ?
Joey Armstrongad7b1e02023-03-27 11:55:48 -0400540 o PATH += golang appended 3 times, release needs a single, reliable answer.
541 o do_login, do_logout and api calls do not use the my_gh wrapper:
542 - Add a lookup function to cache and retrieve path to downloaded gh command.
Joey Armstrong76026b72023-03-29 12:19:17 -0400543
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400544EOT
545
546 return
547}
548
549## -----------------------------------------------------------------------
Joey Armstrong76026b72023-03-29 12:19:17 -0400550## Intent: Verify a directory contains content for release.
551## -----------------------------------------------------------------------
552## Given:
553## scalar Path to release/ directory
554## ref Results returned through this indirect var.
555## -----------------------------------------------------------------------
556## Usage:
557## declare -a artifacts=()
558## get_artifacts '/foo/bar/tans' artifacts
559## declare -p artifacts
560## -----------------------------------------------------------------------
561function get_artifacts()
562{
563 local dir="$1" ; shift
564 declare -n ref=$1 ; shift
565
Joey Armstrong55fe80e2023-04-05 18:32:14 -0400566 # Glob available files, exclude checksums
567 readarray -t __artifacts < <(find "$dir" -mindepth 1 ! -type d \
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400568 | grep -iv -e 'sum256' -e 'checksum')
569 # func_echo "$(declare -p __artifacts)"
Joey Armstrong76026b72023-03-29 12:19:17 -0400570
571 # -----------------------------------------------------------------------
572 # Verify count>0 to inhibit source-only release
573 # Problem children:
574 # o build or make release failures.
575 # o docker container filesystem mount problem (~volume)
576 # -----------------------------------------------------------------------
577 [[ ${#__artifacts[@]} -eq 0 ]] \
Joey Armstrong43c48422023-04-03 10:17:32 -0400578 && error "Artifact dir is empty: $(declare -p dir)"
Joey Armstrong76026b72023-03-29 12:19:17 -0400579
580 ref=("${__artifacts[@]}")
581 return
582}
583
584## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400585## Intent: Copy files from the build directory into the release staging
586## directory for publishing to github releases/ endpoint.
Joey Armstrong26707f02023-01-26 12:41:12 -0500587## -----------------------------------------------------------------------
588function copyToRelease()
589{
Joey Armstrong76026b72023-03-29 12:19:17 -0400590 banner ''
Joey Armstrong26707f02023-01-26 12:41:12 -0500591
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400592 local artifact_glob="${ARTIFACT_GLOB%/*}"
593 func_echo "$(declare -p artifact_glob)"
594
595 local work
596 get_release_dir work
Joey Armstrong76026b72023-03-29 12:19:17 -0400597 func_echo "Artifact dir: $(declare -p work)"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400598
Joey Armstrong76026b72023-03-29 12:19:17 -0400599 ## Verify release content is available
600 declare -a artifacts=()
Joey Armstrong43c48422023-04-03 10:17:32 -0400601 # get_artifacts "$work" artifacts
602 get_artifacts "$artifact_glob" artifacts
603 func_echo "Artifacts in copy_from: $(declare -p artifacts)"
Joey Armstrong472bfba2023-03-27 18:12:28 -0400604
Joey Armstrong26707f02023-01-26 12:41:12 -0500605 # Copy artifacts into the release temp dir
606 # shellcheck disable=SC2086
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400607 echo "rsync -rv --checksum \"$artifact_glob/.\" \"$work/.\""
608 rsync -rv --checksum "$artifact_glob/." "$work/."
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500609
Joey Armstrong43c48422023-04-03 10:17:32 -0400610 get_artifacts "$work" artifacts
611 func_echo "Artifacts in copy_to: $(declare -p artifacts)"
612
Joey Armstrong26707f02023-01-26 12:41:12 -0500613 return
614}
615
616## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400617## https://docs.github.com/en/rest/releases?apiVersion=2022-11-28
618 # https://cli.github.com/manual/gh_release_create
619 # --target <branch> or commit SHA
620 # --title
621 # --generate-notes
622 # --release-notes (notes file)
623 # --release
624 # release create dist/*.tgz
625 # --discussion-category "General"
Joey Armstrongf085d872023-01-28 17:52:29 -0500626## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400627# https://cli.github.com/manual/gh_release_create
628## -----------------------------------------------------------------------
629function gh_release_create()
Joey Armstrong38bfeea2023-02-06 18:01:29 -0500630{
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400631 banner ''
Joey Armstrong38bfeea2023-02-06 18:01:29 -0500632
Joey Armstrong43c48422023-04-03 10:17:32 -0400633 local version
634 getGitVersion version
Joey Armstrong38bfeea2023-02-06 18:01:29 -0500635
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400636 local work
637 get_release_dir work
638
Joey Armstrong43c48422023-04-03 10:17:32 -0400639 declare -a args=()
640 args+=('--host-repo')
641 args+=('--title')
642 if [[ -v draft_release ]]; then
643 args+=('--draft')
644 else
645 args+=('--discussion-category' 'Announcements')
646 fi
647
Joey Armstrong55fe80e2023-04-05 18:32:14 -0400648 if [[ -v release_notes ]]; then
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400649 args+=('--notes-file' "$release_notes")
Joey Armstrong55fe80e2023-04-05 18:32:14 -0400650 fi
Joey Armstrong43c48422023-04-03 10:17:32 -0400651
Joey Armstrong78cecc52023-04-03 11:39:11 -0400652 pushd "$work/.." >/dev/null
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400653 # func_echo "WORK=$work"
Joey Armstrong78cecc52023-04-03 11:39:11 -0400654 readarray -t payload < <(find 'release' -maxdepth 4 ! -type d -print)
Joey Armstrong76026b72023-03-29 12:19:17 -0400655
Joey Armstrongad7b1e02023-03-27 11:55:48 -0400656 func_echo "$gh_cmd release create ${version} ${args[@]}" "${payload[@]}"
Joey Armstrong43c48422023-04-03 10:17:32 -0400657
658 if [[ -v dry_run ]]; then
659 echo "[SKIP] dry run"
660 else
Joey Armstrong78cecc52023-04-03 11:39:11 -0400661 func_echo "my_gh release create '$version' ${args[@]} ${payload[@]}"
Joey Armstrong4dbe7002023-04-04 12:47:38 -0400662 my_gh 'release' 'create' "$version" "${args[@]}" "${payload[@]}"
Joey Armstrong43c48422023-04-03 10:17:32 -0400663 fi
Joey Armstrong76026b72023-03-29 12:19:17 -0400664 popd >/dev/null
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400665
Joey Armstrong38bfeea2023-02-06 18:01:29 -0500666 return
667}
668
669## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400670## Intent: Authenticate credentials for a github gh session
Joey Armstrong38bfeea2023-02-06 18:01:29 -0500671## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400672## NOTE: my_gh currently unused due to --with-token < "$pac"
673## -----------------------------------------------------------------------
674function do_login()
Joey Armstrongf085d872023-01-28 17:52:29 -0500675{
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400676 declare -g pac
Joey Armstrong485081f2023-03-27 13:34:08 -0400677 declare -a login_args=()
678 [[ $# -gt 0 ]] && login_args+=("$@")
Joey Armstrongf085d872023-01-28 17:52:29 -0500679
Joey Armstrong485081f2023-03-27 13:34:08 -0400680 # https://github.com/cli/cli/issues/2922#issuecomment-775027762
Joey Armstrong75a0d932023-03-28 08:59:54 -0400681 # (sigh) why not quietly return VS forcing a special logic path
Joey Armstronge6e18eb2023-03-27 14:19:21 -0400682 # [[ -v WORKSPACE ]] && [[ -v GITHUB_TOKEN ]] && return
683
684# 12:58:36 ** -----------------------------------------------------------------------
685# 12:58:36 ** jenkins582353203049151829.sh::do_login: --hostname github.com
686# 12:58:36 ** --------------------------------------------------------------------# ---
687# 12:58:36 ** jenkins582353203049151829.sh :: do_login: Detected ENV{GITHUB_TOKEN}=
688# 12:58:36 The value of the GITHUB_TOKEN environment variable is being used for authentication.
689# 12:58:36 To have GitHub CLI store credentials instead, first clear the value from the environment.
Joey Armstrong43c48422023-04-03 10:17:32 -0400690# -----------------------------------------------------------------------
Joey Armstrong76026b72023-03-29 12:19:17 -0400691
Joey Armstrong485081f2023-03-27 13:34:08 -0400692 get_gh_hostname login_args
Joey Armstrongf085d872023-01-28 17:52:29 -0500693
Joey Armstrong485081f2023-03-27 13:34:08 -0400694 banner "${login_args[@]}"
Joey Armstrong78cecc52023-04-03 11:39:11 -0400695 func_echo "$(declare -p WORKSPACE)"
Joey Armstrongf085d872023-01-28 17:52:29 -0500696
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400697 ## Read from disk is safer than export GITHUB_TOKEN=
698 if [[ -v pac ]] && [[ ${#pac} -gt 0 ]]; then # interactive/debugging
Joey Armstrong43c48422023-04-03 10:17:32 -0400699 [ ! -f "$pac" ] && error "PAC token file $pac does not exist"
Joey Armstrong78cecc52023-04-03 11:39:11 -0400700 func_echo "$gh_cmd auth login ${login_args[@]} --with-token < $pac"
Joey Armstrong485081f2023-03-27 13:34:08 -0400701 "$gh_cmd" auth login "${login_args[@]}" --with-token < "$pac"
Joey Armstrongf085d872023-01-28 17:52:29 -0500702
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400703 elif [[ ! -v GITHUB_TOKEN ]]; then
704 error "--token [t] or GITHUB_TOKEN= are required"
Joey Armstrong41923cc2023-01-30 14:38:16 -0500705
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400706 else # jenkins
Joey Armstrong78cecc52023-04-03 11:39:11 -0400707 func_echo "$gh_cmd auth login ${login_args[@]} (ie: jenkins)"
Joey Armstrong733ea9f2023-04-03 21:19:46 -0400708
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400709 # https://github.com/cli/cli/issues/2922#issuecomment-775027762
710 # When using GITHUB_TOKEN, there is no need to even run gh auth login
Joey Armstrong733ea9f2023-04-03 21:19:46 -0400711 # "$gh_cmd" auth login "${login_args[@]}"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400712 fi
Joey Armstrongf085d872023-01-28 17:52:29 -0500713
Joey Armstrong43c48422023-04-03 10:17:32 -0400714 declare -i -g active_login=1 # signal logout needed
Joey Armstrongf085d872023-01-28 17:52:29 -0500715
Joey Armstrongf085d872023-01-28 17:52:29 -0500716 return
717}
718
719## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400720## Intent: Destroy credentials/gh session authenticated by do_login
721## -----------------------------------------------------------------------
722## NOTE: my_gh currently unused due to "<<< 'y'"
723## -----------------------------------------------------------------------
724function do_logout()
725{
726 declare -i -g active_login
727 [[ ! -v active_login ]] && return
728
Joey Armstrong78cecc52023-04-03 11:39:11 -0400729 declare -a logout_args=()
730 [[ $# -gt 0 ]] && logout_args+=("$@")
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400731
Joey Armstrong78cecc52023-04-03 11:39:11 -0400732 get_gh_hostname logout_args
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400733
Joey Armstrong78cecc52023-04-03 11:39:11 -0400734 banner "${logout_args[@]}"
735 func_echo "$gh_cmd auth logout ${logout_args[@]} <<< 'Y'"
736 "$gh_cmd" auth logout "${logout_args[@]}" <<< 'Y'
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400737
738 unset active_login
739 return
740}
741
742## -----------------------------------------------------------------------
743## Intent: Query for repository version strings
744## -----------------------------------------------------------------------
745function get_releases()
746{
747 declare -n ref="$1"; shift
748 local func="${FUNCNAME[0]}"
749
750 banner ""
751 pushd "$scratch" >/dev/null
Joey Armstrong76026b72023-03-29 12:19:17 -0400752
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400753 # gh api repos/{owner}/{repo}/releases
754 local releases_uri
755 get_gh_releases releases_uri
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400756 # declare -p releases_uri
Joey Armstrong76026b72023-03-29 12:19:17 -0400757
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400758 ref=()
Joey Armstrongad7b1e02023-03-27 11:55:48 -0400759 "$gh_cmd" api "$releases_uri" "${common[@]}" | jq . > 'release.raw'
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400760 readarray -t __tmp < <(jq '.[] | "\(.tag_name)"' 'release.raw')
761
762 local release
763 for release in "${__tmp[@]}";
764 do
Joey Armstrong43c48422023-04-03 10:17:32 -0400765 release="${release//\"/}"
766 ref+=("$release")
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400767 done
768
769 popd >/dev/null
770 return
771}
772
773## -----------------------------------------------------------------------
774## Intent: Display repository query strings.
775## Indirect: verify authentication and API
776## -----------------------------------------------------------------------
777function showReleases()
778{
Joey Armstrong43c48422023-04-03 10:17:32 -0400779 declare -a raw=()
780 get_releases raw
781
782 ## Sort for display, we may need to prune volume later on
783 IFS=$'\n' releases=($(sort -nr <<<"${raw[*]}"))
784 unset IFS
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400785
786 local release
787 for release in "${releases[@]}";
788 do
Joey Armstrong43c48422023-04-03 10:17:32 -0400789 func_echo "$release"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400790 done
791 return
792}
793
794## -----------------------------------------------------------------------
795## Intent: Install the gh command line tool locally
Joey Armstrong26707f02023-01-26 12:41:12 -0500796## -----------------------------------------------------------------------
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500797function install_gh_binary()
Joey Armstrong26707f02023-01-26 12:41:12 -0500798{
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400799 banner
Joey Armstrong26707f02023-01-26 12:41:12 -0500800
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500801 pushd "$scratch"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400802 func_echo "Retrieve latest gh download URLs"
Joey Armstrongf085d872023-01-28 17:52:29 -0500803
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500804 local latest="https://github.com/cli/cli/releases/latest"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400805 local tarball="gh.tar.tgz"
806
Joey Armstrong76026b72023-03-29 12:19:17 -0400807 readarray -t latest < <(\
808 curl --silent -qI "$latest" \
Joey Armstrong43c48422023-04-03 10:17:32 -0400809 | awk -F '/' '/^location/ {print substr($NF, 1, length($NF)-1)}')
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400810 declare -p latest
811 if [ ${#latest[@]} -ne 1 ]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400812 error "Unable to determine latest gh package version"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400813 fi
Joey Armstrong76026b72023-03-29 12:19:17 -0400814
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400815 local VER="${latest[0]}"
816
817 func_echo "Download latest gh binary"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500818 local url="https://github.com/cli/cli/releases/download/${VER}/gh_${VER#v}_linux_amd64.tar.gz"
Joey Armstrong43c48422023-04-03 10:17:32 -0400819 func_echo "wget --quiet --output-document='$tarball' '$url'"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500820 wget --quiet --output-document="$tarball" "$url"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400821
822 func_echo "Unpack tarball"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500823 tar zxf "$tarball"
824
Joey Armstrong78cecc52023-04-03 11:39:11 -0400825 declare -g gh_cmd
826 gh_cmd="$(find "${scratch}" -name 'gh' -print)"
827 #declare -g gh_cmd='/usr/bin/gh'
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500828 readonly gh_cmd
829
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400830 func_echo "Command: ${gh_cmd}"
831 func_echo "Version: $("$gh_cmd" --version)"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500832 popd
833
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400834 return
835}
836
837## -----------------------------------------------------------------------
838## Intent: Danger Will Robinson
839## -----------------------------------------------------------------------
840function releaseDelete()
841{
842 local version="$1"; shift
Joey Armstrong76026b72023-03-29 12:19:17 -0400843
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400844 banner "${in_args[@]}"
845 declare -a args=()
846 args+=('--host-repo')
847 args+=('--yes')
848 # args+=('--cleanup-tag')
849
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400850 echo
851 echo "==========================================================================="
852 my_gh 'release' 'delete' "$version" "${args[@]}"
853 echo "==========================================================================="
854 echo
855
856 showReleases
857 return
858}
859
860## -----------------------------------------------------------------------
861## Intent: Copy binaries into temp/release, checksum then publish
862## -----------------------------------------------------------------------
863function release_staging()
864{
865 local release_temp
866 get_release_dir release_temp
867
868 banner ''
869 func_echo "Packaging release files"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400870
Joey Armstrong76026b72023-03-29 12:19:17 -0400871 pushd "$release_temp" >/dev/null \
Joey Armstrong43c48422023-04-03 10:17:32 -0400872 || error "pushd failed: dir is [$release_temp]"
Joey Armstrong76026b72023-03-29 12:19:17 -0400873
874 declare -a to_release=()
875 get_artifacts '.' to_release
876
Joey Armstrong55fe80e2023-04-05 18:32:14 -0400877 if false; then
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400878 for fyl in "${to_release[@]}";
879 do
880 func_echo "sha256sum $fyl > ${fyl}.sha256"
881 sha256sum "$fyl" > "${fyl}.sha256"
882 done
Joey Armstrong55fe80e2023-04-05 18:32:14 -0400883 fi
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400884
885 # Generate and check checksums
Joey Armstrong55fe80e2023-04-05 18:32:14 -0400886 sha256sum -- * | grep -iv -e 'checksum' -e 'sha256' > checksum.SHA256
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400887 sha256sum -c < checksum.SHA256
888
889 echo
890 func_echo "Checksums(checksum.SHA256):"
891 cat checksum.SHA256
892
Joey Armstrong43c48422023-04-03 10:17:32 -0400893 if false; then
Joey Armstrong78cecc52023-04-03 11:39:11 -0400894 # Careful with credentials display
Joey Armstrong43c48422023-04-03 10:17:32 -0400895 get_gh_hostname login_args
896 banner "gh auth status ${login_args[@]}"
897 gh auth status "${login_args[@]}"
898 fi
899
Joey Armstrong76026b72023-03-29 12:19:17 -0400900 gh_release_create # publish
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400901
902 popd >/dev/null || error "pushd failed: dir is [$release_temp]"
903
904 return
905}
906
907## -----------------------------------------------------------------------
908## Intent: Display program usage
909## -----------------------------------------------------------------------
910function usage()
911{
912 [[ $# -gt 0 ]] && func_echo "$*"
913
914 cat <<EOH
915Usage: github-release.sh [options] [target] ...
916
917[Github CLI (gh) arguments]
918 --login Perform authentication using a PAC
919 --logout
920 --host [h] Specify github server for connection.
921
922[Options]
923 --token Login debugging, alternative to env var use.
Joey Armstrong55fe80e2023-04-05 18:32:14 -0400924 --release-notes [f] Release notes are passed in this file.
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400925
926[Modes]
927 --debug Enable script debug mode
928 --verbose Enable script verbose mode
929
930All remaining arguments are passthrough to the gh command.
931EOH
932
933 exit 0
934}
935
936## -----------------------------------------------------------------------
Joey Armstrong43c48422023-04-03 10:17:32 -0400937## Intent: Normalize common arguments and access to the gh command.
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400938## o Cache path to the gh command
939## o Construct a gh command line from given args
940## o Command wrapper can provide defaults (--hostname github.com)
941## -----------------------------------------------------------------------
942## Given:
943## scalar Array variable name (declare -n is a reference)
944## Return:
945## ref gh command line passed back to caller
946## Switches:
947## --host Pass default github hostname
948## --verbose Enable verbose mode#
949## --version Display command version
950## @array Remaining arguments passed as command switches.
951## -----------------------------------------------------------------------
952## See Also:
953## o https://cli.github.com/manual
954## -----------------------------------------------------------------------
955function my_gh()
956{
Joey Armstrong73bb2f62023-04-17 19:31:50 -0400957 func_echo "ENTER"
958 set -x
959
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400960 declare -a cmd=()
961 cmd+=("$gh_cmd")
962
963 ## ----------------------
964 ## Construct command line
965 ## ----------------------
966 # shellcheck disable=SC2034
967 declare -A action=() # pending operations
968 declare -a args=() # common gh command line args
969
970 while [ $# -gt 0 ]; do
971 local arg="$1"; shift
Joey Armstrong73bb2f62023-04-17 19:31:50 -0400972 func_echo "function arg is [$arg]"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400973 case "$arg" in
974
Joey Armstrong43c48422023-04-03 10:17:32 -0400975 # Modes
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400976 -*debug) declare -i -g debug=1 ;;
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400977 -*verbose) args+=('--verbose') ;;
978
Joey Armstrong43c48422023-04-03 10:17:32 -0400979 -*hostname)
980 get_gh_hostname in_args
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400981 ;;
982
Joey Armstrong43c48422023-04-03 10:17:32 -0400983 --host-repo)
984 local val
985 get_argv_repo val
Joey Armstrong76026b72023-03-29 12:19:17 -0400986
Joey Armstrong43c48422023-04-03 10:17:32 -0400987 # --repo <[HOST/]OWNER/REPO>
Joey Armstrong78cecc52023-04-03 11:39:11 -0400988 args+=('--repo' "${__githost}/${val}")
Joey Armstrong43c48422023-04-03 10:17:32 -0400989 ;;
Joey Armstrong76026b72023-03-29 12:19:17 -0400990
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400991# args+=('--repo' 'github.com/opencord/bbsim')
992
Joey Armstrong43c48422023-04-03 10:17:32 -0400993 --repo)
994 local val
995 get_argv_repo val
996 args+=('--repo' "$val")
997 ;;
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400998
Joey Armstrong43c48422023-04-03 10:17:32 -0400999 --tag)
1000 local val
1001 get_argv_tag val
Joey Armstrong4dbe7002023-04-04 12:47:38 -04001002 args+=("$val") # No switch, pass inline
Joey Armstrong43c48422023-04-03 10:17:32 -04001003 ;;
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001004
Joey Armstrong43c48422023-04-03 10:17:32 -04001005 --title)
1006 local val
1007 get_argv_name val
Joey Armstrongf9a0a882023-04-17 11:53:57 -04001008 args+=('--title' "'$val'")
Joey Armstrong43c48422023-04-03 10:17:32 -04001009 ;;
Joey Armstrong76026b72023-03-29 12:19:17 -04001010
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001011 *) args+=("$arg") ;;
1012 esac
1013 done
Joey Armstrong76026b72023-03-29 12:19:17 -04001014
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001015 cmd+=("${args[@]}")
Joey Armstrong73bb2f62023-04-17 19:31:50 -04001016
1017 echo
1018 declare -p cmd
1019
1020 echo
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001021 echo "** Running: ${cmd[*]}"
1022 "${cmd[@]}"
1023 local status=$?
1024
Joey Armstrong73bb2f62023-04-17 19:31:50 -04001025 set +x
1026 func_echo "LEAVE"
1027
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001028 [[ $status -eq 0 ]] && { true; } || { false; }
1029 return
1030}
1031
1032## ---------------------------------------------------------------------------
1033## Intent:
1034## ---------------------------------------------------------------------------
1035function usage()
1036{
1037 cat <<EOH
Joey Armstrong76026b72023-03-29 12:19:17 -04001038
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001039Usage: $0
1040Usage: make [options] [target] ...
1041 --help This mesage
Joey Armstrong55fe80e2023-04-05 18:32:14 -04001042 --pac Personal Access Token (path to containing file or a string)
1043 --repo-name ex: voltctl
1044 --repo-org ex: opencord
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001045
1046[DEBUG]
1047 --gen-version Generate a random release version string.
1048 --git-hostname Git server hostname (default=github.com)
Joey Armstrong55fe80e2023-04-05 18:32:14 -04001049 --version-file Read version string from local version file (vs env var)
Joey Armstrongf9a0a882023-04-17 11:53:57 -04001050
1051[MODES]
Joey Armstrong55fe80e2023-04-05 18:32:14 -04001052 --debug Enable script debug mode
1053 --draft Create a draft release (vs published)
Joey Armstrong43c48422023-04-03 10:17:32 -04001054 --dry-run Simulation mode
Joey Armstrong55fe80e2023-04-05 18:32:14 -04001055 --todo Display future enhancement list
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001056
Joey Armstrong55fe80e2023-04-05 18:32:14 -04001057Usage: $0 --draft --repo-org opencord --repo-name voltctl --git-hostname github.com --pac ~/access.pac
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001058EOH
1059 return
1060}
1061
1062## ---------------------------------------------------------------------------
1063## Intent: Parse script command line arguments
1064## ---------------------------------------------------------------------------
1065function parse_args()
1066{
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001067 [[ -v DEBUG ]] && func_echo "ENTER"
1068
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001069 while [ $# -gt 0 ]; do
Joey Armstrong43c48422023-04-03 10:17:32 -04001070 local arg="$1"; shift
1071 func_echo "ARGV: $arg"
1072 case "$arg" in
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001073
Joey Armstrong43c48422023-04-03 10:17:32 -04001074 -*debug) declare -i -g debug=1 ;;
1075 --draft) declare -i -g draft_release=1 ;;
1076 --dry-run) declare -i -g dry_run=1 ;;
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001077
Joey Armstrong43c48422023-04-03 10:17:32 -04001078 --version-file)
1079 declare -i -g argv_version_file=1
1080 ;;
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001081
Joey Armstrong43c48422023-04-03 10:17:32 -04001082 -*gen-version)
1083 declare -g -i argv_gen_version=1
1084 ;;
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001085
Joey Armstrong43c48422023-04-03 10:17:32 -04001086 -*git-hostname)
1087 __githost="$1"; shift
1088 ;;
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001089
Joey Armstrong55fe80e2023-04-05 18:32:14 -04001090 -*release-notes)
Joey Armstrongf9a0a882023-04-17 11:53:57 -04001091 if [ -f "$1" ]; then
1092 declare -g release_notes="$1";
1093 shift
1094 fi
1095 ;;
Joey Armstrong55fe80e2023-04-05 18:32:14 -04001096
Joey Armstrong43c48422023-04-03 10:17:32 -04001097 -*repo-name)
1098 __repo_name="$1"; shift
1099 ;;
1100
1101 -*repo-org)
1102 __repo_org="$1"; shift
1103 ;;
1104
1105 -*pac)
1106 declare -g pac="$1"; shift
1107 readonly pac
1108 [[ ! -f "$pac" ]] && error "--token= does not exist ($pac)"
1109 : # nop/true
1110 ;;
1111
1112 -*todo) todo ;;
1113
1114 -*help)
1115 usage
1116 exit 0
1117 ;;
1118
1119 *) error "Detected unknown argument $arg" ;;
1120 esac
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001121 done
1122
Joey Armstrong26707f02023-01-26 12:41:12 -05001123 return
1124}
1125
Joey Armstrongaf577ab2022-12-15 14:43:33 -05001126##----------------##
1127##---] MAIN [---##
1128##----------------##
Joey Armstrong1962bcf2023-01-27 13:53:18 -05001129iam="${0##*/}"
1130
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001131full_banner
1132parse_args $@
Joey Armstrongd99e3d22023-01-29 12:35:43 -05001133init
1134install_gh_binary
Joey Armstrong7f382ef2023-01-25 12:00:08 -05001135
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001136do_login
Zack Williams27cd3e52018-09-18 16:44:50 -07001137
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001138release_path='/dev/null'
1139get_release_path release_path
1140declare -p release_path
Zack Williams27cd3e52018-09-18 16:44:50 -07001141
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001142pushd "$release_path" || error "pushd failed: dir is [$release_path]"
Zack Williams27cd3e52018-09-18 16:44:50 -07001143
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001144 # legacy: getGitVersion "$GERRIT_PROJECT" GIT_VERSION
1145 getGitVersion GIT_VERSION
1146 getReleaseDescription RELEASE_DESCRIPTION
Joey Armstrong55fe80e2023-04-05 18:32:14 -04001147 if [[ ! -v release_notes ]]; then
Joey Armstrongf9a0a882023-04-17 11:53:57 -04001148 declare -g release_notes="$scratch/release.notes"
1149 echo "$RELEASE_DESCRIPTION" > "$release_notes"
Joey Armstrong55fe80e2023-04-05 18:32:14 -04001150 fi
Zack Williams27cd3e52018-09-18 16:44:50 -07001151
Joey Armstrongeef8c2c2023-03-27 17:27:43 -04001152 cat <<EOM
1153
1154** -----------------------------------------------------------------------
1155** GIT VERSION: $(declare -p GIT_VERSION)
1156** RELEASE_DESCRIPTION: $(declare -p RELEASE_DESCRIPTION)
1157** RELEASE_TARGETS: $(declare -p RELEASE_TARGETS)
Joey Armstrong43c48422023-04-03 10:17:32 -04001158** -----------------------------------------------------------------------
1159** Running: make ${RELEASE_TARGETS}
Joey Armstrongeef8c2c2023-03-27 17:27:43 -04001160** -----------------------------------------------------------------------
1161EOM
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001162 # build the release, can be multiple space separated targets
1163 # -----------------------------------------------------------------------
1164 # % go build command-line-arguments:
1165 # copying /tmp/go-build4212845548/b001/exe/a.out:
1166 # open release/voltctl-1.8.25-linux-amd64: permission denied
1167 # missing: docker run mkdir
1168 # -----------------------------------------------------------------------
1169 # shellcheck disable=SC2086
Joey Armstrongeef8c2c2023-03-27 17:27:43 -04001170 make "$RELEASE_TARGETS"
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001171 copyToRelease
Joey Armstrong50f6e0b2023-01-24 14:14:08 -05001172
Joey Armstrong26707f02023-01-26 12:41:12 -05001173 cat <<EOM
Joey Armstrong1962bcf2023-01-27 13:53:18 -05001174
1175** -----------------------------------------------------------------------
1176** Create the release:
1177** 1) Create initial github release with download area.
1178** 2) Generate checksum.SHA256 for all released files.
1179** 3) Upload files to complete the release.
Joey Armstrong75a0d932023-03-28 08:59:54 -04001180** 4) Display released info from github
Joey Armstrong1962bcf2023-01-27 13:53:18 -05001181** -----------------------------------------------------------------------
Joey Armstrong26707f02023-01-26 12:41:12 -05001182EOM
1183
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001184 showReleases
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001185 release_staging
1186 popd || error "pushd failed: dir is [$release_path]"
Joey Armstrong43c48422023-04-03 10:17:32 -04001187# fi
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]