blob: b0004ffa9658b30d7dedafa6b75f8729bfe28fbd [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# -----------------------------------------------------------------------
Joey Armstrongf085d872023-01-28 17:52:29 -050022# 1) Staging to replace command github-release with gh.
23# 2) gh auth / GIT_TOKEN handling may be needed
24# -----------------------------------------------------------------------
25# gh release create abc/1.2.3-rc2 --discussion-category "Announcements" --generate-notes hello.txt
26# https://github.com/cli/cli/issues/4993
27# -----------------------------------------------------------------------
Zack Williams27cd3e52018-09-18 16:44:50 -070028
Joey Armstrong7f382ef2023-01-25 12:00:08 -050029##-------------------##
30##---] GLOBALS [---##
31##-------------------##
Joey Armstrongd99e3d22023-01-29 12:35:43 -050032set -euo pipefail
33
34declare -g scratch # temp workspace for downloads
35declare -g gh_cmd # path to gh command
36
Joey Armstrong7f382ef2023-01-25 12:00:08 -050037declare -g ARGV="$*" # archive for display
Joey Armstrong1962bcf2023-01-27 13:53:18 -050038declare -g SCRIPT_VERSION='1.0' # git changeset needed
39declare -g TRACE=0 # uncomment to set -x
Joey Armstrong7f382ef2023-01-25 12:00:08 -050040
Joey Armstrong26707f02023-01-26 12:41:12 -050041declare -g RELEASE_TEMP
42
Joey Armstrong7f382ef2023-01-25 12:00:08 -050043##--------------------##
44##---] INCLUDES [---##
Joey Armstrong1962bcf2023-01-27 13:53:18 -050045##--------------------#
Joey Armstrong7f382ef2023-01-25 12:00:08 -050046# shellcheck disable=SC1091
Joey Armstrong1962bcf2023-01-27 13:53:18 -050047# source "${pgmdir}/common/common.sh" "${common_args[@]}"
Joey Armstrong7f382ef2023-01-25 12:00:08 -050048
49## -----------------------------------------------------------------------
Joey Armstrongd99e3d22023-01-29 12:35:43 -050050## Intent: Cleanup scratch area on exit
51## -----------------------------------------------------------------------
52function sigtrap()
53{
54 ## Prevent mishaps
55 local is_read_only
56 is_read_only="$(declare -p scratch)"
57 if [[ $is_read_only != *"declare -r scratch="* ]]; then
58 echo "ERROR: variable scratch is not read-only, cleanup skipped"
59 exit 1
60 fi
61
62 if [ -d "$scratch" ]; then
63 /bin/rm -fr "$scratch"
64 fi
65
66 return
67}
68trap sigtrap EXIT
69
70## -----------------------------------------------------------------------
71## Intent: Create a scratch area for downloads and transient tools
72## -----------------------------------------------------------------------
73function init()
74{
75 declare -g scratch
76
77 local pkgbase="${0##*/}" # basename
78 local pkgname="${pkgbase%.*}"
79
80 scratch="$(mktemp -d -t "${pkgname}.XXXXXXXXXX")"
81 readonly scratch
82 return
83}
84
85## -----------------------------------------------------------------------
Joey Armstrong7f382ef2023-01-25 12:00:08 -050086## Intent: Output a log banner to identify the running script/version.
87## -----------------------------------------------------------------------
88## TODO:
89## o SCRIPT_VERSION => git changeset for repo:ci-managment
90## o echo "library version: ${env."library.libName.version"}"
91# -----------------------------------------------------------------------
92# 14:18:38 > git fetch --no-tags --progress -- https://gerrit.opencord.org/ci-management.git +refs/heads/*:refs/remotes/origin/* # timeout=10
93# 14:18:39 Checking out Revision 50f6e0b97f449b32d32ec0e02d59642000351847 (master)
94# -----------------------------------------------------------------------
95function banner()
96{
97 local iam="${0##*/}"
98
99cat <<EOH
100
101** -----------------------------------------------------------------------
102** IAM: ${iam} :: ${FUNCNAME[0]}
103** ARGV: ${ARGV}
104** PWD: $(/bin/pwd)
105** NOW: $(date '+%Y/%m/%d %H:%M:%S')
106** VER: ${SCRIPT_VERSION:-'unknown'}
107** -----------------------------------------------------------------------
108EOH
109
110 return
111}
112
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500113## -----------------------------------------------------------------------
Joey Armstrong50f6e0b2023-01-24 14:14:08 -0500114## Intent:
115## Display available command versions
116## A github release job is failing due to a command being mia.
117## -----------------------------------------------------------------------
118function displayCommands()
119{
120 # which git || /bin/true
121 # git --version || /bin/true
122 # go version || /bin/true
123 # helm version || /bin/true
124 return
125}
126
127## -----------------------------------------------------------------------
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500128## Intent: Display filesystem with a banner for logging
129## -----------------------------------------------------------------------
130function displayPwd()
131{
132 local iam="${0##*/}"
133 echo "** ${iam}: ENTER"
134
135cat <<EOM
136
137** -----------------------------------------------------------------------
138** IAM: $iam :: ${FUNCNAME[0]}
139** PWD: $(/bin/pwd)
140** -----------------------------------------------------------------------
141EOM
142 find . -maxdepth 1 -ls
143 echo "** ${iam}: LEAVE"
144 return
145}
146
147## -----------------------------------------------------------------------
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500148## Intent: Copy files from the build directory into the release staging directory.
Joey Armstrong26707f02023-01-26 12:41:12 -0500149## -----------------------------------------------------------------------
150function copyToRelease()
151{
152 local iam="${FUNCNAME[0]}"
153 echo "** ${iam}: ENTER"
154
155 local artifact_glob="${ARTIFACT_GLOB%/*}"
156 echo "** ${iam}: $(declare -p ARTIFACT_GLOB) $(declare -p artifact_glob)"
Joey Armstrong26707f02023-01-26 12:41:12 -0500157
158 # Copy artifacts into the release temp dir
159 # shellcheck disable=SC2086
160 # cp -v "$ARTIFACT_GLOB" "$RELEASE_TEMP"
161 echo "rsync -rv --checksum \"$artifact_glob/.\" \"$RELEASE_TEMP/.\""
162 rsync -rv --checksum "$artifact_glob/." "$RELEASE_TEMP/."
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500163
Joey Armstrong26707f02023-01-26 12:41:12 -0500164 echo "** ${iam}: RELEASE_TEMP=${RELEASE_TEMP}"
165 find "$RELEASE_TEMP" -print
166
167 echo "** ${iam}: LEAVE"
168 echo
169 return
170}
171
172## -----------------------------------------------------------------------
Joey Armstrongf085d872023-01-28 17:52:29 -0500173## Intent:
174## -----------------------------------------------------------------------
175function github_release_pre()
176{
177 local what="$1" ; shift
178 local user="$1" ; shift
179 local repo="$1" ; shift
180 local tag="$1" ; shift
181 local name="$1" ; shift
182 local descr="$1" ; shift
183
184 local iam="${FUNCNAME[0]}"
185 echo "** ${iam}: ENTER"
186
187 case "$what" in
188 gh)
189 declare -a cmd=()
190
Joey Armstrongd94b5e62023-01-29 19:23:42 -0500191 ## [TODO] Refactor into a function accepting:
192 ## --create
193 ## --info
194 ## --upload
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500195 cmd+=("$gh_cmd")
Joey Armstrongf085d872023-01-28 17:52:29 -0500196 # cmd+=('--verbose')
Joey Armstrongd94b5e62023-01-29 19:23:42 -0500197 cmd+=('release' 'create')
198 # cmd+=('--latest')
Joey Armstrongf085d872023-01-28 17:52:29 -0500199 cmd+=('--repo' "$repo")
200 cmd+=('--title' "$name")
Joey Armstrongd94b5e62023-01-29 19:23:42 -0500201 # cmd+=('--descripton' "$descr") # not supported
Joey Armstrongf085d872023-01-28 17:52:29 -0500202 cmd+=('--discussion-category' "Announcements")
203 # cmd+=('--latest') - auto based on date & ver
204 cmd+=('--verify-tag')
205
206 # --branch exists, omit switch for tag
207 cmd+=("$tag")
208
209 echo "** ${iam}: RUNNING " "${cmd[@]}"
210 "${cmd[@]}"
211 ;;
212
213 *)
214 declare -a cmd=()
215
216 cmd+=('github-release')
217 # cmd+=('--verbose')
218 cmd+=('release')
219 cmd+=('--user' "$user")
220 cmd+=('--repo' "$repo")
221 cmd+=('--tag' "$tag")
222 cmd+=('--name' "$name")
223 cmd+=('--descripton' "$descr")
224
225 echo "** ${iam}: RUNNING " "${cmd[@]}"
226 "${cmd[@]}"
227 ;;
228 esac
Joey Armstrongf085d872023-01-28 17:52:29 -0500229
230 echo "** ${iam}: ENTER"
231 return
232}
233
234## -----------------------------------------------------------------------
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500235## Intent:
Joey Armstrong26707f02023-01-26 12:41:12 -0500236## -----------------------------------------------------------------------
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500237function install_gh_binary()
Joey Armstrong26707f02023-01-26 12:41:12 -0500238{
239 local iam="${FUNCNAME[0]}"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500240 echo "** $iam: ENTER"
Joey Armstrong26707f02023-01-26 12:41:12 -0500241
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500242 pushd "$scratch"
243 echo -e "\n** ${iam}: Retrieve latest gh download URLs"
Joey Armstrongf085d872023-01-28 17:52:29 -0500244
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500245 local latest="https://github.com/cli/cli/releases/latest"
246 local tarball="gh.tar.tgz"
247
248 local VER
249 VER=$(curl --silent -qI "$latest" \
250 | awk -F '/' '/^location/ {print substr($NF, 1, length($NF)-1)}')
251 # echo "VER=[$VER]"
252
253 echo "** ${iam}: Download latest gh binary"
254 local url="https://github.com/cli/cli/releases/download/${VER}/gh_${VER#v}_linux_amd64.tar.gz"
255 wget --quiet --output-document="$tarball" "$url"
Joey Armstrong26707f02023-01-26 12:41:12 -0500256
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500257 echo "** ${iam}: Unpack tarball"
258 tar zxf "$tarball"
259
260 gh_cmd="$(find "${scratch}" -name 'gh' -print)"
261 readonly gh_cmd
262
263 echo "** ${iam} Command: ${gh_cmd}"
264 echo "** ${iam} Version: $("$gh_cmd" --version)"
265 popd
266
267 echo "** $iam: LEAVE"
Joey Armstrong26707f02023-01-26 12:41:12 -0500268 return
269}
270
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500271##----------------##
272##---] MAIN [---##
273##----------------##
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500274iam="${0##*/}"
275
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500276banner
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500277init
278install_gh_binary
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500279
Zack Williams27cd3e52018-09-18 16:44:50 -0700280# when not running under Jenkins, use current dir as workspace and a blank
281# project name
282WORKSPACE=${WORKSPACE:-.}
283GERRIT_PROJECT=${GERRIT_PROJECT:-}
284
285# Github organization (or user) this project is published on. Project name should
286# be the same on both Gerrit and GitHub
287GITHUB_ORGANIZATION=${GITHUB_ORGANIZATION:-}
288
289# glob pattern relative to project dir matching release artifacts
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500290# ARTIFACT_GLOB=${ARTIFACT_GLOB:-"release/*"} # stat -- release/* not found, literal string (?)
Joey Armstrong50f6e0b2023-01-24 14:14:08 -0500291ARTIFACT_GLOB=${ARTIFACT_GLOB:-"release/."}
Zack Williams27cd3e52018-09-18 16:44:50 -0700292
293# Temporary staging directory to copy artifacts to
294RELEASE_TEMP="$WORKSPACE/release"
Zack Williams2f8e8472019-05-21 16:29:06 -0700295mkdir -p "$RELEASE_TEMP"
Zack Williams27cd3e52018-09-18 16:44:50 -0700296
297# Use "release" as the default makefile target, can be a space separated list
298RELEASE_TARGETS=${RELEASE_TARGETS:-release}
299
Zack Williams0f398492019-10-23 16:02:24 -0700300
Zack Williams27cd3e52018-09-18 16:44:50 -0700301# check that we're on a semver released version, or exit
302pushd "$GERRIT_PROJECT"
303 GIT_VERSION=$(git tag -l --points-at HEAD)
304
Zack Williams6e070f52019-10-04 11:08:59 -0700305 # match bare versions or v-prefixed golang style version
306 if [[ "$GIT_VERSION" =~ ^v?([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]
Zack Williams27cd3e52018-09-18 16:44:50 -0700307 then
308 echo "git has a SemVer released version tag: '$GIT_VERSION'"
309 echo "Building artifacts for GitHub release."
310 else
311 echo "No SemVer released version tag found, exiting..."
312 exit 0
313 fi
314popd
315
Zack Williams0f398492019-10-23 16:02:24 -0700316# Set and handle GOPATH and PATH
317export GOPATH=${GOPATH:-$WORKSPACE/go}
318export PATH=$PATH:/usr/lib/go-1.12/bin:/usr/local/go/bin:$GOPATH/bin
319
320# To support golang projects that require GOPATH to be set and code checked out there
Zack Williams27cd3e52018-09-18 16:44:50 -0700321# If $DEST_GOPATH is not an empty string:
Zack Williams76356cb2019-08-30 10:44:42 -0700322# - create GOPATH within WORKSPACE, and destination directory within
Zack Williams27cd3e52018-09-18 16:44:50 -0700323# - set PATH to include $GOPATH/bin and the system go binaries
Zack Williams76356cb2019-08-30 10:44:42 -0700324# - move project from $WORKSPACE/$GERRIT_PROJECT to new location in $GOPATH
325# - start release process within that directory
Zack Williams27cd3e52018-09-18 16:44:50 -0700326
327DEST_GOPATH=${DEST_GOPATH:-}
Joey Armstrong28eddda2023-01-10 03:09:34 -0500328if [ -n "$DEST_GOPATH" ]; then
Zack Williams27cd3e52018-09-18 16:44:50 -0700329 mkdir -p "$GOPATH/src/$DEST_GOPATH"
Zack Williams27cd3e52018-09-18 16:44:50 -0700330 release_path="$GOPATH/src/$DEST_GOPATH/$GERRIT_PROJECT"
Zack Williams76356cb2019-08-30 10:44:42 -0700331 mv "$WORKSPACE/$GERRIT_PROJECT" "$release_path"
Zack Williams27cd3e52018-09-18 16:44:50 -0700332else
333 release_path="$WORKSPACE/$GERRIT_PROJECT"
334fi
335
336if [ ! -f "$release_path/Makefile" ]; then
337 echo "Makefile not found at $release_path!"
338 exit 1
339else
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500340
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500341 # shellcheck disable=SC2015
342 [[ -v TRACE ]] && { set -x; } || { set +x; } # SC2015 (shellcheck -x)
343
Zack Williams27cd3e52018-09-18 16:44:50 -0700344 pushd "$release_path"
345
346 # Release description is sanitized version of the log message
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500347 RELEASE_DESCRIPTION="$(git log -1 --pretty=%B | tr -dc "[:alnum:]\n\r\.\[\]\:\-\\\/\`\' ")"
Zack Williams27cd3e52018-09-18 16:44:50 -0700348
349 # build the release, can be multiple space separated targets
350 # shellcheck disable=SC2086
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500351 make "$RELEASE_TARGETS"
Zack Williams27cd3e52018-09-18 16:44:50 -0700352
Joey Armstrong26707f02023-01-26 12:41:12 -0500353 # doDebug # deterine why ARTIFACT_GLOB is empty
354 copyToRelease
Joey Armstrong50f6e0b2023-01-24 14:14:08 -0500355
Joey Armstrong26707f02023-01-26 12:41:12 -0500356 cat <<EOM
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500357
358** -----------------------------------------------------------------------
359** Create the release:
360** 1) Create initial github release with download area.
361** 2) Generate checksum.SHA256 for all released files.
362** 3) Upload files to complete the release.
363** 4) Display released info from github.
364** -----------------------------------------------------------------------
Joey Armstrong26707f02023-01-26 12:41:12 -0500365EOM
366
Joey Armstrongf085d872023-01-28 17:52:29 -0500367# git auth login
368# git auth logout
369
Joey Armstrong26707f02023-01-26 12:41:12 -0500370 # Usage: github-release [global options] <verb> [verb options]
Zack Williams27cd3e52018-09-18 16:44:50 -0700371 # create release
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500372 echo "** ${iam} Creating Release: $GERRIT_PROJECT - $GIT_VERSION"
Joey Armstrongf085d872023-01-28 17:52:29 -0500373 github_release_pre 'gh'\
374 "$GITHUB_ORGANIZATION"\
375 "$GERRIT_PROJECT"\
376 "$GIT_VERSION"\
377 "$GERRIT_PROJECT - $GIT_VERSION"\
378 "$RELEASE_DESCRIPTION"
Zack Williams27cd3e52018-09-18 16:44:50 -0700379
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500380 echo "** ${iam} Packaging release files"
Zack Williams27cd3e52018-09-18 16:44:50 -0700381 pushd "$RELEASE_TEMP"
382
Joey Armstrongf085d872023-01-28 17:52:29 -0500383 echo "** ${iam}: Files to release:"
384 readarray -t to_release < <(find . -mindepth 1 -maxdepth 1 -type f -print)
385 declare -p to_release
Joey Armstrongd8b6f332023-01-25 18:35:17 -0500386
Zack Williams27cd3e52018-09-18 16:44:50 -0700387 # Generate and check checksums
Zack Williams2f8e8472019-05-21 16:29:06 -0700388 sha256sum -- * > checksum.SHA256
Zack Williams27cd3e52018-09-18 16:44:50 -0700389 sha256sum -c < checksum.SHA256
390
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500391 echo
392 echo "** ${iam} Checksums(checksum.SHA256):"
Zack Williams0d93d842019-05-21 17:02:49 -0700393 cat checksum.SHA256
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500394 echo
Zack Williams0d93d842019-05-21 17:02:49 -0700395
Joey Armstrongf085d872023-01-28 17:52:29 -0500396 echo "** ${iam} Upload files being released"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500397
398 # shellcheck disable=SC2194
399 case 'gh' in
Joey Armstrongf085d872023-01-28 17:52:29 -0500400 gh)
Joey Armstrongd94b5e62023-01-29 19:23:42 -0500401 declare -a cmd=()
402 cmd+=("$gh_cmd")
403 cmd+=('release' 'upload')
404 cmd+=('--repo' "$GERRIT_PROJECT")
405 cmd+=("${to_release[@]}")
406 "${cmd[@]}"
Joey Armstrongf085d872023-01-28 17:52:29 -0500407 ;;
408
409 *)
410
411 # upload all files to the release
412 for rel_file in *
413 do
414 echo "** ${iam} Uploading file: $rel_file"
415 github-release \
416 upload \
417 --user "$GITHUB_ORGANIZATION" \
418 --repo "$GERRIT_PROJECT" \
419 --tag "$GIT_VERSION" \
420 --name "$rel_file" \
421 --file "$rel_file"
422 done
423 ;;
424 esac
Zack Williams27cd3e52018-09-18 16:44:50 -0700425
426 popd
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500427 popd
Zack Williams27cd3e52018-09-18 16:44:50 -0700428fi
Joey Armstrong28eddda2023-01-10 03:09:34 -0500429
430# [SEE ALSO]
431# -----------------------------------------------------------------------
432# https://www.shellcheck.net/wiki/SC2236
Joey Armstrong26707f02023-01-26 12:41:12 -0500433# https://docs.github.com/en/rest/releases/releases?apiVersion=2022-11-28#create-a-release
Joey Armstrong28eddda2023-01-10 03:09:34 -0500434# -----------------------------------------------------------------------
Joey Armstrongf085d872023-01-28 17:52:29 -0500435# https://cli.github.com/manual/gh_help_reference
436# https://cli.github.com/manual/gh_release
437# -----------------------------------------------------------------------
Joey Armstrong28eddda2023-01-10 03:09:34 -0500438
439# [EOF]