Zack Williams | 12783ac | 2018-06-12 15:13:12 -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 | # versiontag.sh |
| 18 | # Tags a git commit with the SemVer version discovered within the commit, |
| 19 | # if the tag doesn't already exist. Ignore non-SemVer commits. |
| 20 | |
| 21 | set -eu -o pipefail |
| 22 | |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 23 | VERSIONFILE="" # file path to file containing version number |
| 24 | NEW_VERSION="" # version number found in $VERSIONFILE |
Zack Williams | 6e070f5 | 2019-10-04 11:08:59 -0700 | [diff] [blame] | 25 | TAG_VERSION="" # version file that might have a leading v to work around go mod funkyness |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 26 | |
Zack Williams | 6650000 | 2018-09-06 15:29:05 -0700 | [diff] [blame] | 27 | SEMVER_STRICT=${SEMVER_STRICT:-0} # require semver versions |
| 28 | |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 29 | releaseversion=0 |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 30 | fail_validation=0 |
| 31 | |
| 32 | # when not running under Jenkins, use current dir as workspace |
| 33 | WORKSPACE=${WORKSPACE:-.} |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 34 | |
| 35 | # find the version string in the repo, read into NEW_VERSION |
| 36 | # Add additional places NEW_VERSION could be found to this function |
| 37 | function read_version { |
| 38 | if [ -f "VERSION" ] |
| 39 | then |
| 40 | NEW_VERSION=$(head -n1 "VERSION") |
| 41 | VERSIONFILE="VERSION" |
Zack Williams | 6e070f5 | 2019-10-04 11:08:59 -0700 | [diff] [blame] | 42 | |
| 43 | # If this is a golang project, use funky v-prefixed versions |
| 44 | if [ -f "Gopkg.toml" ] || [ -f "go.mod" ] |
| 45 | then |
| 46 | echo "go-based project found, using v-prefixed version for git tags: v${NEW_VERSION}" |
| 47 | TAG_VERSION=v${NEW_VERSION} |
| 48 | else |
| 49 | TAG_VERSION=${NEW_VERSION} |
| 50 | fi |
| 51 | |
Zack Williams | 6a9d2e6 | 2018-06-22 15:18:23 -0700 | [diff] [blame] | 52 | elif [ -f "package.json" ] |
| 53 | then |
| 54 | NEW_VERSION=$(python -c 'import json,sys;obj=json.load(sys.stdin); print obj["version"]' < package.json) |
Zack Williams | 6e070f5 | 2019-10-04 11:08:59 -0700 | [diff] [blame] | 55 | TAG_VERSION=$NEW_VERSION |
Zack Williams | 6a9d2e6 | 2018-06-22 15:18:23 -0700 | [diff] [blame] | 56 | VERSIONFILE="package.json" |
Zack Williams | 866ef3c | 2019-09-27 15:41:02 -0700 | [diff] [blame] | 57 | elif [ -f "pom.xml" ] |
| 58 | then |
| 59 | NEW_VERSION=$(xmllint --xpath '/*[local-name()="project"]/*[local-name()="version"]/text()' pom.xml) |
Zack Williams | 6e070f5 | 2019-10-04 11:08:59 -0700 | [diff] [blame] | 60 | TAG_VERSION=$NEW_VERSION |
Zack Williams | 866ef3c | 2019-09-27 15:41:02 -0700 | [diff] [blame] | 61 | VERSIONFILE="pom.xml" |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 62 | else |
| 63 | echo "ERROR: No versioning file found!" |
| 64 | exit 1 |
| 65 | fi |
| 66 | } |
| 67 | |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 68 | # check if the version is a released version |
| 69 | function check_if_releaseversion { |
| 70 | if [[ "$NEW_VERSION" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]] |
| 71 | then |
| 72 | echo "Version string '$NEW_VERSION' in '$VERSIONFILE' is a SemVer released version!" |
| 73 | releaseversion=1 |
| 74 | else |
Zack Williams | 6650000 | 2018-09-06 15:29:05 -0700 | [diff] [blame] | 75 | if [ "$SEMVER_STRICT" -eq "1" ] |
| 76 | then |
| 77 | echo "Version string '$NEW_VERSION' in '$VERSIONFILE' is not a SemVer released version, SEMVER_STRICT enabled, failing!" |
| 78 | fail_validation=1 |
| 79 | else |
| 80 | echo "Version string '$NEW_VERSION' in '$VERSIONFILE' is not a SemVer released version, skipping." |
| 81 | fi |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 82 | fi |
| 83 | } |
| 84 | |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 85 | # check if the version is already a tag in git |
| 86 | function is_git_tag_duplicated { |
| 87 | for existing_tag in $(git tag) |
| 88 | do |
Zack Williams | 6e070f5 | 2019-10-04 11:08:59 -0700 | [diff] [blame] | 89 | if [ "$TAG_VERSION" = "$existing_tag" ] |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 90 | then |
| 91 | echo "ERROR: Duplicate tag: $existing_tag" |
| 92 | exit 2 |
| 93 | fi |
| 94 | done |
| 95 | } |
| 96 | |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 97 | # check if Dockerfiles have a released version as their parent |
| 98 | function dockerfile_parentcheck { |
| 99 | while IFS= read -r -d '' dockerfile |
| 100 | do |
| 101 | echo "Checking dockerfile: '$dockerfile'" |
| 102 | |
| 103 | # split on newlines |
| 104 | IFS=$'\n' |
| 105 | df_parents=($(grep "^FROM" "$dockerfile")) |
| 106 | |
| 107 | # check all parents in the Dockerfile |
| 108 | for df_parent in "${df_parents[@]}" |
| 109 | do |
| 110 | |
David K. Bainbridge | cc8127b | 2021-04-07 15:51:30 +0000 | [diff] [blame] | 111 | df_pattern="[FfRrOoMm] +(--platform=[^ ]+ +)?([^@: ]+)(:([^: ]+)|@sha[^ ]+)?" |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 112 | if [[ "$df_parent" =~ $df_pattern ]] |
| 113 | then |
| 114 | |
David K. Bainbridge | cc8127b | 2021-04-07 15:51:30 +0000 | [diff] [blame] | 115 | p_image="${BASH_REMATCH[2]}" |
| 116 | p_sha=${BASH_REMATCH[3]} |
| 117 | p_version="${BASH_REMATCH[4]}" |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 118 | |
David K. Bainbridge | cc8127b | 2021-04-07 15:51:30 +0000 | [diff] [blame] | 119 | echo "IMAGE: '${p_image}'" |
| 120 | echo "VERSION: '$p_version'" |
| 121 | echo "SHA: '$p_sha'" |
| 122 | |
| 123 | if [[ "${p_image}" == "scratch" ]] |
| 124 | then |
| 125 | echo " OK: Using the versionless 'scratch' parent: '$df_parent'" |
| 126 | elif [[ "${p_image}:${p_version}" == "gcr.io/distroless/static:nonroot" ]] |
| 127 | then |
| 128 | echo " OK: Using static distroless image with nonroot: '${p_image}:${p_version}'" |
| 129 | elif [[ "${p_version}" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]] |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 130 | then |
| 131 | echo " OK: Parent '$p_image:$p_version' is a released SemVer version" |
David K. Bainbridge | cc8127b | 2021-04-07 15:51:30 +0000 | [diff] [blame] | 132 | elif [[ "${p_sha}" =~ ^@sha256:[0-9a-f]{64}.*$ ]] |
Zack Williams | c800dbe | 2020-02-14 10:25:12 -0700 | [diff] [blame] | 133 | then |
| 134 | # allow sha256 hashes to be used as version specifiers |
David K. Bainbridge | cc8127b | 2021-04-07 15:51:30 +0000 | [diff] [blame] | 135 | echo " OK: Parent '$p_image$p_sha' is using a specific sha256 hash as a version" |
Zack Williams | 0dc2754 | 2018-10-11 08:09:10 -0700 | [diff] [blame] | 136 | elif [[ "${p_version}" =~ ^.*([0-9]+)\.([0-9]+).*$ ]] |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 137 | then |
Zack Williams | 0dc2754 | 2018-10-11 08:09:10 -0700 | [diff] [blame] | 138 | # handle non-SemVer versions that have a Major.Minor version specifier in the name |
| 139 | # 'ubuntu:16.04' |
| 140 | # 'postgres:10.3-alpine' |
| 141 | # 'openjdk:8-jre-alpine3.8' |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 142 | echo " OK: Parent '$p_image:$p_version' is using a non-SemVer, but sufficient, version" |
David K. Bainbridge | cc8127b | 2021-04-07 15:51:30 +0000 | [diff] [blame] | 143 | elif [[ -z "${p_version}" ]] |
| 144 | then |
| 145 | echo " ERROR: Parent '$p_image' is NOT using a specific version" |
| 146 | fail_validation=1 |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 147 | else |
David K. Bainbridge | cc8127b | 2021-04-07 15:51:30 +0000 | [diff] [blame] | 148 | echo " ERROR: Parent '$p_image:$p_version' is NOT using a specific version" |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 149 | fail_validation=1 |
| 150 | fi |
| 151 | |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 152 | else |
| 153 | echo " ERROR: Couldn't find a parent image in $df_parent" |
| 154 | fi |
| 155 | |
| 156 | done |
| 157 | |
Matteo Scandolo | b28a02e | 2019-11-04 09:35:16 -0800 | [diff] [blame] | 158 | done < <( find "${WORKSPACE}" -name 'Dockerfile*' ! -path "*/vendor/*" ! -name "*dockerignore" -print0 ) |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 161 | |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 162 | # create a git tag |
| 163 | function create_git_tag { |
Zack Williams | 6e070f5 | 2019-10-04 11:08:59 -0700 | [diff] [blame] | 164 | echo "Creating git tag: $TAG_VERSION" |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 165 | git checkout "$GERRIT_PATCHSET_REVISION" |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 166 | |
Zack Williams | 7a7a318 | 2020-08-13 14:15:40 -0700 | [diff] [blame] | 167 | git config --global user.email "do-not-reply@opennetworking.org" |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 168 | git config --global user.name "Jenkins" |
| 169 | |
Zack Williams | 6e070f5 | 2019-10-04 11:08:59 -0700 | [diff] [blame] | 170 | git tag -a "$TAG_VERSION" -m "Tagged by CORD Jenkins version-tag job: $BUILD_NUMBER, for Gerrit patchset: $GERRIT_CHANGE_NUMBER" |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 171 | |
| 172 | echo "Tags including new tag:" |
| 173 | git tag -n |
| 174 | |
Zack Williams | 6e070f5 | 2019-10-04 11:08:59 -0700 | [diff] [blame] | 175 | git push origin "$TAG_VERSION" |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | echo "Checking git repo with remotes:" |
| 179 | git remote -v |
| 180 | |
| 181 | echo "Branches:" |
| 182 | git branch -v |
| 183 | |
| 184 | echo "Existing git tags:" |
| 185 | git tag -n |
| 186 | |
| 187 | read_version |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 188 | check_if_releaseversion |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 189 | |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 190 | # perform checks if a released version |
| 191 | if [ "$releaseversion" -eq "1" ] |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 192 | then |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 193 | is_git_tag_duplicated |
| 194 | dockerfile_parentcheck |
| 195 | |
| 196 | if [ "$fail_validation" -eq "0" ] |
| 197 | then |
| 198 | create_git_tag |
| 199 | else |
| 200 | echo "ERROR: commit merged but failed validation, not tagging!" |
| 201 | fi |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 202 | fi |
| 203 | |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 204 | exit $fail_validation |