Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 1 | #!/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 | |
| 22 | set -eu -o pipefail |
| 23 | |
| 24 | # when not running under Jenkins, use current dir as workspace and a blank |
| 25 | # project name |
| 26 | WORKSPACE=${WORKSPACE:-.} |
| 27 | GERRIT_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 |
| 31 | GITHUB_ORGANIZATION=${GITHUB_ORGANIZATION:-} |
| 32 | |
| 33 | # glob pattern relative to project dir matching release artifacts |
| 34 | ARTIFACT_GLOB=${ARTIFACT_GLOB:-"release/*"} |
| 35 | |
| 36 | # Temporary staging directory to copy artifacts to |
| 37 | RELEASE_TEMP="$WORKSPACE/release" |
Zack Williams | 2f8e847 | 2019-05-21 16:29:06 -0700 | [diff] [blame] | 38 | mkdir -p "$RELEASE_TEMP" |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 39 | |
| 40 | # Use "release" as the default makefile target, can be a space separated list |
| 41 | RELEASE_TARGETS=${RELEASE_TARGETS:-release} |
| 42 | |
Zack Williams | 0f39849 | 2019-10-23 16:02:24 -0700 | [diff] [blame] | 43 | |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 44 | # check that we're on a semver released version, or exit |
| 45 | pushd "$GERRIT_PROJECT" |
| 46 | GIT_VERSION=$(git tag -l --points-at HEAD) |
| 47 | |
Zack Williams | 6e070f5 | 2019-10-04 11:08:59 -0700 | [diff] [blame] | 48 | # match bare versions or v-prefixed golang style version |
| 49 | if [[ "$GIT_VERSION" =~ ^v?([0-9]+)\.([0-9]+)\.([0-9]+)$ ]] |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 50 | then |
| 51 | echo "git has a SemVer released version tag: '$GIT_VERSION'" |
| 52 | echo "Building artifacts for GitHub release." |
| 53 | else |
| 54 | echo "No SemVer released version tag found, exiting..." |
| 55 | exit 0 |
| 56 | fi |
| 57 | popd |
| 58 | |
Zack Williams | 0f39849 | 2019-10-23 16:02:24 -0700 | [diff] [blame] | 59 | # Set and handle GOPATH and PATH |
| 60 | export GOPATH=${GOPATH:-$WORKSPACE/go} |
| 61 | export PATH=$PATH:/usr/lib/go-1.12/bin:/usr/local/go/bin:$GOPATH/bin |
| 62 | |
| 63 | # To support golang projects that require GOPATH to be set and code checked out there |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 64 | # If $DEST_GOPATH is not an empty string: |
Zack Williams | 76356cb | 2019-08-30 10:44:42 -0700 | [diff] [blame] | 65 | # - create GOPATH within WORKSPACE, and destination directory within |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 66 | # - set PATH to include $GOPATH/bin and the system go binaries |
Zack Williams | 76356cb | 2019-08-30 10:44:42 -0700 | [diff] [blame] | 67 | # - move project from $WORKSPACE/$GERRIT_PROJECT to new location in $GOPATH |
| 68 | # - start release process within that directory |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 69 | |
| 70 | DEST_GOPATH=${DEST_GOPATH:-} |
| 71 | if [ ! -z "$DEST_GOPATH" ]; then |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 72 | mkdir -p "$GOPATH/src/$DEST_GOPATH" |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 73 | release_path="$GOPATH/src/$DEST_GOPATH/$GERRIT_PROJECT" |
Zack Williams | 76356cb | 2019-08-30 10:44:42 -0700 | [diff] [blame] | 74 | mv "$WORKSPACE/$GERRIT_PROJECT" "$release_path" |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 75 | else |
| 76 | release_path="$WORKSPACE/$GERRIT_PROJECT" |
| 77 | fi |
| 78 | |
| 79 | if [ ! -f "$release_path/Makefile" ]; then |
| 80 | echo "Makefile not found at $release_path!" |
| 81 | exit 1 |
| 82 | else |
| 83 | pushd "$release_path" |
| 84 | |
| 85 | # Release description is sanitized version of the log message |
| 86 | RELEASE_DESCRIPTION=$(git log -1 --pretty=%B | tr -dc "[:alnum:]\n\r\.\[\]\:\-\\\/\`\' ") |
| 87 | |
| 88 | # build the release, can be multiple space separated targets |
| 89 | # shellcheck disable=SC2086 |
| 90 | make $RELEASE_TARGETS |
| 91 | |
| 92 | # Copy artifacts into the release temp dir |
| 93 | # shellcheck disable=SC2086 |
| 94 | cp $ARTIFACT_GLOB "$RELEASE_TEMP" |
| 95 | |
| 96 | # create release |
Zack Williams | 0d93d84 | 2019-05-21 17:02:49 -0700 | [diff] [blame] | 97 | echo "Creating Release: $GERRIT_PROJECT - $GIT_VERSION" |
| 98 | gothub release \ |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 99 | --user "$GITHUB_ORGANIZATION" \ |
| 100 | --repo "$GERRIT_PROJECT" \ |
| 101 | --tag "$GIT_VERSION" \ |
| 102 | --name "$GERRIT_PROJECT - $GIT_VERSION" \ |
| 103 | --description "$RELEASE_DESCRIPTION" |
| 104 | |
| 105 | # handle release files |
| 106 | pushd "$RELEASE_TEMP" |
| 107 | |
| 108 | # Generate and check checksums |
Zack Williams | 2f8e847 | 2019-05-21 16:29:06 -0700 | [diff] [blame] | 109 | sha256sum -- * > checksum.SHA256 |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 110 | sha256sum -c < checksum.SHA256 |
| 111 | |
Zack Williams | 0d93d84 | 2019-05-21 17:02:49 -0700 | [diff] [blame] | 112 | echo "Checksums:" |
| 113 | cat checksum.SHA256 |
| 114 | |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 115 | # upload all files to the release |
Zack Williams | 2f8e847 | 2019-05-21 16:29:06 -0700 | [diff] [blame] | 116 | for rel_file in * |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 117 | do |
Zack Williams | 0d93d84 | 2019-05-21 17:02:49 -0700 | [diff] [blame] | 118 | echo "Uploading file: $rel_file" |
| 119 | gothub upload \ |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 120 | --user "$GITHUB_ORGANIZATION" \ |
| 121 | --repo "$GERRIT_PROJECT" \ |
| 122 | --tag "$GIT_VERSION" \ |
| 123 | --name "$rel_file" \ |
| 124 | --file "$rel_file" |
| 125 | done |
| 126 | popd |
| 127 | |
| 128 | popd |
| 129 | fi |