blob: 8636cd4820a3d38b0626c0882815424d5475bc2c [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
23NEW_VERSION=""
24VERSIONFILE=""
25
26releaseversion=0
27
28# find the version string in the repo, read into NEW_VERSION
29# Add additional places NEW_VERSION could be found to this function
30function read_version {
31 if [ -f "VERSION" ]
32 then
33 NEW_VERSION=$(head -n1 "VERSION")
34 VERSIONFILE="VERSION"
35 echo "New version: $NEW_VERSION"
36 else
37 echo "ERROR: No versioning file found!"
38 exit 1
39 fi
40}
41
42# check if the version is already a tag in git
43function is_git_tag_duplicated {
44 for existing_tag in $(git tag)
45 do
46 if [ "$NEW_VERSION" = "$existing_tag" ]
47 then
48 echo "ERROR: Duplicate tag: $existing_tag"
49 exit 2
50 fi
51 done
52}
53
54# check if the version is a released version
55function check_if_releasever {
56 if [[ "$NEW_VERSION" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]
57 then
58 echo "Version string '$NEW_VERSION' in '$VERSIONFILE' is a SemVer released version!"
59 releaseversion=1
60 else
61 echo "Version string '$NEW_VERSION' in '$VERSIONFILE' is not a SemVer released version, skipping."
62 fi
63}
64
65# create a git tag
66function create_git_tag {
67 echo "Creating git tag: $NEW_VERSION"
68 git checkout "$GERRIT_PATCHSET_REVISION"
69 git tag -a "$NEW_VERSION" -m "Tagged by CORD Jenkins version-tag job: $BUILD_NUMBER, for Gerrit patchset: $GERRIT_CHANGE_NUMBER"
70 git push origin "$NEW_VERSION"
71}
72
73echo "Checking git repo with remotes:"
74git remote -v
75
76echo "Branches:"
77git branch -v
78
79echo "Existing git tags:"
80git tag -n
81
82read_version
83is_git_tag_duplicated
84check_if_releasever
85
86if $releaseversion
87then
88 create_git_tag
89fi
90
91exit 0