blob: 7a2307a0d17dfca0e169ba3cfbdc0c654df2f208 [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"
Zack Williams0dc27542018-10-11 08:09:10 -0700103 elif [[ "${p_version}" =~ ^.*([0-9]+)\.([0-9]+).*$ ]]
Zack Williams8e69efd2018-06-13 15:05:18 -0700104 then
Zack Williams0dc27542018-10-11 08:09:10 -0700105 # 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 Williams8e69efd2018-06-13 15:05:18 -0700109 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
126 done < <( find "${WORKSPACE}" -name 'Dockerfile*' -print0 )
127}
128
Zack Williams12783ac2018-06-12 15:13:12 -0700129echo "Checking git repo with remotes:"
130git remote -v
131
132echo "Branches:"
133git branch -v
134
135echo "Existing git tags:"
136git tag -n
137
138read_version
Zack Williams8e69efd2018-06-13 15:05:18 -0700139check_if_releaseversion
Zack Williams12783ac2018-06-12 15:13:12 -0700140
Zack Williams8e69efd2018-06-13 15:05:18 -0700141# perform checks if a released version
142if [ "$releaseversion" -eq "1" ]
143then
144 is_git_tag_duplicated
145 dockerfile_parentcheck
146fi
Zack Williams12783ac2018-06-12 15:13:12 -0700147
Zack Williams8e69efd2018-06-13 15:05:18 -0700148exit $fail_validation