blob: 0b289ecb11202a5308fbb07fe776bca745b7e614 [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## -----------------------------------------------------------------------
24## Intent:
25## Display available commands and paths for golang.
26## A github release job is failing due to a command being mia.
27##
28## Note: This function is being used for side effects, when commands
29## are unavailable to query for version info, fail the job.
30## -----------------------------------------------------------------------
31## 05:02:50 bash: go: command not found
32## 05:03:00 Unable to find image 'voltha/voltha-ci-tools:2.4.0-golang' locally
33## -----------------------------------------------------------------------
34function displayCommands()
35{
Joey Armstrong28eddda2023-01-10 03:09:34 -050036 set +euo pipefail
37
Joey Armstrongaf577ab2022-12-15 14:43:33 -050038 echo
39 echo "Installed /usr/lib/go"
40 echo "-----------------------------------------------------------------------"
41 find /usr/lib -mindepth 1 -maxdepth 1 -name 'go-*' -print || /bin/true
42
43 echo
44 echo "Go commands in \$PATH"
45 echo "-----------------------------------------------------------------------"
46 which -a go || /bin/true
47
48 echo
49 echo "VERSIONS:"
50 echo "-----------------------------------------------------------------------"
51 git --version
52 go version
53
Joey Armstrong28eddda2023-01-10 03:09:34 -050054 set -euo pipefail
Joey Armstrongaf577ab2022-12-15 14:43:33 -050055 return
56}
57
58##----------------##
59##---] MAIN [---##
60##----------------##
Zack Williams27cd3e52018-09-18 16:44:50 -070061set -eu -o pipefail
62
63# when not running under Jenkins, use current dir as workspace and a blank
64# project name
65WORKSPACE=${WORKSPACE:-.}
66GERRIT_PROJECT=${GERRIT_PROJECT:-}
67
68# Github organization (or user) this project is published on. Project name should
69# be the same on both Gerrit and GitHub
70GITHUB_ORGANIZATION=${GITHUB_ORGANIZATION:-}
71
72# glob pattern relative to project dir matching release artifacts
73ARTIFACT_GLOB=${ARTIFACT_GLOB:-"release/*"}
74
75# Temporary staging directory to copy artifacts to
76RELEASE_TEMP="$WORKSPACE/release"
Zack Williams2f8e8472019-05-21 16:29:06 -070077mkdir -p "$RELEASE_TEMP"
Zack Williams27cd3e52018-09-18 16:44:50 -070078
79# Use "release" as the default makefile target, can be a space separated list
80RELEASE_TARGETS=${RELEASE_TARGETS:-release}
81
Zack Williams0f398492019-10-23 16:02:24 -070082
Zack Williams27cd3e52018-09-18 16:44:50 -070083# check that we're on a semver released version, or exit
84pushd "$GERRIT_PROJECT"
85 GIT_VERSION=$(git tag -l --points-at HEAD)
86
Zack Williams6e070f52019-10-04 11:08:59 -070087 # match bare versions or v-prefixed golang style version
88 if [[ "$GIT_VERSION" =~ ^v?([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]
Zack Williams27cd3e52018-09-18 16:44:50 -070089 then
90 echo "git has a SemVer released version tag: '$GIT_VERSION'"
91 echo "Building artifacts for GitHub release."
92 else
93 echo "No SemVer released version tag found, exiting..."
94 exit 0
95 fi
96popd
97
Zack Williams0f398492019-10-23 16:02:24 -070098# Set and handle GOPATH and PATH
99export GOPATH=${GOPATH:-$WORKSPACE/go}
100export PATH=$PATH:/usr/lib/go-1.12/bin:/usr/local/go/bin:$GOPATH/bin
101
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500102displayCommands
103
Zack Williams0f398492019-10-23 16:02:24 -0700104# To support golang projects that require GOPATH to be set and code checked out there
Zack Williams27cd3e52018-09-18 16:44:50 -0700105# If $DEST_GOPATH is not an empty string:
Zack Williams76356cb2019-08-30 10:44:42 -0700106# - create GOPATH within WORKSPACE, and destination directory within
Zack Williams27cd3e52018-09-18 16:44:50 -0700107# - set PATH to include $GOPATH/bin and the system go binaries
Zack Williams76356cb2019-08-30 10:44:42 -0700108# - move project from $WORKSPACE/$GERRIT_PROJECT to new location in $GOPATH
109# - start release process within that directory
Zack Williams27cd3e52018-09-18 16:44:50 -0700110
111DEST_GOPATH=${DEST_GOPATH:-}
Joey Armstrong28eddda2023-01-10 03:09:34 -0500112if [ -n "$DEST_GOPATH" ]; then
Zack Williams27cd3e52018-09-18 16:44:50 -0700113 mkdir -p "$GOPATH/src/$DEST_GOPATH"
Zack Williams27cd3e52018-09-18 16:44:50 -0700114 release_path="$GOPATH/src/$DEST_GOPATH/$GERRIT_PROJECT"
Zack Williams76356cb2019-08-30 10:44:42 -0700115 mv "$WORKSPACE/$GERRIT_PROJECT" "$release_path"
Zack Williams27cd3e52018-09-18 16:44:50 -0700116else
117 release_path="$WORKSPACE/$GERRIT_PROJECT"
118fi
119
120if [ ! -f "$release_path/Makefile" ]; then
121 echo "Makefile not found at $release_path!"
122 exit 1
123else
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500124
Zack Williams27cd3e52018-09-18 16:44:50 -0700125 pushd "$release_path"
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500126 set -x
Zack Williams27cd3e52018-09-18 16:44:50 -0700127
128 # Release description is sanitized version of the log message
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500129 RELEASE_DESCRIPTION="$(git log -1 --pretty=%B | tr -dc "[:alnum:]\n\r\.\[\]\:\-\\\/\`\' ")"
Zack Williams27cd3e52018-09-18 16:44:50 -0700130
131 # build the release, can be multiple space separated targets
132 # shellcheck disable=SC2086
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500133 make "$RELEASE_TARGETS"
Zack Williams27cd3e52018-09-18 16:44:50 -0700134
135 # Copy artifacts into the release temp dir
136 # shellcheck disable=SC2086
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500137 cp "$ARTIFACT_GLOB" "$RELEASE_TEMP"
Zack Williams27cd3e52018-09-18 16:44:50 -0700138
139 # create release
Zack Williams0d93d842019-05-21 17:02:49 -0700140 echo "Creating Release: $GERRIT_PROJECT - $GIT_VERSION"
Zack Williams0ba8a9b2020-04-30 22:25:52 -0700141 github-release release \
Zack Williams27cd3e52018-09-18 16:44:50 -0700142 --user "$GITHUB_ORGANIZATION" \
143 --repo "$GERRIT_PROJECT" \
144 --tag "$GIT_VERSION" \
145 --name "$GERRIT_PROJECT - $GIT_VERSION" \
146 --description "$RELEASE_DESCRIPTION"
147
148 # handle release files
149 pushd "$RELEASE_TEMP"
150
151 # Generate and check checksums
Zack Williams2f8e8472019-05-21 16:29:06 -0700152 sha256sum -- * > checksum.SHA256
Zack Williams27cd3e52018-09-18 16:44:50 -0700153 sha256sum -c < checksum.SHA256
154
Zack Williams0d93d842019-05-21 17:02:49 -0700155 echo "Checksums:"
156 cat checksum.SHA256
157
Zack Williams27cd3e52018-09-18 16:44:50 -0700158 # upload all files to the release
Zack Williams2f8e8472019-05-21 16:29:06 -0700159 for rel_file in *
Zack Williams27cd3e52018-09-18 16:44:50 -0700160 do
Zack Williams0d93d842019-05-21 17:02:49 -0700161 echo "Uploading file: $rel_file"
Zack Williams0ba8a9b2020-04-30 22:25:52 -0700162 github-release upload \
Zack Williams27cd3e52018-09-18 16:44:50 -0700163 --user "$GITHUB_ORGANIZATION" \
164 --repo "$GERRIT_PROJECT" \
165 --tag "$GIT_VERSION" \
166 --name "$rel_file" \
167 --file "$rel_file"
168 done
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500169 set +x
Zack Williams27cd3e52018-09-18 16:44:50 -0700170 popd
171
172 popd
173fi
Joey Armstrong28eddda2023-01-10 03:09:34 -0500174
175# [SEE ALSO]
176# -----------------------------------------------------------------------
177# https://www.shellcheck.net/wiki/SC2236
178# -----------------------------------------------------------------------
179
180# [EOF]