blob: 669aeec159190223f16cb8f93d161a3a15218892 [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"
Zack Williams0dc27542018-10-11 08:09:10 -0700104 elif [[ "${p_version}" =~ ^.*([0-9]+)\.([0-9]+).*$ ]]
Zack Williams8e69efd2018-06-13 15:05:18 -0700105 then
Zack Williams0dc27542018-10-11 08:09:10 -0700106 # handle non-SemVer versions that have a Major.Minor version specifier in the name
107 # 'ubuntu:16.04'
108 # 'postgres:10.3-alpine'
109 # 'openjdk:8-jre-alpine3.8'
Zack Williams8e69efd2018-06-13 15:05:18 -0700110 echo " OK: Parent '$p_image:$p_version' is using a non-SemVer, but sufficient, version"
111 else
112 echo " ERROR: Parent '$p_image:$p_version' is NOT using an specific version"
113 fail_validation=1
114 fi
115
116 elif [[ "$df_parent" =~ ^FROM\ scratch$ ]]
117 then
118 # Handle the parent-less `FROM scratch` case:
119 # https://docs.docker.com/develop/develop-images/baseimages/
120 echo " OK: Using the versionless 'scratch' parent: '$df_parent'"
121 else
122 echo " ERROR: Couldn't find a parent image in $df_parent"
123 fi
124
125 done
126
Zack Williams4a6af0f2019-05-13 08:38:32 -0700127 done < <( find "${WORKSPACE}" -name 'Dockerfile*' ! -path "*/vendor/*" -print0 )
Zack Williams12783ac2018-06-12 15:13:12 -0700128}
129
Zack Williams8e69efd2018-06-13 15:05:18 -0700130
Zack Williams12783ac2018-06-12 15:13:12 -0700131# create a git tag
132function create_git_tag {
133 echo "Creating git tag: $NEW_VERSION"
134 git checkout "$GERRIT_PATCHSET_REVISION"
Zack Williams8e69efd2018-06-13 15:05:18 -0700135
136 git config --global user.email "do-not-reply@opencord.org"
137 git config --global user.name "Jenkins"
138
Zack Williams12783ac2018-06-12 15:13:12 -0700139 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 -0700140
141 echo "Tags including new tag:"
142 git tag -n
143
Zack Williams12783ac2018-06-12 15:13:12 -0700144 git push origin "$NEW_VERSION"
145}
146
147echo "Checking git repo with remotes:"
148git remote -v
149
150echo "Branches:"
151git branch -v
152
153echo "Existing git tags:"
154git tag -n
155
156read_version
Zack Williams8e69efd2018-06-13 15:05:18 -0700157check_if_releaseversion
Zack Williams12783ac2018-06-12 15:13:12 -0700158
Zack Williams8e69efd2018-06-13 15:05:18 -0700159# perform checks if a released version
160if [ "$releaseversion" -eq "1" ]
Zack Williams12783ac2018-06-12 15:13:12 -0700161then
Zack Williams8e69efd2018-06-13 15:05:18 -0700162 is_git_tag_duplicated
163 dockerfile_parentcheck
164
165 if [ "$fail_validation" -eq "0" ]
166 then
167 create_git_tag
168 else
169 echo "ERROR: commit merged but failed validation, not tagging!"
170 fi
Zack Williams12783ac2018-06-12 15:13:12 -0700171fi
172
Zack Williams8e69efd2018-06-13 15:05:18 -0700173exit $fail_validation