blob: d4091e66b629cd0ac1249e50961ef85ae4bedfec [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
Zack Williamsc800dbe2020-02-14 10:25:12 -0700111 df_pattern="FROM ([^:]*):(.*)"
Zack Williams8e69efd2018-06-13 15:05:18 -0700112 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 Williamsc800dbe2020-02-14 10:25:12 -0700121 elif [[ "${p_version}" =~ ^.*@sha256:[0-9a-f]{64}.*$ ]]
122 then
123 # allow sha256 hashes to be used as version specifiers
124 echo " OK: Parent '$p_image:$p_version' is using a specific sha256 hash as a version"
Zack Williams0dc27542018-10-11 08:09:10 -0700125 elif [[ "${p_version}" =~ ^.*([0-9]+)\.([0-9]+).*$ ]]
Zack Williams8e69efd2018-06-13 15:05:18 -0700126 then
Zack Williams0dc27542018-10-11 08:09:10 -0700127 # handle non-SemVer versions that have a Major.Minor version specifier in the name
128 # 'ubuntu:16.04'
129 # 'postgres:10.3-alpine'
130 # 'openjdk:8-jre-alpine3.8'
Zack Williams8e69efd2018-06-13 15:05:18 -0700131 echo " OK: Parent '$p_image:$p_version' is using a non-SemVer, but sufficient, version"
132 else
133 echo " ERROR: Parent '$p_image:$p_version' is NOT using an specific version"
134 fail_validation=1
135 fi
136
137 elif [[ "$df_parent" =~ ^FROM\ scratch$ ]]
138 then
139 # Handle the parent-less `FROM scratch` case:
140 # https://docs.docker.com/develop/develop-images/baseimages/
141 echo " OK: Using the versionless 'scratch' parent: '$df_parent'"
142 else
143 echo " ERROR: Couldn't find a parent image in $df_parent"
144 fi
145
146 done
147
Matteo Scandolob28a02e2019-11-04 09:35:16 -0800148 done < <( find "${WORKSPACE}" -name 'Dockerfile*' ! -path "*/vendor/*" ! -name "*dockerignore" -print0 )
Zack Williams12783ac2018-06-12 15:13:12 -0700149}
150
Zack Williams8e69efd2018-06-13 15:05:18 -0700151
Zack Williams12783ac2018-06-12 15:13:12 -0700152# create a git tag
153function create_git_tag {
Zack Williams6e070f52019-10-04 11:08:59 -0700154 echo "Creating git tag: $TAG_VERSION"
Zack Williams12783ac2018-06-12 15:13:12 -0700155 git checkout "$GERRIT_PATCHSET_REVISION"
Zack Williams8e69efd2018-06-13 15:05:18 -0700156
157 git config --global user.email "do-not-reply@opencord.org"
158 git config --global user.name "Jenkins"
159
Zack Williams6e070f52019-10-04 11:08:59 -0700160 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 -0700161
162 echo "Tags including new tag:"
163 git tag -n
164
Zack Williams6e070f52019-10-04 11:08:59 -0700165 git push origin "$TAG_VERSION"
Zack Williams12783ac2018-06-12 15:13:12 -0700166}
167
168echo "Checking git repo with remotes:"
169git remote -v
170
171echo "Branches:"
172git branch -v
173
174echo "Existing git tags:"
175git tag -n
176
177read_version
Zack Williams8e69efd2018-06-13 15:05:18 -0700178check_if_releaseversion
Zack Williams12783ac2018-06-12 15:13:12 -0700179
Zack Williams8e69efd2018-06-13 15:05:18 -0700180# perform checks if a released version
181if [ "$releaseversion" -eq "1" ]
Zack Williams12783ac2018-06-12 15:13:12 -0700182then
Zack Williams8e69efd2018-06-13 15:05:18 -0700183 is_git_tag_duplicated
184 dockerfile_parentcheck
185
186 if [ "$fail_validation" -eq "0" ]
187 then
188 create_git_tag
189 else
190 echo "ERROR: commit merged but failed validation, not tagging!"
191 fi
Zack Williams12783ac2018-06-12 15:13:12 -0700192fi
193
Zack Williams8e69efd2018-06-13 15:05:18 -0700194exit $fail_validation