blob: 58164c459cfcfdcd2260b12e1053f54a63754861 [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 Armstrongd99e3d22023-01-29 12:35:43 -0500191 cmd+=("$gh_cmd")
Joey Armstrongf085d872023-01-28 17:52:29 -0500192 # cmd+=('--verbose')
193 cmd+=('release')
194 cmd+=('create')
195 cmd+=('--user' "$user")
196 cmd+=('--repo' "$repo")
197 cmd+=('--title' "$name")
198 cmd+=('--descripton' "$descr")
199 cmd+=('--discussion-category' "Announcements")
200 # cmd+=('--latest') - auto based on date & ver
201 cmd+=('--verify-tag')
202
203 # --branch exists, omit switch for tag
204 cmd+=("$tag")
205
206 echo "** ${iam}: RUNNING " "${cmd[@]}"
207 "${cmd[@]}"
208 ;;
209
210 *)
211 declare -a cmd=()
212
213 cmd+=('github-release')
214 # cmd+=('--verbose')
215 cmd+=('release')
216 cmd+=('--user' "$user")
217 cmd+=('--repo' "$repo")
218 cmd+=('--tag' "$tag")
219 cmd+=('--name' "$name")
220 cmd+=('--descripton' "$descr")
221
222 echo "** ${iam}: RUNNING " "${cmd[@]}"
223 "${cmd[@]}"
224 ;;
225 esac
Joey Armstrongf085d872023-01-28 17:52:29 -0500226
227 echo "** ${iam}: ENTER"
228 return
229}
230
231## -----------------------------------------------------------------------
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500232## Intent:
Joey Armstrong26707f02023-01-26 12:41:12 -0500233## -----------------------------------------------------------------------
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500234function install_gh_binary()
Joey Armstrong26707f02023-01-26 12:41:12 -0500235{
236 local iam="${FUNCNAME[0]}"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500237 echo "** $iam: ENTER"
Joey Armstrong26707f02023-01-26 12:41:12 -0500238
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500239 pushd "$scratch"
240 echo -e "\n** ${iam}: Retrieve latest gh download URLs"
Joey Armstrongf085d872023-01-28 17:52:29 -0500241
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500242 local latest="https://github.com/cli/cli/releases/latest"
243 local tarball="gh.tar.tgz"
244
245 local VER
246 VER=$(curl --silent -qI "$latest" \
247 | awk -F '/' '/^location/ {print substr($NF, 1, length($NF)-1)}')
248 # echo "VER=[$VER]"
249
250 echo "** ${iam}: Download latest gh binary"
251 local url="https://github.com/cli/cli/releases/download/${VER}/gh_${VER#v}_linux_amd64.tar.gz"
252 wget --quiet --output-document="$tarball" "$url"
Joey Armstrong26707f02023-01-26 12:41:12 -0500253
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500254 echo "** ${iam}: Unpack tarball"
255 tar zxf "$tarball"
256
257 gh_cmd="$(find "${scratch}" -name 'gh' -print)"
258 readonly gh_cmd
259
260 echo "** ${iam} Command: ${gh_cmd}"
261 echo "** ${iam} Version: $("$gh_cmd" --version)"
262 popd
263
264 echo "** $iam: LEAVE"
Joey Armstrong26707f02023-01-26 12:41:12 -0500265 return
266}
267
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500268##----------------##
269##---] MAIN [---##
270##----------------##
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500271iam="${0##*/}"
272
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500273banner
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500274init
275install_gh_binary
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500276
Zack Williams27cd3e52018-09-18 16:44:50 -0700277# when not running under Jenkins, use current dir as workspace and a blank
278# project name
279WORKSPACE=${WORKSPACE:-.}
280GERRIT_PROJECT=${GERRIT_PROJECT:-}
281
282# Github organization (or user) this project is published on. Project name should
283# be the same on both Gerrit and GitHub
284GITHUB_ORGANIZATION=${GITHUB_ORGANIZATION:-}
285
286# glob pattern relative to project dir matching release artifacts
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500287# ARTIFACT_GLOB=${ARTIFACT_GLOB:-"release/*"} # stat -- release/* not found, literal string (?)
Joey Armstrong50f6e0b2023-01-24 14:14:08 -0500288ARTIFACT_GLOB=${ARTIFACT_GLOB:-"release/."}
Zack Williams27cd3e52018-09-18 16:44:50 -0700289
290# Temporary staging directory to copy artifacts to
291RELEASE_TEMP="$WORKSPACE/release"
Zack Williams2f8e8472019-05-21 16:29:06 -0700292mkdir -p "$RELEASE_TEMP"
Zack Williams27cd3e52018-09-18 16:44:50 -0700293
294# Use "release" as the default makefile target, can be a space separated list
295RELEASE_TARGETS=${RELEASE_TARGETS:-release}
296
Zack Williams0f398492019-10-23 16:02:24 -0700297
Zack Williams27cd3e52018-09-18 16:44:50 -0700298# check that we're on a semver released version, or exit
299pushd "$GERRIT_PROJECT"
300 GIT_VERSION=$(git tag -l --points-at HEAD)
301
Zack Williams6e070f52019-10-04 11:08:59 -0700302 # match bare versions or v-prefixed golang style version
303 if [[ "$GIT_VERSION" =~ ^v?([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]
Zack Williams27cd3e52018-09-18 16:44:50 -0700304 then
305 echo "git has a SemVer released version tag: '$GIT_VERSION'"
306 echo "Building artifacts for GitHub release."
307 else
308 echo "No SemVer released version tag found, exiting..."
309 exit 0
310 fi
311popd
312
Zack Williams0f398492019-10-23 16:02:24 -0700313# Set and handle GOPATH and PATH
314export GOPATH=${GOPATH:-$WORKSPACE/go}
315export PATH=$PATH:/usr/lib/go-1.12/bin:/usr/local/go/bin:$GOPATH/bin
316
317# To support golang projects that require GOPATH to be set and code checked out there
Zack Williams27cd3e52018-09-18 16:44:50 -0700318# If $DEST_GOPATH is not an empty string:
Zack Williams76356cb2019-08-30 10:44:42 -0700319# - create GOPATH within WORKSPACE, and destination directory within
Zack Williams27cd3e52018-09-18 16:44:50 -0700320# - set PATH to include $GOPATH/bin and the system go binaries
Zack Williams76356cb2019-08-30 10:44:42 -0700321# - move project from $WORKSPACE/$GERRIT_PROJECT to new location in $GOPATH
322# - start release process within that directory
Zack Williams27cd3e52018-09-18 16:44:50 -0700323
324DEST_GOPATH=${DEST_GOPATH:-}
Joey Armstrong28eddda2023-01-10 03:09:34 -0500325if [ -n "$DEST_GOPATH" ]; then
Zack Williams27cd3e52018-09-18 16:44:50 -0700326 mkdir -p "$GOPATH/src/$DEST_GOPATH"
Zack Williams27cd3e52018-09-18 16:44:50 -0700327 release_path="$GOPATH/src/$DEST_GOPATH/$GERRIT_PROJECT"
Zack Williams76356cb2019-08-30 10:44:42 -0700328 mv "$WORKSPACE/$GERRIT_PROJECT" "$release_path"
Zack Williams27cd3e52018-09-18 16:44:50 -0700329else
330 release_path="$WORKSPACE/$GERRIT_PROJECT"
331fi
332
333if [ ! -f "$release_path/Makefile" ]; then
334 echo "Makefile not found at $release_path!"
335 exit 1
336else
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500337
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500338 # shellcheck disable=SC2015
339 [[ -v TRACE ]] && { set -x; } || { set +x; } # SC2015 (shellcheck -x)
340
Zack Williams27cd3e52018-09-18 16:44:50 -0700341 pushd "$release_path"
342
343 # Release description is sanitized version of the log message
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500344 RELEASE_DESCRIPTION="$(git log -1 --pretty=%B | tr -dc "[:alnum:]\n\r\.\[\]\:\-\\\/\`\' ")"
Zack Williams27cd3e52018-09-18 16:44:50 -0700345
346 # build the release, can be multiple space separated targets
347 # shellcheck disable=SC2086
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500348 make "$RELEASE_TARGETS"
Zack Williams27cd3e52018-09-18 16:44:50 -0700349
Joey Armstrong26707f02023-01-26 12:41:12 -0500350 # doDebug # deterine why ARTIFACT_GLOB is empty
351 copyToRelease
Joey Armstrong50f6e0b2023-01-24 14:14:08 -0500352
Joey Armstrong26707f02023-01-26 12:41:12 -0500353 cat <<EOM
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500354
355** -----------------------------------------------------------------------
356** Create the release:
357** 1) Create initial github release with download area.
358** 2) Generate checksum.SHA256 for all released files.
359** 3) Upload files to complete the release.
360** 4) Display released info from github.
361** -----------------------------------------------------------------------
Joey Armstrong26707f02023-01-26 12:41:12 -0500362EOM
363
Joey Armstrongf085d872023-01-28 17:52:29 -0500364# git auth login
365# git auth logout
366
Joey Armstrong26707f02023-01-26 12:41:12 -0500367 # Usage: github-release [global options] <verb> [verb options]
Zack Williams27cd3e52018-09-18 16:44:50 -0700368 # create release
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500369 echo "** ${iam} Creating Release: $GERRIT_PROJECT - $GIT_VERSION"
Joey Armstrongf085d872023-01-28 17:52:29 -0500370 github_release_pre 'gh'\
371 "$GITHUB_ORGANIZATION"\
372 "$GERRIT_PROJECT"\
373 "$GIT_VERSION"\
374 "$GERRIT_PROJECT - $GIT_VERSION"\
375 "$RELEASE_DESCRIPTION"
Zack Williams27cd3e52018-09-18 16:44:50 -0700376
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500377 echo "** ${iam} Packaging release files"
Zack Williams27cd3e52018-09-18 16:44:50 -0700378 pushd "$RELEASE_TEMP"
379
Joey Armstrongf085d872023-01-28 17:52:29 -0500380 echo "** ${iam}: Files to release:"
381 readarray -t to_release < <(find . -mindepth 1 -maxdepth 1 -type f -print)
382 declare -p to_release
Joey Armstrongd8b6f332023-01-25 18:35:17 -0500383
Zack Williams27cd3e52018-09-18 16:44:50 -0700384 # Generate and check checksums
Zack Williams2f8e8472019-05-21 16:29:06 -0700385 sha256sum -- * > checksum.SHA256
Zack Williams27cd3e52018-09-18 16:44:50 -0700386 sha256sum -c < checksum.SHA256
387
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500388 echo
389 echo "** ${iam} Checksums(checksum.SHA256):"
Zack Williams0d93d842019-05-21 17:02:49 -0700390 cat checksum.SHA256
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500391 echo
Zack Williams0d93d842019-05-21 17:02:49 -0700392
Joey Armstrongf085d872023-01-28 17:52:29 -0500393 echo "** ${iam} Upload files being released"
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500394
395 # shellcheck disable=SC2194
396 case 'gh' in
Joey Armstrongf085d872023-01-28 17:52:29 -0500397 gh)
Joey Armstrongd99e3d22023-01-29 12:35:43 -0500398 "$gh_cmd" release upload "$GIT_VERSION" "${to_release[@]}"
Joey Armstrongf085d872023-01-28 17:52:29 -0500399 ;;
400
401 *)
402
403 # upload all files to the release
404 for rel_file in *
405 do
406 echo "** ${iam} Uploading file: $rel_file"
407 github-release \
408 upload \
409 --user "$GITHUB_ORGANIZATION" \
410 --repo "$GERRIT_PROJECT" \
411 --tag "$GIT_VERSION" \
412 --name "$rel_file" \
413 --file "$rel_file"
414 done
415 ;;
416 esac
Zack Williams27cd3e52018-09-18 16:44:50 -0700417
418 popd
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500419 popd
Zack Williams27cd3e52018-09-18 16:44:50 -0700420fi
Joey Armstrong28eddda2023-01-10 03:09:34 -0500421
422# [SEE ALSO]
423# -----------------------------------------------------------------------
424# https://www.shellcheck.net/wiki/SC2236
Joey Armstrong26707f02023-01-26 12:41:12 -0500425# https://docs.github.com/en/rest/releases/releases?apiVersion=2022-11-28#create-a-release
Joey Armstrong28eddda2023-01-10 03:09:34 -0500426# -----------------------------------------------------------------------
Joey Armstrongf085d872023-01-28 17:52:29 -0500427# https://cli.github.com/manual/gh_help_reference
428# https://cli.github.com/manual/gh_release
429# -----------------------------------------------------------------------
Joey Armstrong28eddda2023-01-10 03:09:34 -0500430
431# [EOF]