blob: 1ee9246aa7c6370370c2c4814bf9b616c6cb9830 [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
26releaseversion=0
Zack Williams8e69efd2018-06-13 15:05:18 -070027fail_validation=0
28
29# when not running under Jenkins, use current dir as workspace
30WORKSPACE=${WORKSPACE:-.}
Zack Williams12783ac2018-06-12 15:13:12 -070031
32# find the version string in the repo, read into NEW_VERSION
33# Add additional places NEW_VERSION could be found to this function
34function read_version {
35 if [ -f "VERSION" ]
36 then
37 NEW_VERSION=$(head -n1 "VERSION")
38 VERSIONFILE="VERSION"
Zack Williams12783ac2018-06-12 15:13:12 -070039 else
40 echo "ERROR: No versioning file found!"
41 exit 1
42 fi
43}
44
Zack Williams8e69efd2018-06-13 15:05:18 -070045# check if the version is a released version
46function check_if_releaseversion {
47 if [[ "$NEW_VERSION" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]
48 then
49 echo "Version string '$NEW_VERSION' in '$VERSIONFILE' is a SemVer released version!"
50 releaseversion=1
51 else
52 echo "Version string '$NEW_VERSION' in '$VERSIONFILE' is not a SemVer released version, skipping."
53 fi
54}
55
Zack Williams12783ac2018-06-12 15:13:12 -070056# check if the version is already a tag in git
57function is_git_tag_duplicated {
58 for existing_tag in $(git tag)
59 do
60 if [ "$NEW_VERSION" = "$existing_tag" ]
61 then
62 echo "ERROR: Duplicate tag: $existing_tag"
63 exit 2
64 fi
65 done
66}
67
Zack Williams8e69efd2018-06-13 15:05:18 -070068# check if Dockerfiles have a released version as their parent
69function dockerfile_parentcheck {
70 while IFS= read -r -d '' dockerfile
71 do
72 echo "Checking dockerfile: '$dockerfile'"
73
74 # split on newlines
75 IFS=$'\n'
76 df_parents=($(grep "^FROM" "$dockerfile"))
77
78 # check all parents in the Dockerfile
79 for df_parent in "${df_parents[@]}"
80 do
81
82 df_pattern="FROM (.*):(.*)"
83 if [[ "$df_parent" =~ $df_pattern ]]
84 then
85
86 p_image="${BASH_REMATCH[1]}"
87 p_version="${BASH_REMATCH[2]}"
88
89 if [[ "${p_version}" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]
90 then
91 echo " OK: Parent '$p_image:$p_version' is a released SemVer version"
92 elif [[ "${p_version}" =~ ^([0-9]+)\.([0-9]+).*$ ]]
93 then
94 # handle the non-SemVer 'ubuntu:16.04' and 'postgres:10.3-alpine' cases
95 echo " OK: Parent '$p_image:$p_version' is using a non-SemVer, but sufficient, version"
96 else
97 echo " ERROR: Parent '$p_image:$p_version' is NOT using an specific version"
98 fail_validation=1
99 fi
100
101 elif [[ "$df_parent" =~ ^FROM\ scratch$ ]]
102 then
103 # Handle the parent-less `FROM scratch` case:
104 # https://docs.docker.com/develop/develop-images/baseimages/
105 echo " OK: Using the versionless 'scratch' parent: '$df_parent'"
106 else
107 echo " ERROR: Couldn't find a parent image in $df_parent"
108 fi
109
110 done
111
112 done < <( find "${WORKSPACE}" -name 'Dockerfile*' -print0 )
Zack Williams12783ac2018-06-12 15:13:12 -0700113}
114
Zack Williams8e69efd2018-06-13 15:05:18 -0700115
Zack Williams12783ac2018-06-12 15:13:12 -0700116# create a git tag
117function create_git_tag {
118 echo "Creating git tag: $NEW_VERSION"
119 git checkout "$GERRIT_PATCHSET_REVISION"
Zack Williams8e69efd2018-06-13 15:05:18 -0700120
121 git config --global user.email "do-not-reply@opencord.org"
122 git config --global user.name "Jenkins"
123
Zack Williams12783ac2018-06-12 15:13:12 -0700124 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 -0700125
126 echo "Tags including new tag:"
127 git tag -n
128
Zack Williams12783ac2018-06-12 15:13:12 -0700129 git push origin "$NEW_VERSION"
130}
131
132echo "Checking git repo with remotes:"
133git remote -v
134
135echo "Branches:"
136git branch -v
137
138echo "Existing git tags:"
139git tag -n
140
141read_version
Zack Williams8e69efd2018-06-13 15:05:18 -0700142check_if_releaseversion
Zack Williams12783ac2018-06-12 15:13:12 -0700143
Zack Williams8e69efd2018-06-13 15:05:18 -0700144# perform checks if a released version
145if [ "$releaseversion" -eq "1" ]
Zack Williams12783ac2018-06-12 15:13:12 -0700146then
Zack Williams8e69efd2018-06-13 15:05:18 -0700147 is_git_tag_duplicated
148 dockerfile_parentcheck
149
150 if [ "$fail_validation" -eq "0" ]
151 then
152 create_git_tag
153 else
154 echo "ERROR: commit merged but failed validation, not tagging!"
155 fi
Zack Williams12783ac2018-06-12 15:13:12 -0700156fi
157
Zack Williams8e69efd2018-06-13 15:05:18 -0700158exit $fail_validation