blob: 09fbe55dc2e699c01433fecbe90be77085ac4200 [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 Armstrongaf577ab2022-12-15 14:43:33 -050023## -----------------------------------------------------------------------
Joey Armstrongaf577ab2022-12-15 14:43:33 -050024## -----------------------------------------------------------------------
Joey Armstrong1b443bf2023-01-24 10:22:18 -050025function doDebug()
Joey Armstrongaf577ab2022-12-15 14:43:33 -050026{
Joey Armstrong1b443bf2023-01-24 10:22:18 -050027 echo "** ${FUNCNAME[0]}: ENTER"
Joey Armstrong28eddda2023-01-10 03:09:34 -050028
Joey Armstrongaf577ab2022-12-15 14:43:33 -050029 echo
Joey Armstrong1b443bf2023-01-24 10:22:18 -050030 echo "** PWD: $(/bin/pwd)"
31 echo "** make-pre: $(/bin/ls -l)"
32 echo
33
34 declare -p ARTIFACT_GLOB
35 declare -p RELEASE_TEMP
Joey Armstrongaf577ab2022-12-15 14:43:33 -050036
37 echo
Joey Armstrong1b443bf2023-01-24 10:22:18 -050038 echo "** ${FUNCNAME[0]}: ARTIFACT_GLOB=${ARTIFACT_GLOB}"
39 local artifact_glob="${ARTIFACT_GLOB%/*}"
40 declare -p artifact_glob
41 find "$artifact_glob" -print || /bin/true
Joey Armstrongaf577ab2022-12-15 14:43:33 -050042
Joey Armstrong1b443bf2023-01-24 10:22:18 -050043 # Copy artifacts into the release temp dir
44 # shellcheck disable=SC2086
45 cp -v "$ARTIFACT_GLOB" "$RELEASE_TEMP"
46
Joey Armstrongaf577ab2022-12-15 14:43:33 -050047 echo
Joey Armstrong1b443bf2023-01-24 10:22:18 -050048 echo "** ${FUNCNAME[0]}: RELEASE_TEMP=${RELEASE_TEMP}"
49 find "$RELEASE_TEMP" -print || /bin/true
Joey Armstrongaf577ab2022-12-15 14:43:33 -050050
Joey Armstrong1b443bf2023-01-24 10:22:18 -050051 echo "** ${FUNCNAME[0]}: LEAVE"
52 echo
Joey Armstrongaf577ab2022-12-15 14:43:33 -050053 return
54}
55
56##----------------##
57##---] MAIN [---##
58##----------------##
Zack Williams27cd3e52018-09-18 16:44:50 -070059set -eu -o pipefail
60
61# when not running under Jenkins, use current dir as workspace and a blank
62# project name
63WORKSPACE=${WORKSPACE:-.}
64GERRIT_PROJECT=${GERRIT_PROJECT:-}
65
66# Github organization (or user) this project is published on. Project name should
67# be the same on both Gerrit and GitHub
68GITHUB_ORGANIZATION=${GITHUB_ORGANIZATION:-}
69
70# glob pattern relative to project dir matching release artifacts
71ARTIFACT_GLOB=${ARTIFACT_GLOB:-"release/*"}
72
73# Temporary staging directory to copy artifacts to
74RELEASE_TEMP="$WORKSPACE/release"
Zack Williams2f8e8472019-05-21 16:29:06 -070075mkdir -p "$RELEASE_TEMP"
Zack Williams27cd3e52018-09-18 16:44:50 -070076
77# Use "release" as the default makefile target, can be a space separated list
78RELEASE_TARGETS=${RELEASE_TARGETS:-release}
79
Zack Williams0f398492019-10-23 16:02:24 -070080
Zack Williams27cd3e52018-09-18 16:44:50 -070081# check that we're on a semver released version, or exit
82pushd "$GERRIT_PROJECT"
83 GIT_VERSION=$(git tag -l --points-at HEAD)
84
Zack Williams6e070f52019-10-04 11:08:59 -070085 # match bare versions or v-prefixed golang style version
86 if [[ "$GIT_VERSION" =~ ^v?([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]
Zack Williams27cd3e52018-09-18 16:44:50 -070087 then
88 echo "git has a SemVer released version tag: '$GIT_VERSION'"
89 echo "Building artifacts for GitHub release."
90 else
91 echo "No SemVer released version tag found, exiting..."
92 exit 0
93 fi
94popd
95
Zack Williams0f398492019-10-23 16:02:24 -070096# Set and handle GOPATH and PATH
97export GOPATH=${GOPATH:-$WORKSPACE/go}
98export PATH=$PATH:/usr/lib/go-1.12/bin:/usr/local/go/bin:$GOPATH/bin
99
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500100displayCommands
101
Zack Williams0f398492019-10-23 16:02:24 -0700102# To support golang projects that require GOPATH to be set and code checked out there
Zack Williams27cd3e52018-09-18 16:44:50 -0700103# If $DEST_GOPATH is not an empty string:
Zack Williams76356cb2019-08-30 10:44:42 -0700104# - create GOPATH within WORKSPACE, and destination directory within
Zack Williams27cd3e52018-09-18 16:44:50 -0700105# - set PATH to include $GOPATH/bin and the system go binaries
Zack Williams76356cb2019-08-30 10:44:42 -0700106# - move project from $WORKSPACE/$GERRIT_PROJECT to new location in $GOPATH
107# - start release process within that directory
Zack Williams27cd3e52018-09-18 16:44:50 -0700108
109DEST_GOPATH=${DEST_GOPATH:-}
Joey Armstrong28eddda2023-01-10 03:09:34 -0500110if [ -n "$DEST_GOPATH" ]; then
Zack Williams27cd3e52018-09-18 16:44:50 -0700111 mkdir -p "$GOPATH/src/$DEST_GOPATH"
Zack Williams27cd3e52018-09-18 16:44:50 -0700112 release_path="$GOPATH/src/$DEST_GOPATH/$GERRIT_PROJECT"
Zack Williams76356cb2019-08-30 10:44:42 -0700113 mv "$WORKSPACE/$GERRIT_PROJECT" "$release_path"
Zack Williams27cd3e52018-09-18 16:44:50 -0700114else
115 release_path="$WORKSPACE/$GERRIT_PROJECT"
116fi
117
118if [ ! -f "$release_path/Makefile" ]; then
119 echo "Makefile not found at $release_path!"
120 exit 1
121else
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500122
Zack Williams27cd3e52018-09-18 16:44:50 -0700123 pushd "$release_path"
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500124# set -x
Zack Williams27cd3e52018-09-18 16:44:50 -0700125
126 # Release description is sanitized version of the log message
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500127 RELEASE_DESCRIPTION="$(git log -1 --pretty=%B | tr -dc "[:alnum:]\n\r\.\[\]\:\-\\\/\`\' ")"
Zack Williams27cd3e52018-09-18 16:44:50 -0700128
129 # build the release, can be multiple space separated targets
130 # shellcheck disable=SC2086
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500131 make "$RELEASE_TARGETS"
Zack Williams27cd3e52018-09-18 16:44:50 -0700132
Joey Armstrong1b443bf2023-01-24 10:22:18 -0500133 doDebug # deterine why ARTIFACT_GLOB is empty
134 # cp -v "$ARTIFACT_GLOB" "$RELEASE_TEMP"
Joey Armstrong65fcc3d2023-01-23 17:38:38 -0500135
Zack Williams27cd3e52018-09-18 16:44:50 -0700136 # create release
Zack Williams0d93d842019-05-21 17:02:49 -0700137 echo "Creating Release: $GERRIT_PROJECT - $GIT_VERSION"
Zack Williams0ba8a9b2020-04-30 22:25:52 -0700138 github-release release \
Zack Williams27cd3e52018-09-18 16:44:50 -0700139 --user "$GITHUB_ORGANIZATION" \
140 --repo "$GERRIT_PROJECT" \
141 --tag "$GIT_VERSION" \
142 --name "$GERRIT_PROJECT - $GIT_VERSION" \
143 --description "$RELEASE_DESCRIPTION"
144
145 # handle release files
146 pushd "$RELEASE_TEMP"
147
148 # Generate and check checksums
Zack Williams2f8e8472019-05-21 16:29:06 -0700149 sha256sum -- * > checksum.SHA256
Zack Williams27cd3e52018-09-18 16:44:50 -0700150 sha256sum -c < checksum.SHA256
151
Zack Williams0d93d842019-05-21 17:02:49 -0700152 echo "Checksums:"
153 cat checksum.SHA256
154
Zack Williams27cd3e52018-09-18 16:44:50 -0700155 # upload all files to the release
Zack Williams2f8e8472019-05-21 16:29:06 -0700156 for rel_file in *
Zack Williams27cd3e52018-09-18 16:44:50 -0700157 do
Zack Williams0d93d842019-05-21 17:02:49 -0700158 echo "Uploading file: $rel_file"
Zack Williams0ba8a9b2020-04-30 22:25:52 -0700159 github-release upload \
Zack Williams27cd3e52018-09-18 16:44:50 -0700160 --user "$GITHUB_ORGANIZATION" \
161 --repo "$GERRIT_PROJECT" \
162 --tag "$GIT_VERSION" \
163 --name "$rel_file" \
164 --file "$rel_file"
165 done
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500166 set +x
Zack Williams27cd3e52018-09-18 16:44:50 -0700167 popd
168
169 popd
170fi
Joey Armstrong28eddda2023-01-10 03:09:34 -0500171
172# [SEE ALSO]
173# -----------------------------------------------------------------------
174# https://www.shellcheck.net/wiki/SC2236
175# -----------------------------------------------------------------------
176
177# [EOF]