blob: da2badad8ba1a8b69f292fb47efa1aaeea2342c8 [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 Armstrongaf679da2023-01-31 14:22:41 -050044declare -g SCRIPT_VERSION='1.1' # 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
Joey Armstrongaf679da2023-01-31 14:22:41 -0500192 declare -p user
193 declare -p repo
194 declare -p tag
195 declare -p name
196 declare -p descr
197
Joey Armstrongf085d872023-01-28 17:52:29 -0500198 case "$what" in
199 gh)
200 declare -a cmd=()
201
Joey Armstrongd94b5e62023-01-29 19:23:42 -0500202 ## [TODO] Refactor into a function accepting:
203 ## --create
204 ## --info
205 ## --upload
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500206 cmd+=("$gh_cmd")
Joey Armstrongf085d872023-01-28 17:52:29 -0500207 # cmd+=('--verbose')
Joey Armstrongcf41d332023-01-30 11:43:05 -0500208 cmd+=('release' 'create')
209 cmd+=("$tag") # no switch for tag, pass inline.
210 # cmd+=('--latest') # auto tag based on origin/master
211 # cmd+=('--target') # Use when a branch based release is needed.
212
Joey Armstrong717c7782023-01-30 09:18:58 -0500213 cmd+=('--repo' "opencord/$repo")
Joey Armstrongf085d872023-01-28 17:52:29 -0500214 cmd+=('--title' "$name")
Joey Armstrongd94b5e62023-01-29 19:23:42 -0500215 # cmd+=('--descripton' "$descr") # not supported
Joey Armstrongf085d872023-01-28 17:52:29 -0500216 cmd+=('--discussion-category' "Announcements")
Joey Armstrongcf41d332023-01-30 11:43:05 -0500217 # cmd+=('--verify-tag') # fatal
Joey Armstrongf085d872023-01-28 17:52:29 -0500218
Joey Armstrong41923cc2023-01-30 14:38:16 -0500219 declare -p cmd
220
Joey Armstrongcf41d332023-01-30 11:43:05 -0500221 # gh release create [<tag>] [<files>...]
Joey Armstrongf085d872023-01-28 17:52:29 -0500222 echo "** ${iam}: RUNNING " "${cmd[@]}"
223 "${cmd[@]}"
224 ;;
225
226 *)
227 declare -a cmd=()
228
229 cmd+=('github-release')
230 # cmd+=('--verbose')
231 cmd+=('release')
232 cmd+=('--user' "$user")
233 cmd+=('--repo' "$repo")
234 cmd+=('--tag' "$tag")
235 cmd+=('--name' "$name")
236 cmd+=('--descripton' "$descr")
237
238 echo "** ${iam}: RUNNING " "${cmd[@]}"
239 "${cmd[@]}"
240 ;;
241 esac
Joey Armstrongf085d872023-01-28 17:52:29 -0500242
243 echo "** ${iam}: ENTER"
244 return
245}
246
247## -----------------------------------------------------------------------
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500248## Intent:
Joey Armstrong26707f02023-01-26 12:41:12 -0500249## -----------------------------------------------------------------------
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500250function install_gh_binary()
Joey Armstrong26707f02023-01-26 12:41:12 -0500251{
252 local iam="${FUNCNAME[0]}"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500253 echo "** $iam: ENTER"
Joey Armstrong26707f02023-01-26 12:41:12 -0500254
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500255 pushd "$scratch"
256 echo -e "\n** ${iam}: Retrieve latest gh download URLs"
Joey Armstrongf085d872023-01-28 17:52:29 -0500257
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500258 local latest="https://github.com/cli/cli/releases/latest"
259 local tarball="gh.tar.tgz"
260
261 local VER
262 VER=$(curl --silent -qI "$latest" \
263 | awk -F '/' '/^location/ {print substr($NF, 1, length($NF)-1)}')
264 # echo "VER=[$VER]"
265
266 echo "** ${iam}: Download latest gh binary"
267 local url="https://github.com/cli/cli/releases/download/${VER}/gh_${VER#v}_linux_amd64.tar.gz"
268 wget --quiet --output-document="$tarball" "$url"
Joey Armstrong26707f02023-01-26 12:41:12 -0500269
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500270 echo "** ${iam}: Unpack tarball"
271 tar zxf "$tarball"
272
273 gh_cmd="$(find "${scratch}" -name 'gh' -print)"
274 readonly gh_cmd
275
276 echo "** ${iam} Command: ${gh_cmd}"
277 echo "** ${iam} Version: $("$gh_cmd" --version)"
278 popd
279
280 echo "** $iam: LEAVE"
Joey Armstrong26707f02023-01-26 12:41:12 -0500281 return
282}
283
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500284##----------------##
285##---] MAIN [---##
286##----------------##
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500287iam="${0##*/}"
288
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500289banner
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500290init
291install_gh_binary
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500292
Zack Williams27cd3e52018-09-18 16:44:50 -0700293# when not running under Jenkins, use current dir as workspace and a blank
294# project name
295WORKSPACE=${WORKSPACE:-.}
296GERRIT_PROJECT=${GERRIT_PROJECT:-}
297
298# Github organization (or user) this project is published on. Project name should
299# be the same on both Gerrit and GitHub
300GITHUB_ORGANIZATION=${GITHUB_ORGANIZATION:-}
301
302# glob pattern relative to project dir matching release artifacts
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500303# ARTIFACT_GLOB=${ARTIFACT_GLOB:-"release/*"} # stat -- release/* not found, literal string (?)
Joey Armstrong50f6e0b2023-01-24 14:14:08 -0500304ARTIFACT_GLOB=${ARTIFACT_GLOB:-"release/."}
Zack Williams27cd3e52018-09-18 16:44:50 -0700305
306# Temporary staging directory to copy artifacts to
307RELEASE_TEMP="$WORKSPACE/release"
Zack Williams2f8e8472019-05-21 16:29:06 -0700308mkdir -p "$RELEASE_TEMP"
Zack Williams27cd3e52018-09-18 16:44:50 -0700309
310# Use "release" as the default makefile target, can be a space separated list
311RELEASE_TARGETS=${RELEASE_TARGETS:-release}
312
Zack Williams0f398492019-10-23 16:02:24 -0700313
Zack Williams27cd3e52018-09-18 16:44:50 -0700314# check that we're on a semver released version, or exit
315pushd "$GERRIT_PROJECT"
316 GIT_VERSION=$(git tag -l --points-at HEAD)
317
Zack Williams6e070f52019-10-04 11:08:59 -0700318 # match bare versions or v-prefixed golang style version
319 if [[ "$GIT_VERSION" =~ ^v?([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]
Zack Williams27cd3e52018-09-18 16:44:50 -0700320 then
321 echo "git has a SemVer released version tag: '$GIT_VERSION'"
322 echo "Building artifacts for GitHub release."
323 else
324 echo "No SemVer released version tag found, exiting..."
325 exit 0
326 fi
327popd
328
Zack Williams0f398492019-10-23 16:02:24 -0700329# Set and handle GOPATH and PATH
330export GOPATH=${GOPATH:-$WORKSPACE/go}
331export PATH=$PATH:/usr/lib/go-1.12/bin:/usr/local/go/bin:$GOPATH/bin
332
333# To support golang projects that require GOPATH to be set and code checked out there
Zack Williams27cd3e52018-09-18 16:44:50 -0700334# If $DEST_GOPATH is not an empty string:
Zack Williams76356cb2019-08-30 10:44:42 -0700335# - create GOPATH within WORKSPACE, and destination directory within
Zack Williams27cd3e52018-09-18 16:44:50 -0700336# - set PATH to include $GOPATH/bin and the system go binaries
Zack Williams76356cb2019-08-30 10:44:42 -0700337# - move project from $WORKSPACE/$GERRIT_PROJECT to new location in $GOPATH
338# - start release process within that directory
Zack Williams27cd3e52018-09-18 16:44:50 -0700339
340DEST_GOPATH=${DEST_GOPATH:-}
Joey Armstrong28eddda2023-01-10 03:09:34 -0500341if [ -n "$DEST_GOPATH" ]; then
Zack Williams27cd3e52018-09-18 16:44:50 -0700342 mkdir -p "$GOPATH/src/$DEST_GOPATH"
Zack Williams27cd3e52018-09-18 16:44:50 -0700343 release_path="$GOPATH/src/$DEST_GOPATH/$GERRIT_PROJECT"
Zack Williams76356cb2019-08-30 10:44:42 -0700344 mv "$WORKSPACE/$GERRIT_PROJECT" "$release_path"
Zack Williams27cd3e52018-09-18 16:44:50 -0700345else
346 release_path="$WORKSPACE/$GERRIT_PROJECT"
347fi
348
349if [ ! -f "$release_path/Makefile" ]; then
350 echo "Makefile not found at $release_path!"
351 exit 1
352else
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500353
Zack Williams27cd3e52018-09-18 16:44:50 -0700354 pushd "$release_path"
355
356 # Release description is sanitized version of the log message
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500357 RELEASE_DESCRIPTION="$(git log -1 --pretty=%B | tr -dc "[:alnum:]\n\r\.\[\]\:\-\\\/\`\' ")"
Zack Williams27cd3e52018-09-18 16:44:50 -0700358
359 # build the release, can be multiple space separated targets
360 # shellcheck disable=SC2086
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500361 make "$RELEASE_TARGETS"
Zack Williams27cd3e52018-09-18 16:44:50 -0700362
Joey Armstrong26707f02023-01-26 12:41:12 -0500363 # doDebug # deterine why ARTIFACT_GLOB is empty
364 copyToRelease
Joey Armstrong50f6e0b2023-01-24 14:14:08 -0500365
Joey Armstrong26707f02023-01-26 12:41:12 -0500366 cat <<EOM
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500367
368** -----------------------------------------------------------------------
369** Create the release:
370** 1) Create initial github release with download area.
371** 2) Generate checksum.SHA256 for all released files.
372** 3) Upload files to complete the release.
373** 4) Display released info from github.
374** -----------------------------------------------------------------------
Joey Armstrong26707f02023-01-26 12:41:12 -0500375EOM
376
Joey Armstrongf085d872023-01-28 17:52:29 -0500377# git auth login
378# git auth logout
379
Joey Armstrong26707f02023-01-26 12:41:12 -0500380 # Usage: github-release [global options] <verb> [verb options]
Zack Williams27cd3e52018-09-18 16:44:50 -0700381 # create release
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500382 echo "** ${iam} Creating Release: $GERRIT_PROJECT - $GIT_VERSION"
Joey Armstrongf085d872023-01-28 17:52:29 -0500383 github_release_pre 'gh'\
384 "$GITHUB_ORGANIZATION"\
385 "$GERRIT_PROJECT"\
386 "$GIT_VERSION"\
387 "$GERRIT_PROJECT - $GIT_VERSION"\
388 "$RELEASE_DESCRIPTION"
Zack Williams27cd3e52018-09-18 16:44:50 -0700389
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500390 echo "** ${iam} Packaging release files"
Zack Williams27cd3e52018-09-18 16:44:50 -0700391 pushd "$RELEASE_TEMP"
392
Joey Armstrongf085d872023-01-28 17:52:29 -0500393 echo "** ${iam}: Files to release:"
394 readarray -t to_release < <(find . -mindepth 1 -maxdepth 1 -type f -print)
395 declare -p to_release
Joey Armstrongd8b6f332023-01-25 18:35:17 -0500396
Zack Williams27cd3e52018-09-18 16:44:50 -0700397 # Generate and check checksums
Zack Williams2f8e8472019-05-21 16:29:06 -0700398 sha256sum -- * > checksum.SHA256
Zack Williams27cd3e52018-09-18 16:44:50 -0700399 sha256sum -c < checksum.SHA256
400
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500401 echo
402 echo "** ${iam} Checksums(checksum.SHA256):"
Zack Williams0d93d842019-05-21 17:02:49 -0700403 cat checksum.SHA256
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500404 echo
Zack Williams0d93d842019-05-21 17:02:49 -0700405
Joey Armstrongf085d872023-01-28 17:52:29 -0500406 echo "** ${iam} Upload files being released"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500407
408 # shellcheck disable=SC2194
409 case 'gh' in
Joey Armstrongf085d872023-01-28 17:52:29 -0500410 gh)
Joey Armstrongd94b5e62023-01-29 19:23:42 -0500411 declare -a cmd=()
412 cmd+=("$gh_cmd")
413 cmd+=('release' 'upload')
Joey Armstrongcf41d332023-01-30 11:43:05 -0500414 cmd+=("$GIT_VERSION")
Joey Armstrong717c7782023-01-30 09:18:58 -0500415 cmd+=('--repo' "opencord/${GERRIT_PROJECT}")
Joey Armstrongd94b5e62023-01-29 19:23:42 -0500416 cmd+=("${to_release[@]}")
Joey Armstrongcf41d332023-01-30 11:43:05 -0500417
Joey Armstrong41923cc2023-01-30 14:38:16 -0500418 declare -p cmd
419
Joey Armstrongcf41d332023-01-30 11:43:05 -0500420 # gh release upload <tag> <files>... [flags]
Joey Armstrongd94b5e62023-01-29 19:23:42 -0500421 "${cmd[@]}"
Joey Armstrongf085d872023-01-28 17:52:29 -0500422 ;;
423
424 *)
425
426 # upload all files to the release
427 for rel_file in *
428 do
429 echo "** ${iam} Uploading file: $rel_file"
430 github-release \
431 upload \
432 --user "$GITHUB_ORGANIZATION" \
433 --repo "$GERRIT_PROJECT" \
434 --tag "$GIT_VERSION" \
435 --name "$rel_file" \
436 --file "$rel_file"
437 done
438 ;;
439 esac
Zack Williams27cd3e52018-09-18 16:44:50 -0700440
441 popd
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500442 popd
Zack Williams27cd3e52018-09-18 16:44:50 -0700443fi
Joey Armstrong28eddda2023-01-10 03:09:34 -0500444
445# [SEE ALSO]
446# -----------------------------------------------------------------------
447# https://www.shellcheck.net/wiki/SC2236
Joey Armstrong26707f02023-01-26 12:41:12 -0500448# https://docs.github.com/en/rest/releases/releases?apiVersion=2022-11-28#create-a-release
Joey Armstrong28eddda2023-01-10 03:09:34 -0500449# -----------------------------------------------------------------------
Joey Armstrongf085d872023-01-28 17:52:29 -0500450# https://cli.github.com/manual/gh_help_reference
451# https://cli.github.com/manual/gh_release
452# -----------------------------------------------------------------------
Joey Armstrong28eddda2023-01-10 03:09:34 -0500453
454# [EOF]