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 | |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 23 | VERSIONFILE="" # file path to file containing version number |
| 24 | NEW_VERSION="" # version number found in $VERSIONFILE |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 25 | |
Zack Williams | 6650000 | 2018-09-06 15:29:05 -0700 | [diff] [blame] | 26 | SEMVER_STRICT=${SEMVER_STRICT:-0} # require semver versions |
| 27 | |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 28 | releaseversion=0 |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 29 | fail_validation=0 |
| 30 | |
| 31 | # when not running under Jenkins, use current dir as workspace |
| 32 | WORKSPACE=${WORKSPACE:-.} |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 33 | |
| 34 | # find the version string in the repo, read into NEW_VERSION |
| 35 | # Add additional places NEW_VERSION could be found to this function |
| 36 | function read_version { |
| 37 | if [ -f "VERSION" ] |
| 38 | then |
| 39 | NEW_VERSION=$(head -n1 "VERSION") |
| 40 | VERSIONFILE="VERSION" |
Zack Williams | 6a9d2e6 | 2018-06-22 15:18:23 -0700 | [diff] [blame] | 41 | elif [ -f "package.json" ] |
| 42 | then |
| 43 | NEW_VERSION=$(python -c 'import json,sys;obj=json.load(sys.stdin); print obj["version"]' < package.json) |
| 44 | VERSIONFILE="package.json" |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 45 | else |
| 46 | echo "ERROR: No versioning file found!" |
| 47 | exit 1 |
| 48 | fi |
| 49 | } |
| 50 | |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 51 | # check if the version is a released version |
| 52 | function check_if_releaseversion { |
| 53 | if [[ "$NEW_VERSION" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]] |
| 54 | then |
| 55 | echo "Version string '$NEW_VERSION' in '$VERSIONFILE' is a SemVer released version!" |
| 56 | releaseversion=1 |
| 57 | else |
Zack Williams | 6650000 | 2018-09-06 15:29:05 -0700 | [diff] [blame] | 58 | if [ "$SEMVER_STRICT" -eq "1" ] |
| 59 | then |
| 60 | echo "Version string '$NEW_VERSION' in '$VERSIONFILE' is not a SemVer released version, SEMVER_STRICT enabled, failing!" |
| 61 | fail_validation=1 |
| 62 | else |
| 63 | echo "Version string '$NEW_VERSION' in '$VERSIONFILE' is not a SemVer released version, skipping." |
| 64 | fi |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 65 | fi |
| 66 | } |
| 67 | |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 68 | # check if the version is already a tag in git |
| 69 | function is_git_tag_duplicated { |
| 70 | for existing_tag in $(git tag) |
| 71 | do |
| 72 | if [ "$NEW_VERSION" = "$existing_tag" ] |
| 73 | then |
| 74 | echo "ERROR: Duplicate tag: $existing_tag" |
| 75 | exit 2 |
| 76 | fi |
| 77 | done |
| 78 | } |
| 79 | |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 80 | # check if Dockerfiles have a released version as their parent |
| 81 | function dockerfile_parentcheck { |
| 82 | while IFS= read -r -d '' dockerfile |
| 83 | do |
| 84 | echo "Checking dockerfile: '$dockerfile'" |
| 85 | |
| 86 | # split on newlines |
| 87 | IFS=$'\n' |
| 88 | df_parents=($(grep "^FROM" "$dockerfile")) |
| 89 | |
| 90 | # check all parents in the Dockerfile |
| 91 | for df_parent in "${df_parents[@]}" |
| 92 | do |
| 93 | |
| 94 | df_pattern="FROM (.*):(.*)" |
| 95 | if [[ "$df_parent" =~ $df_pattern ]] |
| 96 | then |
| 97 | |
| 98 | p_image="${BASH_REMATCH[1]}" |
| 99 | p_version="${BASH_REMATCH[2]}" |
| 100 | |
| 101 | if [[ "${p_version}" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]] |
| 102 | then |
| 103 | echo " OK: Parent '$p_image:$p_version' is a released SemVer version" |
Zack Williams | 0dc2754 | 2018-10-11 08:09:10 -0700 | [diff] [blame] | 104 | elif [[ "${p_version}" =~ ^.*([0-9]+)\.([0-9]+).*$ ]] |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 105 | then |
Zack Williams | 0dc2754 | 2018-10-11 08:09:10 -0700 | [diff] [blame] | 106 | # handle non-SemVer versions that have a Major.Minor version specifier in the name |
| 107 | # 'ubuntu:16.04' |
| 108 | # 'postgres:10.3-alpine' |
| 109 | # 'openjdk:8-jre-alpine3.8' |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 110 | echo " OK: Parent '$p_image:$p_version' is using a non-SemVer, but sufficient, version" |
| 111 | else |
| 112 | echo " ERROR: Parent '$p_image:$p_version' is NOT using an specific version" |
| 113 | fail_validation=1 |
| 114 | fi |
| 115 | |
| 116 | elif [[ "$df_parent" =~ ^FROM\ scratch$ ]] |
| 117 | then |
| 118 | # Handle the parent-less `FROM scratch` case: |
| 119 | # https://docs.docker.com/develop/develop-images/baseimages/ |
| 120 | echo " OK: Using the versionless 'scratch' parent: '$df_parent'" |
| 121 | else |
| 122 | echo " ERROR: Couldn't find a parent image in $df_parent" |
| 123 | fi |
| 124 | |
| 125 | done |
| 126 | |
| 127 | done < <( find "${WORKSPACE}" -name 'Dockerfile*' -print0 ) |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 130 | |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 131 | # create a git tag |
| 132 | function create_git_tag { |
| 133 | echo "Creating git tag: $NEW_VERSION" |
| 134 | git checkout "$GERRIT_PATCHSET_REVISION" |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 135 | |
| 136 | git config --global user.email "do-not-reply@opencord.org" |
| 137 | git config --global user.name "Jenkins" |
| 138 | |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 139 | git tag -a "$NEW_VERSION" -m "Tagged by CORD Jenkins version-tag job: $BUILD_NUMBER, for Gerrit patchset: $GERRIT_CHANGE_NUMBER" |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 140 | |
| 141 | echo "Tags including new tag:" |
| 142 | git tag -n |
| 143 | |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 144 | git push origin "$NEW_VERSION" |
| 145 | } |
| 146 | |
| 147 | echo "Checking git repo with remotes:" |
| 148 | git remote -v |
| 149 | |
| 150 | echo "Branches:" |
| 151 | git branch -v |
| 152 | |
| 153 | echo "Existing git tags:" |
| 154 | git tag -n |
| 155 | |
| 156 | read_version |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 157 | check_if_releaseversion |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 158 | |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 159 | # perform checks if a released version |
| 160 | if [ "$releaseversion" -eq "1" ] |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 161 | then |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 162 | is_git_tag_duplicated |
| 163 | dockerfile_parentcheck |
| 164 | |
| 165 | if [ "$fail_validation" -eq "0" ] |
| 166 | then |
| 167 | create_git_tag |
| 168 | else |
| 169 | echo "ERROR: commit merged but failed validation, not tagging!" |
| 170 | fi |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 171 | fi |
| 172 | |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 173 | exit $fail_validation |