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 |
| 24 | |
Zack Williams | 6650000 | 2018-09-06 15:29:05 -0700 | [diff] [blame] | 25 | SEMVER_STRICT=${SEMVER_STRICT:-0} # require semver versions |
| 26 | |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 27 | releaseversion=0 |
| 28 | fail_validation=0 |
| 29 | |
| 30 | # when not running under Jenkins, use current dir as workspace |
| 31 | WORKSPACE=${WORKSPACE:-.} |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 32 | |
| 33 | # find the version string in the repo, read into NEW_VERSION |
| 34 | # Add additional places NEW_VERSION could be found to this function |
| 35 | function read_version { |
| 36 | if [ -f "VERSION" ] |
| 37 | then |
| 38 | NEW_VERSION=$(head -n1 "VERSION") |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 39 | VERSIONFILE="VERSION" |
Zack Williams | 6a9d2e6 | 2018-06-22 15:18:23 -0700 | [diff] [blame] | 40 | elif [ -f "package.json" ] |
| 41 | then |
| 42 | NEW_VERSION=$(python -c 'import json,sys;obj=json.load(sys.stdin); print obj["version"]' < package.json) |
| 43 | VERSIONFILE="package.json" |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 44 | else |
| 45 | echo "ERROR: No versioning file found!" |
| 46 | exit 1 |
| 47 | fi |
| 48 | } |
| 49 | |
| 50 | # check if the version is already a tag in git |
| 51 | function is_git_tag_duplicated { |
| 52 | for existing_tag in $(git tag) |
| 53 | do |
| 54 | if [ "$NEW_VERSION" = "$existing_tag" ] |
| 55 | then |
| 56 | echo "ERROR: Duplicate tag: $existing_tag" |
| 57 | exit 2 |
| 58 | fi |
| 59 | done |
| 60 | } |
| 61 | |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 62 | # check if the version is a released version |
| 63 | function check_if_releaseversion { |
| 64 | if [[ "$NEW_VERSION" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]] |
| 65 | then |
| 66 | echo "Version string '$NEW_VERSION' found in '$VERSIONFILE' is a SemVer released version!" |
| 67 | releaseversion=1 |
| 68 | else |
Zack Williams | 6650000 | 2018-09-06 15:29:05 -0700 | [diff] [blame] | 69 | if [ "$SEMVER_STRICT" -eq "1" ] |
| 70 | then |
| 71 | echo "Version string '$NEW_VERSION' in '$VERSIONFILE' is not a SemVer released version, SEMVER_STRICT enabled, failing!" |
| 72 | fail_validation=1 |
| 73 | else |
| 74 | echo "Version string '$NEW_VERSION' in '$VERSIONFILE' is not a SemVer released version, skipping." |
| 75 | fi |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 76 | fi |
| 77 | } |
| 78 | |
| 79 | # check if Dockerfiles have a released version as their parent |
| 80 | function dockerfile_parentcheck { |
| 81 | while IFS= read -r -d '' dockerfile |
| 82 | do |
| 83 | echo "Checking dockerfile: '$dockerfile'" |
| 84 | |
| 85 | # split on newlines |
| 86 | IFS=$'\n' |
| 87 | df_parents=($(grep "^FROM" "$dockerfile")) |
| 88 | |
| 89 | # check all parents in the Dockerfile |
| 90 | for df_parent in "${df_parents[@]}" |
| 91 | do |
| 92 | |
| 93 | df_pattern="FROM (.*):(.*)" |
| 94 | if [[ "$df_parent" =~ $df_pattern ]] |
| 95 | then |
| 96 | |
| 97 | p_image="${BASH_REMATCH[1]}" |
| 98 | p_version="${BASH_REMATCH[2]}" |
| 99 | |
| 100 | if [[ "${p_version}" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]] |
| 101 | then |
| 102 | echo " OK: Parent '$p_image:$p_version' is a released SemVer version" |
Zack Williams | 0dc2754 | 2018-10-11 08:09:10 -0700 | [diff] [blame] | 103 | elif [[ "${p_version}" =~ ^.*([0-9]+)\.([0-9]+).*$ ]] |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 104 | then |
Zack Williams | 0dc2754 | 2018-10-11 08:09:10 -0700 | [diff] [blame] | 105 | # handle non-SemVer versions that have a Major.Minor version specifier in the name |
| 106 | # 'ubuntu:16.04' |
| 107 | # 'postgres:10.3-alpine' |
| 108 | # 'openjdk:8-jre-alpine3.8' |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 109 | echo " OK: Parent '$p_image:$p_version' is using a non-SemVer, but sufficient, version" |
| 110 | else |
| 111 | echo " ERROR: Parent '$p_image:$p_version' is NOT using an specific version" |
| 112 | fail_validation=1 |
| 113 | fi |
| 114 | |
| 115 | elif [[ "$df_parent" =~ ^FROM\ scratch$ ]] |
| 116 | then |
| 117 | # Handle the parent-less `FROM scratch` case: |
| 118 | # https://docs.docker.com/develop/develop-images/baseimages/ |
| 119 | echo " OK: Using the versionless 'scratch' parent: '$df_parent'" |
| 120 | else |
| 121 | echo " ERROR: Couldn't find a parent image in $df_parent" |
| 122 | fi |
| 123 | |
| 124 | done |
| 125 | |
Zack Williams | 4a6af0f | 2019-05-13 08:38:32 -0700 | [diff] [blame] | 126 | done < <( find "${WORKSPACE}" -name 'Dockerfile*' ! -path "*/vendor/*" -print0 ) |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 129 | echo "Checking git repo with remotes:" |
| 130 | git remote -v |
| 131 | |
| 132 | echo "Branches:" |
| 133 | git branch -v |
| 134 | |
| 135 | echo "Existing git tags:" |
| 136 | git tag -n |
| 137 | |
| 138 | read_version |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 139 | check_if_releaseversion |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 140 | |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 141 | # perform checks if a released version |
| 142 | if [ "$releaseversion" -eq "1" ] |
| 143 | then |
| 144 | is_git_tag_duplicated |
| 145 | dockerfile_parentcheck |
| 146 | fi |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 147 | |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 148 | exit $fail_validation |