blob: 81ffaeb6fa44687b1e84de29357a34308e5307f1 [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
25releaseversion=0
26fail_validation=0
27
28# when not running under Jenkins, use current dir as workspace
29WORKSPACE=${WORKSPACE:-.}
Zack Williams12783ac2018-06-12 15:13:12 -070030
31# find the version string in the repo, read into NEW_VERSION
32# Add additional places NEW_VERSION could be found to this function
33function read_version {
34 if [ -f "VERSION" ]
35 then
36 NEW_VERSION=$(head -n1 "VERSION")
Zack Williams8e69efd2018-06-13 15:05:18 -070037 VERSIONFILE="VERSION"
Zack Williams12783ac2018-06-12 15:13:12 -070038 else
39 echo "ERROR: No versioning file found!"
40 exit 1
41 fi
42}
43
44# check if the version is already a tag in git
45function is_git_tag_duplicated {
46 for existing_tag in $(git tag)
47 do
48 if [ "$NEW_VERSION" = "$existing_tag" ]
49 then
50 echo "ERROR: Duplicate tag: $existing_tag"
51 exit 2
52 fi
53 done
54}
55
Zack Williams8e69efd2018-06-13 15:05:18 -070056# check if the version is a released version
57function check_if_releaseversion {
58 if [[ "$NEW_VERSION" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]
59 then
60 echo "Version string '$NEW_VERSION' found in '$VERSIONFILE' is a SemVer released version!"
61 releaseversion=1
62 else
63 echo "Version string '$NEW_VERSION' found in '$VERSIONFILE' is not a SemVer released version, skipping."
64 fi
65}
66
67# check if Dockerfiles have a released version as their parent
68function dockerfile_parentcheck {
69 while IFS= read -r -d '' dockerfile
70 do
71 echo "Checking dockerfile: '$dockerfile'"
72
73 # split on newlines
74 IFS=$'\n'
75 df_parents=($(grep "^FROM" "$dockerfile"))
76
77 # check all parents in the Dockerfile
78 for df_parent in "${df_parents[@]}"
79 do
80
81 df_pattern="FROM (.*):(.*)"
82 if [[ "$df_parent" =~ $df_pattern ]]
83 then
84
85 p_image="${BASH_REMATCH[1]}"
86 p_version="${BASH_REMATCH[2]}"
87
88 if [[ "${p_version}" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]
89 then
90 echo " OK: Parent '$p_image:$p_version' is a released SemVer version"
91 elif [[ "${p_version}" =~ ^([0-9]+)\.([0-9]+).*$ ]]
92 then
93 # handle the non-SemVer 'ubuntu:16.04' and 'postgres:10.3-alpine' cases
94 echo " OK: Parent '$p_image:$p_version' is using a non-SemVer, but sufficient, version"
95 else
96 echo " ERROR: Parent '$p_image:$p_version' is NOT using an specific version"
97 fail_validation=1
98 fi
99
100 elif [[ "$df_parent" =~ ^FROM\ scratch$ ]]
101 then
102 # Handle the parent-less `FROM scratch` case:
103 # https://docs.docker.com/develop/develop-images/baseimages/
104 echo " OK: Using the versionless 'scratch' parent: '$df_parent'"
105 else
106 echo " ERROR: Couldn't find a parent image in $df_parent"
107 fi
108
109 done
110
111 done < <( find "${WORKSPACE}" -name 'Dockerfile*' -print0 )
112}
113
Zack Williams12783ac2018-06-12 15:13:12 -0700114echo "Checking git repo with remotes:"
115git remote -v
116
117echo "Branches:"
118git branch -v
119
120echo "Existing git tags:"
121git tag -n
122
123read_version
Zack Williams8e69efd2018-06-13 15:05:18 -0700124check_if_releaseversion
Zack Williams12783ac2018-06-12 15:13:12 -0700125
Zack Williams8e69efd2018-06-13 15:05:18 -0700126# perform checks if a released version
127if [ "$releaseversion" -eq "1" ]
128then
129 is_git_tag_duplicated
130 dockerfile_parentcheck
131fi
Zack Williams12783ac2018-06-12 15:13:12 -0700132
Zack Williams8e69efd2018-06-13 15:05:18 -0700133exit $fail_validation