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