Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
Joey Armstrong | af577ab | 2022-12-15 14:43:33 -0500 | [diff] [blame] | 3 | # Copyright 2018-2022 Open Networking Foundation (ONF) and the ONF Contributors |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 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 | |
Joey Armstrong | af577ab | 2022-12-15 14:43:33 -0500 | [diff] [blame] | 22 | ## ----------------------------------------------------------------------- |
| 23 | ## Intent: |
| 24 | ## Display available commands and paths for golang. |
| 25 | ## A github release job is failing due to a command being mia. |
| 26 | ## |
| 27 | ## Note: This function is being used for side effects, when commands |
| 28 | ## are unavailable to query for version info, fail the job. |
| 29 | ## ----------------------------------------------------------------------- |
| 30 | ## 05:02:50 bash: go: command not found |
| 31 | ## 05:03:00 Unable to find image 'voltha/voltha-ci-tools:2.4.0-golang' locally |
| 32 | ## ----------------------------------------------------------------------- |
| 33 | function displayCommands() |
| 34 | { |
| 35 | echo |
| 36 | echo "Installed /usr/lib/go" |
| 37 | echo "-----------------------------------------------------------------------" |
| 38 | find /usr/lib -mindepth 1 -maxdepth 1 -name 'go-*' -print || /bin/true |
| 39 | |
| 40 | echo |
| 41 | echo "Go commands in \$PATH" |
| 42 | echo "-----------------------------------------------------------------------" |
| 43 | which -a go || /bin/true |
| 44 | |
| 45 | echo |
| 46 | echo "VERSIONS:" |
| 47 | echo "-----------------------------------------------------------------------" |
| 48 | git --version |
| 49 | go version |
| 50 | |
| 51 | return |
| 52 | } |
| 53 | |
| 54 | ##----------------## |
| 55 | ##---] MAIN [---## |
| 56 | ##----------------## |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 57 | set -eu -o pipefail |
| 58 | |
| 59 | # when not running under Jenkins, use current dir as workspace and a blank |
| 60 | # project name |
| 61 | WORKSPACE=${WORKSPACE:-.} |
| 62 | GERRIT_PROJECT=${GERRIT_PROJECT:-} |
| 63 | |
| 64 | # Github organization (or user) this project is published on. Project name should |
| 65 | # be the same on both Gerrit and GitHub |
| 66 | GITHUB_ORGANIZATION=${GITHUB_ORGANIZATION:-} |
| 67 | |
| 68 | # glob pattern relative to project dir matching release artifacts |
| 69 | ARTIFACT_GLOB=${ARTIFACT_GLOB:-"release/*"} |
| 70 | |
| 71 | # Temporary staging directory to copy artifacts to |
| 72 | RELEASE_TEMP="$WORKSPACE/release" |
Zack Williams | 2f8e847 | 2019-05-21 16:29:06 -0700 | [diff] [blame] | 73 | mkdir -p "$RELEASE_TEMP" |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 74 | |
| 75 | # Use "release" as the default makefile target, can be a space separated list |
| 76 | RELEASE_TARGETS=${RELEASE_TARGETS:-release} |
| 77 | |
Zack Williams | 0f39849 | 2019-10-23 16:02:24 -0700 | [diff] [blame] | 78 | |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 79 | # check that we're on a semver released version, or exit |
| 80 | pushd "$GERRIT_PROJECT" |
| 81 | GIT_VERSION=$(git tag -l --points-at HEAD) |
| 82 | |
Zack Williams | 6e070f5 | 2019-10-04 11:08:59 -0700 | [diff] [blame] | 83 | # match bare versions or v-prefixed golang style version |
| 84 | if [[ "$GIT_VERSION" =~ ^v?([0-9]+)\.([0-9]+)\.([0-9]+)$ ]] |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 85 | then |
| 86 | echo "git has a SemVer released version tag: '$GIT_VERSION'" |
| 87 | echo "Building artifacts for GitHub release." |
| 88 | else |
| 89 | echo "No SemVer released version tag found, exiting..." |
| 90 | exit 0 |
| 91 | fi |
| 92 | popd |
| 93 | |
Zack Williams | 0f39849 | 2019-10-23 16:02:24 -0700 | [diff] [blame] | 94 | # Set and handle GOPATH and PATH |
| 95 | export GOPATH=${GOPATH:-$WORKSPACE/go} |
| 96 | export PATH=$PATH:/usr/lib/go-1.12/bin:/usr/local/go/bin:$GOPATH/bin |
| 97 | |
Joey Armstrong | af577ab | 2022-12-15 14:43:33 -0500 | [diff] [blame] | 98 | displayCommands |
| 99 | |
Zack Williams | 0f39849 | 2019-10-23 16:02:24 -0700 | [diff] [blame] | 100 | # 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] | 101 | # If $DEST_GOPATH is not an empty string: |
Zack Williams | 76356cb | 2019-08-30 10:44:42 -0700 | [diff] [blame] | 102 | # - create GOPATH within WORKSPACE, and destination directory within |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 103 | # - set PATH to include $GOPATH/bin and the system go binaries |
Zack Williams | 76356cb | 2019-08-30 10:44:42 -0700 | [diff] [blame] | 104 | # - move project from $WORKSPACE/$GERRIT_PROJECT to new location in $GOPATH |
| 105 | # - start release process within that directory |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 106 | |
| 107 | DEST_GOPATH=${DEST_GOPATH:-} |
| 108 | if [ ! -z "$DEST_GOPATH" ]; then |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 109 | mkdir -p "$GOPATH/src/$DEST_GOPATH" |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 110 | release_path="$GOPATH/src/$DEST_GOPATH/$GERRIT_PROJECT" |
Zack Williams | 76356cb | 2019-08-30 10:44:42 -0700 | [diff] [blame] | 111 | mv "$WORKSPACE/$GERRIT_PROJECT" "$release_path" |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 112 | else |
| 113 | release_path="$WORKSPACE/$GERRIT_PROJECT" |
| 114 | fi |
| 115 | |
| 116 | if [ ! -f "$release_path/Makefile" ]; then |
| 117 | echo "Makefile not found at $release_path!" |
| 118 | exit 1 |
| 119 | else |
Joey Armstrong | af577ab | 2022-12-15 14:43:33 -0500 | [diff] [blame] | 120 | |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 121 | pushd "$release_path" |
Joey Armstrong | af577ab | 2022-12-15 14:43:33 -0500 | [diff] [blame] | 122 | set -x |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 123 | |
| 124 | # Release description is sanitized version of the log message |
Joey Armstrong | af577ab | 2022-12-15 14:43:33 -0500 | [diff] [blame] | 125 | RELEASE_DESCRIPTION="$(git log -1 --pretty=%B | tr -dc "[:alnum:]\n\r\.\[\]\:\-\\\/\`\' ")" |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 126 | |
| 127 | # build the release, can be multiple space separated targets |
| 128 | # shellcheck disable=SC2086 |
Joey Armstrong | af577ab | 2022-12-15 14:43:33 -0500 | [diff] [blame] | 129 | make "$RELEASE_TARGETS" |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 130 | |
| 131 | # Copy artifacts into the release temp dir |
| 132 | # shellcheck disable=SC2086 |
Joey Armstrong | af577ab | 2022-12-15 14:43:33 -0500 | [diff] [blame] | 133 | cp "$ARTIFACT_GLOB" "$RELEASE_TEMP" |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 134 | |
| 135 | # create release |
Zack Williams | 0d93d84 | 2019-05-21 17:02:49 -0700 | [diff] [blame] | 136 | echo "Creating Release: $GERRIT_PROJECT - $GIT_VERSION" |
Zack Williams | 0ba8a9b | 2020-04-30 22:25:52 -0700 | [diff] [blame] | 137 | github-release release \ |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 138 | --user "$GITHUB_ORGANIZATION" \ |
| 139 | --repo "$GERRIT_PROJECT" \ |
| 140 | --tag "$GIT_VERSION" \ |
| 141 | --name "$GERRIT_PROJECT - $GIT_VERSION" \ |
| 142 | --description "$RELEASE_DESCRIPTION" |
| 143 | |
| 144 | # handle release files |
| 145 | pushd "$RELEASE_TEMP" |
| 146 | |
| 147 | # Generate and check checksums |
Zack Williams | 2f8e847 | 2019-05-21 16:29:06 -0700 | [diff] [blame] | 148 | sha256sum -- * > checksum.SHA256 |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 149 | sha256sum -c < checksum.SHA256 |
| 150 | |
Zack Williams | 0d93d84 | 2019-05-21 17:02:49 -0700 | [diff] [blame] | 151 | echo "Checksums:" |
| 152 | cat checksum.SHA256 |
| 153 | |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 154 | # upload all files to the release |
Zack Williams | 2f8e847 | 2019-05-21 16:29:06 -0700 | [diff] [blame] | 155 | for rel_file in * |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 156 | do |
Zack Williams | 0d93d84 | 2019-05-21 17:02:49 -0700 | [diff] [blame] | 157 | echo "Uploading file: $rel_file" |
Zack Williams | 0ba8a9b | 2020-04-30 22:25:52 -0700 | [diff] [blame] | 158 | github-release upload \ |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 159 | --user "$GITHUB_ORGANIZATION" \ |
| 160 | --repo "$GERRIT_PROJECT" \ |
| 161 | --tag "$GIT_VERSION" \ |
| 162 | --name "$rel_file" \ |
| 163 | --file "$rel_file" |
| 164 | done |
Joey Armstrong | af577ab | 2022-12-15 14:43:33 -0500 | [diff] [blame] | 165 | set +x |
Zack Williams | 27cd3e5 | 2018-09-18 16:44:50 -0700 | [diff] [blame] | 166 | popd |
| 167 | |
| 168 | popd |
| 169 | fi |