blob: d3a5b8c02ec5d9fe5f60fc583559e91a23f81dbf [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 Armstrong41923cc2023-01-30 14:38:16 -050029# declare -g TRACE=0 # uncomment to set -x
30
31# shellcheck disable=SC2015
32[[ -v TRACE ]] && { set -x; } || { set +x; } # SC2015 (shellcheck -x)
33
34
Joey Armstrong7f382ef2023-01-25 12:00:08 -050035##-------------------##
36##---] GLOBALS [---##
37##-------------------##
Joey Armstrongd99e3d22023-01-29 12:35:43 -050038set -euo pipefail
39
40declare -g scratch # temp workspace for downloads
41declare -g gh_cmd # path to gh command
42
Joey Armstrong7f382ef2023-01-25 12:00:08 -050043declare -g ARGV="$*" # archive for display
Joey Armstrong1962bcf2023-01-27 13:53:18 -050044declare -g SCRIPT_VERSION='1.0' # git changeset needed
Joey Armstrong7f382ef2023-01-25 12:00:08 -050045
Joey Armstrong26707f02023-01-26 12:41:12 -050046declare -g RELEASE_TEMP
47
Joey Armstrong7f382ef2023-01-25 12:00:08 -050048##--------------------##
49##---] INCLUDES [---##
Joey Armstrong1962bcf2023-01-27 13:53:18 -050050##--------------------#
Joey Armstrong7f382ef2023-01-25 12:00:08 -050051# shellcheck disable=SC1091
Joey Armstrong1962bcf2023-01-27 13:53:18 -050052# source "${pgmdir}/common/common.sh" "${common_args[@]}"
Joey Armstrong7f382ef2023-01-25 12:00:08 -050053
54## -----------------------------------------------------------------------
Joey Armstrongd99e3d22023-01-29 12:35:43 -050055## Intent: Cleanup scratch area on exit
56## -----------------------------------------------------------------------
57function sigtrap()
58{
59 ## Prevent mishaps
60 local is_read_only
61 is_read_only="$(declare -p scratch)"
62 if [[ $is_read_only != *"declare -r scratch="* ]]; then
63 echo "ERROR: variable scratch is not read-only, cleanup skipped"
64 exit 1
65 fi
66
67 if [ -d "$scratch" ]; then
68 /bin/rm -fr "$scratch"
69 fi
70
71 return
72}
73trap sigtrap EXIT
74
75## -----------------------------------------------------------------------
76## Intent: Create a scratch area for downloads and transient tools
77## -----------------------------------------------------------------------
78function init()
79{
80 declare -g scratch
81
82 local pkgbase="${0##*/}" # basename
83 local pkgname="${pkgbase%.*}"
84
85 scratch="$(mktemp -d -t "${pkgname}.XXXXXXXXXX")"
86 readonly scratch
87 return
88}
89
90## -----------------------------------------------------------------------
Joey Armstrong7f382ef2023-01-25 12:00:08 -050091## Intent: Output a log banner to identify the running script/version.
92## -----------------------------------------------------------------------
93## TODO:
94## o SCRIPT_VERSION => git changeset for repo:ci-managment
95## o echo "library version: ${env."library.libName.version"}"
96# -----------------------------------------------------------------------
97# 14:18:38 > git fetch --no-tags --progress -- https://gerrit.opencord.org/ci-management.git +refs/heads/*:refs/remotes/origin/* # timeout=10
98# 14:18:39 Checking out Revision 50f6e0b97f449b32d32ec0e02d59642000351847 (master)
99# -----------------------------------------------------------------------
100function banner()
101{
102 local iam="${0##*/}"
103
104cat <<EOH
105
106** -----------------------------------------------------------------------
107** IAM: ${iam} :: ${FUNCNAME[0]}
108** ARGV: ${ARGV}
109** PWD: $(/bin/pwd)
110** NOW: $(date '+%Y/%m/%d %H:%M:%S')
111** VER: ${SCRIPT_VERSION:-'unknown'}
112** -----------------------------------------------------------------------
113EOH
114
115 return
116}
117
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500118## -----------------------------------------------------------------------
Joey Armstrong50f6e0b2023-01-24 14:14:08 -0500119## Intent:
120## Display available command versions
121## A github release job is failing due to a command being mia.
122## -----------------------------------------------------------------------
123function displayCommands()
124{
125 # which git || /bin/true
126 # git --version || /bin/true
127 # go version || /bin/true
128 # helm version || /bin/true
129 return
130}
131
132## -----------------------------------------------------------------------
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500133## Intent: Display filesystem with a banner for logging
134## -----------------------------------------------------------------------
135function displayPwd()
136{
137 local iam="${0##*/}"
138 echo "** ${iam}: ENTER"
139
140cat <<EOM
141
142** -----------------------------------------------------------------------
143** IAM: $iam :: ${FUNCNAME[0]}
144** PWD: $(/bin/pwd)
145** -----------------------------------------------------------------------
146EOM
147 find . -maxdepth 1 -ls
148 echo "** ${iam}: LEAVE"
149 return
150}
151
152## -----------------------------------------------------------------------
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500153## Intent: Copy files from the build directory into the release staging directory.
Joey Armstrong26707f02023-01-26 12:41:12 -0500154## -----------------------------------------------------------------------
155function copyToRelease()
156{
157 local iam="${FUNCNAME[0]}"
158 echo "** ${iam}: ENTER"
159
160 local artifact_glob="${ARTIFACT_GLOB%/*}"
161 echo "** ${iam}: $(declare -p ARTIFACT_GLOB) $(declare -p artifact_glob)"
Joey Armstrong26707f02023-01-26 12:41:12 -0500162
163 # Copy artifacts into the release temp dir
164 # shellcheck disable=SC2086
165 # cp -v "$ARTIFACT_GLOB" "$RELEASE_TEMP"
166 echo "rsync -rv --checksum \"$artifact_glob/.\" \"$RELEASE_TEMP/.\""
167 rsync -rv --checksum "$artifact_glob/." "$RELEASE_TEMP/."
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500168
Joey Armstrong26707f02023-01-26 12:41:12 -0500169 echo "** ${iam}: RELEASE_TEMP=${RELEASE_TEMP}"
170 find "$RELEASE_TEMP" -print
171
172 echo "** ${iam}: LEAVE"
173 echo
174 return
175}
176
177## -----------------------------------------------------------------------
Joey Armstrongf085d872023-01-28 17:52:29 -0500178## Intent:
179## -----------------------------------------------------------------------
180function github_release_pre()
181{
182 local what="$1" ; shift
183 local user="$1" ; shift
184 local repo="$1" ; shift
185 local tag="$1" ; shift
186 local name="$1" ; shift
187 local descr="$1" ; shift
188
189 local iam="${FUNCNAME[0]}"
190 echo "** ${iam}: ENTER"
191
192 case "$what" in
193 gh)
194 declare -a cmd=()
195
Joey Armstrongd94b5e62023-01-29 19:23:42 -0500196 ## [TODO] Refactor into a function accepting:
197 ## --create
198 ## --info
199 ## --upload
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500200 cmd+=("$gh_cmd")
Joey Armstrongf085d872023-01-28 17:52:29 -0500201 # cmd+=('--verbose')
Joey Armstrongcf41d332023-01-30 11:43:05 -0500202 cmd+=('release' 'create')
203 cmd+=("$tag") # no switch for tag, pass inline.
204 # cmd+=('--latest') # auto tag based on origin/master
205 # cmd+=('--target') # Use when a branch based release is needed.
206
Joey Armstrong717c7782023-01-30 09:18:58 -0500207 cmd+=('--repo' "opencord/$repo")
Joey Armstrongf085d872023-01-28 17:52:29 -0500208 cmd+=('--title' "$name")
Joey Armstrongd94b5e62023-01-29 19:23:42 -0500209 # cmd+=('--descripton' "$descr") # not supported
Joey Armstrongf085d872023-01-28 17:52:29 -0500210 cmd+=('--discussion-category' "Announcements")
Joey Armstrongcf41d332023-01-30 11:43:05 -0500211 # cmd+=('--verify-tag') # fatal
Joey Armstrongf085d872023-01-28 17:52:29 -0500212
Joey Armstrong41923cc2023-01-30 14:38:16 -0500213 declare -p cmd
214
Joey Armstrongcf41d332023-01-30 11:43:05 -0500215 # gh release create [<tag>] [<files>...]
Joey Armstrongf085d872023-01-28 17:52:29 -0500216 echo "** ${iam}: RUNNING " "${cmd[@]}"
217 "${cmd[@]}"
218 ;;
219
220 *)
221 declare -a cmd=()
222
223 cmd+=('github-release')
224 # cmd+=('--verbose')
225 cmd+=('release')
226 cmd+=('--user' "$user")
227 cmd+=('--repo' "$repo")
228 cmd+=('--tag' "$tag")
229 cmd+=('--name' "$name")
230 cmd+=('--descripton' "$descr")
231
232 echo "** ${iam}: RUNNING " "${cmd[@]}"
233 "${cmd[@]}"
234 ;;
235 esac
Joey Armstrongf085d872023-01-28 17:52:29 -0500236
237 echo "** ${iam}: ENTER"
238 return
239}
240
241## -----------------------------------------------------------------------
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500242## Intent:
Joey Armstrong26707f02023-01-26 12:41:12 -0500243## -----------------------------------------------------------------------
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500244function install_gh_binary()
Joey Armstrong26707f02023-01-26 12:41:12 -0500245{
246 local iam="${FUNCNAME[0]}"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500247 echo "** $iam: ENTER"
Joey Armstrong26707f02023-01-26 12:41:12 -0500248
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500249 pushd "$scratch"
250 echo -e "\n** ${iam}: Retrieve latest gh download URLs"
Joey Armstrongf085d872023-01-28 17:52:29 -0500251
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500252 local latest="https://github.com/cli/cli/releases/latest"
253 local tarball="gh.tar.tgz"
254
255 local VER
256 VER=$(curl --silent -qI "$latest" \
257 | awk -F '/' '/^location/ {print substr($NF, 1, length($NF)-1)}')
258 # echo "VER=[$VER]"
259
260 echo "** ${iam}: Download latest gh binary"
261 local url="https://github.com/cli/cli/releases/download/${VER}/gh_${VER#v}_linux_amd64.tar.gz"
262 wget --quiet --output-document="$tarball" "$url"
Joey Armstrong26707f02023-01-26 12:41:12 -0500263
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500264 echo "** ${iam}: Unpack tarball"
265 tar zxf "$tarball"
266
267 gh_cmd="$(find "${scratch}" -name 'gh' -print)"
268 readonly gh_cmd
269
270 echo "** ${iam} Command: ${gh_cmd}"
271 echo "** ${iam} Version: $("$gh_cmd" --version)"
272 popd
273
274 echo "** $iam: LEAVE"
Joey Armstrong26707f02023-01-26 12:41:12 -0500275 return
276}
277
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500278##----------------##
279##---] MAIN [---##
280##----------------##
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500281iam="${0##*/}"
282
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500283banner
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500284init
285install_gh_binary
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500286
Zack Williams27cd3e52018-09-18 16:44:50 -0700287# when not running under Jenkins, use current dir as workspace and a blank
288# project name
289WORKSPACE=${WORKSPACE:-.}
290GERRIT_PROJECT=${GERRIT_PROJECT:-}
291
292# Github organization (or user) this project is published on. Project name should
293# be the same on both Gerrit and GitHub
294GITHUB_ORGANIZATION=${GITHUB_ORGANIZATION:-}
295
296# glob pattern relative to project dir matching release artifacts
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500297# ARTIFACT_GLOB=${ARTIFACT_GLOB:-"release/*"} # stat -- release/* not found, literal string (?)
Joey Armstrong50f6e0b2023-01-24 14:14:08 -0500298ARTIFACT_GLOB=${ARTIFACT_GLOB:-"release/."}
Zack Williams27cd3e52018-09-18 16:44:50 -0700299
300# Temporary staging directory to copy artifacts to
301RELEASE_TEMP="$WORKSPACE/release"
Zack Williams2f8e8472019-05-21 16:29:06 -0700302mkdir -p "$RELEASE_TEMP"
Zack Williams27cd3e52018-09-18 16:44:50 -0700303
304# Use "release" as the default makefile target, can be a space separated list
305RELEASE_TARGETS=${RELEASE_TARGETS:-release}
306
Zack Williams0f398492019-10-23 16:02:24 -0700307
Zack Williams27cd3e52018-09-18 16:44:50 -0700308# check that we're on a semver released version, or exit
309pushd "$GERRIT_PROJECT"
310 GIT_VERSION=$(git tag -l --points-at HEAD)
311
Zack Williams6e070f52019-10-04 11:08:59 -0700312 # match bare versions or v-prefixed golang style version
313 if [[ "$GIT_VERSION" =~ ^v?([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]
Zack Williams27cd3e52018-09-18 16:44:50 -0700314 then
315 echo "git has a SemVer released version tag: '$GIT_VERSION'"
316 echo "Building artifacts for GitHub release."
317 else
318 echo "No SemVer released version tag found, exiting..."
319 exit 0
320 fi
321popd
322
Zack Williams0f398492019-10-23 16:02:24 -0700323# Set and handle GOPATH and PATH
324export GOPATH=${GOPATH:-$WORKSPACE/go}
325export PATH=$PATH:/usr/lib/go-1.12/bin:/usr/local/go/bin:$GOPATH/bin
326
327# To support golang projects that require GOPATH to be set and code checked out there
Zack Williams27cd3e52018-09-18 16:44:50 -0700328# If $DEST_GOPATH is not an empty string:
Zack Williams76356cb2019-08-30 10:44:42 -0700329# - create GOPATH within WORKSPACE, and destination directory within
Zack Williams27cd3e52018-09-18 16:44:50 -0700330# - set PATH to include $GOPATH/bin and the system go binaries
Zack Williams76356cb2019-08-30 10:44:42 -0700331# - move project from $WORKSPACE/$GERRIT_PROJECT to new location in $GOPATH
332# - start release process within that directory
Zack Williams27cd3e52018-09-18 16:44:50 -0700333
334DEST_GOPATH=${DEST_GOPATH:-}
Joey Armstrong28eddda2023-01-10 03:09:34 -0500335if [ -n "$DEST_GOPATH" ]; then
Zack Williams27cd3e52018-09-18 16:44:50 -0700336 mkdir -p "$GOPATH/src/$DEST_GOPATH"
Zack Williams27cd3e52018-09-18 16:44:50 -0700337 release_path="$GOPATH/src/$DEST_GOPATH/$GERRIT_PROJECT"
Zack Williams76356cb2019-08-30 10:44:42 -0700338 mv "$WORKSPACE/$GERRIT_PROJECT" "$release_path"
Zack Williams27cd3e52018-09-18 16:44:50 -0700339else
340 release_path="$WORKSPACE/$GERRIT_PROJECT"
341fi
342
343if [ ! -f "$release_path/Makefile" ]; then
344 echo "Makefile not found at $release_path!"
345 exit 1
346else
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500347
Zack Williams27cd3e52018-09-18 16:44:50 -0700348 pushd "$release_path"
349
350 # Release description is sanitized version of the log message
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500351 RELEASE_DESCRIPTION="$(git log -1 --pretty=%B | tr -dc "[:alnum:]\n\r\.\[\]\:\-\\\/\`\' ")"
Zack Williams27cd3e52018-09-18 16:44:50 -0700352
353 # build the release, can be multiple space separated targets
354 # shellcheck disable=SC2086
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500355 make "$RELEASE_TARGETS"
Zack Williams27cd3e52018-09-18 16:44:50 -0700356
Joey Armstrong26707f02023-01-26 12:41:12 -0500357 # doDebug # deterine why ARTIFACT_GLOB is empty
358 copyToRelease
Joey Armstrong50f6e0b2023-01-24 14:14:08 -0500359
Joey Armstrong26707f02023-01-26 12:41:12 -0500360 cat <<EOM
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500361
362** -----------------------------------------------------------------------
363** Create the release:
364** 1) Create initial github release with download area.
365** 2) Generate checksum.SHA256 for all released files.
366** 3) Upload files to complete the release.
367** 4) Display released info from github.
368** -----------------------------------------------------------------------
Joey Armstrong26707f02023-01-26 12:41:12 -0500369EOM
370
Joey Armstrongf085d872023-01-28 17:52:29 -0500371# git auth login
372# git auth logout
373
Joey Armstrong26707f02023-01-26 12:41:12 -0500374 # Usage: github-release [global options] <verb> [verb options]
Zack Williams27cd3e52018-09-18 16:44:50 -0700375 # create release
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500376 echo "** ${iam} Creating Release: $GERRIT_PROJECT - $GIT_VERSION"
Joey Armstrongf085d872023-01-28 17:52:29 -0500377 github_release_pre 'gh'\
378 "$GITHUB_ORGANIZATION"\
379 "$GERRIT_PROJECT"\
380 "$GIT_VERSION"\
381 "$GERRIT_PROJECT - $GIT_VERSION"\
382 "$RELEASE_DESCRIPTION"
Zack Williams27cd3e52018-09-18 16:44:50 -0700383
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500384 echo "** ${iam} Packaging release files"
Zack Williams27cd3e52018-09-18 16:44:50 -0700385 pushd "$RELEASE_TEMP"
386
Joey Armstrongf085d872023-01-28 17:52:29 -0500387 echo "** ${iam}: Files to release:"
388 readarray -t to_release < <(find . -mindepth 1 -maxdepth 1 -type f -print)
389 declare -p to_release
Joey Armstrongd8b6f332023-01-25 18:35:17 -0500390
Zack Williams27cd3e52018-09-18 16:44:50 -0700391 # Generate and check checksums
Zack Williams2f8e8472019-05-21 16:29:06 -0700392 sha256sum -- * > checksum.SHA256
Zack Williams27cd3e52018-09-18 16:44:50 -0700393 sha256sum -c < checksum.SHA256
394
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500395 echo
396 echo "** ${iam} Checksums(checksum.SHA256):"
Zack Williams0d93d842019-05-21 17:02:49 -0700397 cat checksum.SHA256
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500398 echo
Zack Williams0d93d842019-05-21 17:02:49 -0700399
Joey Armstrongf085d872023-01-28 17:52:29 -0500400 echo "** ${iam} Upload files being released"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500401
402 # shellcheck disable=SC2194
403 case 'gh' in
Joey Armstrongf085d872023-01-28 17:52:29 -0500404 gh)
Joey Armstrongd94b5e62023-01-29 19:23:42 -0500405 declare -a cmd=()
406 cmd+=("$gh_cmd")
407 cmd+=('release' 'upload')
Joey Armstrongcf41d332023-01-30 11:43:05 -0500408 cmd+=("$GIT_VERSION")
Joey Armstrong717c7782023-01-30 09:18:58 -0500409 cmd+=('--repo' "opencord/${GERRIT_PROJECT}")
Joey Armstrongd94b5e62023-01-29 19:23:42 -0500410 cmd+=("${to_release[@]}")
Joey Armstrongcf41d332023-01-30 11:43:05 -0500411
Joey Armstrong41923cc2023-01-30 14:38:16 -0500412 declare -p cmd
413
Joey Armstrongcf41d332023-01-30 11:43:05 -0500414 # gh release upload <tag> <files>... [flags]
Joey Armstrongd94b5e62023-01-29 19:23:42 -0500415 "${cmd[@]}"
Joey Armstrongf085d872023-01-28 17:52:29 -0500416 ;;
417
418 *)
419
420 # upload all files to the release
421 for rel_file in *
422 do
423 echo "** ${iam} Uploading file: $rel_file"
424 github-release \
425 upload \
426 --user "$GITHUB_ORGANIZATION" \
427 --repo "$GERRIT_PROJECT" \
428 --tag "$GIT_VERSION" \
429 --name "$rel_file" \
430 --file "$rel_file"
431 done
432 ;;
433 esac
Zack Williams27cd3e52018-09-18 16:44:50 -0700434
435 popd
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500436 popd
Zack Williams27cd3e52018-09-18 16:44:50 -0700437fi
Joey Armstrong28eddda2023-01-10 03:09:34 -0500438
439# [SEE ALSO]
440# -----------------------------------------------------------------------
441# https://www.shellcheck.net/wiki/SC2236
Joey Armstrong26707f02023-01-26 12:41:12 -0500442# https://docs.github.com/en/rest/releases/releases?apiVersion=2022-11-28#create-a-release
Joey Armstrong28eddda2023-01-10 03:09:34 -0500443# -----------------------------------------------------------------------
Joey Armstrongf085d872023-01-28 17:52:29 -0500444# https://cli.github.com/manual/gh_help_reference
445# https://cli.github.com/manual/gh_release
446# -----------------------------------------------------------------------
Joey Armstrong28eddda2023-01-10 03:09:34 -0500447
448# [EOF]