blob: 41971ce5127fd8f7788c4f32e9ea2ab6d6687666 [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 Williams6e070f52019-10-04 11:08:59 -070025TAG_VERSION="" # version file that might have a leading v to work around go mod funkyness
Zack Williams12783ac2018-06-12 15:13:12 -070026
Zack Williams66500002018-09-06 15:29:05 -070027SEMVER_STRICT=${SEMVER_STRICT:-0} # require semver versions
28
Zack Williams12783ac2018-06-12 15:13:12 -070029releaseversion=0
Zack Williams8e69efd2018-06-13 15:05:18 -070030fail_validation=0
31
32# when not running under Jenkins, use current dir as workspace
33WORKSPACE=${WORKSPACE:-.}
Zack Williams12783ac2018-06-12 15:13:12 -070034
35# find the version string in the repo, read into NEW_VERSION
36# Add additional places NEW_VERSION could be found to this function
37function read_version {
38 if [ -f "VERSION" ]
39 then
40 NEW_VERSION=$(head -n1 "VERSION")
41 VERSIONFILE="VERSION"
Zack Williams6e070f52019-10-04 11:08:59 -070042
43 # If this is a golang project, use funky v-prefixed versions
44 if [ -f "Gopkg.toml" ] || [ -f "go.mod" ]
45 then
46 echo "go-based project found, using v-prefixed version for git tags: v${NEW_VERSION}"
47 TAG_VERSION=v${NEW_VERSION}
48 else
49 TAG_VERSION=${NEW_VERSION}
50 fi
51
Zack Williams6a9d2e62018-06-22 15:18:23 -070052 elif [ -f "package.json" ]
53 then
54 NEW_VERSION=$(python -c 'import json,sys;obj=json.load(sys.stdin); print obj["version"]' < package.json)
Zack Williams6e070f52019-10-04 11:08:59 -070055 TAG_VERSION=$NEW_VERSION
Zack Williams6a9d2e62018-06-22 15:18:23 -070056 VERSIONFILE="package.json"
Zack Williams866ef3c2019-09-27 15:41:02 -070057 elif [ -f "pom.xml" ]
58 then
59 NEW_VERSION=$(xmllint --xpath '/*[local-name()="project"]/*[local-name()="version"]/text()' pom.xml)
Zack Williams6e070f52019-10-04 11:08:59 -070060 TAG_VERSION=$NEW_VERSION
Zack Williams866ef3c2019-09-27 15:41:02 -070061 VERSIONFILE="pom.xml"
Zack Williams12783ac2018-06-12 15:13:12 -070062 else
63 echo "ERROR: No versioning file found!"
64 exit 1
65 fi
66}
67
Zack Williams8e69efd2018-06-13 15:05:18 -070068# check if the version is a released version
69function check_if_releaseversion {
70 if [[ "$NEW_VERSION" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]
71 then
72 echo "Version string '$NEW_VERSION' in '$VERSIONFILE' is a SemVer released version!"
73 releaseversion=1
74 else
Zack Williams66500002018-09-06 15:29:05 -070075 if [ "$SEMVER_STRICT" -eq "1" ]
76 then
77 echo "Version string '$NEW_VERSION' in '$VERSIONFILE' is not a SemVer released version, SEMVER_STRICT enabled, failing!"
78 fail_validation=1
79 else
80 echo "Version string '$NEW_VERSION' in '$VERSIONFILE' is not a SemVer released version, skipping."
81 fi
Zack Williams8e69efd2018-06-13 15:05:18 -070082 fi
83}
84
Zack Williams12783ac2018-06-12 15:13:12 -070085# check if the version is already a tag in git
86function is_git_tag_duplicated {
87 for existing_tag in $(git tag)
88 do
Zack Williams6e070f52019-10-04 11:08:59 -070089 if [ "$TAG_VERSION" = "$existing_tag" ]
Zack Williams12783ac2018-06-12 15:13:12 -070090 then
91 echo "ERROR: Duplicate tag: $existing_tag"
92 exit 2
93 fi
94 done
95}
96
Zack Williams8e69efd2018-06-13 15:05:18 -070097# check if Dockerfiles have a released version as their parent
98function dockerfile_parentcheck {
99 while IFS= read -r -d '' dockerfile
100 do
101 echo "Checking dockerfile: '$dockerfile'"
102
103 # split on newlines
104 IFS=$'\n'
105 df_parents=($(grep "^FROM" "$dockerfile"))
106
107 # check all parents in the Dockerfile
108 for df_parent in "${df_parents[@]}"
109 do
110
111 df_pattern="FROM (.*):(.*)"
112 if [[ "$df_parent" =~ $df_pattern ]]
113 then
114
115 p_image="${BASH_REMATCH[1]}"
116 p_version="${BASH_REMATCH[2]}"
117
118 if [[ "${p_version}" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]
119 then
120 echo " OK: Parent '$p_image:$p_version' is a released SemVer version"
Zack Williams0dc27542018-10-11 08:09:10 -0700121 elif [[ "${p_version}" =~ ^.*([0-9]+)\.([0-9]+).*$ ]]
Zack Williams8e69efd2018-06-13 15:05:18 -0700122 then
Zack Williams0dc27542018-10-11 08:09:10 -0700123 # handle non-SemVer versions that have a Major.Minor version specifier in the name
124 # 'ubuntu:16.04'
125 # 'postgres:10.3-alpine'
126 # 'openjdk:8-jre-alpine3.8'
Zack Williams8e69efd2018-06-13 15:05:18 -0700127 echo " OK: Parent '$p_image:$p_version' is using a non-SemVer, but sufficient, version"
128 else
129 echo " ERROR: Parent '$p_image:$p_version' is NOT using an specific version"
130 fail_validation=1
131 fi
132
133 elif [[ "$df_parent" =~ ^FROM\ scratch$ ]]
134 then
135 # Handle the parent-less `FROM scratch` case:
136 # https://docs.docker.com/develop/develop-images/baseimages/
137 echo " OK: Using the versionless 'scratch' parent: '$df_parent'"
138 else
139 echo " ERROR: Couldn't find a parent image in $df_parent"
140 fi
141
142 done
143
Matteo Scandolob28a02e2019-11-04 09:35:16 -0800144 done < <( find "${WORKSPACE}" -name 'Dockerfile*' ! -path "*/vendor/*" ! -name "*dockerignore" -print0 )
Zack Williams12783ac2018-06-12 15:13:12 -0700145}
146
Zack Williams8e69efd2018-06-13 15:05:18 -0700147
Zack Williams12783ac2018-06-12 15:13:12 -0700148# create a git tag
149function create_git_tag {
Zack Williams6e070f52019-10-04 11:08:59 -0700150 echo "Creating git tag: $TAG_VERSION"
Zack Williams12783ac2018-06-12 15:13:12 -0700151 git checkout "$GERRIT_PATCHSET_REVISION"
Zack Williams8e69efd2018-06-13 15:05:18 -0700152
153 git config --global user.email "do-not-reply@opencord.org"
154 git config --global user.name "Jenkins"
155
Zack Williams6e070f52019-10-04 11:08:59 -0700156 git tag -a "$TAG_VERSION" -m "Tagged by CORD Jenkins version-tag job: $BUILD_NUMBER, for Gerrit patchset: $GERRIT_CHANGE_NUMBER"
Zack Williams8e69efd2018-06-13 15:05:18 -0700157
158 echo "Tags including new tag:"
159 git tag -n
160
Zack Williams6e070f52019-10-04 11:08:59 -0700161 git push origin "$TAG_VERSION"
Zack Williams12783ac2018-06-12 15:13:12 -0700162}
163
164echo "Checking git repo with remotes:"
165git remote -v
166
167echo "Branches:"
168git branch -v
169
170echo "Existing git tags:"
171git tag -n
172
173read_version
Zack Williams8e69efd2018-06-13 15:05:18 -0700174check_if_releaseversion
Zack Williams12783ac2018-06-12 15:13:12 -0700175
Zack Williams8e69efd2018-06-13 15:05:18 -0700176# perform checks if a released version
177if [ "$releaseversion" -eq "1" ]
Zack Williams12783ac2018-06-12 15:13:12 -0700178then
Zack Williams8e69efd2018-06-13 15:05:18 -0700179 is_git_tag_duplicated
180 dockerfile_parentcheck
181
182 if [ "$fail_validation" -eq "0" ]
183 then
184 create_git_tag
185 else
186 echo "ERROR: commit merged but failed validation, not tagging!"
187 fi
Zack Williams12783ac2018-06-12 15:13:12 -0700188fi
189
Zack Williams8e69efd2018-06-13 15:05:18 -0700190exit $fail_validation