Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 1 | #!/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 | |
| 21 | set -eu -o pipefail |
| 22 | |
| 23 | NEW_VERSION="" |
| 24 | VERSIONFILE="" |
| 25 | |
| 26 | releaseversion=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 |
| 30 | function 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 |
| 43 | function 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 |
| 55 | function 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 |
| 66 | function 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 | |
| 73 | echo "Checking git repo with remotes:" |
| 74 | git remote -v |
| 75 | |
| 76 | echo "Branches:" |
| 77 | git branch -v |
| 78 | |
| 79 | echo "Existing git tags:" |
| 80 | git tag -n |
| 81 | |
| 82 | read_version |
| 83 | is_git_tag_duplicated |
| 84 | check_if_releasever |
| 85 | |
| 86 | if $releaseversion |
| 87 | then |
| 88 | create_git_tag |
| 89 | fi |
| 90 | |
| 91 | exit 0 |