blob: 56cc4cf562e3267d974d73df063096e724a3c512 [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
Joey Armstrong65fcc3d2023-01-23 17:38:38 -0500135 echo
136 echo "** PWD: $(/bin/pwd)"
137 echo "** make-pre: $(/bin/ls -l)"
138 echo
139
Zack Williams27cd3e52018-09-18 16:44:50 -0700140 # Copy artifacts into the release temp dir
141 # shellcheck disable=SC2086
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500142 cp "$ARTIFACT_GLOB" "$RELEASE_TEMP"
Zack Williams27cd3e52018-09-18 16:44:50 -0700143
Joey Armstrong65fcc3d2023-01-23 17:38:38 -0500144 echo
145 echo "** make-post: $(/bin/ls -l)"
Joey Armstrong0e3aaaa2023-01-23 18:05:13 -0500146 case "$ARTIFACT_GLOB" in
147 release/*) find 'release/.' -ls ;;
148 esac
149
Joey Armstrong65fcc3d2023-01-23 17:38:38 -0500150 echo
151
Zack Williams27cd3e52018-09-18 16:44:50 -0700152 # create release
Zack Williams0d93d842019-05-21 17:02:49 -0700153 echo "Creating Release: $GERRIT_PROJECT - $GIT_VERSION"
Zack Williams0ba8a9b2020-04-30 22:25:52 -0700154 github-release release \
Zack Williams27cd3e52018-09-18 16:44:50 -0700155 --user "$GITHUB_ORGANIZATION" \
156 --repo "$GERRIT_PROJECT" \
157 --tag "$GIT_VERSION" \
158 --name "$GERRIT_PROJECT - $GIT_VERSION" \
159 --description "$RELEASE_DESCRIPTION"
160
161 # handle release files
162 pushd "$RELEASE_TEMP"
163
164 # Generate and check checksums
Zack Williams2f8e8472019-05-21 16:29:06 -0700165 sha256sum -- * > checksum.SHA256
Zack Williams27cd3e52018-09-18 16:44:50 -0700166 sha256sum -c < checksum.SHA256
167
Zack Williams0d93d842019-05-21 17:02:49 -0700168 echo "Checksums:"
169 cat checksum.SHA256
170
Zack Williams27cd3e52018-09-18 16:44:50 -0700171 # upload all files to the release
Zack Williams2f8e8472019-05-21 16:29:06 -0700172 for rel_file in *
Zack Williams27cd3e52018-09-18 16:44:50 -0700173 do
Zack Williams0d93d842019-05-21 17:02:49 -0700174 echo "Uploading file: $rel_file"
Zack Williams0ba8a9b2020-04-30 22:25:52 -0700175 github-release upload \
Zack Williams27cd3e52018-09-18 16:44:50 -0700176 --user "$GITHUB_ORGANIZATION" \
177 --repo "$GERRIT_PROJECT" \
178 --tag "$GIT_VERSION" \
179 --name "$rel_file" \
180 --file "$rel_file"
181 done
Joey Armstrongaf577ab2022-12-15 14:43:33 -0500182 set +x
Zack Williams27cd3e52018-09-18 16:44:50 -0700183 popd
184
185 popd
186fi
Joey Armstrong28eddda2023-01-10 03:09:34 -0500187
188# [SEE ALSO]
189# -----------------------------------------------------------------------
190# https://www.shellcheck.net/wiki/SC2236
191# -----------------------------------------------------------------------
192
193# [EOF]