blob: d64b8aa2e222e4f76be258a4624d525bd6559a26 [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
30##--------------------##
31##---] INCLUDES [---##
32##--------------------##
33declare -g pgmdir="${0%/*}" # dirname($script)
Joey Armstrongbd54b572023-01-25 14:28:10 -050034echo "** ${0}: PWD=$(/bin/pwd)"
35find . -maxdepth 1 -ls
36
Joey Armstrong7f382ef2023-01-25 12:00:08 -050037declare -a common_args=()
38common_args+=('--common-args-begin--')
39common_args+=('--traputils')
40common_args+=('--stacktrace')
41# common_args+=('--tempdir')
42
Joey Armstrongd8b6f332023-01-25 18:35:17 -050043# -----------------------------------------------------------------------
44# Jenkins must have checked out/copied the script -vs- repository
45# -----------------------------------------------------------------------
46# 17:56:27 [github-release_voltctl] $ /usr/bin/env bash /tmp/jenkins1043949650153731384.sh
47# 17:56:27 ** /tmp/jenkins1043949650153731384.sh: PWD=/w/workspace/github-release_voltctl
48# 17:56:27 5120009 4 drwxrwxr-x 4 jenkins jenkins 4096 Jan 25 22:56 .
49# 17:56:27 5120010 4 drwxrwxr-x 9 jenkins jenkins 4096 Jan 25 22:56 ./voltctl
50# 17:56:27 5120036 4 drwxrwxr-x 2 jenkins jenkins 4096 Jan 25 22:56 ./voltctl@tmp
51# 17:56:27 /tmp/jenkins1043949650153731384.sh: line 44: /tmp/common/common.sh: No such file or directory
52# -----------------------------------------------------------------------
53
Joey Armstrong7f382ef2023-01-25 12:00:08 -050054# shellcheck disable=SC1091
55source "${pgmdir}/common/common.sh" "${common_args[@]}"
56
57## -----------------------------------------------------------------------
58## Intent: Output a log banner to identify the running script/version.
59## -----------------------------------------------------------------------
60## TODO:
61## o SCRIPT_VERSION => git changeset for repo:ci-managment
62## o echo "library version: ${env."library.libName.version"}"
63# -----------------------------------------------------------------------
64# 14:18:38 > git fetch --no-tags --progress -- https://gerrit.opencord.org/ci-management.git +refs/heads/*:refs/remotes/origin/* # timeout=10
65# 14:18:39 Checking out Revision 50f6e0b97f449b32d32ec0e02d59642000351847 (master)
66# -----------------------------------------------------------------------
67function banner()
68{
69 local iam="${0##*/}"
70
71cat <<EOH
72
73** -----------------------------------------------------------------------
74** IAM: ${iam} :: ${FUNCNAME[0]}
75** ARGV: ${ARGV}
76** PWD: $(/bin/pwd)
77** NOW: $(date '+%Y/%m/%d %H:%M:%S')
78** VER: ${SCRIPT_VERSION:-'unknown'}
79** -----------------------------------------------------------------------
80EOH
81
82 return
83}
84
Joey Armstrongaf577ab2022-12-15 14:43:33 -050085## -----------------------------------------------------------------------
Joey Armstrong50f6e0b2023-01-24 14:14:08 -050086## Intent:
87## Display available command versions
88## A github release job is failing due to a command being mia.
89## -----------------------------------------------------------------------
90function displayCommands()
91{
92 # which git || /bin/true
93 # git --version || /bin/true
94 # go version || /bin/true
95 # helm version || /bin/true
96 return
97}
98
99## -----------------------------------------------------------------------
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500100## -----------------------------------------------------------------------
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500101function doDebug()
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500102{
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500103 echo "** ${FUNCNAME[0]}: ENTER"
Joey Armstrong28eddda2023-01-10 03:09:34 -0500104
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500105 echo
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500106 echo "** PWD: $(/bin/pwd)"
107 echo "** make-pre: $(/bin/ls -l)"
108 echo
109
110 declare -p ARTIFACT_GLOB
111 declare -p RELEASE_TEMP
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500112
113 echo
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500114 echo "** ${FUNCNAME[0]}: ARTIFACT_GLOB=${ARTIFACT_GLOB}"
115 local artifact_glob="${ARTIFACT_GLOB%/*}"
116 declare -p artifact_glob
Joey Armstrongbd54b572023-01-25 14:28:10 -0500117
118 echo "** ${FUNCNAME[0]}: PWD=$(/bin/pwd)"
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500119 find "$artifact_glob" -print || /bin/true
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500120
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500121 # Copy artifacts into the release temp dir
122 # shellcheck disable=SC2086
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500123 # cp -v "$ARTIFACT_GLOB" "$RELEASE_TEMP"
Joey Armstrongd8b6f332023-01-25 18:35:17 -0500124 echo "rsync -rv --checksum \"$artifact_glob/.\" \"$RELEASE_TEMP/.\""
125 rsync -rv --checksum "$artifact_glob/." "$RELEASE_TEMP/."
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500126
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500127 echo
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500128 echo "** ${FUNCNAME[0]}: RELEASE_TEMP=${RELEASE_TEMP}"
129 find "$RELEASE_TEMP" -print || /bin/true
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500130
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500131 echo "** ${FUNCNAME[0]}: LEAVE"
132 echo
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500133 return
134}
135
136##----------------##
137##---] MAIN [---##
138##----------------##
Zack Williams27cd3e52018-09-18 16:44:50 -0700139set -eu -o pipefail
140
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500141banner
142
Zack Williams27cd3e52018-09-18 16:44:50 -0700143# when not running under Jenkins, use current dir as workspace and a blank
144# project name
145WORKSPACE=${WORKSPACE:-.}
146GERRIT_PROJECT=${GERRIT_PROJECT:-}
147
148# Github organization (or user) this project is published on. Project name should
149# be the same on both Gerrit and GitHub
150GITHUB_ORGANIZATION=${GITHUB_ORGANIZATION:-}
151
152# glob pattern relative to project dir matching release artifacts
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500153# ARTIFACT_GLOB=${ARTIFACT_GLOB:-"release/*"} # stat -- release/* not found, literal string (?)
Joey Armstrong50f6e0b2023-01-24 14:14:08 -0500154ARTIFACT_GLOB=${ARTIFACT_GLOB:-"release/."}
Zack Williams27cd3e52018-09-18 16:44:50 -0700155
156# Temporary staging directory to copy artifacts to
157RELEASE_TEMP="$WORKSPACE/release"
Zack Williams2f8e8472019-05-21 16:29:06 -0700158mkdir -p "$RELEASE_TEMP"
Zack Williams27cd3e52018-09-18 16:44:50 -0700159
160# Use "release" as the default makefile target, can be a space separated list
161RELEASE_TARGETS=${RELEASE_TARGETS:-release}
162
Zack Williams0f398492019-10-23 16:02:24 -0700163
Zack Williams27cd3e52018-09-18 16:44:50 -0700164# check that we're on a semver released version, or exit
165pushd "$GERRIT_PROJECT"
166 GIT_VERSION=$(git tag -l --points-at HEAD)
167
Zack Williams6e070f52019-10-04 11:08:59 -0700168 # match bare versions or v-prefixed golang style version
169 if [[ "$GIT_VERSION" =~ ^v?([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]
Zack Williams27cd3e52018-09-18 16:44:50 -0700170 then
171 echo "git has a SemVer released version tag: '$GIT_VERSION'"
172 echo "Building artifacts for GitHub release."
173 else
174 echo "No SemVer released version tag found, exiting..."
175 exit 0
176 fi
177popd
178
Zack Williams0f398492019-10-23 16:02:24 -0700179# Set and handle GOPATH and PATH
180export GOPATH=${GOPATH:-$WORKSPACE/go}
181export PATH=$PATH:/usr/lib/go-1.12/bin:/usr/local/go/bin:$GOPATH/bin
182
183# To support golang projects that require GOPATH to be set and code checked out there
Zack Williams27cd3e52018-09-18 16:44:50 -0700184# If $DEST_GOPATH is not an empty string:
Zack Williams76356cb2019-08-30 10:44:42 -0700185# - create GOPATH within WORKSPACE, and destination directory within
Zack Williams27cd3e52018-09-18 16:44:50 -0700186# - set PATH to include $GOPATH/bin and the system go binaries
Zack Williams76356cb2019-08-30 10:44:42 -0700187# - move project from $WORKSPACE/$GERRIT_PROJECT to new location in $GOPATH
188# - start release process within that directory
Zack Williams27cd3e52018-09-18 16:44:50 -0700189
190DEST_GOPATH=${DEST_GOPATH:-}
Joey Armstrong28eddda2023-01-10 03:09:34 -0500191if [ -n "$DEST_GOPATH" ]; then
Zack Williams27cd3e52018-09-18 16:44:50 -0700192 mkdir -p "$GOPATH/src/$DEST_GOPATH"
Zack Williams27cd3e52018-09-18 16:44:50 -0700193 release_path="$GOPATH/src/$DEST_GOPATH/$GERRIT_PROJECT"
Zack Williams76356cb2019-08-30 10:44:42 -0700194 mv "$WORKSPACE/$GERRIT_PROJECT" "$release_path"
Zack Williams27cd3e52018-09-18 16:44:50 -0700195else
196 release_path="$WORKSPACE/$GERRIT_PROJECT"
197fi
198
199if [ ! -f "$release_path/Makefile" ]; then
200 echo "Makefile not found at $release_path!"
201 exit 1
202else
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500203
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500204 declare -p release_path
205
206 # shellcheck disable=SC2015
207 [[ -v TRACE ]] && { set -x; } || { set +x; } # SC2015 (shellcheck -x)
208
Zack Williams27cd3e52018-09-18 16:44:50 -0700209 pushd "$release_path"
210
211 # Release description is sanitized version of the log message
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500212 RELEASE_DESCRIPTION="$(git log -1 --pretty=%B | tr -dc "[:alnum:]\n\r\.\[\]\:\-\\\/\`\' ")"
Zack Williams27cd3e52018-09-18 16:44:50 -0700213
214 # build the release, can be multiple space separated targets
215 # shellcheck disable=SC2086
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500216 make "$RELEASE_TARGETS"
Zack Williams27cd3e52018-09-18 16:44:50 -0700217
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500218 doDebug # deterine why ARTIFACT_GLOB is empty
Joey Armstrong50f6e0b2023-01-24 14:14:08 -0500219
220 # Are we failing on a literal string "release/*" ?
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500221 # cp -v "$ARTIFACT_GLOB" "$RELEASE_TEMP"
Joey Armstrongd8b6f332023-01-25 18:35:17 -0500222 # echo "rsync -rv --checksum \"$artifact_glob/.\" \"$RELEASE_TEMP/.\""
223 # rsync -rv --checksum "$artifact_glob/." "$RELEASE_TEMP/."
Joey Armstrong50f6e0b2023-01-24 14:14:08 -0500224
225 echo
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500226 echo "RELEASE_TEMP(${RELEASE_TEMP}) contains:"
Joey Armstrong50f6e0b2023-01-24 14:14:08 -0500227 find "$RELEASE_TEMP" -ls
228
Zack Williams27cd3e52018-09-18 16:44:50 -0700229 # create release
Zack Williams0d93d842019-05-21 17:02:49 -0700230 echo "Creating Release: $GERRIT_PROJECT - $GIT_VERSION"
Zack Williams0ba8a9b2020-04-30 22:25:52 -0700231 github-release release \
Zack Williams27cd3e52018-09-18 16:44:50 -0700232 --user "$GITHUB_ORGANIZATION" \
233 --repo "$GERRIT_PROJECT" \
234 --tag "$GIT_VERSION" \
235 --name "$GERRIT_PROJECT - $GIT_VERSION" \
236 --description "$RELEASE_DESCRIPTION"
237
238 # handle release files
239 pushd "$RELEASE_TEMP"
240
Joey Armstrongd8b6f332023-01-25 18:35:17 -0500241 ## [TODO] Add a content check, err when only source tarballs exist
242 find . -mindepth 1 -maxdepth 1 ! -type d -ls
243
Zack Williams27cd3e52018-09-18 16:44:50 -0700244 # Generate and check checksums
Zack Williams2f8e8472019-05-21 16:29:06 -0700245 sha256sum -- * > checksum.SHA256
Zack Williams27cd3e52018-09-18 16:44:50 -0700246 sha256sum -c < checksum.SHA256
247
Zack Williams0d93d842019-05-21 17:02:49 -0700248 echo "Checksums:"
249 cat checksum.SHA256
250
Zack Williams27cd3e52018-09-18 16:44:50 -0700251 # upload all files to the release
Zack Williams2f8e8472019-05-21 16:29:06 -0700252 for rel_file in *
Zack Williams27cd3e52018-09-18 16:44:50 -0700253 do
Zack Williams0d93d842019-05-21 17:02:49 -0700254 echo "Uploading file: $rel_file"
Zack Williams0ba8a9b2020-04-30 22:25:52 -0700255 github-release upload \
Zack Williams27cd3e52018-09-18 16:44:50 -0700256 --user "$GITHUB_ORGANIZATION" \
257 --repo "$GERRIT_PROJECT" \
258 --tag "$GIT_VERSION" \
259 --name "$rel_file" \
260 --file "$rel_file"
261 done
Zack Williams27cd3e52018-09-18 16:44:50 -0700262 popd
263
264 popd
265fi
Joey Armstrong28eddda2023-01-10 03:09:34 -0500266
267# [SEE ALSO]
268# -----------------------------------------------------------------------
269# https://www.shellcheck.net/wiki/SC2236
270# -----------------------------------------------------------------------
271
272# [EOF]