blob: 4a3585b805338dfcb026383ee4c5b276a2090ef9 [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##-------------------##
26declare -g SCRIPT_VERSION='1.0' # git changeset needed
27declare -g TRACE=1 # uncomment to set -x
28declare -g ARGV="$*" # archive for display
29
Joey Armstrong26707f02023-01-26 12:41:12 -050030declare -g RELEASE_TEMP
31
Joey Armstrong7f382ef2023-01-25 12:00:08 -050032##--------------------##
33##---] INCLUDES [---##
34##--------------------##
35declare -g pgmdir="${0%/*}" # dirname($script)
Joey Armstrongbd54b572023-01-25 14:28:10 -050036echo "** ${0}: PWD=$(/bin/pwd)"
37find . -maxdepth 1 -ls
38
Joey Armstrong7f382ef2023-01-25 12:00:08 -050039declare -a common_args=()
40common_args+=('--common-args-begin--')
41common_args+=('--traputils')
42common_args+=('--stacktrace')
43# common_args+=('--tempdir')
44
Joey Armstrongd8b6f332023-01-25 18:35:17 -050045# -----------------------------------------------------------------------
46# Jenkins must have checked out/copied the script -vs- repository
47# -----------------------------------------------------------------------
48# 17:56:27 [github-release_voltctl] $ /usr/bin/env bash /tmp/jenkins1043949650153731384.sh
49# 17:56:27 ** /tmp/jenkins1043949650153731384.sh: PWD=/w/workspace/github-release_voltctl
50# 17:56:27 5120009 4 drwxrwxr-x 4 jenkins jenkins 4096 Jan 25 22:56 .
51# 17:56:27 5120010 4 drwxrwxr-x 9 jenkins jenkins 4096 Jan 25 22:56 ./voltctl
52# 17:56:27 5120036 4 drwxrwxr-x 2 jenkins jenkins 4096 Jan 25 22:56 ./voltctl@tmp
53# 17:56:27 /tmp/jenkins1043949650153731384.sh: line 44: /tmp/common/common.sh: No such file or directory
54# -----------------------------------------------------------------------
55
Joey Armstrong7f382ef2023-01-25 12:00:08 -050056# shellcheck disable=SC1091
57source "${pgmdir}/common/common.sh" "${common_args[@]}"
58
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 Armstrongaf577ab2022-12-15 14:43:33 -0500102## -----------------------------------------------------------------------
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500103function doDebug()
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500104{
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500105 echo "** ${FUNCNAME[0]}: ENTER"
Joey Armstrong28eddda2023-01-10 03:09:34 -0500106
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500107 echo
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500108 echo "** PWD: $(/bin/pwd)"
109 echo "** make-pre: $(/bin/ls -l)"
110 echo
111
112 declare -p ARTIFACT_GLOB
113 declare -p RELEASE_TEMP
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500114
115 echo
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500116 echo "** ${FUNCNAME[0]}: ARTIFACT_GLOB=${ARTIFACT_GLOB}"
117 local artifact_glob="${ARTIFACT_GLOB%/*}"
118 declare -p artifact_glob
Joey Armstrongbd54b572023-01-25 14:28:10 -0500119
120 echo "** ${FUNCNAME[0]}: PWD=$(/bin/pwd)"
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500121 find "$artifact_glob" -print || /bin/true
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500122
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500123 # Copy artifacts into the release temp dir
124 # shellcheck disable=SC2086
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500125 # cp -v "$ARTIFACT_GLOB" "$RELEASE_TEMP"
Joey Armstrongd8b6f332023-01-25 18:35:17 -0500126 echo "rsync -rv --checksum \"$artifact_glob/.\" \"$RELEASE_TEMP/.\""
127 rsync -rv --checksum "$artifact_glob/." "$RELEASE_TEMP/."
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500128
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500129 echo
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500130 echo "** ${FUNCNAME[0]}: RELEASE_TEMP=${RELEASE_TEMP}"
131 find "$RELEASE_TEMP" -print || /bin/true
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500132
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500133 echo "** ${FUNCNAME[0]}: LEAVE"
134 echo
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500135 return
136}
137
Joey Armstrong26707f02023-01-26 12:41:12 -0500138## -----------------------------------------------------------------------
139## -----------------------------------------------------------------------
140function copyToRelease()
141{
142 local iam="${FUNCNAME[0]}"
143 echo "** ${iam}: ENTER"
144
145 local artifact_glob="${ARTIFACT_GLOB%/*}"
146 echo "** ${iam}: $(declare -p ARTIFACT_GLOB) $(declare -p artifact_glob)"
147 find "$artifact_glob" -print
148
149 # Copy artifacts into the release temp dir
150 # shellcheck disable=SC2086
151 # cp -v "$ARTIFACT_GLOB" "$RELEASE_TEMP"
152 echo "rsync -rv --checksum \"$artifact_glob/.\" \"$RELEASE_TEMP/.\""
153 rsync -rv --checksum "$artifact_glob/." "$RELEASE_TEMP/."
154
155 echo "** ${iam}: RELEASE_TEMP=${RELEASE_TEMP}"
156 find "$RELEASE_TEMP" -print
157
158 echo "** ${iam}: LEAVE"
159 echo
160 return
161}
162
163## -----------------------------------------------------------------------
164## -----------------------------------------------------------------------
165function showGithubRelease()
166{
167 local iam="${FUNCNAME[0]}"
168 echo "** ${iam}: ENTER"
169
170 echo "** ${iam}: which github-release: $(which -a github-release)"
171 local switch
172 for switch in '--version' '--help';
173 do
174 echo "** ${iam}: github-release ${switch}: $(github-release ${switch})"
175 done
176 echo "** ${iam}: LEAVE"
177 echo
178 return
179}
180
181## -----------------------------------------------------------------------
182## -----------------------------------------------------------------------
183function showReleaseInfo()
184{
185 local user="$1" ; shift
186 local repo="$1" ; shift
187 local tag=''
188 if [ $# -gt 0 ]; then
189 tag="$1" ; shift
190 fi
191
192 local iam="${FUNCNAME[0]}"
193 echo "** ${iam}: ENTER"
194
195 cat <<EOM
196
197** -------------------------------------------------------------------
198** ${iam}
199** -------------------------------------------------------------------
200EOM
201
202 declare -a args=()
203 args+=('--user' "$user")
204 args+=('--repo' "$repo")
205 if [ ${#tag} -gt 0 ]; then
206 args+=('--tag' "$tag")
207 fi
208
209 echo "${iam}: github-relase info"
210 github-release\
211 --verbose\
212 info\
213 "${args[@]}"
214
215 echo "** ${iam}: LEAVE"
216 return
217}
218
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500219##----------------##
220##---] MAIN [---##
221##----------------##
Zack Williams27cd3e52018-09-18 16:44:50 -0700222set -eu -o pipefail
223
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500224banner
225
Zack Williams27cd3e52018-09-18 16:44:50 -0700226# when not running under Jenkins, use current dir as workspace and a blank
227# project name
228WORKSPACE=${WORKSPACE:-.}
229GERRIT_PROJECT=${GERRIT_PROJECT:-}
230
231# Github organization (or user) this project is published on. Project name should
232# be the same on both Gerrit and GitHub
233GITHUB_ORGANIZATION=${GITHUB_ORGANIZATION:-}
234
235# glob pattern relative to project dir matching release artifacts
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500236# ARTIFACT_GLOB=${ARTIFACT_GLOB:-"release/*"} # stat -- release/* not found, literal string (?)
Joey Armstrong50f6e0b2023-01-24 14:14:08 -0500237ARTIFACT_GLOB=${ARTIFACT_GLOB:-"release/."}
Zack Williams27cd3e52018-09-18 16:44:50 -0700238
239# Temporary staging directory to copy artifacts to
240RELEASE_TEMP="$WORKSPACE/release"
Zack Williams2f8e8472019-05-21 16:29:06 -0700241mkdir -p "$RELEASE_TEMP"
Zack Williams27cd3e52018-09-18 16:44:50 -0700242
243# Use "release" as the default makefile target, can be a space separated list
244RELEASE_TARGETS=${RELEASE_TARGETS:-release}
245
Zack Williams0f398492019-10-23 16:02:24 -0700246
Zack Williams27cd3e52018-09-18 16:44:50 -0700247# check that we're on a semver released version, or exit
248pushd "$GERRIT_PROJECT"
249 GIT_VERSION=$(git tag -l --points-at HEAD)
250
Zack Williams6e070f52019-10-04 11:08:59 -0700251 # match bare versions or v-prefixed golang style version
252 if [[ "$GIT_VERSION" =~ ^v?([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]
Zack Williams27cd3e52018-09-18 16:44:50 -0700253 then
254 echo "git has a SemVer released version tag: '$GIT_VERSION'"
255 echo "Building artifacts for GitHub release."
256 else
257 echo "No SemVer released version tag found, exiting..."
258 exit 0
259 fi
260popd
261
Zack Williams0f398492019-10-23 16:02:24 -0700262# Set and handle GOPATH and PATH
263export GOPATH=${GOPATH:-$WORKSPACE/go}
264export PATH=$PATH:/usr/lib/go-1.12/bin:/usr/local/go/bin:$GOPATH/bin
265
266# To support golang projects that require GOPATH to be set and code checked out there
Zack Williams27cd3e52018-09-18 16:44:50 -0700267# If $DEST_GOPATH is not an empty string:
Zack Williams76356cb2019-08-30 10:44:42 -0700268# - create GOPATH within WORKSPACE, and destination directory within
Zack Williams27cd3e52018-09-18 16:44:50 -0700269# - set PATH to include $GOPATH/bin and the system go binaries
Zack Williams76356cb2019-08-30 10:44:42 -0700270# - move project from $WORKSPACE/$GERRIT_PROJECT to new location in $GOPATH
271# - start release process within that directory
Zack Williams27cd3e52018-09-18 16:44:50 -0700272
273DEST_GOPATH=${DEST_GOPATH:-}
Joey Armstrong28eddda2023-01-10 03:09:34 -0500274if [ -n "$DEST_GOPATH" ]; then
Zack Williams27cd3e52018-09-18 16:44:50 -0700275 mkdir -p "$GOPATH/src/$DEST_GOPATH"
Zack Williams27cd3e52018-09-18 16:44:50 -0700276 release_path="$GOPATH/src/$DEST_GOPATH/$GERRIT_PROJECT"
Zack Williams76356cb2019-08-30 10:44:42 -0700277 mv "$WORKSPACE/$GERRIT_PROJECT" "$release_path"
Zack Williams27cd3e52018-09-18 16:44:50 -0700278else
279 release_path="$WORKSPACE/$GERRIT_PROJECT"
280fi
281
282if [ ! -f "$release_path/Makefile" ]; then
283 echo "Makefile not found at $release_path!"
284 exit 1
285else
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500286
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500287 declare -p release_path
288
289 # shellcheck disable=SC2015
290 [[ -v TRACE ]] && { set -x; } || { set +x; } # SC2015 (shellcheck -x)
291
Zack Williams27cd3e52018-09-18 16:44:50 -0700292 pushd "$release_path"
293
294 # Release description is sanitized version of the log message
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500295 RELEASE_DESCRIPTION="$(git log -1 --pretty=%B | tr -dc "[:alnum:]\n\r\.\[\]\:\-\\\/\`\' ")"
Zack Williams27cd3e52018-09-18 16:44:50 -0700296
297 # build the release, can be multiple space separated targets
298 # shellcheck disable=SC2086
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500299 make "$RELEASE_TARGETS"
Zack Williams27cd3e52018-09-18 16:44:50 -0700300
Joey Armstrong26707f02023-01-26 12:41:12 -0500301 # doDebug # deterine why ARTIFACT_GLOB is empty
302 copyToRelease
Joey Armstrong50f6e0b2023-01-24 14:14:08 -0500303
304 # Are we failing on a literal string "release/*" ?
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500305 # cp -v "$ARTIFACT_GLOB" "$RELEASE_TEMP"
Joey Armstrongd8b6f332023-01-25 18:35:17 -0500306 # echo "rsync -rv --checksum \"$artifact_glob/.\" \"$RELEASE_TEMP/.\""
307 # rsync -rv --checksum "$artifact_glob/." "$RELEASE_TEMP/."
Joey Armstrong50f6e0b2023-01-24 14:14:08 -0500308
Joey Armstrong26707f02023-01-26 12:41:12 -0500309 echo "PRE-RELEASE INFO"
310 showReleaseInfo \
311 "$GITHUB_ORGANIZATION"\
312 "$GERRIT_PROJECT"
Joey Armstrong50f6e0b2023-01-24 14:14:08 -0500313
Joey Armstrong26707f02023-01-26 12:41:12 -0500314 showGithubRelease
315
316 cat <<EOM
317Create the release in three steps:
318 1) Create initial github release with download area.
319 2) Generate checksum.SHA256 for all released files.
320 3) Upload files to complete the release.
321 4) Display released info from github.
322EOM
323
324 # Usage: github-release [global options] <verb> [verb options]
Zack Williams27cd3e52018-09-18 16:44:50 -0700325 # create release
Zack Williams0d93d842019-05-21 17:02:49 -0700326 echo "Creating Release: $GERRIT_PROJECT - $GIT_VERSION"
Joey Armstrong26707f02023-01-26 12:41:12 -0500327 github-release \
328 --verbose \
329 release \
330 --user "$GITHUB_ORGANIZATION" \
331 --repo "$GERRIT_PROJECT" \
332 --tag "$GIT_VERSION" \
333 --name "$GERRIT_PROJECT - $GIT_VERSION" \
334 --description "$RELEASE_DESCRIPTION"
Zack Williams27cd3e52018-09-18 16:44:50 -0700335
336 # handle release files
337 pushd "$RELEASE_TEMP"
338
Joey Armstrongd8b6f332023-01-25 18:35:17 -0500339 ## [TODO] Add a content check, err when only source tarballs exist
340 find . -mindepth 1 -maxdepth 1 ! -type d -ls
341
Zack Williams27cd3e52018-09-18 16:44:50 -0700342 # Generate and check checksums
Zack Williams2f8e8472019-05-21 16:29:06 -0700343 sha256sum -- * > checksum.SHA256
Zack Williams27cd3e52018-09-18 16:44:50 -0700344 sha256sum -c < checksum.SHA256
345
Joey Armstrong26707f02023-01-26 12:41:12 -0500346 echo "Checksums(checksum.SHA256):"
Zack Williams0d93d842019-05-21 17:02:49 -0700347 cat checksum.SHA256
348
Zack Williams27cd3e52018-09-18 16:44:50 -0700349 # upload all files to the release
Zack Williams2f8e8472019-05-21 16:29:06 -0700350 for rel_file in *
Zack Williams27cd3e52018-09-18 16:44:50 -0700351 do
Zack Williams0d93d842019-05-21 17:02:49 -0700352 echo "Uploading file: $rel_file"
Joey Armstrong26707f02023-01-26 12:41:12 -0500353 github-release \
354 --verbose \
355 upload \
356 --user "$GITHUB_ORGANIZATION" \
357 --repo "$GERRIT_PROJECT" \
358 --tag "$GIT_VERSION" \
359 --name "$rel_file" \
360 --file "$rel_file"
Zack Williams27cd3e52018-09-18 16:44:50 -0700361 done
Zack Williams27cd3e52018-09-18 16:44:50 -0700362 popd
363
364 popd
Joey Armstrong26707f02023-01-26 12:41:12 -0500365
366 showReleaseInfo \
367 "$GITHUB_ORGANIZATION"\
368 "$GERRIT_PROJECT"\
369 "$GIT_VERSION"
Zack Williams27cd3e52018-09-18 16:44:50 -0700370fi
Joey Armstrong28eddda2023-01-10 03:09:34 -0500371
372# [SEE ALSO]
373# -----------------------------------------------------------------------
374# https://www.shellcheck.net/wiki/SC2236
Joey Armstrong26707f02023-01-26 12:41:12 -0500375# https://docs.github.com/en/rest/releases/releases?apiVersion=2022-11-28#create-a-release
Joey Armstrong28eddda2023-01-10 03:09:34 -0500376# -----------------------------------------------------------------------
377
378# [EOF]