blob: 60042d874e0dec084042fb796cb318b91bb1cfd1 [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# versiontag.sh
18# Tags a git commit with the SemVer version discovered within the commit,
19# if the tag doesn't already exist. Ignore non-SemVer commits.
20
21set -eu -o pipefail
22
Zack Williams8e69efd2018-06-13 15:05:18 -070023VERSIONFILE="" # file path to file containing version number
24NEW_VERSION="" # version number found in $VERSIONFILE
Zack Williams12783ac2018-06-12 15:13:12 -070025
Zack Williams66500002018-09-06 15:29:05 -070026SEMVER_STRICT=${SEMVER_STRICT:-0} # require semver versions
27
Zack Williams12783ac2018-06-12 15:13:12 -070028releaseversion=0
Zack Williams8e69efd2018-06-13 15:05:18 -070029fail_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")
40 VERSIONFILE="VERSION"
Zack Williams6a9d2e62018-06-22 15:18:23 -070041 elif [ -f "package.json" ]
42 then
43 NEW_VERSION=$(python -c 'import json,sys;obj=json.load(sys.stdin); print obj["version"]' < package.json)
44 VERSIONFILE="package.json"
Zack Williams12783ac2018-06-12 15:13:12 -070045 else
46 echo "ERROR: No versioning file found!"
47 exit 1
48 fi
49}
50
Zack Williams8e69efd2018-06-13 15:05:18 -070051# check if the version is a released version
52function check_if_releaseversion {
53 if [[ "$NEW_VERSION" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]
54 then
55 echo "Version string '$NEW_VERSION' in '$VERSIONFILE' is a SemVer released version!"
56 releaseversion=1
57 else
Zack Williams66500002018-09-06 15:29:05 -070058 if [ "$SEMVER_STRICT" -eq "1" ]
59 then
60 echo "Version string '$NEW_VERSION' in '$VERSIONFILE' is not a SemVer released version, SEMVER_STRICT enabled, failing!"
61 fail_validation=1
62 else
63 echo "Version string '$NEW_VERSION' in '$VERSIONFILE' is not a SemVer released version, skipping."
64 fi
Zack Williams8e69efd2018-06-13 15:05:18 -070065 fi
66}
67
Zack Williams12783ac2018-06-12 15:13:12 -070068# check if the version is already a tag in git
69function is_git_tag_duplicated {
70 for existing_tag in $(git tag)
71 do
72 if [ "$NEW_VERSION" = "$existing_tag" ]
73 then
74 echo "ERROR: Duplicate tag: $existing_tag"
75 exit 2
76 fi
77 done
78}
79
Zack Williams8e69efd2018-06-13 15:05:18 -070080# check if Dockerfiles have a released version as their parent
81function dockerfile_parentcheck {
82 while IFS= read -r -d '' dockerfile
83 do
84 echo "Checking dockerfile: '$dockerfile'"
85
86 # split on newlines
87 IFS=$'\n'
88 df_parents=($(grep "^FROM" "$dockerfile"))
89
90 # check all parents in the Dockerfile
91 for df_parent in "${df_parents[@]}"
92 do
93
94 df_pattern="FROM (.*):(.*)"
95 if [[ "$df_parent" =~ $df_pattern ]]
96 then
97
98 p_image="${BASH_REMATCH[1]}"
99 p_version="${BASH_REMATCH[2]}"
100
101 if [[ "${p_version}" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]
102 then
103 echo " OK: Parent '$p_image:$p_version' is a released SemVer version"
104 elif [[ "${p_version}" =~ ^([0-9]+)\.([0-9]+).*$ ]]
105 then
106 # handle the non-SemVer 'ubuntu:16.04' and 'postgres:10.3-alpine' cases
107 echo " OK: Parent '$p_image:$p_version' is using a non-SemVer, but sufficient, version"
108 else
109 echo " ERROR: Parent '$p_image:$p_version' is NOT using an specific version"
110 fail_validation=1
111 fi
112
113 elif [[ "$df_parent" =~ ^FROM\ scratch$ ]]
114 then
115 # Handle the parent-less `FROM scratch` case:
116 # https://docs.docker.com/develop/develop-images/baseimages/
117 echo " OK: Using the versionless 'scratch' parent: '$df_parent'"
118 else
119 echo " ERROR: Couldn't find a parent image in $df_parent"
120 fi
121
122 done
123
124 done < <( find "${WORKSPACE}" -name 'Dockerfile*' -print0 )
Zack Williams12783ac2018-06-12 15:13:12 -0700125}
126
Zack Williams8e69efd2018-06-13 15:05:18 -0700127
Zack Williams12783ac2018-06-12 15:13:12 -0700128# create a git tag
129function create_git_tag {
130 echo "Creating git tag: $NEW_VERSION"
131 git checkout "$GERRIT_PATCHSET_REVISION"
Zack Williams8e69efd2018-06-13 15:05:18 -0700132
133 git config --global user.email "do-not-reply@opencord.org"
134 git config --global user.name "Jenkins"
135
Zack Williams12783ac2018-06-12 15:13:12 -0700136 git tag -a "$NEW_VERSION" -m "Tagged by CORD Jenkins version-tag job: $BUILD_NUMBER, for Gerrit patchset: $GERRIT_CHANGE_NUMBER"
Zack Williams8e69efd2018-06-13 15:05:18 -0700137
138 echo "Tags including new tag:"
139 git tag -n
140
Zack Williams12783ac2018-06-12 15:13:12 -0700141 git push origin "$NEW_VERSION"
142}
143
144echo "Checking git repo with remotes:"
145git remote -v
146
147echo "Branches:"
148git branch -v
149
150echo "Existing git tags:"
151git tag -n
152
153read_version
Zack Williams8e69efd2018-06-13 15:05:18 -0700154check_if_releaseversion
Zack Williams12783ac2018-06-12 15:13:12 -0700155
Zack Williams8e69efd2018-06-13 15:05:18 -0700156# perform checks if a released version
157if [ "$releaseversion" -eq "1" ]
Zack Williams12783ac2018-06-12 15:13:12 -0700158then
Zack Williams8e69efd2018-06-13 15:05:18 -0700159 is_git_tag_duplicated
160 dockerfile_parentcheck
161
162 if [ "$fail_validation" -eq "0" ]
163 then
164 create_git_tag
165 else
166 echo "ERROR: commit merged but failed validation, not tagging!"
167 fi
Zack Williams12783ac2018-06-12 15:13:12 -0700168fi
169
Zack Williams8e69efd2018-06-13 15:05:18 -0700170exit $fail_validation