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