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 | # tagcollisionreject.sh |
| 18 | # checks that there isn't an existing tag in the repo that has this tag |
| 19 | |
| 20 | set -eu -o pipefail |
| 21 | |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 22 | VERSIONFILE="" # file path to file containing version number |
| 23 | NEW_VERSION="" # version number found in $VERSIONFILE |
Zack Williams | 6e070f5 | 2019-10-04 11:08:59 -0700 | [diff] [blame] | 24 | TAG_VERSION="" # version file that might have a leading v to work around go mod funkyness |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 25 | |
Zack Williams | 6650000 | 2018-09-06 15:29:05 -0700 | [diff] [blame] | 26 | SEMVER_STRICT=${SEMVER_STRICT:-0} # require semver versions |
| 27 | |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 28 | releaseversion=0 |
| 29 | fail_validation=0 |
| 30 | |
| 31 | # when not running under Jenkins, use current dir as workspace |
| 32 | WORKSPACE=${WORKSPACE:-.} |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 33 | |
| 34 | # find the version string in the repo, read into NEW_VERSION |
| 35 | # Add additional places NEW_VERSION could be found to this function |
| 36 | function read_version { |
| 37 | if [ -f "VERSION" ] |
| 38 | then |
| 39 | NEW_VERSION=$(head -n1 "VERSION") |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 40 | VERSIONFILE="VERSION" |
Zack Williams | 6e070f5 | 2019-10-04 11:08:59 -0700 | [diff] [blame] | 41 | |
| 42 | # If this is a golang project, use funky v-prefixed versions |
| 43 | if [ -f "Gopkg.toml" ] || [ -f "go.mod" ] |
| 44 | then |
| 45 | echo "go-based project found, using v-prefixed version for git tags: v${NEW_VERSION}" |
| 46 | TAG_VERSION=v${NEW_VERSION} |
| 47 | else |
| 48 | TAG_VERSION=${NEW_VERSION} |
| 49 | fi |
| 50 | |
Zack Williams | 6a9d2e6 | 2018-06-22 15:18:23 -0700 | [diff] [blame] | 51 | elif [ -f "package.json" ] |
| 52 | then |
| 53 | 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] | 54 | TAG_VERSION=$NEW_VERSION |
Zack Williams | 6a9d2e6 | 2018-06-22 15:18:23 -0700 | [diff] [blame] | 55 | VERSIONFILE="package.json" |
Zack Williams | 866ef3c | 2019-09-27 15:41:02 -0700 | [diff] [blame] | 56 | elif [ -f "pom.xml" ] |
| 57 | then |
| 58 | 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] | 59 | TAG_VERSION=$NEW_VERSION |
Zack Williams | 866ef3c | 2019-09-27 15:41:02 -0700 | [diff] [blame] | 60 | VERSIONFILE="pom.xml" |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 61 | else |
| 62 | echo "ERROR: No versioning file found!" |
Matteo Scandolo | 93b531d | 2021-03-22 14:36:48 -0700 | [diff] [blame] | 63 | fail_validation=1 |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 64 | fi |
| 65 | } |
| 66 | |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 67 | # check if the version is a released version |
| 68 | function check_if_releaseversion { |
| 69 | if [[ "$NEW_VERSION" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]] |
| 70 | then |
| 71 | echo "Version string '$NEW_VERSION' found in '$VERSIONFILE' is a SemVer released version!" |
| 72 | releaseversion=1 |
| 73 | else |
Zack Williams | 6650000 | 2018-09-06 15:29:05 -0700 | [diff] [blame] | 74 | if [ "$SEMVER_STRICT" -eq "1" ] |
| 75 | then |
| 76 | echo "Version string '$NEW_VERSION' in '$VERSIONFILE' is not a SemVer released version, SEMVER_STRICT enabled, failing!" |
| 77 | fail_validation=1 |
| 78 | else |
| 79 | echo "Version string '$NEW_VERSION' in '$VERSIONFILE' is not a SemVer released version, skipping." |
| 80 | fi |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 81 | fi |
| 82 | } |
| 83 | |
Zack Williams | 6e070f5 | 2019-10-04 11:08:59 -0700 | [diff] [blame] | 84 | # check if the version is already a tag in git |
| 85 | function is_git_tag_duplicated { |
Matteo Scandolo | e089043 | 2021-04-06 06:37:21 -0700 | [diff] [blame] | 86 | for existing_tag in $existing_tags |
Zack Williams | 6e070f5 | 2019-10-04 11:08:59 -0700 | [diff] [blame] | 87 | do |
| 88 | if [ "$TAG_VERSION" = "$existing_tag" ] |
| 89 | then |
| 90 | echo "ERROR: Duplicate tag: $existing_tag" |
Matteo Scandolo | 93b531d | 2021-03-22 14:36:48 -0700 | [diff] [blame] | 91 | fail_validation=2 |
Zack Williams | 6e070f5 | 2019-10-04 11:08:59 -0700 | [diff] [blame] | 92 | fi |
Matteo Scandolo | e089043 | 2021-04-06 06:37:21 -0700 | [diff] [blame] | 93 | done |
Matteo Scandolo | 93b531d | 2021-03-22 14:36:48 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | # from https://github.com/cloudflare/semver_bash/blob/master/semver.sh |
| 97 | function semverParseInto() { |
| 98 | local RE='[^0-9]*\([0-9]*\)[.]\([0-9]*\)[.]\([0-9]*\)\([0-9A-Za-z-]*\)' |
| 99 | #MAJOR |
| 100 | eval $2=`echo $1 | sed -e "s#$RE#\1#"` |
| 101 | #MINOR |
| 102 | eval $3=`echo $1 | sed -e "s#$RE#\2#"` |
| 103 | #MINOR |
| 104 | eval $4=`echo $1 | sed -e "s#$RE#\3#"` |
| 105 | #SPECIAL |
| 106 | eval $5=`echo $1 | sed -e "s#$RE#\4#"` |
| 107 | } |
| 108 | |
| 109 | # if it's a -dev version check if a previous tag has been created (to avoid going from 2.7.0-dev to 2.7.1-dev) |
| 110 | function is_valid_version { |
| 111 | local MAJOR=0 MINOR=0 PATCH=0 SPECIAL="" |
| 112 | local C_MAJOR=0 C_MINOR=0 C_PATCH=0 C_SPECIAL="" # these are used in the inner loops to compare |
| 113 | |
| 114 | semverParseInto $NEW_VERSION MAJOR MINOR PATCH SPECIAL |
| 115 | |
| 116 | found_parent=false |
| 117 | |
| 118 | # if minor == 0, check that there was a release with MAJOR-1.X.X |
| 119 | if [[ "$MINOR" == 0 ]]; then |
| 120 | new_major=$(( $MAJOR - 1 )) |
| 121 | parent_version="$new_major.x.x" |
Matteo Scandolo | 3e89c07 | 2021-04-01 09:36:26 -0700 | [diff] [blame] | 122 | for existing_tag in $existing_tags |
Matteo Scandolo | 93b531d | 2021-03-22 14:36:48 -0700 | [diff] [blame] | 123 | do |
| 124 | semverParseInto $existing_tag C_MAJOR C_MINOR C_PATCH C_SPECIAL |
| 125 | if [[ "$new_major" == "$C_MAJOR" ]]; then |
| 126 | found_parent=true |
| 127 | fi |
Matteo Scandolo | 3e89c07 | 2021-04-01 09:36:26 -0700 | [diff] [blame] | 128 | done |
Matteo Scandolo | 9725cdb | 2021-06-03 09:03:43 -0700 | [diff] [blame] | 129 | fi |
Matteo Scandolo | 93b531d | 2021-03-22 14:36:48 -0700 | [diff] [blame] | 130 | |
| 131 | # if patch == 0, check that there was a release with MAJOR.MINOR-1.X |
Matteo Scandolo | 9725cdb | 2021-06-03 09:03:43 -0700 | [diff] [blame] | 132 | if [[ "$PATCH" == 0 ]]; then |
Matteo Scandolo | 93b531d | 2021-03-22 14:36:48 -0700 | [diff] [blame] | 133 | new_minor=$(( $MINOR - 1 )) |
| 134 | parent_version="$MAJOR.$new_minor.x" |
Matteo Scandolo | 3e89c07 | 2021-04-01 09:36:26 -0700 | [diff] [blame] | 135 | for existing_tag in $existing_tags |
Matteo Scandolo | 93b531d | 2021-03-22 14:36:48 -0700 | [diff] [blame] | 136 | do |
| 137 | semverParseInto $existing_tag C_MAJOR C_MINOR C_PATCH C_SPECIAL |
| 138 | if [[ "$new_minor" == "$C_MINOR" ]]; then |
| 139 | found_parent=true |
| 140 | fi |
Matteo Scandolo | 3e89c07 | 2021-04-01 09:36:26 -0700 | [diff] [blame] | 141 | done |
Matteo Scandolo | 9725cdb | 2021-06-03 09:03:43 -0700 | [diff] [blame] | 142 | fi |
Matteo Scandolo | 93b531d | 2021-03-22 14:36:48 -0700 | [diff] [blame] | 143 | |
| 144 | # if patch != 0 check that there was a release with MAJOR.MINOR.PATCH-1 |
Matteo Scandolo | 9725cdb | 2021-06-03 09:03:43 -0700 | [diff] [blame] | 145 | if [[ "$PATCH" != 0 ]]; then |
Matteo Scandolo | 93b531d | 2021-03-22 14:36:48 -0700 | [diff] [blame] | 146 | new_patch=$(( $PATCH - 1 )) |
| 147 | parent_version="$MAJOR.$MINOR.$new_patch" |
Matteo Scandolo | 3e89c07 | 2021-04-01 09:36:26 -0700 | [diff] [blame] | 148 | for existing_tag in $existing_tags |
Matteo Scandolo | 93b531d | 2021-03-22 14:36:48 -0700 | [diff] [blame] | 149 | do |
Matteo Scandolo | acccab4 | 2021-04-01 12:05:04 -0700 | [diff] [blame] | 150 | semverParseInto $existing_tag C_MAJOR C_MINOR C_PATCH C_SPECIAL |
| 151 | if [[ "$MAJOR" == "$C_MAJOR" && "$MINOR" == "$C_MINOR" && "$new_patch" == "$C_PATCH" ]] |
Matteo Scandolo | 93b531d | 2021-03-22 14:36:48 -0700 | [diff] [blame] | 152 | then |
| 153 | found_parent=true |
| 154 | fi |
Matteo Scandolo | 3e89c07 | 2021-04-01 09:36:26 -0700 | [diff] [blame] | 155 | done |
Matteo Scandolo | 93b531d | 2021-03-22 14:36:48 -0700 | [diff] [blame] | 156 | fi |
| 157 | |
| 158 | # if we are the beginning the is no parent, but that's fine |
| 159 | if [[ "$MAJOR" == 0 ]]; then |
| 160 | found_parent=true |
| 161 | fi |
| 162 | |
| 163 | if [[ $found_parent == false ]]; then |
| 164 | echo "Invalid $NEW_VERSION version. Expected parent version $parent_version does not exist." |
| 165 | fail_validation=1 |
| 166 | fi |
Zack Williams | 6e070f5 | 2019-10-04 11:08:59 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 169 | # check if Dockerfiles have a released version as their parent |
| 170 | function dockerfile_parentcheck { |
| 171 | while IFS= read -r -d '' dockerfile |
| 172 | do |
| 173 | echo "Checking dockerfile: '$dockerfile'" |
| 174 | |
| 175 | # split on newlines |
| 176 | IFS=$'\n' |
| 177 | df_parents=($(grep "^FROM" "$dockerfile")) |
| 178 | |
| 179 | # check all parents in the Dockerfile |
| 180 | for df_parent in "${df_parents[@]}" |
| 181 | do |
| 182 | |
Matteo Scandolo | c3aaa03 | 2021-04-07 10:33:40 -0700 | [diff] [blame] | 183 | df_pattern="[FfRrOoMm] +(--platform=[^ ]+ +)?([^@: ]+)(:([^: ]+)|@sha[^ ]+)?" |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 184 | if [[ "$df_parent" =~ $df_pattern ]] |
| 185 | then |
| 186 | |
Matteo Scandolo | c3aaa03 | 2021-04-07 10:33:40 -0700 | [diff] [blame] | 187 | p_image="${BASH_REMATCH[2]}" |
| 188 | p_sha=${BASH_REMATCH[3]} |
| 189 | p_version="${BASH_REMATCH[4]}" |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 190 | |
Matteo Scandolo | c3aaa03 | 2021-04-07 10:33:40 -0700 | [diff] [blame] | 191 | echo "IMAGE: '${p_image}'" |
| 192 | echo "VERSION: '$p_version'" |
| 193 | echo "SHA: '$p_sha'" |
| 194 | |
| 195 | if [[ "${p_image}" == "scratch" ]] |
| 196 | then |
| 197 | echo " OK: Using the versionless 'scratch' parent: '$df_parent'" |
| 198 | elif [[ "${p_image}:${p_version}" == "gcr.io/distroless/static:nonroot" ]] |
| 199 | then |
| 200 | echo " OK: Using static distroless image with nonroot: '${p_image}:${p_version}'" |
| 201 | elif [[ "${p_version}" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]] |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 202 | then |
| 203 | echo " OK: Parent '$p_image:$p_version' is a released SemVer version" |
Matteo Scandolo | c3aaa03 | 2021-04-07 10:33:40 -0700 | [diff] [blame] | 204 | elif [[ "${p_sha}" =~ ^@sha256:[0-9a-f]{64}.*$ ]] |
Zack Williams | c800dbe | 2020-02-14 10:25:12 -0700 | [diff] [blame] | 205 | then |
| 206 | # allow sha256 hashes to be used as version specifiers |
Matteo Scandolo | c3aaa03 | 2021-04-07 10:33:40 -0700 | [diff] [blame] | 207 | 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] | 208 | elif [[ "${p_version}" =~ ^.*([0-9]+)\.([0-9]+).*$ ]] |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 209 | then |
Zack Williams | 0dc2754 | 2018-10-11 08:09:10 -0700 | [diff] [blame] | 210 | # handle non-SemVer versions that have a Major.Minor version specifier in the name |
| 211 | # 'ubuntu:16.04' |
| 212 | # 'postgres:10.3-alpine' |
| 213 | # 'openjdk:8-jre-alpine3.8' |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 214 | echo " OK: Parent '$p_image:$p_version' is using a non-SemVer, but sufficient, version" |
Matteo Scandolo | c3aaa03 | 2021-04-07 10:33:40 -0700 | [diff] [blame] | 215 | elif [[ -z "${p_version}" ]] |
| 216 | then |
| 217 | echo " ERROR: Parent '$p_image' is NOT using a specific version" |
| 218 | fail_validation=1 |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 219 | else |
Matteo Scandolo | c3aaa03 | 2021-04-07 10:33:40 -0700 | [diff] [blame] | 220 | 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] | 221 | fail_validation=1 |
| 222 | fi |
| 223 | |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 224 | else |
| 225 | echo " ERROR: Couldn't find a parent image in $df_parent" |
| 226 | fi |
| 227 | |
| 228 | done |
| 229 | |
Matteo Scandolo | b28a02e | 2019-11-04 09:35:16 -0800 | [diff] [blame] | 230 | done < <( find "${WORKSPACE}" -name 'Dockerfile*' ! -path "*/vendor/*" ! -name "*dockerignore" -print0 ) |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 233 | echo "Checking git repo with remotes:" |
| 234 | git remote -v |
| 235 | |
| 236 | echo "Branches:" |
Matteo Scandolo | 93b531d | 2021-03-22 14:36:48 -0700 | [diff] [blame] | 237 | branches=$(git branch -v) |
| 238 | echo $branches |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 239 | |
| 240 | echo "Existing git tags:" |
Matteo Scandolo | 3e89c07 | 2021-04-01 09:36:26 -0700 | [diff] [blame] | 241 | existing_tags=$(git tag -l) |
Matteo Scandolo | 93b531d | 2021-03-22 14:36:48 -0700 | [diff] [blame] | 242 | echo $existing_tags |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 243 | |
| 244 | read_version |
Matteo Scandolo | 93b531d | 2021-03-22 14:36:48 -0700 | [diff] [blame] | 245 | is_valid_version |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 246 | check_if_releaseversion |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 247 | |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 248 | # perform checks if a released version |
| 249 | if [ "$releaseversion" -eq "1" ] |
| 250 | then |
| 251 | is_git_tag_duplicated |
| 252 | dockerfile_parentcheck |
| 253 | fi |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 254 | |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 255 | exit $fail_validation |