blob: 31c54b7f947f00c9c983924aca850fd6930763ab [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 Armstrong43c48422023-04-03 10:17:32 -040036# declare -i -g gen_version=1
Joey Armstrong78cecc52023-04-03 11:39:11 -040037# declare -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 Armstrong2097d3e2023-03-26 10:32:03 -0400130 ver="v${rev[0]}.${rev[1]}.${rev[2]}"
131
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
279 __ver="$(git tag -l --points-at HEAD)"
280
281 elif [[ -v argv_version_file ]]; then # local debug
282 [[ ! -f VERSION ]] && error "./VERSION file not found"
283 readarray -t tmp < <(sed -e 's/[[:blank:]]//g' 'VERSION')
284 __ver="v${tmp[0]}"
285
286 else
287 __ver="$(git tag -l --points-at HEAD)"
288 fi
Joey Armstrong76026b72023-03-29 12:19:17 -0400289
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400290 # ------------------------------------------------------
291 # match bare versions or v-prefixed golang style version
292 # Critical failure for new/yet-to-be-released repo ?
293 # ------------------------------------------------------
Joey Armstrong43c48422023-04-03 10:17:32 -0400294 if [[ "$__ver" =~ ^v?([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then
295 echo "git has a SemVer released version tag: '$ver'"
296 echo "Building artifacts for GitHub release."
297
298 elif [[ "$__ver" =~ ^v?([0-9]+)\.([0-9]+)\.([0-9]+)-dev([0-9]+)$ ]]; then
299 declare -i -g draft_release=1
300 echo "Detected --draft SemVer release version tag [$__ver]"
301 echo "Building artifacts for GitHub release."
302
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400303 else
Joey Armstrong43c48422023-04-03 10:17:32 -0400304 error "No SemVer released version tag found, exiting..."
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400305 fi
306
Joey Armstrong43c48422023-04-03 10:17:32 -0400307 ## Cache value for subsequent calls.
308 ## readonly is a guarantee assignment happens once.
309 declare -g cached_getGitVersion="$__ver"
310 readonly cached_getGitVersion;
311
312 varname="$__ver"
313 func_echo "Version is [$__ver] from $(/bin/pwd)"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400314 return
315}
316
317## -----------------------------------------------------------------------
318## Intent:
319## Note: Release description is sanitized version of the log message
320## -----------------------------------------------------------------------
321function getReleaseDescription()
322{
323 declare -n varname="$1"; shift
324
325 local msg
326 msg="$(git log -1 --pretty=%B)"
327
328 local val
329 val="$(tr -dc "[:alnum:]\n\r\.\[\]\:\-\\\/\`\' " <<< "$msg")"
330
331 [[ ${#val} -eq 0 ]] && error "Release Description is empty ($msg)"
332 varname="$val"
333 return
334}
335
336## -----------------------------------------------------------------------
337## Intent: Retrieve value of the release temp directory.
338## -----------------------------------------------------------------------
339## Note: Limit use of globals so logic can be isolated in function calls.
340## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400341function get_release_dir()
342{
343 declare -n varname=$1; shift
344
345 # Temporary staging directory to copy artifacts to
346 varname="$scratch/release"
347 mkdir -p "$varname"
348 return
349}
350
351## -----------------------------------------------------------------------
352## Intent: Retrieve github server hostname
353## -----------------------------------------------------------------------
354function get_gh_hostname()
355{
356 declare -n varname=$1; shift
357 varname+=('--hostname' "${__githost}")
358 return
359}
360
361## -----------------------------------------------------------------------
362## Intent: Retrieve repository organizaiton name
363## -----------------------------------------------------------------------
364function get_gh_repo_org()
365{
366 declare -n varname=$1; shift
367 declare -g __repo_org
Joey Armstrong76026b72023-03-29 12:19:17 -0400368
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400369 local org
370 if [[ -v __repo_org ]]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400371 org="$__repo_org"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400372 elif [[ ! -v GITHUB_ORGANIZATION ]]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400373 error "--repo-org or GITHUB_ORGANIZATION= are required"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400374 else
Joey Armstrong43c48422023-04-03 10:17:32 -0400375 org="${GITHUB_ORGANIZATION}"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400376 fi
377
378 # shellcheck disable=SC2178
379 varname="$org"
380 return
381}
382
383## -----------------------------------------------------------------------
384## Intent: Retrieve repository organizaiton name
385## -----------------------------------------------------------------------
386function get_gh_repo_name()
387{
388 declare -n varname=$1; shift
389 declare -g __repo_name
Joey Armstrong76026b72023-03-29 12:19:17 -0400390
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400391 local name
392 if [[ -v __repo_name ]]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400393 name="$__repo_name"
Joey Armstrong39fac652023-03-27 18:50:43 -0400394 elif [[ ! -v GERRIT_PROJECT ]]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400395 error "--repo-name or GERRIT_PROJECT= are required"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400396 else
Joey Armstrong43c48422023-04-03 10:17:32 -0400397 name="${GERRIT_PROJECT}"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400398 fi
399
400 varname="$name"
401 return
402}
403
404## -----------------------------------------------------------------------
405## Intent: Return path for the gh release query
406## -----------------------------------------------------------------------
407function get_gh_releases()
408{
409 declare -n ref="$1"
410
411 local repo_org
412 get_gh_repo_org repo_org
413
414 local repo_name
415 get_gh_repo_name repo_name
416
417 ref="repos/${repo_org}/${repo_name}/releases"
418 func_echo "ref=$ref"
419 return
420}
421
422## -----------------------------------------------------------------------
423## Intent: Retrieve repository path argument
424## -----------------------------------------------------------------------
425function get_argv_repo()
426{
427 declare -n varname=$1; shift
428
429 local repo_org
430 get_gh_repo_org repo_org
Joey Armstrong76026b72023-03-29 12:19:17 -0400431
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400432 local repo_name
Joey Armstrong76026b72023-03-29 12:19:17 -0400433 get_gh_repo_name repo_name
434
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400435 varname="${repo_org}/${repo_name}"
Joey Armstrong43c48422023-04-03 10:17:32 -0400436 # func_echo "VARNAME=$varname"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400437 return
438}
439
440## -----------------------------------------------------------------------
441## Intent: Retrieve release name string "project - version"
442## -----------------------------------------------------------------------
443function get_argv_name()
444{
445 declare -n varname=$1; shift
446
447 local repo_name
448 get_gh_repo_name repo_name
Joey Armstrong43c48422023-04-03 10:17:32 -0400449
450 local repo_ver
451 getGitVersion repo_ver
452
453 # varname="${repo_name} - $GIT_VERSION"
454 varname="${repo_name} - ${repo_ver}"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400455 func_echo "varname=$varname"
456 return
457}
458
459## -----------------------------------------------------------------------
460## Intent: Retrieve tag version
461## -----------------------------------------------------------------------
462function get_argv_tag()
463{
464 declare -n varname=$1; shift
465
466 # cached_argv_tag='v3.41.3204'
467 if [[ ! -v cached_argv_tag ]]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400468 declare -g cached_argv_tag
469 if [[ -v GIT_VERSION ]]; then # hello jenkins
470 cached_argv_tag="$GIT_VERSION"
471 fi
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400472 fi
473
474 [[ ${#cached_argv_tag} -eq 0 ]] && error "Unable to determine GIT_VERSION="
475 varname="$cached_argv_tag"
476 func_echo "varname=$varname"
477 return
478}
479
480## -----------------------------------------------------------------------
481## Intent:
482## -----------------------------------------------------------------------
483# To support golang projects that require GOPATH to be set and code checked out there
484# If $DEST_GOPATH is not an empty string:
485# - create GOPATH within WORKSPACE, and destination directory within
486# - set PATH to include $GOPATH/bin and the system go binaries
487# - move project from $WORKSPACE/$GERRIT_PROJECT to new location in $GOPATH
488# - start release process within that directory
489## -----------------------------------------------------------------------
490function get_release_path()
491{
492 declare -n varname=$1; shift
493
494 DEST_GOPATH=${DEST_GOPATH:-}
495 if [ -n "$DEST_GOPATH" ]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400496 mkdir -p "$GOPATH/src/$DEST_GOPATH"
497 varname="$GOPATH/src/$DEST_GOPATH/$GERRIT_PROJECT"
498 mv "$WORKSPACE/$GERRIT_PROJECT" "$varname"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400499 else
Joey Armstrong43c48422023-04-03 10:17:32 -0400500 varname="$WORKSPACE/$GERRIT_PROJECT"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400501 fi
502
Joey Armstrong43c48422023-04-03 10:17:32 -0400503 ## Verify pwd is OK
504 for path in \
505 "$varname/Makefile"\
506 "$varname/makefile"\
507 "__ERROR__"\
508 ; do
509 case "$path" in
510 __ERROR__) error "Makefile not found at $varname!" ;;
511 *) [[ -f "$path" ]] && break ;;
512 esac
513 done
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400514
515 return
516}
517
518## -----------------------------------------------------------------------
519## Intent: Display future enhancements
520## -----------------------------------------------------------------------
521function todo()
522{
523 local iam="${0##*/}"
524
525cat <<EOT
526
527** -----------------------------------------------------------------------
528** IAM: ${iam} :: ${FUNCNAME[0]}
529** -----------------------------------------------------------------------
530 o get_release_path()
531 - refactor redundant paths into local vars.
532 - see comments, do we have a one-off failure condition ?
Joey Armstrongad7b1e02023-03-27 11:55:48 -0400533 o PATH += golang appended 3 times, release needs a single, reliable answer.
534 o do_login, do_logout and api calls do not use the my_gh wrapper:
535 - Add a lookup function to cache and retrieve path to downloaded gh command.
Joey Armstrong76026b72023-03-29 12:19:17 -0400536
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400537EOT
538
539 return
540}
541
542## -----------------------------------------------------------------------
Joey Armstrong76026b72023-03-29 12:19:17 -0400543## Intent: Verify a directory contains content for release.
544## -----------------------------------------------------------------------
545## Given:
546## scalar Path to release/ directory
547## ref Results returned through this indirect var.
548## -----------------------------------------------------------------------
549## Usage:
550## declare -a artifacts=()
551## get_artifacts '/foo/bar/tans' artifacts
552## declare -p artifacts
553## -----------------------------------------------------------------------
554function get_artifacts()
555{
556 local dir="$1" ; shift
557 declare -n ref=$1 ; shift
558
559 # Glob available files
560 readarray -t __artifacts < <(find "$dir" -mindepth 1 ! -type d)
561 func_echo "$(declare -p __artifacts)"
562
563 # -----------------------------------------------------------------------
564 # Verify count>0 to inhibit source-only release
565 # Problem children:
566 # o build or make release failures.
567 # o docker container filesystem mount problem (~volume)
568 # -----------------------------------------------------------------------
569 [[ ${#__artifacts[@]} -eq 0 ]] \
Joey Armstrong43c48422023-04-03 10:17:32 -0400570 && error "Artifact dir is empty: $(declare -p dir)"
Joey Armstrong76026b72023-03-29 12:19:17 -0400571
572 ref=("${__artifacts[@]}")
573 return
574}
575
576## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400577## Intent: Copy files from the build directory into the release staging
578## directory for publishing to github releases/ endpoint.
Joey Armstrong26707f02023-01-26 12:41:12 -0500579## -----------------------------------------------------------------------
580function copyToRelease()
581{
Joey Armstrong76026b72023-03-29 12:19:17 -0400582 banner ''
Joey Armstrong26707f02023-01-26 12:41:12 -0500583
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400584 local artifact_glob="${ARTIFACT_GLOB%/*}"
585 func_echo "$(declare -p artifact_glob)"
586
587 local work
588 get_release_dir work
Joey Armstrong76026b72023-03-29 12:19:17 -0400589 func_echo "Artifact dir: $(declare -p work)"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400590
Joey Armstrong76026b72023-03-29 12:19:17 -0400591 ## Verify release content is available
592 declare -a artifacts=()
Joey Armstrong43c48422023-04-03 10:17:32 -0400593 # get_artifacts "$work" artifacts
594 get_artifacts "$artifact_glob" artifacts
595 func_echo "Artifacts in copy_from: $(declare -p artifacts)"
Joey Armstrong472bfba2023-03-27 18:12:28 -0400596
Joey Armstrong26707f02023-01-26 12:41:12 -0500597 # Copy artifacts into the release temp dir
598 # shellcheck disable=SC2086
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400599 echo "rsync -rv --checksum \"$artifact_glob/.\" \"$work/.\""
600 rsync -rv --checksum "$artifact_glob/." "$work/."
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500601
Joey Armstrong43c48422023-04-03 10:17:32 -0400602 get_artifacts "$work" artifacts
603 func_echo "Artifacts in copy_to: $(declare -p artifacts)"
604
Joey Armstrong26707f02023-01-26 12:41:12 -0500605 return
606}
607
608## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400609## https://docs.github.com/en/rest/releases?apiVersion=2022-11-28
610 # https://cli.github.com/manual/gh_release_create
611 # --target <branch> or commit SHA
612 # --title
613 # --generate-notes
614 # --release-notes (notes file)
615 # --release
616 # release create dist/*.tgz
617 # --discussion-category "General"
Joey Armstrongf085d872023-01-28 17:52:29 -0500618## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400619# https://cli.github.com/manual/gh_release_create
620## -----------------------------------------------------------------------
621function gh_release_create()
Joey Armstrong38bfeea2023-02-06 18:01:29 -0500622{
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400623 banner ''
Joey Armstrong38bfeea2023-02-06 18:01:29 -0500624
Joey Armstrong43c48422023-04-03 10:17:32 -0400625 local version
626 getGitVersion version
Joey Armstrong38bfeea2023-02-06 18:01:29 -0500627
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400628 local work
629 get_release_dir work
630
Joey Armstrong43c48422023-04-03 10:17:32 -0400631 declare -a args=()
632 args+=('--host-repo')
633 args+=('--title')
634 if [[ -v draft_release ]]; then
635 args+=('--draft')
636 else
637 args+=('--discussion-category' 'Announcements')
638 fi
639
640 # args+=('--notes' "'Testing release create -- ignore'")
641
Joey Armstrong78cecc52023-04-03 11:39:11 -0400642 pushd "$work/.." >/dev/null
643 func_echo "WORK=$work"
644 # readarray -t payload < <(find '.' -maxdepth 4 ! -type d -print)
645 readarray -t payload < <(find 'release' -maxdepth 4 ! -type d -print)
Joey Armstrong76026b72023-03-29 12:19:17 -0400646
Joey Armstrongad7b1e02023-03-27 11:55:48 -0400647 func_echo "$gh_cmd release create ${version} ${args[@]}" "${payload[@]}"
Joey Armstrong43c48422023-04-03 10:17:32 -0400648
649 if [[ -v dry_run ]]; then
650 echo "[SKIP] dry run"
651 else
Joey Armstrong78cecc52023-04-03 11:39:11 -0400652 func_echo "my_gh release create '$version' ${args[@]} ${payload[@]}"
Joey Armstrong43c48422023-04-03 10:17:32 -0400653 my_gh 'release' 'create' "'$version'" "${args[@]}" "${payload[@]}"
654 set +x
655 fi
Joey Armstrong76026b72023-03-29 12:19:17 -0400656 popd >/dev/null
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400657
Joey Armstrong38bfeea2023-02-06 18:01:29 -0500658 return
659}
660
661## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400662## Intent: Authenticate credentials for a github gh session
Joey Armstrong38bfeea2023-02-06 18:01:29 -0500663## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400664## NOTE: my_gh currently unused due to --with-token < "$pac"
665## -----------------------------------------------------------------------
666function do_login()
Joey Armstrongf085d872023-01-28 17:52:29 -0500667{
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400668 declare -g pac
Joey Armstrong485081f2023-03-27 13:34:08 -0400669 declare -a login_args=()
670 [[ $# -gt 0 ]] && login_args+=("$@")
Joey Armstrongf085d872023-01-28 17:52:29 -0500671
Joey Armstrong485081f2023-03-27 13:34:08 -0400672 # https://github.com/cli/cli/issues/2922#issuecomment-775027762
Joey Armstrong75a0d932023-03-28 08:59:54 -0400673 # (sigh) why not quietly return VS forcing a special logic path
Joey Armstronge6e18eb2023-03-27 14:19:21 -0400674 # [[ -v WORKSPACE ]] && [[ -v GITHUB_TOKEN ]] && return
675
676# 12:58:36 ** -----------------------------------------------------------------------
677# 12:58:36 ** jenkins582353203049151829.sh::do_login: --hostname github.com
678# 12:58:36 ** --------------------------------------------------------------------# ---
679# 12:58:36 ** jenkins582353203049151829.sh :: do_login: Detected ENV{GITHUB_TOKEN}=
680# 12:58:36 The value of the GITHUB_TOKEN environment variable is being used for authentication.
681# 12:58:36 To have GitHub CLI store credentials instead, first clear the value from the environment.
Joey Armstrong43c48422023-04-03 10:17:32 -0400682# -----------------------------------------------------------------------
Joey Armstrong76026b72023-03-29 12:19:17 -0400683
Joey Armstrong485081f2023-03-27 13:34:08 -0400684 get_gh_hostname login_args
Joey Armstrongf085d872023-01-28 17:52:29 -0500685
Joey Armstrong485081f2023-03-27 13:34:08 -0400686 banner "${login_args[@]}"
Joey Armstrong78cecc52023-04-03 11:39:11 -0400687 func_echo "$(declare -p WORKSPACE)"
Joey Armstrongf085d872023-01-28 17:52:29 -0500688
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400689 ## Read from disk is safer than export GITHUB_TOKEN=
690 if [[ -v pac ]] && [[ ${#pac} -gt 0 ]]; then # interactive/debugging
Joey Armstrong43c48422023-04-03 10:17:32 -0400691 [ ! -f "$pac" ] && error "PAC token file $pac does not exist"
Joey Armstrong78cecc52023-04-03 11:39:11 -0400692 func_echo "$gh_cmd auth login ${login_args[@]} --with-token < $pac"
Joey Armstrong485081f2023-03-27 13:34:08 -0400693 "$gh_cmd" auth login "${login_args[@]}" --with-token < "$pac"
Joey Armstrongf085d872023-01-28 17:52:29 -0500694
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400695 elif [[ ! -v GITHUB_TOKEN ]]; then
696 error "--token [t] or GITHUB_TOKEN= are required"
Joey Armstrong41923cc2023-01-30 14:38:16 -0500697
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400698 else # jenkins
Joey Armstrong78cecc52023-04-03 11:39:11 -0400699 func_echo "$gh_cmd auth login ${login_args[@]} (ie: jenkins)"
Joey Armstrong43c48422023-04-03 10:17:32 -0400700 "$gh_cmd" auth login "${login_args[@]}"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400701 fi
Joey Armstrongf085d872023-01-28 17:52:29 -0500702
Joey Armstrong43c48422023-04-03 10:17:32 -0400703 declare -i -g active_login=1 # signal logout needed
Joey Armstrongf085d872023-01-28 17:52:29 -0500704
Joey Armstrongf085d872023-01-28 17:52:29 -0500705 return
706}
707
708## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400709## Intent: Destroy credentials/gh session authenticated by do_login
710## -----------------------------------------------------------------------
711## NOTE: my_gh currently unused due to "<<< 'y'"
712## -----------------------------------------------------------------------
713function do_logout()
714{
715 declare -i -g active_login
716 [[ ! -v active_login ]] && return
717
Joey Armstrong78cecc52023-04-03 11:39:11 -0400718 declare -a logout_args=()
719 [[ $# -gt 0 ]] && logout_args+=("$@")
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400720
Joey Armstrong78cecc52023-04-03 11:39:11 -0400721 get_gh_hostname logout_args
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400722
Joey Armstrong78cecc52023-04-03 11:39:11 -0400723 banner "${logout_args[@]}"
724 func_echo "$gh_cmd auth logout ${logout_args[@]} <<< 'Y'"
725 "$gh_cmd" auth logout "${logout_args[@]}" <<< 'Y'
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400726
727 unset active_login
728 return
729}
730
731## -----------------------------------------------------------------------
732## Intent: Query for repository version strings
733## -----------------------------------------------------------------------
734function get_releases()
735{
736 declare -n ref="$1"; shift
737 local func="${FUNCNAME[0]}"
738
739 banner ""
740 pushd "$scratch" >/dev/null
Joey Armstrong76026b72023-03-29 12:19:17 -0400741
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400742 # gh api repos/{owner}/{repo}/releases
743 local releases_uri
744 get_gh_releases releases_uri
745 declare -p releases_uri
Joey Armstrong76026b72023-03-29 12:19:17 -0400746
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400747 ref=()
Joey Armstrongad7b1e02023-03-27 11:55:48 -0400748 "$gh_cmd" api "$releases_uri" "${common[@]}" | jq . > 'release.raw'
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400749 readarray -t __tmp < <(jq '.[] | "\(.tag_name)"' 'release.raw')
750
751 local release
752 for release in "${__tmp[@]}";
753 do
Joey Armstrong43c48422023-04-03 10:17:32 -0400754 release="${release//\"/}"
755 ref+=("$release")
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400756 done
757
758 popd >/dev/null
759 return
760}
761
762## -----------------------------------------------------------------------
763## Intent: Display repository query strings.
764## Indirect: verify authentication and API
765## -----------------------------------------------------------------------
766function showReleases()
767{
Joey Armstrong43c48422023-04-03 10:17:32 -0400768 declare -a raw=()
769 get_releases raw
770
771 ## Sort for display, we may need to prune volume later on
772 IFS=$'\n' releases=($(sort -nr <<<"${raw[*]}"))
773 unset IFS
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400774
775 local release
776 for release in "${releases[@]}";
777 do
Joey Armstrong43c48422023-04-03 10:17:32 -0400778 func_echo "$release"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400779 done
780 return
781}
782
783## -----------------------------------------------------------------------
784## Intent: Install the gh command line tool locally
Joey Armstrong26707f02023-01-26 12:41:12 -0500785## -----------------------------------------------------------------------
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500786function install_gh_binary()
Joey Armstrong26707f02023-01-26 12:41:12 -0500787{
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400788 banner
Joey Armstrong26707f02023-01-26 12:41:12 -0500789
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500790 pushd "$scratch"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400791 func_echo "Retrieve latest gh download URLs"
Joey Armstrongf085d872023-01-28 17:52:29 -0500792
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500793 local latest="https://github.com/cli/cli/releases/latest"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400794 local tarball="gh.tar.tgz"
795
Joey Armstrong76026b72023-03-29 12:19:17 -0400796 readarray -t latest < <(\
797 curl --silent -qI "$latest" \
Joey Armstrong43c48422023-04-03 10:17:32 -0400798 | awk -F '/' '/^location/ {print substr($NF, 1, length($NF)-1)}')
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400799 declare -p latest
800 if [ ${#latest[@]} -ne 1 ]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400801 error "Unable to determine latest gh package version"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400802 fi
Joey Armstrong76026b72023-03-29 12:19:17 -0400803
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400804 local VER="${latest[0]}"
805
806 func_echo "Download latest gh binary"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500807 local url="https://github.com/cli/cli/releases/download/${VER}/gh_${VER#v}_linux_amd64.tar.gz"
Joey Armstrong43c48422023-04-03 10:17:32 -0400808 func_echo "wget --quiet --output-document='$tarball' '$url'"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500809 wget --quiet --output-document="$tarball" "$url"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400810
811 func_echo "Unpack tarball"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500812 tar zxf "$tarball"
813
Joey Armstrong78cecc52023-04-03 11:39:11 -0400814 declare -g gh_cmd
815 gh_cmd="$(find "${scratch}" -name 'gh' -print)"
816 #declare -g gh_cmd='/usr/bin/gh'
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500817 readonly gh_cmd
818
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400819 func_echo "Command: ${gh_cmd}"
820 func_echo "Version: $("$gh_cmd" --version)"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500821 popd
822
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400823 return
824}
825
826## -----------------------------------------------------------------------
827## Intent: Danger Will Robinson
828## -----------------------------------------------------------------------
829function releaseDelete()
830{
831 local version="$1"; shift
Joey Armstrong76026b72023-03-29 12:19:17 -0400832
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400833 banner "${in_args[@]}"
834 declare -a args=()
835 args+=('--host-repo')
836 args+=('--yes')
837 # args+=('--cleanup-tag')
838
839# ** github-release.sh :: get_argv_repo: VARNAME=opencord/voltctl
840#* Running: /tmp/github-release.R7geZo7Ywo/gh_2.25.1_linux_amd64/bin/gh release delete v4.175.710 --repo 'github.com/opencord/voltctl' --yes --cleanup-tag
841#rror connecting to 'github.com
842#heck your internet connection or https://githubstatus.com
843
844 echo
845 echo "==========================================================================="
846 my_gh 'release' 'delete' "$version" "${args[@]}"
847 echo "==========================================================================="
848 echo
849
850 showReleases
851 return
852}
853
854## -----------------------------------------------------------------------
855## Intent: Copy binaries into temp/release, checksum then publish
856## -----------------------------------------------------------------------
857function release_staging()
858{
859 local release_temp
860 get_release_dir release_temp
861
862 banner ''
863 func_echo "Packaging release files"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400864
Joey Armstrong76026b72023-03-29 12:19:17 -0400865 pushd "$release_temp" >/dev/null \
Joey Armstrong43c48422023-04-03 10:17:32 -0400866 || error "pushd failed: dir is [$release_temp]"
Joey Armstrong76026b72023-03-29 12:19:17 -0400867
868 declare -a to_release=()
869 get_artifacts '.' to_release
870
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400871 func_echo "Files to release: $(declare -p to_release)"
872
873 # Generate and check checksums
874 sha256sum -- * > checksum.SHA256
875 sha256sum -c < checksum.SHA256
876
877 echo
878 func_echo "Checksums(checksum.SHA256):"
879 cat checksum.SHA256
880
Joey Armstrong43c48422023-04-03 10:17:32 -0400881 if false; then
Joey Armstrong78cecc52023-04-03 11:39:11 -0400882 # Careful with credentials display
Joey Armstrong43c48422023-04-03 10:17:32 -0400883 get_gh_hostname login_args
884 banner "gh auth status ${login_args[@]}"
885 gh auth status "${login_args[@]}"
886 fi
887
Joey Armstrong76026b72023-03-29 12:19:17 -0400888 gh_release_create # publish
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400889
890 popd >/dev/null || error "pushd failed: dir is [$release_temp]"
891
892 return
893}
894
895## -----------------------------------------------------------------------
896## Intent: Display program usage
897## -----------------------------------------------------------------------
898function usage()
899{
900 [[ $# -gt 0 ]] && func_echo "$*"
901
902 cat <<EOH
903Usage: github-release.sh [options] [target] ...
904
905[Github CLI (gh) arguments]
906 --login Perform authentication using a PAC
907 --logout
908 --host [h] Specify github server for connection.
909
910[Options]
911 --token Login debugging, alternative to env var use.
912
913[Modes]
914 --debug Enable script debug mode
915 --verbose Enable script verbose mode
916
917All remaining arguments are passthrough to the gh command.
918EOH
919
920 exit 0
921}
922
923## -----------------------------------------------------------------------
Joey Armstrong43c48422023-04-03 10:17:32 -0400924## Intent: Normalize common arguments and access to the gh command.
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400925## o Cache path to the gh command
926## o Construct a gh command line from given args
927## o Command wrapper can provide defaults (--hostname github.com)
928## -----------------------------------------------------------------------
929## Given:
930## scalar Array variable name (declare -n is a reference)
931## Return:
932## ref gh command line passed back to caller
933## Switches:
934## --host Pass default github hostname
935## --verbose Enable verbose mode#
936## --version Display command version
937## @array Remaining arguments passed as command switches.
938## -----------------------------------------------------------------------
939## See Also:
940## o https://cli.github.com/manual
941## -----------------------------------------------------------------------
942function my_gh()
943{
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400944 declare -a cmd=()
945 cmd+=("$gh_cmd")
946
947 ## ----------------------
948 ## Construct command line
949 ## ----------------------
950 # shellcheck disable=SC2034
951 declare -A action=() # pending operations
952 declare -a args=() # common gh command line args
953
954 while [ $# -gt 0 ]; do
955 local arg="$1"; shift
956 case "$arg" in
957
Joey Armstrong43c48422023-04-03 10:17:32 -0400958 # Modes
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400959 -*debug) declare -i -g debug=1 ;;
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400960 -*verbose) args+=('--verbose') ;;
961
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 Armstrong43c48422023-04-03 10:17:32 -0400974 --repo)
975 local val
976 get_argv_repo val
977 args+=('--repo' "$val")
978 ;;
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400979
Joey Armstrong43c48422023-04-03 10:17:32 -0400980 --tag)
981 local val
982 get_argv_tag val
983 args+=("'$val'") # No switch, pass inline
984 ;;
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400985
Joey Armstrong43c48422023-04-03 10:17:32 -0400986 --title)
987 local val
988 get_argv_name val
989 args+=('--title' "'$val'")
990 ;;
Joey Armstrong76026b72023-03-29 12:19:17 -0400991
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400992 *) args+=("$arg") ;;
993 esac
994 done
Joey Armstrong76026b72023-03-29 12:19:17 -0400995
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400996 cmd+=("${args[@]}")
997 echo "** Running: ${cmd[*]}"
998 "${cmd[@]}"
999 local status=$?
1000
1001 [[ $status -eq 0 ]] && { true; } || { false; }
1002 return
1003}
1004
1005## ---------------------------------------------------------------------------
1006## Intent:
1007## ---------------------------------------------------------------------------
1008function usage()
1009{
1010 cat <<EOH
Joey Armstrong76026b72023-03-29 12:19:17 -04001011
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001012Usage: $0
1013Usage: make [options] [target] ...
1014 --help This mesage
1015 --pac Personal Access Token (file)
1016
1017[DEBUG]
1018 --gen-version Generate a random release version string.
1019 --git-hostname Git server hostname (default=github.com)
Joey Armstrong43c48422023-04-03 10:17:32 -04001020 --version-file Read version string from local version file
1021
1022 --dry-run Simulation mode
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001023
1024EOH
1025 return
1026}
1027
1028## ---------------------------------------------------------------------------
1029## Intent: Parse script command line arguments
1030## ---------------------------------------------------------------------------
1031function parse_args()
1032{
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001033 [[ -v DEBUG ]] && func_echo "ENTER"
1034
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001035 while [ $# -gt 0 ]; do
Joey Armstrong43c48422023-04-03 10:17:32 -04001036 local arg="$1"; shift
1037 func_echo "ARGV: $arg"
1038 case "$arg" in
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001039
Joey Armstrong43c48422023-04-03 10:17:32 -04001040 -*debug) declare -i -g debug=1 ;;
1041 --draft) declare -i -g draft_release=1 ;;
1042 --dry-run) declare -i -g dry_run=1 ;;
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001043
Joey Armstrong43c48422023-04-03 10:17:32 -04001044 --version-file)
1045 declare -i -g argv_version_file=1
1046 ;;
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001047
Joey Armstrong43c48422023-04-03 10:17:32 -04001048 -*gen-version)
1049 declare -g -i argv_gen_version=1
1050 ;;
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001051
Joey Armstrong43c48422023-04-03 10:17:32 -04001052 -*git-hostname)
1053 __githost="$1"; shift
1054 ;;
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001055
Joey Armstrong43c48422023-04-03 10:17:32 -04001056 -*repo-name)
1057 __repo_name="$1"; shift
1058 ;;
1059
1060 -*repo-org)
1061 __repo_org="$1"; shift
1062 ;;
1063
1064 -*pac)
1065 declare -g pac="$1"; shift
1066 readonly pac
1067 [[ ! -f "$pac" ]] && error "--token= does not exist ($pac)"
1068 : # nop/true
1069 ;;
1070
1071 -*todo) todo ;;
1072
1073 -*help)
1074 usage
1075 exit 0
1076 ;;
1077
1078 *) error "Detected unknown argument $arg" ;;
1079 esac
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001080 done
1081
Joey Armstrong26707f02023-01-26 12:41:12 -05001082 return
1083}
1084
Joey Armstrongaf577ab2022-12-15 14:43:33 -05001085##----------------##
1086##---] MAIN [---##
1087##----------------##
Joey Armstrong1962bcf2023-01-27 13:53:18 -05001088iam="${0##*/}"
1089
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001090full_banner
1091parse_args $@
Joey Armstrongd99e3d22023-01-29 12:35:43 -05001092init
1093install_gh_binary
Joey Armstrong7f382ef2023-01-25 12:00:08 -05001094
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001095do_login
Zack Williams27cd3e52018-09-18 16:44:50 -07001096
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001097release_path='/dev/null'
1098get_release_path release_path
1099declare -p release_path
Zack Williams27cd3e52018-09-18 16:44:50 -07001100
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001101pushd "$release_path" || error "pushd failed: dir is [$release_path]"
Zack Williams27cd3e52018-09-18 16:44:50 -07001102
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001103 # legacy: getGitVersion "$GERRIT_PROJECT" GIT_VERSION
1104 getGitVersion GIT_VERSION
1105 getReleaseDescription RELEASE_DESCRIPTION
Zack Williams27cd3e52018-09-18 16:44:50 -07001106
Joey Armstrongeef8c2c2023-03-27 17:27:43 -04001107 cat <<EOM
1108
1109** -----------------------------------------------------------------------
1110** GIT VERSION: $(declare -p GIT_VERSION)
1111** RELEASE_DESCRIPTION: $(declare -p RELEASE_DESCRIPTION)
1112** RELEASE_TARGETS: $(declare -p RELEASE_TARGETS)
Joey Armstrong43c48422023-04-03 10:17:32 -04001113** -----------------------------------------------------------------------
1114** Running: make ${RELEASE_TARGETS}
Joey Armstrongeef8c2c2023-03-27 17:27:43 -04001115** -----------------------------------------------------------------------
1116EOM
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001117 # build the release, can be multiple space separated targets
1118 # -----------------------------------------------------------------------
1119 # % go build command-line-arguments:
1120 # copying /tmp/go-build4212845548/b001/exe/a.out:
1121 # open release/voltctl-1.8.25-linux-amd64: permission denied
1122 # missing: docker run mkdir
1123 # -----------------------------------------------------------------------
1124 # shellcheck disable=SC2086
Joey Armstrongeef8c2c2023-03-27 17:27:43 -04001125 make "$RELEASE_TARGETS"
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001126 copyToRelease
Joey Armstrong50f6e0b2023-01-24 14:14:08 -05001127
Joey Armstrong26707f02023-01-26 12:41:12 -05001128 cat <<EOM
Joey Armstrong1962bcf2023-01-27 13:53:18 -05001129
1130** -----------------------------------------------------------------------
1131** Create the release:
1132** 1) Create initial github release with download area.
1133** 2) Generate checksum.SHA256 for all released files.
1134** 3) Upload files to complete the release.
Joey Armstrong75a0d932023-03-28 08:59:54 -04001135** 4) Display released info from github
Joey Armstrong1962bcf2023-01-27 13:53:18 -05001136** -----------------------------------------------------------------------
Joey Armstrong26707f02023-01-26 12:41:12 -05001137EOM
1138
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001139 showReleases
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001140 release_staging
1141 popd || error "pushd failed: dir is [$release_path]"
Joey Armstrong43c48422023-04-03 10:17:32 -04001142# fi
Joey Armstrong28eddda2023-01-10 03:09:34 -05001143
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001144do_logout
1145
Joey Armstrong28eddda2023-01-10 03:09:34 -05001146# [SEE ALSO]
1147# -----------------------------------------------------------------------
1148# https://www.shellcheck.net/wiki/SC2236
Joey Armstrong26707f02023-01-26 12:41:12 -05001149# https://docs.github.com/en/rest/releases/releases?apiVersion=2022-11-28#create-a-release
Joey Armstrong28eddda2023-01-10 03:09:34 -05001150# -----------------------------------------------------------------------
Joey Armstrongf085d872023-01-28 17:52:29 -05001151# https://cli.github.com/manual/gh_help_reference
1152# https://cli.github.com/manual/gh_release
1153# -----------------------------------------------------------------------
Joey Armstrong28eddda2023-01-10 03:09:34 -05001154
1155# [EOF]