blob: 49d6e2130fccfeb748ddbb8a3cdbd46e25958da6 [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 Williams866ef3c2019-09-27 15:41:02 -070044 elif [ -f "pom.xml" ]
45 then
46 NEW_VERSION=$(xmllint --xpath '/*[local-name()="project"]/*[local-name()="version"]/text()' pom.xml)
47 VERSIONFILE="pom.xml"
Zack Williams12783ac2018-06-12 15:13:12 -070048 else
49 echo "ERROR: No versioning file found!"
50 exit 1
51 fi
52}
53
54# check if the version is already a tag in git
55function is_git_tag_duplicated {
56 for existing_tag in $(git tag)
57 do
58 if [ "$NEW_VERSION" = "$existing_tag" ]
59 then
60 echo "ERROR: Duplicate tag: $existing_tag"
61 exit 2
62 fi
63 done
64}
65
Zack Williams8e69efd2018-06-13 15:05:18 -070066# check if the version is a released version
67function check_if_releaseversion {
68 if [[ "$NEW_VERSION" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]
69 then
70 echo "Version string '$NEW_VERSION' found in '$VERSIONFILE' is a SemVer released version!"
71 releaseversion=1
72 else
Zack Williams66500002018-09-06 15:29:05 -070073 if [ "$SEMVER_STRICT" -eq "1" ]
74 then
75 echo "Version string '$NEW_VERSION' in '$VERSIONFILE' is not a SemVer released version, SEMVER_STRICT enabled, failing!"
76 fail_validation=1
77 else
78 echo "Version string '$NEW_VERSION' in '$VERSIONFILE' is not a SemVer released version, skipping."
79 fi
Zack Williams8e69efd2018-06-13 15:05:18 -070080 fi
81}
82
83# check if Dockerfiles have a released version as their parent
84function dockerfile_parentcheck {
85 while IFS= read -r -d '' dockerfile
86 do
87 echo "Checking dockerfile: '$dockerfile'"
88
89 # split on newlines
90 IFS=$'\n'
91 df_parents=($(grep "^FROM" "$dockerfile"))
92
93 # check all parents in the Dockerfile
94 for df_parent in "${df_parents[@]}"
95 do
96
97 df_pattern="FROM (.*):(.*)"
98 if [[ "$df_parent" =~ $df_pattern ]]
99 then
100
101 p_image="${BASH_REMATCH[1]}"
102 p_version="${BASH_REMATCH[2]}"
103
104 if [[ "${p_version}" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]
105 then
106 echo " OK: Parent '$p_image:$p_version' is a released SemVer version"
Zack Williams0dc27542018-10-11 08:09:10 -0700107 elif [[ "${p_version}" =~ ^.*([0-9]+)\.([0-9]+).*$ ]]
Zack Williams8e69efd2018-06-13 15:05:18 -0700108 then
Zack Williams0dc27542018-10-11 08:09:10 -0700109 # handle non-SemVer versions that have a Major.Minor version specifier in the name
110 # 'ubuntu:16.04'
111 # 'postgres:10.3-alpine'
112 # 'openjdk:8-jre-alpine3.8'
Zack Williams8e69efd2018-06-13 15:05:18 -0700113 echo " OK: Parent '$p_image:$p_version' is using a non-SemVer, but sufficient, version"
114 else
115 echo " ERROR: Parent '$p_image:$p_version' is NOT using an specific version"
116 fail_validation=1
117 fi
118
119 elif [[ "$df_parent" =~ ^FROM\ scratch$ ]]
120 then
121 # Handle the parent-less `FROM scratch` case:
122 # https://docs.docker.com/develop/develop-images/baseimages/
123 echo " OK: Using the versionless 'scratch' parent: '$df_parent'"
124 else
125 echo " ERROR: Couldn't find a parent image in $df_parent"
126 fi
127
128 done
129
Zack Williams4a6af0f2019-05-13 08:38:32 -0700130 done < <( find "${WORKSPACE}" -name 'Dockerfile*' ! -path "*/vendor/*" -print0 )
Zack Williams8e69efd2018-06-13 15:05:18 -0700131}
132
Zack Williams12783ac2018-06-12 15:13:12 -0700133echo "Checking git repo with remotes:"
134git remote -v
135
136echo "Branches:"
137git branch -v
138
139echo "Existing git tags:"
140git tag -n
141
142read_version
Zack Williams8e69efd2018-06-13 15:05:18 -0700143check_if_releaseversion
Zack Williams12783ac2018-06-12 15:13:12 -0700144
Zack Williams8e69efd2018-06-13 15:05:18 -0700145# perform checks if a released version
146if [ "$releaseversion" -eq "1" ]
147then
148 is_git_tag_duplicated
149 dockerfile_parentcheck
150fi
Zack Williams12783ac2018-06-12 15:13:12 -0700151
Zack Williams8e69efd2018-06-13 15:05:18 -0700152exit $fail_validation