blob: 2ded191b305b67914d786dba77de305aca423258 [file] [log] [blame]
Zack Williams27cd3e52018-09-18 16:44:50 -07001#!/usr/bin/env bash
2
3# Copyright 2018-present Open Networking Foundation
4#
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.
16
17# 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
21
22set -eu -o pipefail
23
24# when not running under Jenkins, use current dir as workspace and a blank
25# project name
26WORKSPACE=${WORKSPACE:-.}
27GERRIT_PROJECT=${GERRIT_PROJECT:-}
28
29# Github organization (or user) this project is published on. Project name should
30# be the same on both Gerrit and GitHub
31GITHUB_ORGANIZATION=${GITHUB_ORGANIZATION:-}
32
33# glob pattern relative to project dir matching release artifacts
34ARTIFACT_GLOB=${ARTIFACT_GLOB:-"release/*"}
35
36# Temporary staging directory to copy artifacts to
37RELEASE_TEMP="$WORKSPACE/release"
Zack Williams2f8e8472019-05-21 16:29:06 -070038mkdir -p "$RELEASE_TEMP"
Zack Williams27cd3e52018-09-18 16:44:50 -070039
40# Use "release" as the default makefile target, can be a space separated list
41RELEASE_TARGETS=${RELEASE_TARGETS:-release}
42
43# check that we're on a semver released version, or exit
44pushd "$GERRIT_PROJECT"
45 GIT_VERSION=$(git tag -l --points-at HEAD)
46
Zack Williams6e070f52019-10-04 11:08:59 -070047 # match bare versions or v-prefixed golang style version
48 if [[ "$GIT_VERSION" =~ ^v?([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]
Zack Williams27cd3e52018-09-18 16:44:50 -070049 then
50 echo "git has a SemVer released version tag: '$GIT_VERSION'"
51 echo "Building artifacts for GitHub release."
52 else
53 echo "No SemVer released version tag found, exiting..."
54 exit 0
55 fi
56popd
57
58# To support golang projects create a GOPATH
59# If $DEST_GOPATH is not an empty string:
Zack Williams76356cb2019-08-30 10:44:42 -070060# - create GOPATH within WORKSPACE, and destination directory within
Zack Williams27cd3e52018-09-18 16:44:50 -070061# - set PATH to include $GOPATH/bin and the system go binaries
Zack Williams76356cb2019-08-30 10:44:42 -070062# - move project from $WORKSPACE/$GERRIT_PROJECT to new location in $GOPATH
63# - start release process within that directory
Zack Williams27cd3e52018-09-18 16:44:50 -070064
65DEST_GOPATH=${DEST_GOPATH:-}
66if [ ! -z "$DEST_GOPATH" ]; then
Zack Williams76356cb2019-08-30 10:44:42 -070067 export GOPATH=${GOPATH:-$WORKSPACE/go}
Zack Williams27cd3e52018-09-18 16:44:50 -070068 mkdir -p "$GOPATH/src/$DEST_GOPATH"
69 export PATH=$PATH:/usr/lib/go-1.12/bin:/usr/local/go/bin:$GOPATH/bin
70 release_path="$GOPATH/src/$DEST_GOPATH/$GERRIT_PROJECT"
Zack Williams76356cb2019-08-30 10:44:42 -070071 mv "$WORKSPACE/$GERRIT_PROJECT" "$release_path"
Zack Williams27cd3e52018-09-18 16:44:50 -070072else
73 release_path="$WORKSPACE/$GERRIT_PROJECT"
74fi
75
76if [ ! -f "$release_path/Makefile" ]; then
77 echo "Makefile not found at $release_path!"
78 exit 1
79else
80 pushd "$release_path"
81
82 # Release description is sanitized version of the log message
83 RELEASE_DESCRIPTION=$(git log -1 --pretty=%B | tr -dc "[:alnum:]\n\r\.\[\]\:\-\\\/\`\' ")
84
85 # build the release, can be multiple space separated targets
86 # shellcheck disable=SC2086
87 make $RELEASE_TARGETS
88
89 # Copy artifacts into the release temp dir
90 # shellcheck disable=SC2086
91 cp $ARTIFACT_GLOB "$RELEASE_TEMP"
92
93 # create release
Zack Williams0d93d842019-05-21 17:02:49 -070094 echo "Creating Release: $GERRIT_PROJECT - $GIT_VERSION"
95 gothub release \
Zack Williams27cd3e52018-09-18 16:44:50 -070096 --user "$GITHUB_ORGANIZATION" \
97 --repo "$GERRIT_PROJECT" \
98 --tag "$GIT_VERSION" \
99 --name "$GERRIT_PROJECT - $GIT_VERSION" \
100 --description "$RELEASE_DESCRIPTION"
101
102 # handle release files
103 pushd "$RELEASE_TEMP"
104
105 # Generate and check checksums
Zack Williams2f8e8472019-05-21 16:29:06 -0700106 sha256sum -- * > checksum.SHA256
Zack Williams27cd3e52018-09-18 16:44:50 -0700107 sha256sum -c < checksum.SHA256
108
Zack Williams0d93d842019-05-21 17:02:49 -0700109 echo "Checksums:"
110 cat checksum.SHA256
111
Zack Williams27cd3e52018-09-18 16:44:50 -0700112 # upload all files to the release
Zack Williams2f8e8472019-05-21 16:29:06 -0700113 for rel_file in *
Zack Williams27cd3e52018-09-18 16:44:50 -0700114 do
Zack Williams0d93d842019-05-21 17:02:49 -0700115 echo "Uploading file: $rel_file"
116 gothub upload \
Zack Williams27cd3e52018-09-18 16:44:50 -0700117 --user "$GITHUB_ORGANIZATION" \
118 --repo "$GERRIT_PROJECT" \
119 --tag "$GIT_VERSION" \
120 --name "$rel_file" \
121 --file "$rel_file"
122 done
123 popd
124
125 popd
126fi