blob: b72ff65502da17e0f0d2ae142b400bac13991c5b [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
Zack Williams6e070f52019-10-04 11:08:59 -070024TAG_VERSION="" # version file that might have a leading v to work around go mod funkyness
Zack Williams8e69efd2018-06-13 15:05:18 -070025
Zack Williams66500002018-09-06 15:29:05 -070026SEMVER_STRICT=${SEMVER_STRICT:-0} # require semver versions
27
Zack Williams8e69efd2018-06-13 15:05:18 -070028releaseversion=0
29fail_validation=0
30
31# when not running under Jenkins, use current dir as workspace
32WORKSPACE=${WORKSPACE:-.}
Zack Williams12783ac2018-06-12 15:13:12 -070033
34# find the version string in the repo, read into NEW_VERSION
35# Add additional places NEW_VERSION could be found to this function
36function read_version {
37 if [ -f "VERSION" ]
38 then
39 NEW_VERSION=$(head -n1 "VERSION")
Zack Williams8e69efd2018-06-13 15:05:18 -070040 VERSIONFILE="VERSION"
Zack Williams6e070f52019-10-04 11:08:59 -070041
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 Williams6a9d2e62018-06-22 15:18:23 -070051 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 Williams6e070f52019-10-04 11:08:59 -070054 TAG_VERSION=$NEW_VERSION
Zack Williams6a9d2e62018-06-22 15:18:23 -070055 VERSIONFILE="package.json"
Zack Williams866ef3c2019-09-27 15:41:02 -070056 elif [ -f "pom.xml" ]
57 then
58 NEW_VERSION=$(xmllint --xpath '/*[local-name()="project"]/*[local-name()="version"]/text()' pom.xml)
Zack Williams6e070f52019-10-04 11:08:59 -070059 TAG_VERSION=$NEW_VERSION
Zack Williams866ef3c2019-09-27 15:41:02 -070060 VERSIONFILE="pom.xml"
Zack Williams12783ac2018-06-12 15:13:12 -070061 else
62 echo "ERROR: No versioning file found!"
Matteo Scandolo93b531d2021-03-22 14:36:48 -070063 fail_validation=1
Zack Williams12783ac2018-06-12 15:13:12 -070064 fi
65}
66
Zack Williams8e69efd2018-06-13 15:05:18 -070067# check if the version is a released version
68function 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 Williams66500002018-09-06 15:29:05 -070074 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 Williams8e69efd2018-06-13 15:05:18 -070081 fi
82}
83
Zack Williams6e070f52019-10-04 11:08:59 -070084# check if the version is already a tag in git
85function is_git_tag_duplicated {
Matteo Scandolo93b531d2021-03-22 14:36:48 -070086 while IFS= read -r existing_tag
Zack Williams6e070f52019-10-04 11:08:59 -070087 do
88 if [ "$TAG_VERSION" = "$existing_tag" ]
89 then
90 echo "ERROR: Duplicate tag: $existing_tag"
Matteo Scandolo93b531d2021-03-22 14:36:48 -070091 fail_validation=2
Zack Williams6e070f52019-10-04 11:08:59 -070092 fi
Matteo Scandolo93b531d2021-03-22 14:36:48 -070093 done <<< $existing_tags
94}
95
96# from https://github.com/cloudflare/semver_bash/blob/master/semver.sh
97function 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)
110function 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"
122 while IFS= read -r existing_tag
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
128 done <<< $existing_tags
129
130 # if patch == 0, check that there was a release with MAJOR.MINOR-1.X
131 elif [[ "$PATCH" == 0 ]]; then
132 new_minor=$(( $MINOR - 1 ))
133 parent_version="$MAJOR.$new_minor.x"
134 while IFS= read -r existing_tag
135 do
136 semverParseInto $existing_tag C_MAJOR C_MINOR C_PATCH C_SPECIAL
137 if [[ "$new_minor" == "$C_MINOR" ]]; then
138 found_parent=true
139 fi
140 done <<< $existing_tags
141
142 # if patch != 0 check that there was a release with MAJOR.MINOR.PATCH-1
143 elif [[ "$PATCH" != 0 ]]; then
144 new_patch=$(( $PATCH - 1 ))
145 parent_version="$MAJOR.$MINOR.$new_patch"
146 while IFS= read -r existing_tag
147 do
148 if [[ "$parent_version" == "$existing_tag" ]]
149 then
150 found_parent=true
151 fi
152 done <<< $existing_tags
153 fi
154
155 # if we are the beginning the is no parent, but that's fine
156 if [[ "$MAJOR" == 0 ]]; then
157 found_parent=true
158 fi
159
160 if [[ $found_parent == false ]]; then
161 echo "Invalid $NEW_VERSION version. Expected parent version $parent_version does not exist."
162 fail_validation=1
163 fi
Zack Williams6e070f52019-10-04 11:08:59 -0700164}
165
Zack Williams8e69efd2018-06-13 15:05:18 -0700166# check if Dockerfiles have a released version as their parent
167function dockerfile_parentcheck {
168 while IFS= read -r -d '' dockerfile
169 do
170 echo "Checking dockerfile: '$dockerfile'"
171
172 # split on newlines
173 IFS=$'\n'
174 df_parents=($(grep "^FROM" "$dockerfile"))
175
176 # check all parents in the Dockerfile
177 for df_parent in "${df_parents[@]}"
178 do
179
Zack Williamsc800dbe2020-02-14 10:25:12 -0700180 df_pattern="FROM ([^:]*):(.*)"
Zack Williams8e69efd2018-06-13 15:05:18 -0700181 if [[ "$df_parent" =~ $df_pattern ]]
182 then
183
184 p_image="${BASH_REMATCH[1]}"
185 p_version="${BASH_REMATCH[2]}"
186
187 if [[ "${p_version}" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]
188 then
189 echo " OK: Parent '$p_image:$p_version' is a released SemVer version"
Zack Williamsc800dbe2020-02-14 10:25:12 -0700190 elif [[ "${p_version}" =~ ^.*@sha256:[0-9a-f]{64}.*$ ]]
191 then
192 # allow sha256 hashes to be used as version specifiers
193 echo " OK: Parent '$p_image:$p_version' is using a specific sha256 hash as a version"
Zack Williams0dc27542018-10-11 08:09:10 -0700194 elif [[ "${p_version}" =~ ^.*([0-9]+)\.([0-9]+).*$ ]]
Zack Williams8e69efd2018-06-13 15:05:18 -0700195 then
Zack Williams0dc27542018-10-11 08:09:10 -0700196 # handle non-SemVer versions that have a Major.Minor version specifier in the name
197 # 'ubuntu:16.04'
198 # 'postgres:10.3-alpine'
199 # 'openjdk:8-jre-alpine3.8'
Zack Williams8e69efd2018-06-13 15:05:18 -0700200 echo " OK: Parent '$p_image:$p_version' is using a non-SemVer, but sufficient, version"
201 else
202 echo " ERROR: Parent '$p_image:$p_version' is NOT using an specific version"
203 fail_validation=1
204 fi
205
206 elif [[ "$df_parent" =~ ^FROM\ scratch$ ]]
207 then
208 # Handle the parent-less `FROM scratch` case:
209 # https://docs.docker.com/develop/develop-images/baseimages/
210 echo " OK: Using the versionless 'scratch' parent: '$df_parent'"
211 else
212 echo " ERROR: Couldn't find a parent image in $df_parent"
213 fi
214
215 done
216
Matteo Scandolob28a02e2019-11-04 09:35:16 -0800217 done < <( find "${WORKSPACE}" -name 'Dockerfile*' ! -path "*/vendor/*" ! -name "*dockerignore" -print0 )
Zack Williams8e69efd2018-06-13 15:05:18 -0700218}
219
Zack Williams12783ac2018-06-12 15:13:12 -0700220echo "Checking git repo with remotes:"
221git remote -v
222
223echo "Branches:"
Matteo Scandolo93b531d2021-03-22 14:36:48 -0700224branches=$(git branch -v)
225echo $branches
Zack Williams12783ac2018-06-12 15:13:12 -0700226
227echo "Existing git tags:"
Matteo Scandolo93b531d2021-03-22 14:36:48 -0700228existing_tags=$(git tag)
229echo $existing_tags
Zack Williams12783ac2018-06-12 15:13:12 -0700230
231read_version
Matteo Scandolo93b531d2021-03-22 14:36:48 -0700232is_valid_version
Zack Williams8e69efd2018-06-13 15:05:18 -0700233check_if_releaseversion
Zack Williams12783ac2018-06-12 15:13:12 -0700234
Zack Williams8e69efd2018-06-13 15:05:18 -0700235# perform checks if a released version
236if [ "$releaseversion" -eq "1" ]
237then
238 is_git_tag_duplicated
239 dockerfile_parentcheck
240fi
Zack Williams12783ac2018-06-12 15:13:12 -0700241
Zack Williams8e69efd2018-06-13 15:05:18 -0700242exit $fail_validation