blob: 09c014c43a8898986a3ce79653cee73452ef2110 [file] [log] [blame]
Zack Williams12783ac2018-06-12 15:13:12 -07001#!/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
20set -eu -o pipefail
21
Zack Williams8e69efd2018-06-13 15:05:18 -070022VERSIONFILE="" # file path to file containing version number
23NEW_VERSION="" # version number found in $VERSIONFILE
24
Zack Williams66500002018-09-06 15:29:05 -070025SEMVER_STRICT=${SEMVER_STRICT:-0} # require semver versions
26
Zack Williams8e69efd2018-06-13 15:05:18 -070027releaseversion=0
28fail_validation=0
29
30# when not running under Jenkins, use current dir as workspace
31WORKSPACE=${WORKSPACE:-.}
Zack Williams12783ac2018-06-12 15:13:12 -070032
33# find the version string in the repo, read into NEW_VERSION
34# Add additional places NEW_VERSION could be found to this function
35function read_version {
36 if [ -f "VERSION" ]
37 then
38 NEW_VERSION=$(head -n1 "VERSION")
Zack Williams8e69efd2018-06-13 15:05:18 -070039 VERSIONFILE="VERSION"
Zack Williams6a9d2e62018-06-22 15:18:23 -070040 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 Williams12783ac2018-06-12 15:13:12 -070044 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
51function 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 Williams8e69efd2018-06-13 15:05:18 -070062# check if the version is a released version
63function 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 Williams66500002018-09-06 15:29:05 -070069 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 Williams8e69efd2018-06-13 15:05:18 -070076 fi
77}
78
79# check if Dockerfiles have a released version as their parent
80function 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"
103 elif [[ "${p_version}" =~ ^([0-9]+)\.([0-9]+).*$ ]]
104 then
105 # handle the non-SemVer 'ubuntu:16.04' and 'postgres:10.3-alpine' cases
106 echo " OK: Parent '$p_image:$p_version' is using a non-SemVer, but sufficient, version"
107 else
108 echo " ERROR: Parent '$p_image:$p_version' is NOT using an specific version"
109 fail_validation=1
110 fi
111
112 elif [[ "$df_parent" =~ ^FROM\ scratch$ ]]
113 then
114 # Handle the parent-less `FROM scratch` case:
115 # https://docs.docker.com/develop/develop-images/baseimages/
116 echo " OK: Using the versionless 'scratch' parent: '$df_parent'"
117 else
118 echo " ERROR: Couldn't find a parent image in $df_parent"
119 fi
120
121 done
122
123 done < <( find "${WORKSPACE}" -name 'Dockerfile*' -print0 )
124}
125
Zack Williams12783ac2018-06-12 15:13:12 -0700126echo "Checking git repo with remotes:"
127git remote -v
128
129echo "Branches:"
130git branch -v
131
132echo "Existing git tags:"
133git tag -n
134
135read_version
Zack Williams8e69efd2018-06-13 15:05:18 -0700136check_if_releaseversion
Zack Williams12783ac2018-06-12 15:13:12 -0700137
Zack Williams8e69efd2018-06-13 15:05:18 -0700138# perform checks if a released version
139if [ "$releaseversion" -eq "1" ]
140then
141 is_git_tag_duplicated
142 dockerfile_parentcheck
143fi
Zack Williams12783ac2018-06-12 15:13:12 -0700144
Zack Williams8e69efd2018-06-13 15:05:18 -0700145exit $fail_validation