blob: a3d60fbbf811bc5271c1758de05eabcad93e0449 [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
43# shellcheck disable=SC1091
44source "${pgmdir}/common/common.sh" "${common_args[@]}"
45
46## -----------------------------------------------------------------------
47## Intent: Output a log banner to identify the running script/version.
48## -----------------------------------------------------------------------
49## TODO:
50## o SCRIPT_VERSION => git changeset for repo:ci-managment
51## o echo "library version: ${env."library.libName.version"}"
52# -----------------------------------------------------------------------
53# 14:18:38 > git fetch --no-tags --progress -- https://gerrit.opencord.org/ci-management.git +refs/heads/*:refs/remotes/origin/* # timeout=10
54# 14:18:39 Checking out Revision 50f6e0b97f449b32d32ec0e02d59642000351847 (master)
55# -----------------------------------------------------------------------
56function banner()
57{
58 local iam="${0##*/}"
59
60cat <<EOH
61
62** -----------------------------------------------------------------------
63** IAM: ${iam} :: ${FUNCNAME[0]}
64** ARGV: ${ARGV}
65** PWD: $(/bin/pwd)
66** NOW: $(date '+%Y/%m/%d %H:%M:%S')
67** VER: ${SCRIPT_VERSION:-'unknown'}
68** -----------------------------------------------------------------------
69EOH
70
71 return
72}
73
Joey Armstrongaf577ab2022-12-15 14:43:33 -050074## -----------------------------------------------------------------------
Joey Armstrong50f6e0b2023-01-24 14:14:08 -050075## Intent:
76## Display available command versions
77## A github release job is failing due to a command being mia.
78## -----------------------------------------------------------------------
79function displayCommands()
80{
81 # which git || /bin/true
82 # git --version || /bin/true
83 # go version || /bin/true
84 # helm version || /bin/true
85 return
86}
87
88## -----------------------------------------------------------------------
Joey Armstrongaf577ab2022-12-15 14:43:33 -050089## -----------------------------------------------------------------------
Joey Armstrong1b443bf2023-01-24 10:22:18 -050090function doDebug()
Joey Armstrongaf577ab2022-12-15 14:43:33 -050091{
Joey Armstrong1b443bf2023-01-24 10:22:18 -050092 echo "** ${FUNCNAME[0]}: ENTER"
Joey Armstrong28eddda2023-01-10 03:09:34 -050093
Joey Armstrongaf577ab2022-12-15 14:43:33 -050094 echo
Joey Armstrong1b443bf2023-01-24 10:22:18 -050095 echo "** PWD: $(/bin/pwd)"
96 echo "** make-pre: $(/bin/ls -l)"
97 echo
98
99 declare -p ARTIFACT_GLOB
100 declare -p RELEASE_TEMP
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500101
102 echo
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500103 echo "** ${FUNCNAME[0]}: ARTIFACT_GLOB=${ARTIFACT_GLOB}"
104 local artifact_glob="${ARTIFACT_GLOB%/*}"
105 declare -p artifact_glob
Joey Armstrongbd54b572023-01-25 14:28:10 -0500106
107 echo "** ${FUNCNAME[0]}: PWD=$(/bin/pwd)"
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500108 find "$artifact_glob" -print || /bin/true
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500109
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500110 # Copy artifacts into the release temp dir
111 # shellcheck disable=SC2086
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500112 # cp -v "$ARTIFACT_GLOB" "$RELEASE_TEMP"
Joey Armstrongbd54b572023-01-25 14:28:10 -0500113 echo "rsync -rv --checksum \"$artifact_glob\" \"$RELEASE_TEMP/.\""
114 rsync -rv --checksum "$artifact_glob" "$RELEASE_TEMP/."
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500115
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500116 echo
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500117 echo "** ${FUNCNAME[0]}: RELEASE_TEMP=${RELEASE_TEMP}"
118 find "$RELEASE_TEMP" -print || /bin/true
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500119
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500120 echo "** ${FUNCNAME[0]}: LEAVE"
121 echo
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500122 return
123}
124
125##----------------##
126##---] MAIN [---##
127##----------------##
Zack Williams27cd3e52018-09-18 16:44:50 -0700128set -eu -o pipefail
129
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500130banner
131
Zack Williams27cd3e52018-09-18 16:44:50 -0700132# when not running under Jenkins, use current dir as workspace and a blank
133# project name
134WORKSPACE=${WORKSPACE:-.}
135GERRIT_PROJECT=${GERRIT_PROJECT:-}
136
137# Github organization (or user) this project is published on. Project name should
138# be the same on both Gerrit and GitHub
139GITHUB_ORGANIZATION=${GITHUB_ORGANIZATION:-}
140
141# glob pattern relative to project dir matching release artifacts
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500142# ARTIFACT_GLOB=${ARTIFACT_GLOB:-"release/*"} # stat -- release/* not found, literal string (?)
Joey Armstrong50f6e0b2023-01-24 14:14:08 -0500143ARTIFACT_GLOB=${ARTIFACT_GLOB:-"release/."}
Zack Williams27cd3e52018-09-18 16:44:50 -0700144
145# Temporary staging directory to copy artifacts to
146RELEASE_TEMP="$WORKSPACE/release"
Zack Williams2f8e8472019-05-21 16:29:06 -0700147mkdir -p "$RELEASE_TEMP"
Zack Williams27cd3e52018-09-18 16:44:50 -0700148
149# Use "release" as the default makefile target, can be a space separated list
150RELEASE_TARGETS=${RELEASE_TARGETS:-release}
151
Zack Williams0f398492019-10-23 16:02:24 -0700152
Zack Williams27cd3e52018-09-18 16:44:50 -0700153# check that we're on a semver released version, or exit
154pushd "$GERRIT_PROJECT"
155 GIT_VERSION=$(git tag -l --points-at HEAD)
156
Zack Williams6e070f52019-10-04 11:08:59 -0700157 # match bare versions or v-prefixed golang style version
158 if [[ "$GIT_VERSION" =~ ^v?([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]
Zack Williams27cd3e52018-09-18 16:44:50 -0700159 then
160 echo "git has a SemVer released version tag: '$GIT_VERSION'"
161 echo "Building artifacts for GitHub release."
162 else
163 echo "No SemVer released version tag found, exiting..."
164 exit 0
165 fi
166popd
167
Zack Williams0f398492019-10-23 16:02:24 -0700168# Set and handle GOPATH and PATH
169export GOPATH=${GOPATH:-$WORKSPACE/go}
170export PATH=$PATH:/usr/lib/go-1.12/bin:/usr/local/go/bin:$GOPATH/bin
171
172# To support golang projects that require GOPATH to be set and code checked out there
Zack Williams27cd3e52018-09-18 16:44:50 -0700173# If $DEST_GOPATH is not an empty string:
Zack Williams76356cb2019-08-30 10:44:42 -0700174# - create GOPATH within WORKSPACE, and destination directory within
Zack Williams27cd3e52018-09-18 16:44:50 -0700175# - set PATH to include $GOPATH/bin and the system go binaries
Zack Williams76356cb2019-08-30 10:44:42 -0700176# - move project from $WORKSPACE/$GERRIT_PROJECT to new location in $GOPATH
177# - start release process within that directory
Zack Williams27cd3e52018-09-18 16:44:50 -0700178
179DEST_GOPATH=${DEST_GOPATH:-}
Joey Armstrong28eddda2023-01-10 03:09:34 -0500180if [ -n "$DEST_GOPATH" ]; then
Zack Williams27cd3e52018-09-18 16:44:50 -0700181 mkdir -p "$GOPATH/src/$DEST_GOPATH"
Zack Williams27cd3e52018-09-18 16:44:50 -0700182 release_path="$GOPATH/src/$DEST_GOPATH/$GERRIT_PROJECT"
Zack Williams76356cb2019-08-30 10:44:42 -0700183 mv "$WORKSPACE/$GERRIT_PROJECT" "$release_path"
Zack Williams27cd3e52018-09-18 16:44:50 -0700184else
185 release_path="$WORKSPACE/$GERRIT_PROJECT"
186fi
187
188if [ ! -f "$release_path/Makefile" ]; then
189 echo "Makefile not found at $release_path!"
190 exit 1
191else
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500192
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500193 declare -p release_path
194
195 # shellcheck disable=SC2015
196 [[ -v TRACE ]] && { set -x; } || { set +x; } # SC2015 (shellcheck -x)
197
Zack Williams27cd3e52018-09-18 16:44:50 -0700198 pushd "$release_path"
199
200 # Release description is sanitized version of the log message
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500201 RELEASE_DESCRIPTION="$(git log -1 --pretty=%B | tr -dc "[:alnum:]\n\r\.\[\]\:\-\\\/\`\' ")"
Zack Williams27cd3e52018-09-18 16:44:50 -0700202
203 # build the release, can be multiple space separated targets
204 # shellcheck disable=SC2086
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500205 make "$RELEASE_TARGETS"
Zack Williams27cd3e52018-09-18 16:44:50 -0700206
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500207 doDebug # deterine why ARTIFACT_GLOB is empty
Joey Armstrong50f6e0b2023-01-24 14:14:08 -0500208
209 # Are we failing on a literal string "release/*" ?
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500210 # cp -v "$ARTIFACT_GLOB" "$RELEASE_TEMP"
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500211 # echo "rsync -rv --checksum \"$ARTIFACT_GLOB\" \"$RELEASE_TEMP/.\""
212 # rsync -rv --checksum "$ARTIFACT_GLOB" "$RELEASE_TEMP/."
Joey Armstrong50f6e0b2023-01-24 14:14:08 -0500213
214 echo
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500215 echo "RELEASE_TEMP(${RELEASE_TEMP}) contains:"
Joey Armstrong50f6e0b2023-01-24 14:14:08 -0500216 find "$RELEASE_TEMP" -ls
217
Zack Williams27cd3e52018-09-18 16:44:50 -0700218 # create release
Zack Williams0d93d842019-05-21 17:02:49 -0700219 echo "Creating Release: $GERRIT_PROJECT - $GIT_VERSION"
Zack Williams0ba8a9b2020-04-30 22:25:52 -0700220 github-release release \
Zack Williams27cd3e52018-09-18 16:44:50 -0700221 --user "$GITHUB_ORGANIZATION" \
222 --repo "$GERRIT_PROJECT" \
223 --tag "$GIT_VERSION" \
224 --name "$GERRIT_PROJECT - $GIT_VERSION" \
225 --description "$RELEASE_DESCRIPTION"
226
227 # handle release files
228 pushd "$RELEASE_TEMP"
229
230 # Generate and check checksums
Zack Williams2f8e8472019-05-21 16:29:06 -0700231 sha256sum -- * > checksum.SHA256
Zack Williams27cd3e52018-09-18 16:44:50 -0700232 sha256sum -c < checksum.SHA256
233
Zack Williams0d93d842019-05-21 17:02:49 -0700234 echo "Checksums:"
235 cat checksum.SHA256
236
Zack Williams27cd3e52018-09-18 16:44:50 -0700237 # upload all files to the release
Zack Williams2f8e8472019-05-21 16:29:06 -0700238 for rel_file in *
Zack Williams27cd3e52018-09-18 16:44:50 -0700239 do
Zack Williams0d93d842019-05-21 17:02:49 -0700240 echo "Uploading file: $rel_file"
Zack Williams0ba8a9b2020-04-30 22:25:52 -0700241 github-release upload \
Zack Williams27cd3e52018-09-18 16:44:50 -0700242 --user "$GITHUB_ORGANIZATION" \
243 --repo "$GERRIT_PROJECT" \
244 --tag "$GIT_VERSION" \
245 --name "$rel_file" \
246 --file "$rel_file"
247 done
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500248 set +x
Zack Williams27cd3e52018-09-18 16:44:50 -0700249 popd
250
251 popd
252fi
Joey Armstrong28eddda2023-01-10 03:09:34 -0500253
254# [SEE ALSO]
255# -----------------------------------------------------------------------
256# https://www.shellcheck.net/wiki/SC2236
257# -----------------------------------------------------------------------
258
259# [EOF]