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" |
| 38 | |
| 39 | # Use "release" as the default makefile target, can be a space separated list |
| 40 | RELEASE_TARGETS=${RELEASE_TARGETS:-release} |
| 41 | |
| 42 | # check that we're on a semver released version, or exit |
| 43 | pushd "$GERRIT_PROJECT" |
| 44 | GIT_VERSION=$(git tag -l --points-at HEAD) |
| 45 | |
| 46 | if [[ "$GIT_VERSION" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]] |
| 47 | then |
| 48 | echo "git has a SemVer released version tag: '$GIT_VERSION'" |
| 49 | echo "Building artifacts for GitHub release." |
| 50 | else |
| 51 | echo "No SemVer released version tag found, exiting..." |
| 52 | exit 0 |
| 53 | fi |
| 54 | popd |
| 55 | |
| 56 | # To support golang projects create a GOPATH |
| 57 | # If $DEST_GOPATH is not an empty string: |
| 58 | # - set create GOPATH, and destination directory within in |
| 59 | # - set PATH to include $GOPATH/bin and the system go binaries |
| 60 | # - symlink from $WORKSPACE/$GERRIT_PROJECT to new location in $GOPATH |
| 61 | # - start release from that directory |
| 62 | |
| 63 | DEST_GOPATH=${DEST_GOPATH:-} |
| 64 | if [ ! -z "$DEST_GOPATH" ]; then |
| 65 | export GOPATH=${GOPATH:-~/go} |
| 66 | mkdir -p "$GOPATH/src/$DEST_GOPATH" |
| 67 | export PATH=$PATH:/usr/lib/go-1.12/bin:/usr/local/go/bin:$GOPATH/bin |
| 68 | release_path="$GOPATH/src/$DEST_GOPATH/$GERRIT_PROJECT" |
| 69 | ln -r -s "$WORKSPACE/$GERRIT_PROJECT" "$release_path" |
| 70 | else |
| 71 | release_path="$WORKSPACE/$GERRIT_PROJECT" |
| 72 | fi |
| 73 | |
| 74 | if [ ! -f "$release_path/Makefile" ]; then |
| 75 | echo "Makefile not found at $release_path!" |
| 76 | exit 1 |
| 77 | else |
| 78 | pushd "$release_path" |
| 79 | |
| 80 | # Release description is sanitized version of the log message |
| 81 | RELEASE_DESCRIPTION=$(git log -1 --pretty=%B | tr -dc "[:alnum:]\n\r\.\[\]\:\-\\\/\`\' ") |
| 82 | |
| 83 | # build the release, can be multiple space separated targets |
| 84 | # shellcheck disable=SC2086 |
| 85 | make $RELEASE_TARGETS |
| 86 | |
| 87 | # Copy artifacts into the release temp dir |
| 88 | # shellcheck disable=SC2086 |
| 89 | cp $ARTIFACT_GLOB "$RELEASE_TEMP" |
| 90 | |
| 91 | # create release |
| 92 | gothub release \ |
| 93 | --user "$GITHUB_ORGANIZATION" \ |
| 94 | --repo "$GERRIT_PROJECT" \ |
| 95 | --tag "$GIT_VERSION" \ |
| 96 | --name "$GERRIT_PROJECT - $GIT_VERSION" \ |
| 97 | --description "$RELEASE_DESCRIPTION" |
| 98 | |
| 99 | # handle release files |
| 100 | pushd "$RELEASE_TEMP" |
| 101 | |
| 102 | # Generate and check checksums |
| 103 | sha256sum ./* > checksum.SHA256 |
| 104 | sha256sum -c < checksum.SHA256 |
| 105 | |
| 106 | # upload all files to the release |
| 107 | for rel_file in ./* |
| 108 | do |
| 109 | gothub upload \ |
| 110 | --user "$GITHUB_ORGANIZATION" \ |
| 111 | --repo "$GERRIT_PROJECT" \ |
| 112 | --tag "$GIT_VERSION" \ |
| 113 | --name "$rel_file" \ |
| 114 | --file "$rel_file" |
| 115 | done |
| 116 | popd |
| 117 | |
| 118 | popd |
| 119 | fi |