blob: 32fa72b3f0209a93d7366f42aad84d5f4a5751e9 [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)
34declare -a common_args=()
35common_args+=('--common-args-begin--')
36common_args+=('--traputils')
37common_args+=('--stacktrace')
38# common_args+=('--tempdir')
39
40# shellcheck disable=SC1091
41source "${pgmdir}/common/common.sh" "${common_args[@]}"
42
43## -----------------------------------------------------------------------
44## Intent: Output a log banner to identify the running script/version.
45## -----------------------------------------------------------------------
46## TODO:
47## o SCRIPT_VERSION => git changeset for repo:ci-managment
48## o echo "library version: ${env."library.libName.version"}"
49# -----------------------------------------------------------------------
50# 14:18:38 > git fetch --no-tags --progress -- https://gerrit.opencord.org/ci-management.git +refs/heads/*:refs/remotes/origin/* # timeout=10
51# 14:18:39 Checking out Revision 50f6e0b97f449b32d32ec0e02d59642000351847 (master)
52# -----------------------------------------------------------------------
53function banner()
54{
55 local iam="${0##*/}"
56
57cat <<EOH
58
59** -----------------------------------------------------------------------
60** IAM: ${iam} :: ${FUNCNAME[0]}
61** ARGV: ${ARGV}
62** PWD: $(/bin/pwd)
63** NOW: $(date '+%Y/%m/%d %H:%M:%S')
64** VER: ${SCRIPT_VERSION:-'unknown'}
65** -----------------------------------------------------------------------
66EOH
67
68 return
69}
70
Joey Armstrongaf577ab2022-12-15 14:43:33 -050071## -----------------------------------------------------------------------
Joey Armstrong50f6e0b2023-01-24 14:14:08 -050072## Intent:
73## Display available command versions
74## A github release job is failing due to a command being mia.
75## -----------------------------------------------------------------------
76function displayCommands()
77{
78 # which git || /bin/true
79 # git --version || /bin/true
80 # go version || /bin/true
81 # helm version || /bin/true
82 return
83}
84
85## -----------------------------------------------------------------------
Joey Armstrongaf577ab2022-12-15 14:43:33 -050086## -----------------------------------------------------------------------
Joey Armstrong1b443bf2023-01-24 10:22:18 -050087function doDebug()
Joey Armstrongaf577ab2022-12-15 14:43:33 -050088{
Joey Armstrong1b443bf2023-01-24 10:22:18 -050089 echo "** ${FUNCNAME[0]}: ENTER"
Joey Armstrong28eddda2023-01-10 03:09:34 -050090
Joey Armstrongaf577ab2022-12-15 14:43:33 -050091 echo
Joey Armstrong1b443bf2023-01-24 10:22:18 -050092 echo "** PWD: $(/bin/pwd)"
93 echo "** make-pre: $(/bin/ls -l)"
94 echo
95
96 declare -p ARTIFACT_GLOB
97 declare -p RELEASE_TEMP
Joey Armstrongaf577ab2022-12-15 14:43:33 -050098
99 echo
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500100 echo "** ${FUNCNAME[0]}: ARTIFACT_GLOB=${ARTIFACT_GLOB}"
101 local artifact_glob="${ARTIFACT_GLOB%/*}"
102 declare -p artifact_glob
103 find "$artifact_glob" -print || /bin/true
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500104
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500105 # Copy artifacts into the release temp dir
106 # shellcheck disable=SC2086
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500107 # cp -v "$ARTIFACT_GLOB" "$RELEASE_TEMP"
108 echo "rsync -rv --checksum \"$ARTIFACT_GLOB\" \"$RELEASE_TEMP/.\""
109 rsync -rv --checksum "$ARTIFACT_GLOB" "$RELEASE_TEMP/."
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500110
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500111 echo
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500112 echo "** ${FUNCNAME[0]}: RELEASE_TEMP=${RELEASE_TEMP}"
113 find "$RELEASE_TEMP" -print || /bin/true
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500114
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500115 echo "** ${FUNCNAME[0]}: LEAVE"
116 echo
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500117 return
118}
119
120##----------------##
121##---] MAIN [---##
122##----------------##
Zack Williams27cd3e52018-09-18 16:44:50 -0700123set -eu -o pipefail
124
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500125banner
126
Zack Williams27cd3e52018-09-18 16:44:50 -0700127# when not running under Jenkins, use current dir as workspace and a blank
128# project name
129WORKSPACE=${WORKSPACE:-.}
130GERRIT_PROJECT=${GERRIT_PROJECT:-}
131
132# Github organization (or user) this project is published on. Project name should
133# be the same on both Gerrit and GitHub
134GITHUB_ORGANIZATION=${GITHUB_ORGANIZATION:-}
135
136# glob pattern relative to project dir matching release artifacts
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500137# ARTIFACT_GLOB=${ARTIFACT_GLOB:-"release/*"} # stat -- release/* not found, literal string (?)
Joey Armstrong50f6e0b2023-01-24 14:14:08 -0500138ARTIFACT_GLOB=${ARTIFACT_GLOB:-"release/."}
Zack Williams27cd3e52018-09-18 16:44:50 -0700139
140# Temporary staging directory to copy artifacts to
141RELEASE_TEMP="$WORKSPACE/release"
Zack Williams2f8e8472019-05-21 16:29:06 -0700142mkdir -p "$RELEASE_TEMP"
Zack Williams27cd3e52018-09-18 16:44:50 -0700143
144# Use "release" as the default makefile target, can be a space separated list
145RELEASE_TARGETS=${RELEASE_TARGETS:-release}
146
Zack Williams0f398492019-10-23 16:02:24 -0700147
Zack Williams27cd3e52018-09-18 16:44:50 -0700148# check that we're on a semver released version, or exit
149pushd "$GERRIT_PROJECT"
150 GIT_VERSION=$(git tag -l --points-at HEAD)
151
Zack Williams6e070f52019-10-04 11:08:59 -0700152 # match bare versions or v-prefixed golang style version
153 if [[ "$GIT_VERSION" =~ ^v?([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]
Zack Williams27cd3e52018-09-18 16:44:50 -0700154 then
155 echo "git has a SemVer released version tag: '$GIT_VERSION'"
156 echo "Building artifacts for GitHub release."
157 else
158 echo "No SemVer released version tag found, exiting..."
159 exit 0
160 fi
161popd
162
Zack Williams0f398492019-10-23 16:02:24 -0700163# Set and handle GOPATH and PATH
164export GOPATH=${GOPATH:-$WORKSPACE/go}
165export PATH=$PATH:/usr/lib/go-1.12/bin:/usr/local/go/bin:$GOPATH/bin
166
167# To support golang projects that require GOPATH to be set and code checked out there
Zack Williams27cd3e52018-09-18 16:44:50 -0700168# If $DEST_GOPATH is not an empty string:
Zack Williams76356cb2019-08-30 10:44:42 -0700169# - create GOPATH within WORKSPACE, and destination directory within
Zack Williams27cd3e52018-09-18 16:44:50 -0700170# - set PATH to include $GOPATH/bin and the system go binaries
Zack Williams76356cb2019-08-30 10:44:42 -0700171# - move project from $WORKSPACE/$GERRIT_PROJECT to new location in $GOPATH
172# - start release process within that directory
Zack Williams27cd3e52018-09-18 16:44:50 -0700173
174DEST_GOPATH=${DEST_GOPATH:-}
Joey Armstrong28eddda2023-01-10 03:09:34 -0500175if [ -n "$DEST_GOPATH" ]; then
Zack Williams27cd3e52018-09-18 16:44:50 -0700176 mkdir -p "$GOPATH/src/$DEST_GOPATH"
Zack Williams27cd3e52018-09-18 16:44:50 -0700177 release_path="$GOPATH/src/$DEST_GOPATH/$GERRIT_PROJECT"
Zack Williams76356cb2019-08-30 10:44:42 -0700178 mv "$WORKSPACE/$GERRIT_PROJECT" "$release_path"
Zack Williams27cd3e52018-09-18 16:44:50 -0700179else
180 release_path="$WORKSPACE/$GERRIT_PROJECT"
181fi
182
183if [ ! -f "$release_path/Makefile" ]; then
184 echo "Makefile not found at $release_path!"
185 exit 1
186else
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500187
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500188 declare -p release_path
189
190 # shellcheck disable=SC2015
191 [[ -v TRACE ]] && { set -x; } || { set +x; } # SC2015 (shellcheck -x)
192
Zack Williams27cd3e52018-09-18 16:44:50 -0700193 pushd "$release_path"
194
195 # Release description is sanitized version of the log message
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500196 RELEASE_DESCRIPTION="$(git log -1 --pretty=%B | tr -dc "[:alnum:]\n\r\.\[\]\:\-\\\/\`\' ")"
Zack Williams27cd3e52018-09-18 16:44:50 -0700197
198 # build the release, can be multiple space separated targets
199 # shellcheck disable=SC2086
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500200 make "$RELEASE_TARGETS"
Zack Williams27cd3e52018-09-18 16:44:50 -0700201
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500202 doDebug # deterine why ARTIFACT_GLOB is empty
Joey Armstrong50f6e0b2023-01-24 14:14:08 -0500203
204 # Are we failing on a literal string "release/*" ?
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500205 # cp -v "$ARTIFACT_GLOB" "$RELEASE_TEMP"
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500206 # echo "rsync -rv --checksum \"$ARTIFACT_GLOB\" \"$RELEASE_TEMP/.\""
207 # rsync -rv --checksum "$ARTIFACT_GLOB" "$RELEASE_TEMP/."
Joey Armstrong50f6e0b2023-01-24 14:14:08 -0500208
209 echo
Joey Armstrong7f382ef2023-01-25 12:00:08 -0500210 echo "RELEASE_TEMP(${RELEASE_TEMP}) contains:"
Joey Armstrong50f6e0b2023-01-24 14:14:08 -0500211 find "$RELEASE_TEMP" -ls
212
Zack Williams27cd3e52018-09-18 16:44:50 -0700213 # create release
Zack Williams0d93d842019-05-21 17:02:49 -0700214 echo "Creating Release: $GERRIT_PROJECT - $GIT_VERSION"
Zack Williams0ba8a9b2020-04-30 22:25:52 -0700215 github-release release \
Zack Williams27cd3e52018-09-18 16:44:50 -0700216 --user "$GITHUB_ORGANIZATION" \
217 --repo "$GERRIT_PROJECT" \
218 --tag "$GIT_VERSION" \
219 --name "$GERRIT_PROJECT - $GIT_VERSION" \
220 --description "$RELEASE_DESCRIPTION"
221
222 # handle release files
223 pushd "$RELEASE_TEMP"
224
225 # Generate and check checksums
Zack Williams2f8e8472019-05-21 16:29:06 -0700226 sha256sum -- * > checksum.SHA256
Zack Williams27cd3e52018-09-18 16:44:50 -0700227 sha256sum -c < checksum.SHA256
228
Zack Williams0d93d842019-05-21 17:02:49 -0700229 echo "Checksums:"
230 cat checksum.SHA256
231
Zack Williams27cd3e52018-09-18 16:44:50 -0700232 # upload all files to the release
Zack Williams2f8e8472019-05-21 16:29:06 -0700233 for rel_file in *
Zack Williams27cd3e52018-09-18 16:44:50 -0700234 do
Zack Williams0d93d842019-05-21 17:02:49 -0700235 echo "Uploading file: $rel_file"
Zack Williams0ba8a9b2020-04-30 22:25:52 -0700236 github-release upload \
Zack Williams27cd3e52018-09-18 16:44:50 -0700237 --user "$GITHUB_ORGANIZATION" \
238 --repo "$GERRIT_PROJECT" \
239 --tag "$GIT_VERSION" \
240 --name "$rel_file" \
241 --file "$rel_file"
242 done
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500243 set +x
Zack Williams27cd3e52018-09-18 16:44:50 -0700244 popd
245
246 popd
247fi
Joey Armstrong28eddda2023-01-10 03:09:34 -0500248
249# [SEE ALSO]
250# -----------------------------------------------------------------------
251# https://www.shellcheck.net/wiki/SC2236
252# -----------------------------------------------------------------------
253
254# [EOF]