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!" |
| 63 | exit 1 |
| 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 { |
| 86 | for existing_tag in $(git tag) |
| 87 | do |
| 88 | if [ "$TAG_VERSION" = "$existing_tag" ] |
| 89 | then |
| 90 | echo "ERROR: Duplicate tag: $existing_tag" |
| 91 | exit 2 |
| 92 | fi |
| 93 | done |
| 94 | } |
| 95 | |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 96 | # check if Dockerfiles have a released version as their parent |
| 97 | function dockerfile_parentcheck { |
| 98 | while IFS= read -r -d '' dockerfile |
| 99 | do |
| 100 | echo "Checking dockerfile: '$dockerfile'" |
| 101 | |
| 102 | # split on newlines |
| 103 | IFS=$'\n' |
| 104 | df_parents=($(grep "^FROM" "$dockerfile")) |
| 105 | |
| 106 | # check all parents in the Dockerfile |
| 107 | for df_parent in "${df_parents[@]}" |
| 108 | do |
| 109 | |
| 110 | df_pattern="FROM (.*):(.*)" |
| 111 | if [[ "$df_parent" =~ $df_pattern ]] |
| 112 | then |
| 113 | |
| 114 | p_image="${BASH_REMATCH[1]}" |
| 115 | p_version="${BASH_REMATCH[2]}" |
| 116 | |
| 117 | if [[ "${p_version}" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]] |
| 118 | then |
| 119 | echo " OK: Parent '$p_image:$p_version' is a released SemVer version" |
Zack Williams | 0dc2754 | 2018-10-11 08:09:10 -0700 | [diff] [blame] | 120 | elif [[ "${p_version}" =~ ^.*([0-9]+)\.([0-9]+).*$ ]] |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 121 | then |
Zack Williams | 0dc2754 | 2018-10-11 08:09:10 -0700 | [diff] [blame] | 122 | # handle non-SemVer versions that have a Major.Minor version specifier in the name |
| 123 | # 'ubuntu:16.04' |
| 124 | # 'postgres:10.3-alpine' |
| 125 | # 'openjdk:8-jre-alpine3.8' |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 126 | echo " OK: Parent '$p_image:$p_version' is using a non-SemVer, but sufficient, version" |
| 127 | else |
| 128 | echo " ERROR: Parent '$p_image:$p_version' is NOT using an specific version" |
| 129 | fail_validation=1 |
| 130 | fi |
| 131 | |
| 132 | elif [[ "$df_parent" =~ ^FROM\ scratch$ ]] |
| 133 | then |
| 134 | # Handle the parent-less `FROM scratch` case: |
| 135 | # https://docs.docker.com/develop/develop-images/baseimages/ |
| 136 | echo " OK: Using the versionless 'scratch' parent: '$df_parent'" |
| 137 | else |
| 138 | echo " ERROR: Couldn't find a parent image in $df_parent" |
| 139 | fi |
| 140 | |
| 141 | done |
| 142 | |
Zack Williams | 4a6af0f | 2019-05-13 08:38:32 -0700 | [diff] [blame] | 143 | done < <( find "${WORKSPACE}" -name 'Dockerfile*' ! -path "*/vendor/*" -print0 ) |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 146 | echo "Checking git repo with remotes:" |
| 147 | git remote -v |
| 148 | |
| 149 | echo "Branches:" |
| 150 | git branch -v |
| 151 | |
| 152 | echo "Existing git tags:" |
| 153 | git tag -n |
| 154 | |
| 155 | read_version |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 156 | check_if_releaseversion |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 157 | |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 158 | # perform checks if a released version |
| 159 | if [ "$releaseversion" -eq "1" ] |
| 160 | then |
| 161 | is_git_tag_duplicated |
| 162 | dockerfile_parentcheck |
| 163 | fi |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 164 | |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 165 | exit $fail_validation |