blob: 428791483ba3c7ee0cabd53196924e0ad4ef92f6 [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 Williams6a9d2e62018-06-22 15:18:23 -070039 elif [ -f "package.json" ]
40 then
41 NEW_VERSION=$(python -c 'import json,sys;obj=json.load(sys.stdin); print obj["version"]' < package.json)
42 VERSIONFILE="package.json"
Zack Williams12783ac2018-06-12 15:13:12 -070043 else
44 echo "ERROR: No versioning file found!"
45 exit 1
46 fi
47}
48
Zack Williams8e69efd2018-06-13 15:05:18 -070049# check if the version is a released version
50function check_if_releaseversion {
51 if [[ "$NEW_VERSION" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]
52 then
53 echo "Version string '$NEW_VERSION' in '$VERSIONFILE' is a SemVer released version!"
54 releaseversion=1
55 else
56 echo "Version string '$NEW_VERSION' in '$VERSIONFILE' is not a SemVer released version, skipping."
57 fi
58}
59
Zack Williams12783ac2018-06-12 15:13:12 -070060# check if the version is already a tag in git
61function is_git_tag_duplicated {
62 for existing_tag in $(git tag)
63 do
64 if [ "$NEW_VERSION" = "$existing_tag" ]
65 then
66 echo "ERROR: Duplicate tag: $existing_tag"
67 exit 2
68 fi
69 done
70}
71
Zack Williams8e69efd2018-06-13 15:05:18 -070072# check if Dockerfiles have a released version as their parent
73function dockerfile_parentcheck {
74 while IFS= read -r -d '' dockerfile
75 do
76 echo "Checking dockerfile: '$dockerfile'"
77
78 # split on newlines
79 IFS=$'\n'
80 df_parents=($(grep "^FROM" "$dockerfile"))
81
82 # check all parents in the Dockerfile
83 for df_parent in "${df_parents[@]}"
84 do
85
86 df_pattern="FROM (.*):(.*)"
87 if [[ "$df_parent" =~ $df_pattern ]]
88 then
89
90 p_image="${BASH_REMATCH[1]}"
91 p_version="${BASH_REMATCH[2]}"
92
93 if [[ "${p_version}" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]
94 then
95 echo " OK: Parent '$p_image:$p_version' is a released SemVer version"
96 elif [[ "${p_version}" =~ ^([0-9]+)\.([0-9]+).*$ ]]
97 then
98 # handle the non-SemVer 'ubuntu:16.04' and 'postgres:10.3-alpine' cases
99 echo " OK: Parent '$p_image:$p_version' is using a non-SemVer, but sufficient, version"
100 else
101 echo " ERROR: Parent '$p_image:$p_version' is NOT using an specific version"
102 fail_validation=1
103 fi
104
105 elif [[ "$df_parent" =~ ^FROM\ scratch$ ]]
106 then
107 # Handle the parent-less `FROM scratch` case:
108 # https://docs.docker.com/develop/develop-images/baseimages/
109 echo " OK: Using the versionless 'scratch' parent: '$df_parent'"
110 else
111 echo " ERROR: Couldn't find a parent image in $df_parent"
112 fi
113
114 done
115
116 done < <( find "${WORKSPACE}" -name 'Dockerfile*' -print0 )
Zack Williams12783ac2018-06-12 15:13:12 -0700117}
118
Zack Williams8e69efd2018-06-13 15:05:18 -0700119
Zack Williams12783ac2018-06-12 15:13:12 -0700120# create a git tag
121function create_git_tag {
122 echo "Creating git tag: $NEW_VERSION"
123 git checkout "$GERRIT_PATCHSET_REVISION"
Zack Williams8e69efd2018-06-13 15:05:18 -0700124
125 git config --global user.email "do-not-reply@opencord.org"
126 git config --global user.name "Jenkins"
127
Zack Williams12783ac2018-06-12 15:13:12 -0700128 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 -0700129
130 echo "Tags including new tag:"
131 git tag -n
132
Zack Williams12783ac2018-06-12 15:13:12 -0700133 git push origin "$NEW_VERSION"
134}
135
136echo "Checking git repo with remotes:"
137git remote -v
138
139echo "Branches:"
140git branch -v
141
142echo "Existing git tags:"
143git tag -n
144
145read_version
Zack Williams8e69efd2018-06-13 15:05:18 -0700146check_if_releaseversion
Zack Williams12783ac2018-06-12 15:13:12 -0700147
Zack Williams8e69efd2018-06-13 15:05:18 -0700148# perform checks if a released version
149if [ "$releaseversion" -eq "1" ]
Zack Williams12783ac2018-06-12 15:13:12 -0700150then
Zack Williams8e69efd2018-06-13 15:05:18 -0700151 is_git_tag_duplicated
152 dockerfile_parentcheck
153
154 if [ "$fail_validation" -eq "0" ]
155 then
156 create_git_tag
157 else
158 echo "ERROR: commit merged but failed validation, not tagging!"
159 fi
Zack Williams12783ac2018-06-12 15:13:12 -0700160fi
161
Zack Williams8e69efd2018-06-13 15:05:18 -0700162exit $fail_validation