blob: 85e15c1c49db8d5dce2e2e1dae1d9e1255818c50 [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
30declare -g __githost='github.com'
31
32## -----------------------------------------------------------------------
33## Uncomment to activate
34## -----------------------------------------------------------------------
35declare -i -g draft_release=1
Joey Armstrong41923cc2023-01-30 14:38:16 -050036# declare -g TRACE=0 # uncomment to set -x
37
38# shellcheck disable=SC2015
39[[ -v TRACE ]] && { set -x; } || { set +x; } # SC2015 (shellcheck -x)
40
Joey Armstrong2097d3e2023-03-26 10:32:03 -040041declare -a -g ARGV=() # Capture args to minimize globals and arg passing
42[[ $# -gt 0 ]] && ARGV=("$@")
Joey Armstrongd99e3d22023-01-29 12:35:43 -050043
44declare -g scratch # temp workspace for downloads
45declare -g gh_cmd # path to gh command
46
Joey Armstrong2097d3e2023-03-26 10:32:03 -040047declare -g SCRIPT_VERSION='1.2' # git changeset needed
Joey Armstrong26707f02023-01-26 12:41:12 -050048
Joey Armstrong7f382ef2023-01-25 12:00:08 -050049##--------------------##
50##---] INCLUDES [---##
Joey Armstrong1962bcf2023-01-27 13:53:18 -050051##--------------------#
Joey Armstrong2097d3e2023-03-26 10:32:03 -040052
53## -----------------------------------------------------------------------
54## Intent: Register an interrupt handler to display a stack trace on error
55## -----------------------------------------------------------------------
56function errexit()
57{
58 local err=$?
59 set +o xtrace
60 local code="${1:-1}"
61
62 local prefix="${BASH_SOURCE[1]}:${BASH_LINENO[0]}"
63 echo -e "\nOFFENDER: ${prefix}"
64 if [ $# -gt 0 ] && [ "$1" == '--stacktrace-quiet' ]; then
65 code=1
66 else
67 echo "ERROR: '${BASH_COMMAND}' exited with status $err"
68 fi
69
70 # Print out the stack trace described by $function_stack
71 if [ ${#FUNCNAME[@]} -gt 2 ]
72 then
73 echo "Call tree:"
74 for ((i=1;i<${#FUNCNAME[@]}-1;i++))
75 do
76 echo " $i: ${BASH_SOURCE[$i+1]}:${BASH_LINENO[$i]} ${FUNCNAME[$i]}(...)"
77 done
78 fi
79
80 echo "Exiting with status ${code}"
81 echo
82 exit "${code}"
83 # return
84}
85
86# trap ERR to provide an error handler whenever a command exits nonzero
87# this is a more verbose version of set -o errexit
88trap errexit ERR
89
90# setting errtrace allows our ERR trap handler to be propagated to functions,
91# expansions and subshells
92set -o errtrace
Joey Armstrong7f382ef2023-01-25 12:00:08 -050093
94## -----------------------------------------------------------------------
Joey Armstrongd99e3d22023-01-29 12:35:43 -050095## Intent: Cleanup scratch area on exit
96## -----------------------------------------------------------------------
97function sigtrap()
98{
99 ## Prevent mishaps
100 local is_read_only
101 is_read_only="$(declare -p scratch)"
102 if [[ $is_read_only != *"declare -r scratch="* ]]; then
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400103 echo "ERROR: variable scratch is not read-only, cleanup skipped"
104 exit 1
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500105 fi
106
107 if [ -d "$scratch" ]; then
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400108 /bin/rm -fr "$scratch"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500109 fi
110
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400111 do_logout
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500112 return
113}
114trap sigtrap EXIT
115
116## -----------------------------------------------------------------------
Joey Armstrongad7b1e02023-03-27 11:55:48 -0400117## Intent: Return a release version for queries
118## Note: Do not use in production, function is intended for interactive use
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400119## -----------------------------------------------------------------------
120function get_version()
121{
122 declare -n ref="$1"
123
124 banner
125 declare -a rev=()
126 rev+=("$(( RANDOM % 10 + 1 ))")
127 rev+=("$(( RANDOM % 256 + 1 ))")
128 rev+=("$(( 6RANDOM % 10000 + 1 ))")
129 ver="v${rev[0]}.${rev[1]}.${rev[2]}"
130
131 func_echo "version: $ver"
132 ref="$ver"
133 return
134}
135
136## -----------------------------------------------------------------------
137## Intent: Provide defaults for environment variables
138## -----------------------------------------------------------------------
139function initEnvVars()
140{
141 # when not running under Jenkins, use current dir as workspace and a blank
142 # project name
143 declare -g WORKSPACE=${WORKSPACE:-.}
144 declare -g GERRIT_PROJECT=${GERRIT_PROJECT:-}
145
146 # Github organization (or user) this project is published on. Project name should
147 # be the same on both Gerrit and GitHub
Joey Armstrongad7b1e02023-03-27 11:55:48 -0400148 declare -g GITHUB_ORGANIZATION=${GITHUB_ORGANIZATION:-}
149
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400150 # glob pattern relative to project dir matching release artifacts
151 # ARTIFACT_GLOB=${ARTIFACT_GLOB:-"release/*"} # stat -- release/* not found, literal string (?)
152 declare -g ARTIFACT_GLOB=${ARTIFACT_GLOB:-"release/."}
153
154 # Use "release" as the default makefile target, can be a space separated list
155 declare -g RELEASE_TARGETS=${RELEASE_TARGETS:-release}
156
157 # Set and handle GOPATH and PATH
158 export GOPATH=${GOPATH:-$WORKSPACE/go}
159 export PATH=$PATH:/usr/lib/go-1.12/bin:/usr/local/go/bin:$GOPATH/bin
160 return
161}
162
163## -----------------------------------------------------------------------
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500164## Intent: Create a scratch area for downloads and transient tools
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400165## temp directory will be automatically removed upon exit.
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500166## -----------------------------------------------------------------------
167function init()
168{
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500169 local pkgbase="${0##*/}" # basename
170 local pkgname="${pkgbase%.*}"
171
Joey Armstrongad7b1e02023-03-27 11:55:48 -0400172 # initEnvVars # moved to full_banner()
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400173
174 ## Create a temp directory for auto-cleanup
175 declare -g scratch
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500176 scratch="$(mktemp -d -t "${pkgname}.XXXXXXXXXX")"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400177 readonly scratch
178 declare -p scratch
179
180 ## prime the stream
181 local work
182 get_release_dir work
183 declare -p work
184 return
185}
186
187## -----------------------------------------------------------------------
188## Intent: Verbose output for logging
189## -----------------------------------------------------------------------
190function banner()
191{
192 local iam="${0##*/}"
193 cat <<EOB
194
195** -----------------------------------------------------------------------
196** ${iam}::${FUNCNAME[1]}: $*
197** -----------------------------------------------------------------------
198EOB
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500199}
200
201## -----------------------------------------------------------------------
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500202## Intent: Output a log banner to identify the running script/version.
203## -----------------------------------------------------------------------
204## TODO:
205## o SCRIPT_VERSION => git changeset for repo:ci-managment
206## o echo "library version: ${env."library.libName.version"}"
207# -----------------------------------------------------------------------
208# 14:18:38 > git fetch --no-tags --progress -- https://gerrit.opencord.org/ci-management.git +refs/heads/*:refs/remotes/origin/* # timeout=10
209# 14:18:39 Checking out Revision 50f6e0b97f449b32d32ec0e02d59642000351847 (master)
210# -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400211function full_banner()
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500212{
Joey Armstrongad7b1e02023-03-27 11:55:48 -0400213 local iam="${0##*/}"
214
215 initEnvVars # set defaults
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500216
217cat <<EOH
218
219** -----------------------------------------------------------------------
220** IAM: ${iam} :: ${FUNCNAME[0]}
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400221** ARGV: ${ARGV[@]}
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500222** PWD: $(/bin/pwd)
223** NOW: $(date '+%Y/%m/%d %H:%M:%S')
224** VER: ${SCRIPT_VERSION:-'unknown'}
225** -----------------------------------------------------------------------
Joey Armstrongad7b1e02023-03-27 11:55:48 -0400226** GERRIT_PROJECT: $(declare -p GERRIT_PROJECT)
227** GITHUB_ORGANIZATION: $(declare -p GITHUB_ORGANIZATION)
228** RELEASE_TARGETS: $(declare -p RELEASE_TARGETS)
229** GOPATH: $(declare -p GOPATH)
230** -----------------------------------------------------------------------
231** PATH += /usr/lib/go-1.12/bin:/usr/local/go/bin:GOPATH/bin
232** -----------------------------------------------------------------------
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500233EOH
234
235 return
236}
237
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500238## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400239## Intent: Display a message with 'iam' identifier for logging
Joey Armstrong50f6e0b2023-01-24 14:14:08 -0500240## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400241function func_echo()
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500242{
243 local iam="${0##*/}"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400244 echo "** ${iam} :: ${FUNCNAME[1]}: $*"
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500245 return
246}
247
248## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400249## Intent: Display an error message then exit with status.
250## -----------------------------------------------------------------------
251function error()
252{
253 local iam="${0##*/}"
254 echo "ERROR ${iam} :: ${FUNCNAME[1]}: $*"
255 exit 1
256}
257
258## -----------------------------------------------------------------------
259## Intent: Verify sandbox/build is versioned for release.
260## -----------------------------------------------------------------------
261function getGitVersion()
262{
263 declare -n varname="$1"; shift
264 local ver
265
266 banner
267 ver="$(git tag -l --points-at HEAD)"
268 declare -p ver
269
270 get_version 'ver'
271 declare -p ver
272
273 # ------------------------------------------------------
274 # match bare versions or v-prefixed golang style version
275 # Critical failure for new/yet-to-be-released repo ?
276 # ------------------------------------------------------
277 if [[ "$ver" =~ ^v?([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]
278 then
279 echo "git has a SemVer released version tag: '$ver'"
280 echo "Building artifacts for GitHub release."
281 else
282 error "No SemVer released version tag found, exiting..."
283 fi
284
285 varname="$ver"
286 func_echo "GIT_VERSION is [$GIT_VERSION] from $(/bin/pwd)"
287 return
288}
289
290## -----------------------------------------------------------------------
291## Intent:
292## Note: Release description is sanitized version of the log message
293## -----------------------------------------------------------------------
294function getReleaseDescription()
295{
296 declare -n varname="$1"; shift
297
298 local msg
299 msg="$(git log -1 --pretty=%B)"
300
301 local val
302 val="$(tr -dc "[:alnum:]\n\r\.\[\]\:\-\\\/\`\' " <<< "$msg")"
303
304 [[ ${#val} -eq 0 ]] && error "Release Description is empty ($msg)"
305 varname="$val"
306 return
307}
308
309## -----------------------------------------------------------------------
310## Intent: Retrieve value of the release temp directory.
311## -----------------------------------------------------------------------
312## Note: Limit use of globals so logic can be isolated in function calls.
313## -----------------------------------------------------------------------
314# declare -g RELEASE_TEMP
315function get_release_dir()
316{
317 declare -n varname=$1; shift
318
319 # Temporary staging directory to copy artifacts to
320 varname="$scratch/release"
321 mkdir -p "$varname"
322 return
323}
324
325## -----------------------------------------------------------------------
326## Intent: Retrieve github server hostname
327## -----------------------------------------------------------------------
328function get_gh_hostname()
329{
330 declare -n varname=$1; shift
331 varname+=('--hostname' "${__githost}")
332 return
333}
334
335## -----------------------------------------------------------------------
336## Intent: Retrieve repository organizaiton name
337## -----------------------------------------------------------------------
338function get_gh_repo_org()
339{
340 declare -n varname=$1; shift
341 declare -g __repo_org
342
343 local org
344 if [[ -v __repo_org ]]; then
345 org="$__repo_org"
346 elif [[ ! -v GITHUB_ORGANIZATION ]]; then
347 error "--repo-org or GITHUB_ORGANIZATION= are required"
348 else
349 org="${GITHUB_ORGANIZATION}"
350 fi
351
352 # shellcheck disable=SC2178
353 varname="$org"
354 return
355}
356
357## -----------------------------------------------------------------------
358## Intent: Retrieve repository organizaiton name
359## -----------------------------------------------------------------------
360function get_gh_repo_name()
361{
362 declare -n varname=$1; shift
363 declare -g __repo_name
364
365 local name
366 if [[ -v __repo_name ]]; then
367 name="$__repo_name"
368 elif [[ ! -v GITHUB_PROJECT ]]; then
369 error "--repo-name or GITHUB_PROJECT= are required"
370 else
371 name="${GITHUB_PROJECT}"
372 fi
373
374 varname="$name"
375 return
376}
377
378## -----------------------------------------------------------------------
379## Intent: Return path for the gh release query
380## -----------------------------------------------------------------------
381function get_gh_releases()
382{
383 declare -n ref="$1"
384
385 local repo_org
386 get_gh_repo_org repo_org
387
388 local repo_name
389 get_gh_repo_name repo_name
390
391 ref="repos/${repo_org}/${repo_name}/releases"
392 func_echo "ref=$ref"
393 return
394}
395
396## -----------------------------------------------------------------------
397## Intent: Retrieve repository path argument
398## -----------------------------------------------------------------------
399function get_argv_repo()
400{
401 declare -n varname=$1; shift
402
403 local repo_org
404 get_gh_repo_org repo_org
405
406 local repo_name
407 get_gh_repo_name repo_name
408
409 varname="${repo_org}/${repo_name}"
410 func_echo "VARNAME=$varname"
411 return
412}
413
414## -----------------------------------------------------------------------
415## Intent: Retrieve release name string "project - version"
416## -----------------------------------------------------------------------
417function get_argv_name()
418{
419 declare -n varname=$1; shift
420
421 local repo_name
422 get_gh_repo_name repo_name
423 varname="${repo_name} - $GIT_VERSION"
424 func_echo "varname=$varname"
425 return
426}
427
428## -----------------------------------------------------------------------
429## Intent: Retrieve tag version
430## -----------------------------------------------------------------------
431function get_argv_tag()
432{
433 declare -n varname=$1; shift
434
435 # cached_argv_tag='v3.41.3204'
436 if [[ ! -v cached_argv_tag ]]; then
437 declare -g cached_argv_tag
438 if [[ -v GIT_VERSION ]]; then # hello jenkins
439 cached_argv_tag="$GIT_VERSION"
440 fi
441 fi
442
443 [[ ${#cached_argv_tag} -eq 0 ]] && error "Unable to determine GIT_VERSION="
444 varname="$cached_argv_tag"
445 func_echo "varname=$varname"
446 return
447}
448
449## -----------------------------------------------------------------------
450## Intent:
451## -----------------------------------------------------------------------
452# To support golang projects that require GOPATH to be set and code checked out there
453# If $DEST_GOPATH is not an empty string:
454# - create GOPATH within WORKSPACE, and destination directory within
455# - set PATH to include $GOPATH/bin and the system go binaries
456# - move project from $WORKSPACE/$GERRIT_PROJECT to new location in $GOPATH
457# - start release process within that directory
458## -----------------------------------------------------------------------
459function get_release_path()
460{
461 declare -n varname=$1; shift
462
463 DEST_GOPATH=${DEST_GOPATH:-}
464 if [ -n "$DEST_GOPATH" ]; then
465 # if [[ -v DEST_GOPATH ]] && [[ -n DEST_GOPATH ]]; then
466 ## [jenkins] Suspect this will taint the golang installation.
467 ## [jenkins] Install succeeds, release fails, next job affected due to corruption.
468 ## [jenkins] Copy golang to temp then augment ?
469 ## [jenkins] Worst case (problematic) backup then restore
470 mkdir -p "$GOPATH/src/$DEST_GOPATH"
471 varname="$GOPATH/src/$DEST_GOPATH/$GERRIT_PROJECT"
472 mv "$WORKSPACE/$GERRIT_PROJECT" "$varname"
473
474 ## [TODO] - support local dev use
475 # elif [[ ! -v WORKSPACE ]] && [[ -d '.git' ]]; then
476 # project=$(git remote -v show | awk -F' ' '{print $2}' | xargs basename)
477 # project=voltctl.git
478
479 else
480 varname="$WORKSPACE/$GERRIT_PROJECT"
481 fi
482
483 if [ ! -f "$varname/Makefile" ]; then
484 :
485 elif [ ! -f "$varname/makefile" ]; then
486 :
487 else
488 error "Makefile not found at $varname!"
489 fi
490
491 return
492}
493
494## -----------------------------------------------------------------------
495## Intent: Display future enhancements
496## -----------------------------------------------------------------------
497function todo()
498{
499 local iam="${0##*/}"
500
501cat <<EOT
502
503** -----------------------------------------------------------------------
504** IAM: ${iam} :: ${FUNCNAME[0]}
505** -----------------------------------------------------------------------
506 o get_release_path()
507 - refactor redundant paths into local vars.
508 - see comments, do we have a one-off failure condition ?
Joey Armstrongad7b1e02023-03-27 11:55:48 -0400509 o PATH += golang appended 3 times, release needs a single, reliable answer.
510 o do_login, do_logout and api calls do not use the my_gh wrapper:
511 - Add a lookup function to cache and retrieve path to downloaded gh command.
512
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400513EOT
514
515 return
516}
517
518## -----------------------------------------------------------------------
519## Intent: Copy files from the build directory into the release staging
520## directory for publishing to github releases/ endpoint.
Joey Armstrong26707f02023-01-26 12:41:12 -0500521## -----------------------------------------------------------------------
522function copyToRelease()
523{
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400524 func_echo "ENTER"
Joey Armstrong26707f02023-01-26 12:41:12 -0500525
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400526 local artifact_glob="${ARTIFACT_GLOB%/*}"
527 func_echo "$(declare -p artifact_glob)"
528
529 local work
530 get_release_dir work
531
532 ## Flatten filesystem, should we recurse here to release subdirs ?
533 # cp $(which ls) "$work"
534 # readarray -t arts < <(find "$artifact_glob" -type f)
535 readarray -t arts < <(find "$work" -type f)
536 [[ ${#arts[@]} -eq 0 ]] && error "Artifact dir is empty, check for build failures: $artifact_glob"
537
Joey Armstrong26707f02023-01-26 12:41:12 -0500538 # Copy artifacts into the release temp dir
539 # shellcheck disable=SC2086
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400540 echo "rsync -rv --checksum \"$artifact_glob/.\" \"$work/.\""
541 rsync -rv --checksum "$artifact_glob/." "$work/."
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500542
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400543 func_echo "LEAVE"
Joey Armstrong26707f02023-01-26 12:41:12 -0500544
Joey Armstrong26707f02023-01-26 12:41:12 -0500545 return
546}
547
548## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400549## https://docs.github.com/en/rest/releases?apiVersion=2022-11-28
550 # https://cli.github.com/manual/gh_release_create
551 # --target <branch> or commit SHA
552 # --title
553 # --generate-notes
554 # --release-notes (notes file)
555 # --release
556 # release create dist/*.tgz
557 # --discussion-category "General"
Joey Armstrongf085d872023-01-28 17:52:29 -0500558## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400559# https://cli.github.com/manual/gh_release_create
560## -----------------------------------------------------------------------
561function gh_release_create()
Joey Armstrong38bfeea2023-02-06 18:01:29 -0500562{
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400563 banner ''
Joey Armstrong38bfeea2023-02-06 18:01:29 -0500564
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400565 local version="$GIT_VERSION"
566# get_version 'version'
567# create_release_by_version "$version"
Joey Armstrong38bfeea2023-02-06 18:01:29 -0500568
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400569 declare -a args=()
570 args+=('--host-repo')
571 args+=('--notes' "'Testing release create -- ignore'")
572 args+=('--title')
573 if [[ -v draft_release ]]; then
574 args+=('--draft')
575 else
576 args+=('--discussion-category' 'Announcements')
577 fi
Joey Armstrong38bfeea2023-02-06 18:01:29 -0500578
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400579 local work
580 get_release_dir work
581
582 # pushd "$work" >/dev/null
583 readarray -t payload < <(find "$work" ! -type d -print)
Joey Armstrongad7b1e02023-03-27 11:55:48 -0400584 func_echo "$gh_cmd release create ${version} ${args[@]}" "${payload[@]}"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400585 my_gh 'release' 'create' "'$version'" "${args[@]}" "${payload[@]}"
586 # popd >/dev/null
587
Joey Armstrong38bfeea2023-02-06 18:01:29 -0500588 return
589}
590
591## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400592## Intent: Authenticate credentials for a github gh session
Joey Armstrong38bfeea2023-02-06 18:01:29 -0500593## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400594## NOTE: my_gh currently unused due to --with-token < "$pac"
595## -----------------------------------------------------------------------
596function do_login()
Joey Armstrongf085d872023-01-28 17:52:29 -0500597{
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400598 declare -g pac
599 declare -a in_args=()
600 [[ $# -gt 0 ]] && in_args+=("$@")
Joey Armstrongf085d872023-01-28 17:52:29 -0500601
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400602 # bridge to my_gh()
603 get_gh_hostname in_args
Joey Armstrongf085d872023-01-28 17:52:29 -0500604
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400605 banner "${in_args[@]}"
Joey Armstrongf085d872023-01-28 17:52:29 -0500606
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400607 ## Read from disk is safer than export GITHUB_TOKEN=
608 if [[ -v pac ]] && [[ ${#pac} -gt 0 ]]; then # interactive/debugging
609 [ ! -f "$pac" ] && error "PAC token file $pac does not exist"
610 # func_echo "--token file is $pac"
Joey Armstrongad7b1e02023-03-27 11:55:48 -0400611 "$gh_cmd" auth login "${in_args[@]}" --with-token < "$pac"
Joey Armstrongf085d872023-01-28 17:52:29 -0500612
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400613 elif [[ ! -v GITHUB_TOKEN ]]; then
614 error "--token [t] or GITHUB_TOKEN= are required"
Joey Armstrong41923cc2023-01-30 14:38:16 -0500615
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400616 else # jenkins
617 func_echo 'Detected ENV{GITHUB_TOKEN}='
Joey Armstrongad7b1e02023-03-27 11:55:48 -0400618 "$gh_cmd" auth login "${in_args[@]}"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400619 fi
Joey Armstrongf085d872023-01-28 17:52:29 -0500620
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400621 declare -i -g active_login=1 # signal logout
Joey Armstrongf085d872023-01-28 17:52:29 -0500622
Joey Armstrongf085d872023-01-28 17:52:29 -0500623 return
624}
625
626## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400627## Intent: Destroy credentials/gh session authenticated by do_login
628## -----------------------------------------------------------------------
629## NOTE: my_gh currently unused due to "<<< 'y'"
630## -----------------------------------------------------------------------
631function do_logout()
632{
633 declare -i -g active_login
634 [[ ! -v active_login ]] && return
635
636 declare -a out_args=()
637 [[ $# -gt 0 ]] && out_args+=("$@")
638
639 # bridge to my_gh()
640
641 get_gh_hostname in_args
642
643 banner "${out_args[@]}"
Joey Armstrongad7b1e02023-03-27 11:55:48 -0400644 "$gh_cmd" auth logout "${out_args[@]}" <<< 'Y'
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400645
646 unset active_login
647 return
648}
649
650## -----------------------------------------------------------------------
651## Intent: Query for repository version strings
652## -----------------------------------------------------------------------
653function get_releases()
654{
655 declare -n ref="$1"; shift
656 local func="${FUNCNAME[0]}"
657
658 banner ""
659 pushd "$scratch" >/dev/null
660
661 # gh api repos/{owner}/{repo}/releases
662 local releases_uri
663 get_gh_releases releases_uri
664 declare -p releases_uri
665
666 ref=()
Joey Armstrongad7b1e02023-03-27 11:55:48 -0400667 "$gh_cmd" api "$releases_uri" "${common[@]}" | jq . > 'release.raw'
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400668 readarray -t __tmp < <(jq '.[] | "\(.tag_name)"' 'release.raw')
669
670 local release
671 for release in "${__tmp[@]}";
672 do
673 release="${release//\"/}"
674 ref+=("$release")
675 done
676
677 popd >/dev/null
678 return
679}
680
681## -----------------------------------------------------------------------
682## Intent: Display repository query strings.
683## Indirect: verify authentication and API
684## -----------------------------------------------------------------------
685function showReleases()
686{
687 declare -a releases=()
688 get_releases releases
689
690 local release
691 for release in "${releases[@]}";
692 do
693 func_echo "$release"
694 done
695 return
696}
697
698## -----------------------------------------------------------------------
699## Intent: Install the gh command line tool locally
Joey Armstrong26707f02023-01-26 12:41:12 -0500700## -----------------------------------------------------------------------
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500701function install_gh_binary()
Joey Armstrong26707f02023-01-26 12:41:12 -0500702{
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400703 banner
Joey Armstrong26707f02023-01-26 12:41:12 -0500704
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500705 pushd "$scratch"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400706 func_echo "Retrieve latest gh download URLs"
Joey Armstrongf085d872023-01-28 17:52:29 -0500707
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500708 local latest="https://github.com/cli/cli/releases/latest"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400709 local tarball="gh.tar.tgz"
710
711 readarray -t latest < <(curl --silent -qI "$latest" \
712 | awk -F '/' '/^location/ {print substr($NF, 1, length($NF)-1)}')
713 declare -p latest
714 if [ ${#latest[@]} -ne 1 ]; then
715 error "Unable to determine latest gh package version"
716 fi
717
718 local VER="${latest[0]}"
719
720 func_echo "Download latest gh binary"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500721 local url="https://github.com/cli/cli/releases/download/${VER}/gh_${VER#v}_linux_amd64.tar.gz"
722 wget --quiet --output-document="$tarball" "$url"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400723
724 func_echo "Unpack tarball"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500725 tar zxf "$tarball"
726
727 gh_cmd="$(find "${scratch}" -name 'gh' -print)"
728 readonly gh_cmd
729
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400730 func_echo "Command: ${gh_cmd}"
731 func_echo "Version: $("$gh_cmd" --version)"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500732 popd
733
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400734 return
735}
736
737## -----------------------------------------------------------------------
738## Intent: Danger Will Robinson
739## -----------------------------------------------------------------------
740function releaseDelete()
741{
742 local version="$1"; shift
743
744 banner "${in_args[@]}"
745 declare -a args=()
746 args+=('--host-repo')
747 args+=('--yes')
748 # args+=('--cleanup-tag')
749
750# ** github-release.sh :: get_argv_repo: VARNAME=opencord/voltctl
751#* 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
752#rror connecting to 'github.com
753#heck your internet connection or https://githubstatus.com
754
755 echo
756 echo "==========================================================================="
757 my_gh 'release' 'delete' "$version" "${args[@]}"
758 echo "==========================================================================="
759 echo
760
761 showReleases
762 return
763}
764
765## -----------------------------------------------------------------------
766## Intent: Copy binaries into temp/release, checksum then publish
767## -----------------------------------------------------------------------
768function release_staging()
769{
770 local release_temp
771 get_release_dir release_temp
772
773 banner ''
774 func_echo "Packaging release files"
775 # pushd "$RELEASE_TEMP"
776 pushd "$release_temp" >/dev/null || error "pushd failed: dir is [$release_temp]"
777
778 readarray -t to_release < <(find . -mindepth 1 -maxdepth 1 -type f -print)
779 func_echo "Files to release: $(declare -p to_release)"
780
781 # Generate and check checksums
782 sha256sum -- * > checksum.SHA256
783 sha256sum -c < checksum.SHA256
784
785 echo
786 func_echo "Checksums(checksum.SHA256):"
787 cat checksum.SHA256
788
789 ## ARGS NEEDED
790 gh_release_create
791
792 popd >/dev/null || error "pushd failed: dir is [$release_temp]"
793
794 return
795}
796
797## -----------------------------------------------------------------------
798## Intent: Display program usage
799## -----------------------------------------------------------------------
800function usage()
801{
802 [[ $# -gt 0 ]] && func_echo "$*"
803
804 cat <<EOH
805Usage: github-release.sh [options] [target] ...
806
807[Github CLI (gh) arguments]
808 --login Perform authentication using a PAC
809 --logout
810 --host [h] Specify github server for connection.
811
812[Options]
813 --token Login debugging, alternative to env var use.
814
815[Modes]
816 --debug Enable script debug mode
817 --verbose Enable script verbose mode
818
819All remaining arguments are passthrough to the gh command.
820EOH
821
822 exit 0
823}
824
825## -----------------------------------------------------------------------
826## Intent: Provide common arguments and access to the gh command.
827## o Cache path to the gh command
828## o Construct a gh command line from given args
829## o Command wrapper can provide defaults (--hostname github.com)
830## -----------------------------------------------------------------------
831## Given:
832## scalar Array variable name (declare -n is a reference)
833## Return:
834## ref gh command line passed back to caller
835## Switches:
836## --host Pass default github hostname
837## --verbose Enable verbose mode#
838## --version Display command version
839## @array Remaining arguments passed as command switches.
840## -----------------------------------------------------------------------
841## See Also:
842## o https://cli.github.com/manual
843## -----------------------------------------------------------------------
844function my_gh()
845{
846 ## ------------------------
847 ## Cache path to gh command
848 ## ------------------------
849 if [[ ! -v gh_cmd ]]; then
850 readarray -t cmds < <(which -a gh)
851 declare -p cmds
852 if [ ${#cmds} -eq 0 ]; then
853 error "Unable to locate the gh command"
854 fi
855 gh_cmd="${cmds[0]}"
856 readonly gh_cmd
857 fi
858
859 declare -a cmd=()
860 cmd+=("$gh_cmd")
861
862 ## ----------------------
863 ## Construct command line
864 ## ----------------------
865 # shellcheck disable=SC2034
866 declare -A action=() # pending operations
867 declare -a args=() # common gh command line args
868
869 while [ $# -gt 0 ]; do
870 local arg="$1"; shift
871 case "$arg" in
872
873 # Modes
874 -*debug) declare -i -g debug=1 ;;
875 -*help) show_help ;;
876 -*verbose) args+=('--verbose') ;;
877
878 -*hostname)
879 get_gh_hostname in_args
880 ;;
881
882 --host-repo)
883 local val
884 get_argv_repo val
885
886 # --repo <[HOST/]OWNER/REPO>
887 args+=('--repo' "'${__githost}/${val}'")
888 ;;
889
890 --repo)
891 local val
892 get_argv_repo val
893 args+=('--repo' "$val")
894 ;;
895
896 --tag)
897 local val
898 get_argv_tag val
899 args+=("'$val'") # No switch, pass inline
900 ;;
901
902 --title)
903 local val
904 get_argv_name val
905 args+=('--title' "'$val'")
906 ;;
907
908 # --draft
909 # --latest
910 *) args+=("$arg") ;;
911 esac
912 done
913
914 cmd+=("${args[@]}")
915 echo "** Running: ${cmd[*]}"
916 "${cmd[@]}"
917 local status=$?
918
919 [[ $status -eq 0 ]] && { true; } || { false; }
920 return
921}
922
923## ---------------------------------------------------------------------------
924## Intent:
925## ---------------------------------------------------------------------------
926function usage()
927{
928 cat <<EOH
929
930Usage: $0
931Usage: make [options] [target] ...
932 --help This mesage
933 --pac Personal Access Token (file)
934
935[DEBUG]
936 --gen-version Generate a random release version string.
937 --git-hostname Git server hostname (default=github.com)
938
939EOH
940 return
941}
942
943## ---------------------------------------------------------------------------
944## Intent: Parse script command line arguments
945## ---------------------------------------------------------------------------
946function parse_args()
947{
948 # declare -n ref="$1"; shift
949
950 [[ -v DEBUG ]] && func_echo "ENTER"
951
952# ref="repos/${__repo_user}/${__repo_name}/releases"
953
954 while [ $# -gt 0 ]; do
955 local arg="$1"; shift
956 case "$arg" in
957 -*gen-version)
958 get_version GIT_VERSION
959 ;;
960
961 -*git-hostname)
962 __githost="$1"; shift
963 ;;
964
965 -*repo-name)
966 __repo_name="$1"; shift
967 ;;
968
969 -*repo-org)
970 __repo_org="$1"; shift
971 ;;
972
973 -*pac)
974 declare -g pac="$1"; shift
975 readonly pac
976 [[ ! -f "$pac" ]] && error "--token= does not exist ($pac)"
977 : # nop/true
978 ;;
979
980 -*help)
981 usage
982 exit 0
983 ;;
984 *) error "Detected unknown argument $arg" ;;
985 esac
986 done
987
Joey Armstrong26707f02023-01-26 12:41:12 -0500988 return
989}
990
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500991##----------------##
992##---] MAIN [---##
993##----------------##
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500994iam="${0##*/}"
995
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400996full_banner
997parse_args $@
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500998init
999install_gh_binary
Joey Armstrong7f382ef2023-01-25 12:00:08 -05001000
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001001do_login
Zack Williams27cd3e52018-09-18 16:44:50 -07001002
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001003release_path='/dev/null'
1004get_release_path release_path
1005declare -p release_path
Zack Williams27cd3e52018-09-18 16:44:50 -07001006
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001007pushd "$release_path" || error "pushd failed: dir is [$release_path]"
Zack Williams27cd3e52018-09-18 16:44:50 -07001008
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001009 # legacy: getGitVersion "$GERRIT_PROJECT" GIT_VERSION
1010 getGitVersion GIT_VERSION
1011 getReleaseDescription RELEASE_DESCRIPTION
Zack Williams27cd3e52018-09-18 16:44:50 -07001012
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001013 # build the release, can be multiple space separated targets
1014 # -----------------------------------------------------------------------
1015 # % go build command-line-arguments:
1016 # copying /tmp/go-build4212845548/b001/exe/a.out:
1017 # open release/voltctl-1.8.25-linux-amd64: permission denied
1018 # missing: docker run mkdir
1019 # -----------------------------------------------------------------------
1020 # shellcheck disable=SC2086
1021 make "$RELEASE_TARGETS" || tyue
Zack Williams27cd3e52018-09-18 16:44:50 -07001022
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001023 copyToRelease
Joey Armstrong50f6e0b2023-01-24 14:14:08 -05001024
Joey Armstrong26707f02023-01-26 12:41:12 -05001025 cat <<EOM
Joey Armstrong1962bcf2023-01-27 13:53:18 -05001026
1027** -----------------------------------------------------------------------
1028** Create the release:
1029** 1) Create initial github release with download area.
1030** 2) Generate checksum.SHA256 for all released files.
1031** 3) Upload files to complete the release.
1032** 4) Display released info from github.
1033** -----------------------------------------------------------------------
Joey Armstrong26707f02023-01-26 12:41:12 -05001034EOM
1035
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001036 showReleases
1037 # releaseDelete 'v4.175.710'
1038 release_staging
1039 popd || error "pushd failed: dir is [$release_path]"
Zack Williams27cd3e52018-09-18 16:44:50 -07001040fi
Joey Armstrong28eddda2023-01-10 03:09:34 -05001041
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001042do_logout
1043
Joey Armstrong28eddda2023-01-10 03:09:34 -05001044# [SEE ALSO]
1045# -----------------------------------------------------------------------
1046# https://www.shellcheck.net/wiki/SC2236
Joey Armstrong26707f02023-01-26 12:41:12 -05001047# https://docs.github.com/en/rest/releases/releases?apiVersion=2022-11-28#create-a-release
Joey Armstrong28eddda2023-01-10 03:09:34 -05001048# -----------------------------------------------------------------------
Joey Armstrongf085d872023-01-28 17:52:29 -05001049# https://cli.github.com/manual/gh_help_reference
1050# https://cli.github.com/manual/gh_release
1051# -----------------------------------------------------------------------
Joey Armstrong28eddda2023-01-10 03:09:34 -05001052
1053# [EOF]