blob: a75087bbbb0b536786dca3be2e6c0578bb619cc0 [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 Williams866ef3c2019-09-27 15:41:02 -070045 elif [ -f "pom.xml" ]
46 then
47 NEW_VERSION=$(xmllint --xpath '/*[local-name()="project"]/*[local-name()="version"]/text()' pom.xml)
48 VERSIONFILE="pom.xml"
Zack Williams12783ac2018-06-12 15:13:12 -070049 else
50 echo "ERROR: No versioning file found!"
51 exit 1
52 fi
53}
54
Zack Williams8e69efd2018-06-13 15:05:18 -070055# check if the version is a released version
56function check_if_releaseversion {
57 if [[ "$NEW_VERSION" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]
58 then
59 echo "Version string '$NEW_VERSION' in '$VERSIONFILE' is a SemVer released version!"
60 releaseversion=1
61 else
Zack Williams66500002018-09-06 15:29:05 -070062 if [ "$SEMVER_STRICT" -eq "1" ]
63 then
64 echo "Version string '$NEW_VERSION' in '$VERSIONFILE' is not a SemVer released version, SEMVER_STRICT enabled, failing!"
65 fail_validation=1
66 else
67 echo "Version string '$NEW_VERSION' in '$VERSIONFILE' is not a SemVer released version, skipping."
68 fi
Zack Williams8e69efd2018-06-13 15:05:18 -070069 fi
70}
71
Zack Williams12783ac2018-06-12 15:13:12 -070072# check if the version is already a tag in git
73function is_git_tag_duplicated {
74 for existing_tag in $(git tag)
75 do
76 if [ "$NEW_VERSION" = "$existing_tag" ]
77 then
78 echo "ERROR: Duplicate tag: $existing_tag"
79 exit 2
80 fi
81 done
82}
83
Zack Williams8e69efd2018-06-13 15:05:18 -070084# check if Dockerfiles have a released version as their parent
85function dockerfile_parentcheck {
86 while IFS= read -r -d '' dockerfile
87 do
88 echo "Checking dockerfile: '$dockerfile'"
89
90 # split on newlines
91 IFS=$'\n'
92 df_parents=($(grep "^FROM" "$dockerfile"))
93
94 # check all parents in the Dockerfile
95 for df_parent in "${df_parents[@]}"
96 do
97
98 df_pattern="FROM (.*):(.*)"
99 if [[ "$df_parent" =~ $df_pattern ]]
100 then
101
102 p_image="${BASH_REMATCH[1]}"
103 p_version="${BASH_REMATCH[2]}"
104
105 if [[ "${p_version}" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]
106 then
107 echo " OK: Parent '$p_image:$p_version' is a released SemVer version"
Zack Williams0dc27542018-10-11 08:09:10 -0700108 elif [[ "${p_version}" =~ ^.*([0-9]+)\.([0-9]+).*$ ]]
Zack Williams8e69efd2018-06-13 15:05:18 -0700109 then
Zack Williams0dc27542018-10-11 08:09:10 -0700110 # handle non-SemVer versions that have a Major.Minor version specifier in the name
111 # 'ubuntu:16.04'
112 # 'postgres:10.3-alpine'
113 # 'openjdk:8-jre-alpine3.8'
Zack Williams8e69efd2018-06-13 15:05:18 -0700114 echo " OK: Parent '$p_image:$p_version' is using a non-SemVer, but sufficient, version"
115 else
116 echo " ERROR: Parent '$p_image:$p_version' is NOT using an specific version"
117 fail_validation=1
118 fi
119
120 elif [[ "$df_parent" =~ ^FROM\ scratch$ ]]
121 then
122 # Handle the parent-less `FROM scratch` case:
123 # https://docs.docker.com/develop/develop-images/baseimages/
124 echo " OK: Using the versionless 'scratch' parent: '$df_parent'"
125 else
126 echo " ERROR: Couldn't find a parent image in $df_parent"
127 fi
128
129 done
130
Zack Williams4a6af0f2019-05-13 08:38:32 -0700131 done < <( find "${WORKSPACE}" -name 'Dockerfile*' ! -path "*/vendor/*" -print0 )
Zack Williams12783ac2018-06-12 15:13:12 -0700132}
133
Zack Williams8e69efd2018-06-13 15:05:18 -0700134
Zack Williams12783ac2018-06-12 15:13:12 -0700135# create a git tag
136function create_git_tag {
137 echo "Creating git tag: $NEW_VERSION"
138 git checkout "$GERRIT_PATCHSET_REVISION"
Zack Williams8e69efd2018-06-13 15:05:18 -0700139
140 git config --global user.email "do-not-reply@opencord.org"
141 git config --global user.name "Jenkins"
142
Zack Williams12783ac2018-06-12 15:13:12 -0700143 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 -0700144
145 echo "Tags including new tag:"
146 git tag -n
147
Zack Williams12783ac2018-06-12 15:13:12 -0700148 git push origin "$NEW_VERSION"
149}
150
151echo "Checking git repo with remotes:"
152git remote -v
153
154echo "Branches:"
155git branch -v
156
157echo "Existing git tags:"
158git tag -n
159
160read_version
Zack Williams8e69efd2018-06-13 15:05:18 -0700161check_if_releaseversion
Zack Williams12783ac2018-06-12 15:13:12 -0700162
Zack Williams8e69efd2018-06-13 15:05:18 -0700163# perform checks if a released version
164if [ "$releaseversion" -eq "1" ]
Zack Williams12783ac2018-06-12 15:13:12 -0700165then
Zack Williams8e69efd2018-06-13 15:05:18 -0700166 is_git_tag_duplicated
167 dockerfile_parentcheck
168
169 if [ "$fail_validation" -eq "0" ]
170 then
171 create_git_tag
172 else
173 echo "ERROR: commit merged but failed validation, not tagging!"
174 fi
Zack Williams12783ac2018-06-12 15:13:12 -0700175fi
176
Zack Williams8e69efd2018-06-13 15:05:18 -0700177exit $fail_validation