blob: 40307e8943292a3b5425be47e4134c5660970381 [file] [log] [blame]
Zack Williams27cd3e52018-09-18 16:44:50 -07001#!/usr/bin/env bash
Joey Armstrong28eddda2023-01-10 03:09:34 -05002# -----------------------------------------------------------------------
Joey Armstrongeb84a632024-03-21 13:36:32 -04003# Copyright 2018-2024 Open Networking Foundation Contributors
Zack Williams27cd3e52018-09-18 16:44:50 -07004#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
Joey Armstrongeb84a632024-03-21 13:36:32 -04009# http:#www.apache.org/licenses/LICENSE-2.0
Zack Williams27cd3e52018-09-18 16:44:50 -070010#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
Joey Armstrongeb84a632024-03-21 13:36:32 -040016# -----------------------------------------------------------------------
17# SPDX-FileCopyrightText: 2018-2024 Open Networking Foundation Contributors
18# SPDX-License-Identifier: Apache-2.0
19# -----------------------------------------------------------------------
Joey Armstrongdb43bde2024-04-01 14:54:35 -040020# Intent:
Zack Williams27cd3e52018-09-18 16:44:50 -070021# builds (with make) and uploads release artifacts (binaries, etc.) to github
22# given a tag also create checksums files and release notes from the commit
23# message
Joey Armstrong28eddda2023-01-10 03:09:34 -050024# -----------------------------------------------------------------------
Zack Williams27cd3e52018-09-18 16:44:50 -070025
Joey Armstrong2097d3e2023-03-26 10:32:03 -040026set -euo pipefail
27
28##-------------------##
29##---] GLOBALS [---##
30##-------------------##
31declare -g WORKSPACE
32declare -g GERRIT_PROJECT
Joey Armstrong78cecc52023-04-03 11:39:11 -040033declare -g __githost=github.com
Joey Armstrong43c48422023-04-03 10:17:32 -040034# export DEBUG=1
Joey Armstrong2097d3e2023-03-26 10:32:03 -040035
36## -----------------------------------------------------------------------
37## Uncomment to activate
38## -----------------------------------------------------------------------
Joey Armstronge8560da2023-04-26 15:44:45 -040039# Debug arguments
40# declare -i -g argv_gen_version=1
41# declare -i -g draft_release=1
Joey Armstrong41923cc2023-01-30 14:38:16 -050042
Joey Armstrong2097d3e2023-03-26 10:32:03 -040043declare -a -g ARGV=() # Capture args to minimize globals and arg passing
44[[ $# -gt 0 ]] && ARGV=("$@")
Joey Armstrongd99e3d22023-01-29 12:35:43 -050045
46declare -g scratch # temp workspace for downloads
Joey Armstrongd99e3d22023-01-29 12:35:43 -050047
Joey Armstronge8560da2023-04-26 15:44:45 -040048declare -g SCRIPT_VERSION='1.4' # git changeset needed
Joey Armstrong26707f02023-01-26 12:41:12 -050049
Joey Armstrong7f382ef2023-01-25 12:00:08 -050050##--------------------##
51##---] INCLUDES [---##
Joey Armstrong1962bcf2023-01-27 13:53:18 -050052##--------------------#
Joey Armstrong2097d3e2023-03-26 10:32:03 -040053
54## -----------------------------------------------------------------------
55## Intent: Register an interrupt handler to display a stack trace on error
56## -----------------------------------------------------------------------
57function errexit()
58{
59 local err=$?
60 set +o xtrace
61 local code="${1:-1}"
62
63 local prefix="${BASH_SOURCE[1]}:${BASH_LINENO[0]}"
64 echo -e "\nOFFENDER: ${prefix}"
65 if [ $# -gt 0 ] && [ "$1" == '--stacktrace-quiet' ]; then
66 code=1
67 else
68 echo "ERROR: '${BASH_COMMAND}' exited with status $err"
69 fi
70
71 # Print out the stack trace described by $function_stack
72 if [ ${#FUNCNAME[@]} -gt 2 ]
73 then
Joey Armstrong43c48422023-04-03 10:17:32 -040074 echo "Call tree:"
75 for ((i=1;i<${#FUNCNAME[@]}-1;i++))
76 do
77 echo " $i: ${BASH_SOURCE[$i+1]}:${BASH_LINENO[$i]} ${FUNCNAME[$i]}(...)"
78 done
Joey Armstrong2097d3e2023-03-26 10:32:03 -040079 fi
80
81 echo "Exiting with status ${code}"
82 echo
83 exit "${code}"
84 # return
85}
86
87# trap ERR to provide an error handler whenever a command exits nonzero
88# this is a more verbose version of set -o errexit
89trap errexit ERR
90
91# setting errtrace allows our ERR trap handler to be propagated to functions,
92# expansions and subshells
93set -o errtrace
Joey Armstrong7f382ef2023-01-25 12:00:08 -050094
95## -----------------------------------------------------------------------
Joey Armstrongd99e3d22023-01-29 12:35:43 -050096## Intent: Cleanup scratch area on exit
97## -----------------------------------------------------------------------
98function sigtrap()
99{
100 ## Prevent mishaps
101 local is_read_only
102 is_read_only="$(declare -p scratch)"
103 if [[ $is_read_only != *"declare -r scratch="* ]]; then
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400104 echo "ERROR: variable scratch is not read-only, cleanup skipped"
105 exit 1
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500106 fi
107
108 if [ -d "$scratch" ]; then
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400109 /bin/rm -fr "$scratch"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500110 fi
111
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400112 do_logout 'shellcheck-SC2119'
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500113 return
114}
115trap sigtrap EXIT
116
117## -----------------------------------------------------------------------
Joey Armstronge8560da2023-04-26 15:44:45 -0400118## Intent: Return a random release version string.
119## -----------------------------------------------------------------------
120## Note: Do not use this function in production. get_version() is
121## intended for local use or debugging $0 from within a jenkins
122## job.
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400123## -----------------------------------------------------------------------
124function get_version()
125{
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400126 local -n ref="$1"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400127
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400128 declare -a rev=()
129 rev+=("$(( RANDOM % 10 + 1 ))")
130 rev+=("$(( RANDOM % 256 + 1 ))")
Joey Armstrongeef8c2c2023-03-27 17:27:43 -0400131 rev+=("$(( RANDOM % 10000 + 1 ))")
Joey Armstrongfc20ed52023-04-03 19:37:58 -0400132 local ver="v${rev[0]}.${rev[1]}.${rev[2]}"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400133
Joey Armstrong43c48422023-04-03 10:17:32 -0400134 func_echo "VERSION: $ver"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400135 ref="$ver"
136 return
137}
138
139## -----------------------------------------------------------------------
140## Intent: Provide defaults for environment variables
141## -----------------------------------------------------------------------
142function initEnvVars()
143{
144 # when not running under Jenkins, use current dir as workspace and a blank
145 # project name
146 declare -g WORKSPACE=${WORKSPACE:-.}
147 declare -g GERRIT_PROJECT=${GERRIT_PROJECT:-}
Joey Armstrong76026b72023-03-29 12:19:17 -0400148
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400149 # Github organization (or user) this project is published on. Project name should
150 # be the same on both Gerrit and GitHub
Joey Armstrong76026b72023-03-29 12:19:17 -0400151 declare -g GITHUB_ORGANIZATION=${GITHUB_ORGANIZATION:-}
152
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400153 # glob pattern relative to project dir matching release artifacts
154 # ARTIFACT_GLOB=${ARTIFACT_GLOB:-"release/*"} # stat -- release/* not found, literal string (?)
155 declare -g ARTIFACT_GLOB=${ARTIFACT_GLOB:-"release/."}
Joey Armstrong76026b72023-03-29 12:19:17 -0400156
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400157 # Use "release" as the default makefile target, can be a space separated list
158 declare -g RELEASE_TARGETS=${RELEASE_TARGETS:-release}
Joey Armstrong76026b72023-03-29 12:19:17 -0400159
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400160 # Set and handle GOPATH and PATH
161 export GOPATH=${GOPATH:-$WORKSPACE/go}
162 export PATH=$PATH:/usr/lib/go-1.12/bin:/usr/local/go/bin:$GOPATH/bin
163 return
164}
165
166## -----------------------------------------------------------------------
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500167## Intent: Create a scratch area for downloads and transient tools
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400168## temp directory will be automatically removed upon exit.
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500169## -----------------------------------------------------------------------
170function init()
171{
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500172 local pkgbase="${0##*/}" # basename
173 local pkgname="${pkgbase%.*}"
174
Joey Armstrongad7b1e02023-03-27 11:55:48 -0400175 # initEnvVars # moved to full_banner()
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400176
177 ## Create a temp directory for auto-cleanup
178 declare -g scratch
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500179 scratch="$(mktemp -d -t "${pkgname}.XXXXXXXXXX")"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400180 readonly scratch
181 declare -p scratch
182
Joey Armstrong43c48422023-04-03 10:17:32 -0400183 ## prime the stream: cache answers
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400184 local work
185 get_release_dir work
186 declare -p work
Joey Armstrong43c48422023-04-03 10:17:32 -0400187
Joey Armstronge8560da2023-04-26 15:44:45 -0400188 local git_version
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400189 getGitVersion git_version
190 func_echo "$(declare -p git_version)"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400191 return
192}
193
194## -----------------------------------------------------------------------
195## Intent: Verbose output for logging
196## -----------------------------------------------------------------------
197function banner()
198{
199 local iam="${0##*/}"
200 cat <<EOB
201
202** -----------------------------------------------------------------------
203** ${iam}::${FUNCNAME[1]}: $*
204** -----------------------------------------------------------------------
205EOB
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500206}
207
208## -----------------------------------------------------------------------
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500209## Intent: Output a log banner to identify the running script/version.
210## -----------------------------------------------------------------------
211## TODO:
212## o SCRIPT_VERSION => git changeset for repo:ci-managment
213## o echo "library version: ${env."library.libName.version"}"
214# -----------------------------------------------------------------------
215# 14:18:38 > git fetch --no-tags --progress -- https://gerrit.opencord.org/ci-management.git +refs/heads/*:refs/remotes/origin/* # timeout=10
216# 14:18:39 Checking out Revision 50f6e0b97f449b32d32ec0e02d59642000351847 (master)
217# -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400218function full_banner()
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500219{
Joey Armstrongad7b1e02023-03-27 11:55:48 -0400220 local iam="${0##*/}"
221
222 initEnvVars # set defaults
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500223
224cat <<EOH
225
226** -----------------------------------------------------------------------
227** IAM: ${iam} :: ${FUNCNAME[0]}
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400228** ARGV: ${ARGV[@]}
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500229** PWD: $(/bin/pwd)
230** NOW: $(date '+%Y/%m/%d %H:%M:%S')
231** VER: ${SCRIPT_VERSION:-'unknown'}
232** -----------------------------------------------------------------------
Joey Armstrongad7b1e02023-03-27 11:55:48 -0400233** GERRIT_PROJECT: $(declare -p GERRIT_PROJECT)
234** GITHUB_ORGANIZATION: $(declare -p GITHUB_ORGANIZATION)
235** RELEASE_TARGETS: $(declare -p RELEASE_TARGETS)
236** GOPATH: $(declare -p GOPATH)
237** -----------------------------------------------------------------------
238** PATH += /usr/lib/go-1.12/bin:/usr/local/go/bin:GOPATH/bin
239** -----------------------------------------------------------------------
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500240EOH
241
242 return
243}
244
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500245## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400246## Intent: Display a message with 'iam' identifier for logging
Joey Armstrong50f6e0b2023-01-24 14:14:08 -0500247## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400248function func_echo()
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500249{
250 local iam="${0##*/}"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400251 echo "** ${iam} :: ${FUNCNAME[1]}: $*"
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500252 return
253}
254
255## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400256## Intent: Display an error message then exit with status.
257## -----------------------------------------------------------------------
258function error()
259{
260 local iam="${0##*/}"
261 echo "ERROR ${iam} :: ${FUNCNAME[1]}: $*"
262 exit 1
263}
264
265## -----------------------------------------------------------------------
266## Intent: Verify sandbox/build is versioned for release.
267## -----------------------------------------------------------------------
268function getGitVersion()
269{
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400270 local -n varname="$1"; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400271
Joey Armstrong43c48422023-04-03 10:17:32 -0400272 local __ver # use prefix('__') to protect callers variable name
273 if [[ -v cached_getGitVersion ]]; then
274 __ver="$cached_getGitVersion"
275 varname="$__ver"
276 return
Joey Armstrong39fac652023-03-27 18:50:43 -0400277
Joey Armstrong43c48422023-04-03 10:17:32 -0400278 elif [[ -v argv_gen_version ]]; then
279 get_version __ver
280
281 elif [[ -v WORKSPACE ]] && [[ -v GITHUB_TOKEN ]]; then # i_am_jenkins
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400282 local path="$GERRIT_PROJECT"
283 pushd "$path" || error "pushd GERRIT_PROJECT= failed $(declare -p path)"
Joey Armstrong43c48422023-04-03 10:17:32 -0400284 __ver="$(git tag -l --points-at HEAD)"
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400285 popd || error "popd GERRIT_PROJECT= failed"
Joey Armstrong43c48422023-04-03 10:17:32 -0400286
287 elif [[ -v argv_version_file ]]; then # local debug
288 [[ ! -f VERSION ]] && error "./VERSION file not found"
289 readarray -t tmp < <(sed -e 's/[[:blank:]]//g' 'VERSION')
290 __ver="v${tmp[0]}"
291
292 else
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400293 cd ..
294 local path="$GERRIT_PROJECT"
295 pushd "$path" || error "pushd GERRIT_PROJECT= failed $(declare -p path)"
Joey Armstrong43c48422023-04-03 10:17:32 -0400296 __ver="$(git tag -l --points-at HEAD)"
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400297 popd || error "popd GERRIT_PROJECT= failed"
Joey Armstrong43c48422023-04-03 10:17:32 -0400298 fi
Joey Armstrong76026b72023-03-29 12:19:17 -0400299
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400300 # ------------------------------------------------------
301 # match bare versions or v-prefixed golang style version
302 # Critical failure for new/yet-to-be-released repo ?
303 # ------------------------------------------------------
Joey Armstrong43c48422023-04-03 10:17:32 -0400304 if [[ "$__ver" =~ ^v?([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then
Joey Armstrongfc20ed52023-04-03 19:37:58 -0400305 echo "git has a SemVer released version tag: '$__ver'"
Joey Armstrong43c48422023-04-03 10:17:32 -0400306 echo "Building artifacts for GitHub release."
307
308 elif [[ "$__ver" =~ ^v?([0-9]+)\.([0-9]+)\.([0-9]+)-dev([0-9]+)$ ]]; then
Joey Armstronge8560da2023-04-26 15:44:45 -0400309 # v1.2.3-dev (*-dev*) is an implicit draft release.
Joey Armstrong43c48422023-04-03 10:17:32 -0400310 declare -i -g draft_release=1
311 echo "Detected --draft SemVer release version tag [$__ver]"
312 echo "Building artifacts for GitHub release."
313
Joey Armstrong4f87de82023-08-25 12:42:39 -0400314 # Should also accept: X.Y.Z-{alpha,beta,...}
315
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400316 else
Joey Armstrong4f87de82023-08-25 12:42:39 -0400317 echo "Version string contains: [${__ver}]"
Joey Armstrong43c48422023-04-03 10:17:32 -0400318 error "No SemVer released version tag found, exiting..."
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400319 fi
320
Joey Armstrong43c48422023-04-03 10:17:32 -0400321 ## Cache value for subsequent calls.
322 ## readonly is a guarantee assignment happens once.
323 declare -g cached_getGitVersion="$__ver"
324 readonly cached_getGitVersion;
325
326 varname="$__ver"
327 func_echo "Version is [$__ver] from $(/bin/pwd)"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400328 return
329}
330
331## -----------------------------------------------------------------------
332## Intent:
333## Note: Release description is sanitized version of the log message
334## -----------------------------------------------------------------------
335function getReleaseDescription()
336{
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400337 local -n varname="$1"; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400338
339 local msg
340 msg="$(git log -1 --pretty=%B)"
341
342 local val
343 val="$(tr -dc "[:alnum:]\n\r\.\[\]\:\-\\\/\`\' " <<< "$msg")"
344
345 [[ ${#val} -eq 0 ]] && error "Release Description is empty ($msg)"
346 varname="$val"
347 return
348}
349
350## -----------------------------------------------------------------------
351## Intent: Retrieve value of the release temp directory.
352## -----------------------------------------------------------------------
353## Note: Limit use of globals so logic can be isolated in function calls.
354## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400355function get_release_dir()
356{
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400357 local -n varname=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400358
359 # Temporary staging directory to copy artifacts to
360 varname="$scratch/release"
361 mkdir -p "$varname"
362 return
363}
364
365## -----------------------------------------------------------------------
366## Intent: Retrieve github server hostname
367## -----------------------------------------------------------------------
368function get_gh_hostname()
369{
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400370 local -n varname=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400371 varname+=('--hostname' "${__githost}")
372 return
373}
374
375## -----------------------------------------------------------------------
376## Intent: Retrieve repository organizaiton name
377## -----------------------------------------------------------------------
378function get_gh_repo_org()
379{
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400380 local -n ref=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400381 declare -g __repo_org
Joey Armstrong76026b72023-03-29 12:19:17 -0400382
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400383 local org
384 if [[ -v __repo_org ]]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400385 org="$__repo_org"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400386 elif [[ ! -v GITHUB_ORGANIZATION ]]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400387 error "--repo-org or GITHUB_ORGANIZATION= are required"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400388 else
Joey Armstrong43c48422023-04-03 10:17:32 -0400389 org="${GITHUB_ORGANIZATION}"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400390 fi
391
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400392 ref="$org"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400393 return
394}
395
396## -----------------------------------------------------------------------
397## Intent: Retrieve repository organizaiton name
398## -----------------------------------------------------------------------
399function get_gh_repo_name()
400{
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400401 local -n ref=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400402 declare -g __repo_name
Joey Armstrong76026b72023-03-29 12:19:17 -0400403
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400404 local name
405 if [[ -v __repo_name ]]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400406 name="$__repo_name"
Joey Armstrong39fac652023-03-27 18:50:43 -0400407 elif [[ ! -v GERRIT_PROJECT ]]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400408 error "--repo-name or GERRIT_PROJECT= are required"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400409 else
Joey Armstrong43c48422023-04-03 10:17:32 -0400410 name="${GERRIT_PROJECT}"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400411 fi
412
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400413 ref="$name"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400414 return
415}
416
417## -----------------------------------------------------------------------
418## Intent: Return path for the gh release query
419## -----------------------------------------------------------------------
420function get_gh_releases()
421{
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400422 local -n ref="$1"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400423
424 local repo_org
425 get_gh_repo_org repo_org
426
427 local repo_name
428 get_gh_repo_name repo_name
429
430 ref="repos/${repo_org}/${repo_name}/releases"
431 func_echo "ref=$ref"
432 return
433}
434
435## -----------------------------------------------------------------------
436## Intent: Retrieve repository path argument
437## -----------------------------------------------------------------------
438function get_argv_repo()
439{
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400440 local -n ref=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400441
442 local repo_org
443 get_gh_repo_org repo_org
Joey Armstrong76026b72023-03-29 12:19:17 -0400444
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400445 local repo_name
Joey Armstrong76026b72023-03-29 12:19:17 -0400446 get_gh_repo_name repo_name
447
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400448 ref="${repo_org}/${repo_name}"
449 # func_echo "ref=$(declare -p ref)"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400450 return
451}
452
453## -----------------------------------------------------------------------
454## Intent: Retrieve release name string "project - version"
455## -----------------------------------------------------------------------
456function get_argv_name()
457{
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400458 local -n ref=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400459
460 local repo_name
461 get_gh_repo_name repo_name
Joey Armstrong43c48422023-04-03 10:17:32 -0400462
463 local repo_ver
464 getGitVersion repo_ver
465
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400466 # ref="${repo_name} - $GIT_VERSION"
467 ref="${repo_name} - ${repo_ver}"
468 func_echo "ref=$(declare -p ref)"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400469 return
470}
471
472## -----------------------------------------------------------------------
473## Intent: Retrieve tag version
474## -----------------------------------------------------------------------
475function get_argv_tag()
476{
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400477 local -n ref=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400478
479 # cached_argv_tag='v3.41.3204'
480 if [[ ! -v cached_argv_tag ]]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400481 declare -g cached_argv_tag
482 if [[ -v GIT_VERSION ]]; then # hello jenkins
483 cached_argv_tag="$GIT_VERSION"
484 fi
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400485 fi
486
487 [[ ${#cached_argv_tag} -eq 0 ]] && error "Unable to determine GIT_VERSION="
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400488 ref="$cached_argv_tag"
489 func_echo "ref=$(declare -p ref)"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400490 return
491}
492
493## -----------------------------------------------------------------------
494## Intent:
495## -----------------------------------------------------------------------
496# To support golang projects that require GOPATH to be set and code checked out there
497# If $DEST_GOPATH is not an empty string:
498# - create GOPATH within WORKSPACE, and destination directory within
499# - set PATH to include $GOPATH/bin and the system go binaries
500# - move project from $WORKSPACE/$GERRIT_PROJECT to new location in $GOPATH
501# - start release process within that directory
502## -----------------------------------------------------------------------
503function get_release_path()
504{
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400505 local -n ref=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400506
Joey Armstronge8560da2023-04-26 15:44:45 -0400507 # shellcheck disable=SC2128
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400508 local varpath="$ref"
Joey Armstronge8560da2023-04-26 15:44:45 -0400509
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400510 DEST_GOPATH=${DEST_GOPATH:-}
511 if [ -n "$DEST_GOPATH" ]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400512 mkdir -p "$GOPATH/src/$DEST_GOPATH"
Joey Armstronge8560da2023-04-26 15:44:45 -0400513 varpath="$GOPATH/src/$DEST_GOPATH/$GERRIT_PROJECT"
514 mv "$WORKSPACE/$GERRIT_PROJECT" "$varpath"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400515 else
Joey Armstronge8560da2023-04-26 15:44:45 -0400516 varpath="$WORKSPACE/$GERRIT_PROJECT"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400517 fi
518
Joey Armstrong43c48422023-04-03 10:17:32 -0400519 ## Verify pwd is OK
520 for path in \
Joey Armstronge8560da2023-04-26 15:44:45 -0400521 "${varpath}/Makefile"\
522 "${varpath}/makefile"\
Joey Armstrong43c48422023-04-03 10:17:32 -0400523 "__ERROR__"\
524 ; do
525 case "$path" in
Joey Armstronge8560da2023-04-26 15:44:45 -0400526 __ERROR__) error "Makefile not found at ${varpath} !" ;;
Joey Armstrong43c48422023-04-03 10:17:32 -0400527 *) [[ -f "$path" ]] && break ;;
528 esac
529 done
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400530
Mahir Gunyel2c9e8502024-03-29 13:05:38 -0700531 ref="$varpath"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400532}
533
534## -----------------------------------------------------------------------
535## Intent: Display future enhancements
536## -----------------------------------------------------------------------
537function todo()
538{
539 local iam="${0##*/}"
540
541cat <<EOT
542
543** -----------------------------------------------------------------------
544** IAM: ${iam} :: ${FUNCNAME[0]}
545** -----------------------------------------------------------------------
546 o get_release_path()
547 - refactor redundant paths into local vars.
548 - see comments, do we have a one-off failure condition ?
Joey Armstrongad7b1e02023-03-27 11:55:48 -0400549 o PATH += golang appended 3 times, release needs a single, reliable answer.
550 o do_login, do_logout and api calls do not use the my_gh wrapper:
551 - Add a lookup function to cache and retrieve path to downloaded gh command.
Joey Armstrong76026b72023-03-29 12:19:17 -0400552
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400553EOT
554
555 return
556}
557
558## -----------------------------------------------------------------------
Joey Armstrong76026b72023-03-29 12:19:17 -0400559## Intent: Verify a directory contains content for release.
560## -----------------------------------------------------------------------
561## Given:
562## scalar Path to release/ directory
563## ref Results returned through this indirect var.
564## -----------------------------------------------------------------------
565## Usage:
566## declare -a artifacts=()
567## get_artifacts '/foo/bar/tans' artifacts
568## declare -p artifacts
569## -----------------------------------------------------------------------
570function get_artifacts()
571{
572 local dir="$1" ; shift
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400573 # shellcheck disable=SC2178
574 local -n refA=$1 ; shift
Joey Armstrong76026b72023-03-29 12:19:17 -0400575
Joey Armstrong55fe80e2023-04-05 18:32:14 -0400576 # Glob available files, exclude checksums
577 readarray -t __artifacts < <(find "$dir" -mindepth 1 ! -type d \
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400578 | grep -iv -e 'sum256' -e 'checksum')
579 # func_echo "$(declare -p __artifacts)"
Joey Armstrong76026b72023-03-29 12:19:17 -0400580
581 # -----------------------------------------------------------------------
582 # Verify count>0 to inhibit source-only release
583 # Problem children:
584 # o build or make release failures.
585 # o docker container filesystem mount problem (~volume)
586 # -----------------------------------------------------------------------
587 [[ ${#__artifacts[@]} -eq 0 ]] \
Joey Armstrong43c48422023-04-03 10:17:32 -0400588 && error "Artifact dir is empty: $(declare -p dir)"
Joey Armstrong76026b72023-03-29 12:19:17 -0400589
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400590 # shellcheck disable=SC2034
591 refA=("${__artifacts[@]}")
Joey Armstrong76026b72023-03-29 12:19:17 -0400592 return
593}
594
595## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400596## Intent: Copy files from the build directory into the release staging
597## directory for publishing to github releases/ endpoint.
Joey Armstrong26707f02023-01-26 12:41:12 -0500598## -----------------------------------------------------------------------
599function copyToRelease()
600{
Joey Armstrong76026b72023-03-29 12:19:17 -0400601 banner ''
Joey Armstrong26707f02023-01-26 12:41:12 -0500602
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400603 local artifact_glob="${ARTIFACT_GLOB%/*}"
604 func_echo "$(declare -p artifact_glob)"
605
606 local work
607 get_release_dir work
Joey Armstrong76026b72023-03-29 12:19:17 -0400608 func_echo "Artifact dir: $(declare -p work)"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400609
Joey Armstrong76026b72023-03-29 12:19:17 -0400610 ## Verify release content is available
611 declare -a artifacts=()
Joey Armstrong43c48422023-04-03 10:17:32 -0400612 # get_artifacts "$work" artifacts
613 get_artifacts "$artifact_glob" artifacts
614 func_echo "Artifacts in copy_from: $(declare -p artifacts)"
Joey Armstrong472bfba2023-03-27 18:12:28 -0400615
Joey Armstrong26707f02023-01-26 12:41:12 -0500616 # Copy artifacts into the release temp dir
617 # shellcheck disable=SC2086
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400618 echo "rsync -rv --checksum \"$artifact_glob/.\" \"$work/.\""
619 rsync -rv --checksum "$artifact_glob/." "$work/."
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500620
Joey Armstrong43c48422023-04-03 10:17:32 -0400621 get_artifacts "$work" artifacts
622 func_echo "Artifacts in copy_to: $(declare -p artifacts)"
623
Joey Armstrong26707f02023-01-26 12:41:12 -0500624 return
625}
626
627## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400628## https://docs.github.com/en/rest/releases?apiVersion=2022-11-28
629 # https://cli.github.com/manual/gh_release_create
630 # --target <branch> or commit SHA
631 # --title
632 # --generate-notes
633 # --release-notes (notes file)
634 # --release
635 # release create dist/*.tgz
636 # --discussion-category "General"
Joey Armstrongf085d872023-01-28 17:52:29 -0500637## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400638# https://cli.github.com/manual/gh_release_create
639## -----------------------------------------------------------------------
640function gh_release_create()
Joey Armstrong38bfeea2023-02-06 18:01:29 -0500641{
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400642 banner ''
Joey Armstrong38bfeea2023-02-06 18:01:29 -0500643
Joey Armstrong43c48422023-04-03 10:17:32 -0400644 local version
645 getGitVersion version
Joey Armstrong38bfeea2023-02-06 18:01:29 -0500646
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400647 local work
648 get_release_dir work
649
Joey Armstrong43c48422023-04-03 10:17:32 -0400650 declare -a args=()
651 args+=('--host-repo')
652 args+=('--title')
653 if [[ -v draft_release ]]; then
654 args+=('--draft')
655 else
656 args+=('--discussion-category' 'Announcements')
657 fi
658
Joey Armstrong55fe80e2023-04-05 18:32:14 -0400659 if [[ -v release_notes ]]; then
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400660 args+=('--notes-file' "$release_notes")
Joey Armstrong55fe80e2023-04-05 18:32:14 -0400661 fi
Joey Armstrong43c48422023-04-03 10:17:32 -0400662
Joey Armstrong78cecc52023-04-03 11:39:11 -0400663 pushd "$work/.." >/dev/null
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400664 # func_echo "WORK=$work"
Joey Armstrong78cecc52023-04-03 11:39:11 -0400665 readarray -t payload < <(find 'release' -maxdepth 4 ! -type d -print)
Joey Armstrong76026b72023-03-29 12:19:17 -0400666
Joey Armstronge8560da2023-04-26 15:44:45 -0400667 func_echo "$gh_cmd release create ${version} ${args[*]} ${payload[*]}"
Joey Armstrong43c48422023-04-03 10:17:32 -0400668
669 if [[ -v dry_run ]]; then
670 echo "[SKIP] dry run"
671 else
Joey Armstronge8560da2023-04-26 15:44:45 -0400672 func_echo "my_gh release create '$version' ${args[*]} ${payload[*]}"
Joey Armstrong4dbe7002023-04-04 12:47:38 -0400673 my_gh 'release' 'create' "$version" "${args[@]}" "${payload[@]}"
Joey Armstrong43c48422023-04-03 10:17:32 -0400674 fi
Joey Armstrong76026b72023-03-29 12:19:17 -0400675 popd >/dev/null
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400676
Joey Armstrong38bfeea2023-02-06 18:01:29 -0500677 return
678}
679
680## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400681## Intent: Authenticate credentials for a github gh session
Joey Armstrong38bfeea2023-02-06 18:01:29 -0500682## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400683## NOTE: my_gh currently unused due to --with-token < "$pac"
684## -----------------------------------------------------------------------
685function do_login()
Joey Armstrongf085d872023-01-28 17:52:29 -0500686{
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400687 # shellcheck disable=SC2120
688 # shellcheck disable=SC2034
689 local unused="$1"; shift
690
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400691 declare -g pac
Joey Armstrong485081f2023-03-27 13:34:08 -0400692 declare -a login_args=()
693 [[ $# -gt 0 ]] && login_args+=("$@")
Joey Armstrongf085d872023-01-28 17:52:29 -0500694
Joey Armstrong485081f2023-03-27 13:34:08 -0400695 # https://github.com/cli/cli/issues/2922#issuecomment-775027762
Joey Armstrong75a0d932023-03-28 08:59:54 -0400696 # (sigh) why not quietly return VS forcing a special logic path
Joey Armstronge6e18eb2023-03-27 14:19:21 -0400697 # [[ -v WORKSPACE ]] && [[ -v GITHUB_TOKEN ]] && return
698
699# 12:58:36 ** -----------------------------------------------------------------------
700# 12:58:36 ** jenkins582353203049151829.sh::do_login: --hostname github.com
701# 12:58:36 ** --------------------------------------------------------------------# ---
702# 12:58:36 ** jenkins582353203049151829.sh :: do_login: Detected ENV{GITHUB_TOKEN}=
703# 12:58:36 The value of the GITHUB_TOKEN environment variable is being used for authentication.
704# 12:58:36 To have GitHub CLI store credentials instead, first clear the value from the environment.
Joey Armstrong43c48422023-04-03 10:17:32 -0400705# -----------------------------------------------------------------------
Joey Armstrong76026b72023-03-29 12:19:17 -0400706
Joey Armstrong485081f2023-03-27 13:34:08 -0400707 get_gh_hostname login_args
Joey Armstrongf085d872023-01-28 17:52:29 -0500708
Joey Armstrong485081f2023-03-27 13:34:08 -0400709 banner "${login_args[@]}"
Joey Armstrong78cecc52023-04-03 11:39:11 -0400710 func_echo "$(declare -p WORKSPACE)"
Joey Armstrongf085d872023-01-28 17:52:29 -0500711
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400712 ## Read from disk is safer than export GITHUB_TOKEN=
713 if [[ -v pac ]] && [[ ${#pac} -gt 0 ]]; then # interactive/debugging
Joey Armstrong43c48422023-04-03 10:17:32 -0400714 [ ! -f "$pac" ] && error "PAC token file $pac does not exist"
Joey Armstronge8560da2023-04-26 15:44:45 -0400715 func_echo "$gh_cmd auth login ${login_args[*]} --with-token < $pac"
Joey Armstrong485081f2023-03-27 13:34:08 -0400716 "$gh_cmd" auth login "${login_args[@]}" --with-token < "$pac"
Joey Armstrongf085d872023-01-28 17:52:29 -0500717
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400718 elif [[ ! -v GITHUB_TOKEN ]]; then
719 error "--token [t] or GITHUB_TOKEN= are required"
Joey Armstrong41923cc2023-01-30 14:38:16 -0500720
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400721 else # jenkins
Joey Armstronge8560da2023-04-26 15:44:45 -0400722 func_echo "$gh_cmd auth login ${login_args[*]} (ie: jenkins)"
Joey Armstrong733ea9f2023-04-03 21:19:46 -0400723
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400724 # https://github.com/cli/cli/issues/2922#issuecomment-775027762
725 # When using GITHUB_TOKEN, there is no need to even run gh auth login
Joey Armstrong733ea9f2023-04-03 21:19:46 -0400726 # "$gh_cmd" auth login "${login_args[@]}"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400727 fi
Joey Armstrongf085d872023-01-28 17:52:29 -0500728
Joey Armstrong43c48422023-04-03 10:17:32 -0400729 declare -i -g active_login=1 # signal logout needed
Joey Armstrongf085d872023-01-28 17:52:29 -0500730
Joey Armstrongf085d872023-01-28 17:52:29 -0500731 return
732}
733
734## -----------------------------------------------------------------------
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400735## Intent: Destroy credentials/gh session authenticated by do_login
736## -----------------------------------------------------------------------
737## NOTE: my_gh currently unused due to "<<< 'y'"
738## -----------------------------------------------------------------------
739function do_logout()
740{
741 declare -i -g active_login
742 [[ ! -v active_login ]] && return
743
Joey Armstrong78cecc52023-04-03 11:39:11 -0400744 declare -a logout_args=()
745 [[ $# -gt 0 ]] && logout_args+=("$@")
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400746
Joey Armstrong78cecc52023-04-03 11:39:11 -0400747 get_gh_hostname logout_args
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400748
Joey Armstrong78cecc52023-04-03 11:39:11 -0400749 banner "${logout_args[@]}"
Joey Armstronge8560da2023-04-26 15:44:45 -0400750 func_echo "$gh_cmd auth logout ${logout_args[*]} <<< 'Y'"
Joey Armstrong78cecc52023-04-03 11:39:11 -0400751 "$gh_cmd" auth logout "${logout_args[@]}" <<< 'Y'
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400752
753 unset active_login
754 return
755}
756
757## -----------------------------------------------------------------------
758## Intent: Query for repository version strings
759## -----------------------------------------------------------------------
760function get_releases()
761{
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400762 # shellcheck disable=SC2178
763 local -n refA="$1"; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400764
765 banner ""
766 pushd "$scratch" >/dev/null
Joey Armstrong76026b72023-03-29 12:19:17 -0400767
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400768 # gh api repos/{owner}/{repo}/releases
769 local releases_uri
770 get_gh_releases releases_uri
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400771 # declare -p releases_uri
Joey Armstrong76026b72023-03-29 12:19:17 -0400772
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400773 refA=()
Joey Armstronge8560da2023-04-26 15:44:45 -0400774 "$gh_cmd" api "$releases_uri" | jq . > 'release.raw'
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400775 readarray -t __tmp < <(jq '.[] | "\(.tag_name)"' 'release.raw')
776
777 local release
778 for release in "${__tmp[@]}";
779 do
Joey Armstrong43c48422023-04-03 10:17:32 -0400780 release="${release//\"/}"
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400781 refA+=("$release")
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400782 done
783
784 popd >/dev/null
785 return
786}
787
788## -----------------------------------------------------------------------
789## Intent: Display repository query strings.
790## Indirect: verify authentication and API
791## -----------------------------------------------------------------------
792function showReleases()
793{
Joey Armstrong43c48422023-04-03 10:17:32 -0400794 declare -a raw=()
795 get_releases raw
796
797 ## Sort for display, we may need to prune volume later on
Joey Armstronge8560da2023-04-26 15:44:45 -0400798 readarray -t releases < <(sort -nr <<<"${raw[*]}")
799 # IFS=$'\n' releases=($(sort -nr <<<"${raw[*]}"))
800 # unset IFS
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400801
802 local release
803 for release in "${releases[@]}";
804 do
Joey Armstrong43c48422023-04-03 10:17:32 -0400805 func_echo "$release"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400806 done
807 return
808}
809
810## -----------------------------------------------------------------------
811## Intent: Install the gh command line tool locally
Joey Armstrong26707f02023-01-26 12:41:12 -0500812## -----------------------------------------------------------------------
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500813function install_gh_binary()
Joey Armstrong26707f02023-01-26 12:41:12 -0500814{
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400815 banner
Joey Armstrong26707f02023-01-26 12:41:12 -0500816
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500817 pushd "$scratch"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400818 func_echo "Retrieve latest gh download URLs"
Joey Armstrongf085d872023-01-28 17:52:29 -0500819
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500820 local latest="https://github.com/cli/cli/releases/latest"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400821 local tarball="gh.tar.tgz"
822
Joey Armstrong76026b72023-03-29 12:19:17 -0400823 readarray -t latest < <(\
824 curl --silent -qI "$latest" \
Joey Armstrong43c48422023-04-03 10:17:32 -0400825 | awk -F '/' '/^location/ {print substr($NF, 1, length($NF)-1)}')
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400826 declare -p latest
827 if [ ${#latest[@]} -ne 1 ]; then
Joey Armstrong43c48422023-04-03 10:17:32 -0400828 error "Unable to determine latest gh package version"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400829 fi
Joey Armstrong76026b72023-03-29 12:19:17 -0400830
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400831 local VER="${latest[0]}"
832
833 func_echo "Download latest gh binary"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500834 local url="https://github.com/cli/cli/releases/download/${VER}/gh_${VER#v}_linux_amd64.tar.gz"
Joey Armstrong43c48422023-04-03 10:17:32 -0400835 func_echo "wget --quiet --output-document='$tarball' '$url'"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500836 wget --quiet --output-document="$tarball" "$url"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400837
838 func_echo "Unpack tarball"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500839 tar zxf "$tarball"
840
Joey Armstrong78cecc52023-04-03 11:39:11 -0400841 declare -g gh_cmd
842 gh_cmd="$(find "${scratch}" -name 'gh' -print)"
843 #declare -g gh_cmd='/usr/bin/gh'
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500844 readonly gh_cmd
845
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400846 func_echo "Command: ${gh_cmd}"
847 func_echo "Version: $("$gh_cmd" --version)"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500848 popd
849
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400850 return
851}
852
853## -----------------------------------------------------------------------
854## Intent: Danger Will Robinson
855## -----------------------------------------------------------------------
856function releaseDelete()
857{
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400858 # shellcheck disable=SC2178
859 local -n refA=$1; shift
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400860 local version="$1"; shift
Joey Armstrong76026b72023-03-29 12:19:17 -0400861
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400862 banner "${refA[@]}"
863 # declare -a refA=()
864 refA+=('--host-repo')
865 refA+=('--yes')
866 # refA+=('--cleanup-tag')
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400867
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400868 echo
869 echo "==========================================================================="
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400870 my_gh 'release' 'delete' "$version" "${refA[@]}"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400871 echo "==========================================================================="
872 echo
873
874 showReleases
875 return
876}
877
878## -----------------------------------------------------------------------
879## Intent: Copy binaries into temp/release, checksum then publish
880## -----------------------------------------------------------------------
881function release_staging()
882{
883 local release_temp
884 get_release_dir release_temp
885
886 banner ''
887 func_echo "Packaging release files"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400888
Joey Armstrong76026b72023-03-29 12:19:17 -0400889 pushd "$release_temp" >/dev/null \
Joey Armstrong43c48422023-04-03 10:17:32 -0400890 || error "pushd failed: dir is [$release_temp]"
Joey Armstrong76026b72023-03-29 12:19:17 -0400891
892 declare -a to_release=()
893 get_artifacts '.' to_release
894
Joey Armstrong55fe80e2023-04-05 18:32:14 -0400895 if false; then
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400896 for fyl in "${to_release[@]}";
897 do
898 func_echo "sha256sum $fyl > ${fyl}.sha256"
899 sha256sum "$fyl" > "${fyl}.sha256"
900 done
Joey Armstrong55fe80e2023-04-05 18:32:14 -0400901 fi
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400902
903 # Generate and check checksums
Joey Armstrong55fe80e2023-04-05 18:32:14 -0400904 sha256sum -- * | grep -iv -e 'checksum' -e 'sha256' > checksum.SHA256
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400905 sha256sum -c < checksum.SHA256
906
907 echo
908 func_echo "Checksums(checksum.SHA256):"
909 cat checksum.SHA256
910
Joey Armstrong43c48422023-04-03 10:17:32 -0400911 if false; then
Joey Armstrong78cecc52023-04-03 11:39:11 -0400912 # Careful with credentials display
Joey Armstrong43c48422023-04-03 10:17:32 -0400913 get_gh_hostname login_args
Joey Armstronge8560da2023-04-26 15:44:45 -0400914 banner "gh auth status ${login_args[*]}"
Joey Armstrong43c48422023-04-03 10:17:32 -0400915 gh auth status "${login_args[@]}"
916 fi
917
Joey Armstrong76026b72023-03-29 12:19:17 -0400918 gh_release_create # publish
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400919
920 popd >/dev/null || error "pushd failed: dir is [$release_temp]"
921
922 return
923}
924
925## -----------------------------------------------------------------------
Joey Armstrong43c48422023-04-03 10:17:32 -0400926## Intent: Normalize common arguments and access to the gh command.
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400927## o Cache path to the gh command
928## o Construct a gh command line from given args
929## o Command wrapper can provide defaults (--hostname github.com)
930## -----------------------------------------------------------------------
931## Given:
Joey Armstrongdb43bde2024-04-01 14:54:35 -0400932## scalar Array variable name (local -n is a reference)
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400933## Return:
934## ref gh command line passed back to caller
935## Switches:
936## --host Pass default github hostname
Joey Armstrong01e4edb2023-08-18 12:50:32 -0400937## --verbose Enable verbose mode
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400938## --version Display command version
939## @array Remaining arguments passed as command switches.
940## -----------------------------------------------------------------------
941## See Also:
942## o https://cli.github.com/manual
943## -----------------------------------------------------------------------
944function my_gh()
945{
Joey Armstrong73bb2f62023-04-17 19:31:50 -0400946 func_echo "ENTER"
947 set -x
948
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400949 declare -a cmd=()
950 cmd+=("$gh_cmd")
951
952 ## ----------------------
953 ## Construct command line
954 ## ----------------------
955 # shellcheck disable=SC2034
956 declare -A action=() # pending operations
957 declare -a args=() # common gh command line args
958
959 while [ $# -gt 0 ]; do
960 local arg="$1"; shift
Joey Armstrong73bb2f62023-04-17 19:31:50 -0400961 func_echo "function arg is [$arg]"
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400962 case "$arg" in
963
Joey Armstrong43c48422023-04-03 10:17:32 -0400964 # Modes
Joey Armstronge8560da2023-04-26 15:44:45 -0400965 -*debug)
966 # shellcheck disable=SC2034
967 declare -i -g debug=1
968 ;;
969 -*verbose) args+=('--verbose') ;;
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400970
Joey Armstrong43c48422023-04-03 10:17:32 -0400971 -*hostname)
972 get_gh_hostname in_args
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400973 ;;
974
Joey Armstrong43c48422023-04-03 10:17:32 -0400975 --host-repo)
976 local val
977 get_argv_repo val
Joey Armstrong76026b72023-03-29 12:19:17 -0400978
Joey Armstrong43c48422023-04-03 10:17:32 -0400979 # --repo <[HOST/]OWNER/REPO>
Joey Armstrong78cecc52023-04-03 11:39:11 -0400980 args+=('--repo' "${__githost}/${val}")
Joey Armstrong43c48422023-04-03 10:17:32 -0400981 ;;
Joey Armstrong76026b72023-03-29 12:19:17 -0400982
Joey Armstrongf9a0a882023-04-17 11:53:57 -0400983# args+=('--repo' 'github.com/opencord/bbsim')
984
Joey Armstrong43c48422023-04-03 10:17:32 -0400985 --repo)
986 local val
987 get_argv_repo val
988 args+=('--repo' "$val")
989 ;;
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400990
Joey Armstrong43c48422023-04-03 10:17:32 -0400991 --tag)
992 local val
993 get_argv_tag val
Joey Armstrong4dbe7002023-04-04 12:47:38 -0400994 args+=("$val") # No switch, pass inline
Joey Armstrong43c48422023-04-03 10:17:32 -0400995 ;;
Joey Armstrong2097d3e2023-03-26 10:32:03 -0400996
Joey Armstrong43c48422023-04-03 10:17:32 -0400997 --title)
998 local val
999 get_argv_name val
Joey Armstrongf9a0a882023-04-17 11:53:57 -04001000 args+=('--title' "'$val'")
Joey Armstrong43c48422023-04-03 10:17:32 -04001001 ;;
Joey Armstrong76026b72023-03-29 12:19:17 -04001002
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001003 *) args+=("$arg") ;;
1004 esac
1005 done
Joey Armstrong76026b72023-03-29 12:19:17 -04001006
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001007 cmd+=("${args[@]}")
Joey Armstrong73bb2f62023-04-17 19:31:50 -04001008
1009 echo
1010 declare -p cmd
1011
1012 echo
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001013 echo "** Running: ${cmd[*]}"
1014 "${cmd[@]}"
1015 local status=$?
1016
Joey Armstrong73bb2f62023-04-17 19:31:50 -04001017 set +x
1018 func_echo "LEAVE"
1019
Joey Armstronge8560da2023-04-26 15:44:45 -04001020 if [[ $status -eq 0 ]]; then
1021 true
1022 else
1023 false
1024 fi
1025
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001026 return
1027}
1028
1029## ---------------------------------------------------------------------------
1030## Intent:
1031## ---------------------------------------------------------------------------
1032function usage()
1033{
1034 cat <<EOH
Joey Armstrong76026b72023-03-29 12:19:17 -04001035
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001036Usage: $0
1037Usage: make [options] [target] ...
1038 --help This mesage
Joey Armstrong55fe80e2023-04-05 18:32:14 -04001039 --pac Personal Access Token (path to containing file or a string)
1040 --repo-name ex: voltctl
1041 --repo-org ex: opencord
Joey Armstronge8560da2023-04-26 15:44:45 -04001042 --release-notes [f] Release notes are passed by file argument
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001043
1044[DEBUG]
1045 --gen-version Generate a random release version string.
1046 --git-hostname Git server hostname (default=github.com)
Joey Armstrong55fe80e2023-04-05 18:32:14 -04001047 --version-file Read version string from local version file (vs env var)
Joey Armstrongf9a0a882023-04-17 11:53:57 -04001048
1049[MODES]
Joey Armstrong55fe80e2023-04-05 18:32:14 -04001050 --debug Enable script debug mode
1051 --draft Create a draft release (vs published)
Joey Armstrong43c48422023-04-03 10:17:32 -04001052 --dry-run Simulation mode
Joey Armstrong55fe80e2023-04-05 18:32:14 -04001053 --todo Display future enhancement list
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001054
Joey Armstronge8560da2023-04-26 15:44:45 -04001055All other arguments are pass-through to the gh command.
1056
Joey Armstrong55fe80e2023-04-05 18:32:14 -04001057Usage: $0 --draft --repo-org opencord --repo-name voltctl --git-hostname github.com --pac ~/access.pac
Joey Armstronge8560da2023-04-26 15:44:45 -04001058
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001059EOH
Joey Armstronge8560da2023-04-26 15:44:45 -04001060
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001061 return
1062}
1063
1064## ---------------------------------------------------------------------------
1065## Intent: Parse script command line arguments
1066## ---------------------------------------------------------------------------
1067function parse_args()
1068{
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001069 [[ -v DEBUG ]] && func_echo "ENTER"
1070
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001071 while [ $# -gt 0 ]; do
Joey Armstrong43c48422023-04-03 10:17:32 -04001072 local arg="$1"; shift
1073 func_echo "ARGV: $arg"
Joey Armstrong01e4edb2023-08-18 12:50:32 -04001074
1075 # shellcheck disable=SC2034
Joey Armstrong43c48422023-04-03 10:17:32 -04001076 case "$arg" in
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001077
Joey Armstrong43c48422023-04-03 10:17:32 -04001078 -*debug) declare -i -g debug=1 ;;
1079 --draft) declare -i -g draft_release=1 ;;
1080 --dry-run) declare -i -g dry_run=1 ;;
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001081
Joey Armstrong43c48422023-04-03 10:17:32 -04001082 --version-file)
1083 declare -i -g argv_version_file=1
1084 ;;
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001085
Joey Armstrong43c48422023-04-03 10:17:32 -04001086 -*gen-version)
1087 declare -g -i argv_gen_version=1
1088 ;;
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001089
Joey Armstrong43c48422023-04-03 10:17:32 -04001090 -*git-hostname)
1091 __githost="$1"; shift
1092 ;;
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001093
Joey Armstrong55fe80e2023-04-05 18:32:14 -04001094 -*release-notes)
Joey Armstronge8560da2023-04-26 15:44:45 -04001095 [[ ! -f "$1" ]] && error "--release-notes: file path required (arg=\"$arg\")"
1096 declare -g release_notes="$1"; shift
Joey Armstrongf9a0a882023-04-17 11:53:57 -04001097 ;;
Joey Armstrong55fe80e2023-04-05 18:32:14 -04001098
Joey Armstrong43c48422023-04-03 10:17:32 -04001099 -*repo-name)
1100 __repo_name="$1"; shift
1101 ;;
1102
1103 -*repo-org)
1104 __repo_org="$1"; shift
1105 ;;
1106
1107 -*pac)
1108 declare -g pac="$1"; shift
1109 readonly pac
1110 [[ ! -f "$pac" ]] && error "--token= does not exist ($pac)"
1111 : # nop/true
1112 ;;
1113
1114 -*todo) todo ;;
1115
1116 -*help)
1117 usage
1118 exit 0
1119 ;;
1120
1121 *) error "Detected unknown argument $arg" ;;
1122 esac
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001123 done
1124
Joey Armstrong26707f02023-01-26 12:41:12 -05001125 return
1126}
1127
Joey Armstrongaf577ab2022-12-15 14:43:33 -05001128##----------------##
1129##---] MAIN [---##
1130##----------------##
Joey Armstrong1962bcf2023-01-27 13:53:18 -05001131iam="${0##*/}"
1132
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001133full_banner
Joey Armstronge8560da2023-04-26 15:44:45 -04001134parse_args "$@"
Joey Armstrongd99e3d22023-01-29 12:35:43 -05001135init
1136install_gh_binary
Joey Armstrong7f382ef2023-01-25 12:00:08 -05001137
Joey Armstrong01e4edb2023-08-18 12:50:32 -04001138do_login "$*"
Zack Williams27cd3e52018-09-18 16:44:50 -07001139
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001140release_path='/dev/null'
1141get_release_path release_path
1142declare -p release_path
Zack Williams27cd3e52018-09-18 16:44:50 -07001143
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001144pushd "$release_path" || error "pushd failed: dir is [$release_path]"
Zack Williams27cd3e52018-09-18 16:44:50 -07001145
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001146 # legacy: getGitVersion "$GERRIT_PROJECT" GIT_VERSION
1147 getGitVersion GIT_VERSION
1148 getReleaseDescription RELEASE_DESCRIPTION
Joey Armstrong55fe80e2023-04-05 18:32:14 -04001149 if [[ ! -v release_notes ]]; then
Joey Armstronge8560da2023-04-26 15:44:45 -04001150 func_echo "Generating release notes from RELEASE_DESCRIPTION"
Joey Armstrongf9a0a882023-04-17 11:53:57 -04001151 declare -g release_notes="$scratch/release.notes"
1152 echo "$RELEASE_DESCRIPTION" > "$release_notes"
Joey Armstrong55fe80e2023-04-05 18:32:14 -04001153 fi
Joey Armstronge8560da2023-04-26 15:44:45 -04001154 cat "$release_notes"
Zack Williams27cd3e52018-09-18 16:44:50 -07001155
Joey Armstrongeef8c2c2023-03-27 17:27:43 -04001156 cat <<EOM
1157
1158** -----------------------------------------------------------------------
1159** GIT VERSION: $(declare -p GIT_VERSION)
1160** RELEASE_DESCRIPTION: $(declare -p RELEASE_DESCRIPTION)
1161** RELEASE_TARGETS: $(declare -p RELEASE_TARGETS)
Joey Armstrong43c48422023-04-03 10:17:32 -04001162** -----------------------------------------------------------------------
Joey Armstronge8560da2023-04-26 15:44:45 -04001163** URL: https://github.com/opencord/bbsim/releases
1164** -----------------------------------------------------------------------
Joey Armstrong43c48422023-04-03 10:17:32 -04001165** Running: make ${RELEASE_TARGETS}
Joey Armstrongeef8c2c2023-03-27 17:27:43 -04001166** -----------------------------------------------------------------------
1167EOM
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001168 # build the release, can be multiple space separated targets
1169 # -----------------------------------------------------------------------
1170 # % go build command-line-arguments:
1171 # copying /tmp/go-build4212845548/b001/exe/a.out:
1172 # open release/voltctl-1.8.25-linux-amd64: permission denied
1173 # missing: docker run mkdir
1174 # -----------------------------------------------------------------------
1175 # shellcheck disable=SC2086
Joey Armstrongeef8c2c2023-03-27 17:27:43 -04001176 make "$RELEASE_TARGETS"
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001177 copyToRelease
Joey Armstrong50f6e0b2023-01-24 14:14:08 -05001178
Joey Armstrong26707f02023-01-26 12:41:12 -05001179 cat <<EOM
Joey Armstrong1962bcf2023-01-27 13:53:18 -05001180
1181** -----------------------------------------------------------------------
1182** Create the release:
1183** 1) Create initial github release with download area.
1184** 2) Generate checksum.SHA256 for all released files.
1185** 3) Upload files to complete the release.
Joey Armstrong75a0d932023-03-28 08:59:54 -04001186** 4) Display released info from github
Joey Armstrong1962bcf2023-01-27 13:53:18 -05001187** -----------------------------------------------------------------------
Joey Armstrong26707f02023-01-26 12:41:12 -05001188EOM
1189
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001190 showReleases
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001191 release_staging
Joey Armstronge8560da2023-04-26 15:44:45 -04001192
1193 # Useful to display but --draft images use a non-standard subdir identifier.
1194 # showReleaseUrl
1195
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001196 popd || error "pushd failed: dir is [$release_path]"
Joey Armstrong28eddda2023-01-10 03:09:34 -05001197
Joey Armstrong2097d3e2023-03-26 10:32:03 -04001198do_logout
1199
Joey Armstrong28eddda2023-01-10 03:09:34 -05001200# [SEE ALSO]
1201# -----------------------------------------------------------------------
1202# https://www.shellcheck.net/wiki/SC2236
Joey Armstrong26707f02023-01-26 12:41:12 -05001203# https://docs.github.com/en/rest/releases/releases?apiVersion=2022-11-28#create-a-release
Joey Armstrong28eddda2023-01-10 03:09:34 -05001204# -----------------------------------------------------------------------
Joey Armstrongf085d872023-01-28 17:52:29 -05001205# https://cli.github.com/manual/gh_help_reference
1206# https://cli.github.com/manual/gh_release
1207# -----------------------------------------------------------------------
Joey Armstrong28eddda2023-01-10 03:09:34 -05001208
1209# [EOF]