blob: 86fe3176d298edbccae03dafc68825db1d63b746 [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 Williams6a9d2e62018-06-22 15:18:23 -070038 elif [ -f "package.json" ]
39 then
40 NEW_VERSION=$(python -c 'import json,sys;obj=json.load(sys.stdin); print obj["version"]' < package.json)
41 VERSIONFILE="package.json"
Zack Williams12783ac2018-06-12 15:13:12 -070042 else
43 echo "ERROR: No versioning file found!"
44 exit 1
45 fi
46}
47
48# check if the version is already a tag in git
49function is_git_tag_duplicated {
50 for existing_tag in $(git tag)
51 do
52 if [ "$NEW_VERSION" = "$existing_tag" ]
53 then
54 echo "ERROR: Duplicate tag: $existing_tag"
55 exit 2
56 fi
57 done
58}
59
Zack Williams8e69efd2018-06-13 15:05:18 -070060# check if the version is a released version
61function check_if_releaseversion {
62 if [[ "$NEW_VERSION" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]
63 then
64 echo "Version string '$NEW_VERSION' found in '$VERSIONFILE' is a SemVer released version!"
65 releaseversion=1
66 else
67 echo "Version string '$NEW_VERSION' found in '$VERSIONFILE' is not a SemVer released version, skipping."
68 fi
69}
70
71# check if Dockerfiles have a released version as their parent
72function dockerfile_parentcheck {
73 while IFS= read -r -d '' dockerfile
74 do
75 echo "Checking dockerfile: '$dockerfile'"
76
77 # split on newlines
78 IFS=$'\n'
79 df_parents=($(grep "^FROM" "$dockerfile"))
80
81 # check all parents in the Dockerfile
82 for df_parent in "${df_parents[@]}"
83 do
84
85 df_pattern="FROM (.*):(.*)"
86 if [[ "$df_parent" =~ $df_pattern ]]
87 then
88
89 p_image="${BASH_REMATCH[1]}"
90 p_version="${BASH_REMATCH[2]}"
91
92 if [[ "${p_version}" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]
93 then
94 echo " OK: Parent '$p_image:$p_version' is a released SemVer version"
95 elif [[ "${p_version}" =~ ^([0-9]+)\.([0-9]+).*$ ]]
96 then
97 # handle the non-SemVer 'ubuntu:16.04' and 'postgres:10.3-alpine' cases
98 echo " OK: Parent '$p_image:$p_version' is using a non-SemVer, but sufficient, version"
99 else
100 echo " ERROR: Parent '$p_image:$p_version' is NOT using an specific version"
101 fail_validation=1
102 fi
103
104 elif [[ "$df_parent" =~ ^FROM\ scratch$ ]]
105 then
106 # Handle the parent-less `FROM scratch` case:
107 # https://docs.docker.com/develop/develop-images/baseimages/
108 echo " OK: Using the versionless 'scratch' parent: '$df_parent'"
109 else
110 echo " ERROR: Couldn't find a parent image in $df_parent"
111 fi
112
113 done
114
115 done < <( find "${WORKSPACE}" -name 'Dockerfile*' -print0 )
116}
117
Zack Williams12783ac2018-06-12 15:13:12 -0700118echo "Checking git repo with remotes:"
119git remote -v
120
121echo "Branches:"
122git branch -v
123
124echo "Existing git tags:"
125git tag -n
126
127read_version
Zack Williams8e69efd2018-06-13 15:05:18 -0700128check_if_releaseversion
Zack Williams12783ac2018-06-12 15:13:12 -0700129
Zack Williams8e69efd2018-06-13 15:05:18 -0700130# perform checks if a released version
131if [ "$releaseversion" -eq "1" ]
132then
133 is_git_tag_duplicated
134 dockerfile_parentcheck
135fi
Zack Williams12783ac2018-06-12 15:13:12 -0700136
Zack Williams8e69efd2018-06-13 15:05:18 -0700137exit $fail_validation