blob: 035efc7b5b527a820070d48158235396ef1d24bf [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# -----------------------------------------------------------------------
Zack Williams27cd3e52018-09-18 16:44:50 -070022
Joey Armstrong7f382ef2023-01-25 12:00:08 -050023##-------------------##
24##---] GLOBALS [---##
25##-------------------##
Joey Armstrong7f382ef2023-01-25 12:00:08 -050026declare -g ARGV="$*" # archive for display
Joey Armstrong1962bcf2023-01-27 13:53:18 -050027declare -g SCRIPT_VERSION='1.0' # git changeset needed
28declare -g TRACE=0 # uncomment to set -x
29declare -g VERBOSE=0 # trace github-release calls
Joey Armstrong7f382ef2023-01-25 12:00:08 -050030
Joey Armstrong26707f02023-01-26 12:41:12 -050031declare -g RELEASE_TEMP
32
Joey Armstrong7f382ef2023-01-25 12:00:08 -050033##--------------------##
34##---] INCLUDES [---##
Joey Armstrong1962bcf2023-01-27 13:53:18 -050035##--------------------#
36# declare -g pgmdir="${0%/*}" # dirname($script)
Joey Armstrongbd54b572023-01-25 14:28:10 -050037echo "** ${0}: PWD=$(/bin/pwd)"
Joey Armstrong7f382ef2023-01-25 12:00:08 -050038
Joey Armstrongd8b6f332023-01-25 18:35:17 -050039# -----------------------------------------------------------------------
40# Jenkins must have checked out/copied the script -vs- repository
41# -----------------------------------------------------------------------
42# 17:56:27 [github-release_voltctl] $ /usr/bin/env bash /tmp/jenkins1043949650153731384.sh
43# 17:56:27 ** /tmp/jenkins1043949650153731384.sh: PWD=/w/workspace/github-release_voltctl
44# 17:56:27 5120009 4 drwxrwxr-x 4 jenkins jenkins 4096 Jan 25 22:56 .
45# 17:56:27 5120010 4 drwxrwxr-x 9 jenkins jenkins 4096 Jan 25 22:56 ./voltctl
46# 17:56:27 5120036 4 drwxrwxr-x 2 jenkins jenkins 4096 Jan 25 22:56 ./voltctl@tmp
47# 17:56:27 /tmp/jenkins1043949650153731384.sh: line 44: /tmp/common/common.sh: No such file or directory
48# -----------------------------------------------------------------------
49
Joey Armstrong1962bcf2023-01-27 13:53:18 -050050# declare -a common_args=()
51# common_args+=('--common-args-begin--')
52# common_args+=('--traputils')
53# common_args+=('--stacktrace')
54# common_args+=('--tempdir')
55
Joey Armstrong7f382ef2023-01-25 12:00:08 -050056# shellcheck disable=SC1091
Joey Armstrong1962bcf2023-01-27 13:53:18 -050057# source "${pgmdir}/common/common.sh" "${common_args[@]}"
Joey Armstrong7f382ef2023-01-25 12:00:08 -050058
59## -----------------------------------------------------------------------
60## Intent: Output a log banner to identify the running script/version.
61## -----------------------------------------------------------------------
62## TODO:
63## o SCRIPT_VERSION => git changeset for repo:ci-managment
64## o echo "library version: ${env."library.libName.version"}"
65# -----------------------------------------------------------------------
66# 14:18:38 > git fetch --no-tags --progress -- https://gerrit.opencord.org/ci-management.git +refs/heads/*:refs/remotes/origin/* # timeout=10
67# 14:18:39 Checking out Revision 50f6e0b97f449b32d32ec0e02d59642000351847 (master)
68# -----------------------------------------------------------------------
69function banner()
70{
71 local iam="${0##*/}"
72
73cat <<EOH
74
75** -----------------------------------------------------------------------
76** IAM: ${iam} :: ${FUNCNAME[0]}
77** ARGV: ${ARGV}
78** PWD: $(/bin/pwd)
79** NOW: $(date '+%Y/%m/%d %H:%M:%S')
80** VER: ${SCRIPT_VERSION:-'unknown'}
81** -----------------------------------------------------------------------
82EOH
83
84 return
85}
86
Joey Armstrongaf577ab2022-12-15 14:43:33 -050087## -----------------------------------------------------------------------
Joey Armstrong50f6e0b2023-01-24 14:14:08 -050088## Intent:
89## Display available command versions
90## A github release job is failing due to a command being mia.
91## -----------------------------------------------------------------------
92function displayCommands()
93{
94 # which git || /bin/true
95 # git --version || /bin/true
96 # go version || /bin/true
97 # helm version || /bin/true
98 return
99}
100
101## -----------------------------------------------------------------------
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500102## Intent: Display filesystem with a banner for logging
103## -----------------------------------------------------------------------
104function displayPwd()
105{
106 local iam="${0##*/}"
107 echo "** ${iam}: ENTER"
108
109cat <<EOM
110
111** -----------------------------------------------------------------------
112** IAM: $iam :: ${FUNCNAME[0]}
113** PWD: $(/bin/pwd)
114** -----------------------------------------------------------------------
115EOM
116 find . -maxdepth 1 -ls
117 echo "** ${iam}: LEAVE"
118 return
119}
120
121## -----------------------------------------------------------------------
122## Intent:
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500123## -----------------------------------------------------------------------
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500124function doDebug()
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500125{
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500126 echo "** ${FUNCNAME[0]}: ENTER"
Joey Armstrong28eddda2023-01-10 03:09:34 -0500127
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500128 echo
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500129 echo "** PWD: $(/bin/pwd)"
130 echo "** make-pre: $(/bin/ls -l)"
131 echo
132
133 declare -p ARTIFACT_GLOB
134 declare -p RELEASE_TEMP
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500135
136 echo
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500137 echo "** ${FUNCNAME[0]}: ARTIFACT_GLOB=${ARTIFACT_GLOB}"
138 local artifact_glob="${ARTIFACT_GLOB%/*}"
139 declare -p artifact_glob
Joey Armstrongbd54b572023-01-25 14:28:10 -0500140
141 echo "** ${FUNCNAME[0]}: PWD=$(/bin/pwd)"
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500142 find "$artifact_glob" -print || /bin/true
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500143
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500144 copyToRelease
145
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500146 # Copy artifacts into the release temp dir
147 # shellcheck disable=SC2086
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500148 # cp -v "$ARTIFACT_GLOB" "$RELEASE_TEMP"
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500149 # echo "rsync -rv --checksum \"$artifact_glob/.\" \"$RELEASE_TEMP/.\""
150 # rsync -rv --checksum "$artifact_glob/." "$RELEASE_TEMP/."
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500151
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500152 echo "** ${FUNCNAME[0]}: LEAVE"
153 echo
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500154 return
155}
156
Joey Armstrong26707f02023-01-26 12:41:12 -0500157## -----------------------------------------------------------------------
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500158## Intent:
Joey Armstrong26707f02023-01-26 12:41:12 -0500159## -----------------------------------------------------------------------
160function copyToRelease()
161{
162 local iam="${FUNCNAME[0]}"
163 echo "** ${iam}: ENTER"
164
165 local artifact_glob="${ARTIFACT_GLOB%/*}"
166 echo "** ${iam}: $(declare -p ARTIFACT_GLOB) $(declare -p artifact_glob)"
Joey Armstrong26707f02023-01-26 12:41:12 -0500167
168 # Copy artifacts into the release temp dir
169 # shellcheck disable=SC2086
170 # cp -v "$ARTIFACT_GLOB" "$RELEASE_TEMP"
171 echo "rsync -rv --checksum \"$artifact_glob/.\" \"$RELEASE_TEMP/.\""
172 rsync -rv --checksum "$artifact_glob/." "$RELEASE_TEMP/."
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500173
Joey Armstrong26707f02023-01-26 12:41:12 -0500174 echo "** ${iam}: RELEASE_TEMP=${RELEASE_TEMP}"
175 find "$RELEASE_TEMP" -print
176
177 echo "** ${iam}: LEAVE"
178 echo
179 return
180}
181
182## -----------------------------------------------------------------------
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500183## Intent:
Joey Armstrong26707f02023-01-26 12:41:12 -0500184## -----------------------------------------------------------------------
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500185function showGithubTool()
Joey Armstrong26707f02023-01-26 12:41:12 -0500186{
187 local iam="${FUNCNAME[0]}"
188 echo "** ${iam}: ENTER"
189
190 echo "** ${iam}: which github-release: $(which -a github-release)"
191 local switch
192 for switch in '--version' '--help';
193 do
194 echo "** ${iam}: github-release ${switch}: $(github-release ${switch})"
195 done
196 echo "** ${iam}: LEAVE"
197 echo
198 return
199}
200
201## -----------------------------------------------------------------------
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500202## Intent: Display info about a release stored on github.
Joey Armstrong26707f02023-01-26 12:41:12 -0500203## -----------------------------------------------------------------------
204function showReleaseInfo()
205{
206 local user="$1" ; shift
207 local repo="$1" ; shift
208 local tag=''
209 if [ $# -gt 0 ]; then
210 tag="$1" ; shift
211 fi
212
213 local iam="${FUNCNAME[0]}"
214 echo "** ${iam}: ENTER"
215
216 cat <<EOM
217
218** -------------------------------------------------------------------
219** ${iam}
220** -------------------------------------------------------------------
221EOM
222
223 declare -a args=()
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500224 [[ $VERBOSE -eq 1 ]] && args+=('--verbose')
225 args+=('info')
Joey Armstrong26707f02023-01-26 12:41:12 -0500226 args+=('--user' "$user")
227 args+=('--repo' "$repo")
228 if [ ${#tag} -gt 0 ]; then
229 args+=('--tag' "$tag")
230 fi
231
232 echo "${iam}: github-relase info"
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500233 github-release "${args[@]}"
Joey Armstrong26707f02023-01-26 12:41:12 -0500234
235 echo "** ${iam}: LEAVE"
236 return
237}
238
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500239##----------------##
240##---] MAIN [---##
241##----------------##
Zack Williams27cd3e52018-09-18 16:44:50 -0700242set -eu -o pipefail
243
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500244iam="${0##*/}"
245
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500246banner
247
Zack Williams27cd3e52018-09-18 16:44:50 -0700248# when not running under Jenkins, use current dir as workspace and a blank
249# project name
250WORKSPACE=${WORKSPACE:-.}
251GERRIT_PROJECT=${GERRIT_PROJECT:-}
252
253# Github organization (or user) this project is published on. Project name should
254# be the same on both Gerrit and GitHub
255GITHUB_ORGANIZATION=${GITHUB_ORGANIZATION:-}
256
257# glob pattern relative to project dir matching release artifacts
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500258# ARTIFACT_GLOB=${ARTIFACT_GLOB:-"release/*"} # stat -- release/* not found, literal string (?)
Joey Armstrong50f6e0b2023-01-24 14:14:08 -0500259ARTIFACT_GLOB=${ARTIFACT_GLOB:-"release/."}
Zack Williams27cd3e52018-09-18 16:44:50 -0700260
261# Temporary staging directory to copy artifacts to
262RELEASE_TEMP="$WORKSPACE/release"
Zack Williams2f8e8472019-05-21 16:29:06 -0700263mkdir -p "$RELEASE_TEMP"
Zack Williams27cd3e52018-09-18 16:44:50 -0700264
265# Use "release" as the default makefile target, can be a space separated list
266RELEASE_TARGETS=${RELEASE_TARGETS:-release}
267
Zack Williams0f398492019-10-23 16:02:24 -0700268
Zack Williams27cd3e52018-09-18 16:44:50 -0700269# check that we're on a semver released version, or exit
270pushd "$GERRIT_PROJECT"
271 GIT_VERSION=$(git tag -l --points-at HEAD)
272
Zack Williams6e070f52019-10-04 11:08:59 -0700273 # match bare versions or v-prefixed golang style version
274 if [[ "$GIT_VERSION" =~ ^v?([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]
Zack Williams27cd3e52018-09-18 16:44:50 -0700275 then
276 echo "git has a SemVer released version tag: '$GIT_VERSION'"
277 echo "Building artifacts for GitHub release."
278 else
279 echo "No SemVer released version tag found, exiting..."
280 exit 0
281 fi
282popd
283
Zack Williams0f398492019-10-23 16:02:24 -0700284# Set and handle GOPATH and PATH
285export GOPATH=${GOPATH:-$WORKSPACE/go}
286export PATH=$PATH:/usr/lib/go-1.12/bin:/usr/local/go/bin:$GOPATH/bin
287
288# To support golang projects that require GOPATH to be set and code checked out there
Zack Williams27cd3e52018-09-18 16:44:50 -0700289# If $DEST_GOPATH is not an empty string:
Zack Williams76356cb2019-08-30 10:44:42 -0700290# - create GOPATH within WORKSPACE, and destination directory within
Zack Williams27cd3e52018-09-18 16:44:50 -0700291# - set PATH to include $GOPATH/bin and the system go binaries
Zack Williams76356cb2019-08-30 10:44:42 -0700292# - move project from $WORKSPACE/$GERRIT_PROJECT to new location in $GOPATH
293# - start release process within that directory
Zack Williams27cd3e52018-09-18 16:44:50 -0700294
295DEST_GOPATH=${DEST_GOPATH:-}
Joey Armstrong28eddda2023-01-10 03:09:34 -0500296if [ -n "$DEST_GOPATH" ]; then
Zack Williams27cd3e52018-09-18 16:44:50 -0700297 mkdir -p "$GOPATH/src/$DEST_GOPATH"
Zack Williams27cd3e52018-09-18 16:44:50 -0700298 release_path="$GOPATH/src/$DEST_GOPATH/$GERRIT_PROJECT"
Zack Williams76356cb2019-08-30 10:44:42 -0700299 mv "$WORKSPACE/$GERRIT_PROJECT" "$release_path"
Zack Williams27cd3e52018-09-18 16:44:50 -0700300else
301 release_path="$WORKSPACE/$GERRIT_PROJECT"
302fi
303
304if [ ! -f "$release_path/Makefile" ]; then
305 echo "Makefile not found at $release_path!"
306 exit 1
307else
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500308
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500309 declare -p release_path
310
311 # shellcheck disable=SC2015
312 [[ -v TRACE ]] && { set -x; } || { set +x; } # SC2015 (shellcheck -x)
313
Zack Williams27cd3e52018-09-18 16:44:50 -0700314 pushd "$release_path"
315
316 # Release description is sanitized version of the log message
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500317 RELEASE_DESCRIPTION="$(git log -1 --pretty=%B | tr -dc "[:alnum:]\n\r\.\[\]\:\-\\\/\`\' ")"
Zack Williams27cd3e52018-09-18 16:44:50 -0700318
319 # build the release, can be multiple space separated targets
320 # shellcheck disable=SC2086
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500321 make "$RELEASE_TARGETS"
Zack Williams27cd3e52018-09-18 16:44:50 -0700322
Joey Armstrong26707f02023-01-26 12:41:12 -0500323 # doDebug # deterine why ARTIFACT_GLOB is empty
324 copyToRelease
Joey Armstrong50f6e0b2023-01-24 14:14:08 -0500325
326 # Are we failing on a literal string "release/*" ?
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500327 # cp -v "$ARTIFACT_GLOB" "$RELEASE_TEMP"
Joey Armstrongd8b6f332023-01-25 18:35:17 -0500328 # echo "rsync -rv --checksum \"$artifact_glob/.\" \"$RELEASE_TEMP/.\""
329 # rsync -rv --checksum "$artifact_glob/." "$RELEASE_TEMP/."
Joey Armstrong50f6e0b2023-01-24 14:14:08 -0500330
Joey Armstrong26707f02023-01-26 12:41:12 -0500331 showReleaseInfo \
332 "$GITHUB_ORGANIZATION"\
333 "$GERRIT_PROJECT"
Joey Armstrong50f6e0b2023-01-24 14:14:08 -0500334
Joey Armstrong26707f02023-01-26 12:41:12 -0500335 showGithubRelease
336
337 cat <<EOM
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500338
339** -----------------------------------------------------------------------
340** Create the release:
341** 1) Create initial github release with download area.
342** 2) Generate checksum.SHA256 for all released files.
343** 3) Upload files to complete the release.
344** 4) Display released info from github.
345** -----------------------------------------------------------------------
Joey Armstrong26707f02023-01-26 12:41:12 -0500346EOM
347
348 # Usage: github-release [global options] <verb> [verb options]
Zack Williams27cd3e52018-09-18 16:44:50 -0700349 # create release
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500350 echo "** ${iam} Creating Release: $GERRIT_PROJECT - $GIT_VERSION"
Joey Armstrong26707f02023-01-26 12:41:12 -0500351 github-release \
Joey Armstrong26707f02023-01-26 12:41:12 -0500352 release \
353 --user "$GITHUB_ORGANIZATION" \
354 --repo "$GERRIT_PROJECT" \
355 --tag "$GIT_VERSION" \
356 --name "$GERRIT_PROJECT - $GIT_VERSION" \
357 --description "$RELEASE_DESCRIPTION"
Zack Williams27cd3e52018-09-18 16:44:50 -0700358
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500359 echo "** ${iam} Packaging release files"
Zack Williams27cd3e52018-09-18 16:44:50 -0700360 pushd "$RELEASE_TEMP"
361
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500362 ## [TODO] Add a filecount check, detect failed source copy
Joey Armstrongd8b6f332023-01-25 18:35:17 -0500363 find . -mindepth 1 -maxdepth 1 ! -type d -ls
364
Zack Williams27cd3e52018-09-18 16:44:50 -0700365 # Generate and check checksums
Zack Williams2f8e8472019-05-21 16:29:06 -0700366 sha256sum -- * > checksum.SHA256
Zack Williams27cd3e52018-09-18 16:44:50 -0700367 sha256sum -c < checksum.SHA256
368
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500369 echo
370 echo "** ${iam} Checksums(checksum.SHA256):"
Zack Williams0d93d842019-05-21 17:02:49 -0700371 cat checksum.SHA256
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500372 echo
Zack Williams0d93d842019-05-21 17:02:49 -0700373
Zack Williams27cd3e52018-09-18 16:44:50 -0700374 # upload all files to the release
Zack Williams2f8e8472019-05-21 16:29:06 -0700375 for rel_file in *
Zack Williams27cd3e52018-09-18 16:44:50 -0700376 do
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500377 echo "** ${iam} Uploading file: $rel_file"
Joey Armstrong26707f02023-01-26 12:41:12 -0500378 github-release \
Joey Armstrong26707f02023-01-26 12:41:12 -0500379 upload \
380 --user "$GITHUB_ORGANIZATION" \
381 --repo "$GERRIT_PROJECT" \
382 --tag "$GIT_VERSION" \
383 --name "$rel_file" \
384 --file "$rel_file"
Zack Williams27cd3e52018-09-18 16:44:50 -0700385 done
Zack Williams27cd3e52018-09-18 16:44:50 -0700386
387 popd
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500388 popd
Joey Armstrong26707f02023-01-26 12:41:12 -0500389
Joey Armstrong1962bcf2023-01-27 13:53:18 -0500390 echo "** ${iam} Display released info"
Joey Armstrong26707f02023-01-26 12:41:12 -0500391 showReleaseInfo \
392 "$GITHUB_ORGANIZATION"\
393 "$GERRIT_PROJECT"\
394 "$GIT_VERSION"
Zack Williams27cd3e52018-09-18 16:44:50 -0700395fi
Joey Armstrong28eddda2023-01-10 03:09:34 -0500396
397# [SEE ALSO]
398# -----------------------------------------------------------------------
399# https://www.shellcheck.net/wiki/SC2236
Joey Armstrong26707f02023-01-26 12:41:12 -0500400# https://docs.github.com/en/rest/releases/releases?apiVersion=2022-11-28#create-a-release
Joey Armstrong28eddda2023-01-10 03:09:34 -0500401# -----------------------------------------------------------------------
402
403# [EOF]