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 | |
| 26 | releaseversion=0 |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 27 | fail_validation=0 |
| 28 | |
| 29 | # when not running under Jenkins, use current dir as workspace |
| 30 | WORKSPACE=${WORKSPACE:-.} |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 31 | |
| 32 | # find the version string in the repo, read into NEW_VERSION |
| 33 | # Add additional places NEW_VERSION could be found to this function |
| 34 | function read_version { |
| 35 | if [ -f "VERSION" ] |
| 36 | then |
| 37 | NEW_VERSION=$(head -n1 "VERSION") |
| 38 | VERSIONFILE="VERSION" |
Zack Williams | 6a9d2e6 | 2018-06-22 15:18:23 -0700 | [diff] [blame] | 39 | 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 Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 43 | else |
| 44 | echo "ERROR: No versioning file found!" |
| 45 | exit 1 |
| 46 | fi |
| 47 | } |
| 48 | |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 49 | # check if the version is a released version |
| 50 | function 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 Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 60 | # check if the version is already a tag in git |
| 61 | function 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 Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 72 | # check if Dockerfiles have a released version as their parent |
| 73 | function 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 Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 119 | |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 120 | # create a git tag |
| 121 | function create_git_tag { |
| 122 | echo "Creating git tag: $NEW_VERSION" |
| 123 | git checkout "$GERRIT_PATCHSET_REVISION" |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 124 | |
| 125 | git config --global user.email "do-not-reply@opencord.org" |
| 126 | git config --global user.name "Jenkins" |
| 127 | |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 128 | 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] | 129 | |
| 130 | echo "Tags including new tag:" |
| 131 | git tag -n |
| 132 | |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 133 | git push origin "$NEW_VERSION" |
| 134 | } |
| 135 | |
| 136 | echo "Checking git repo with remotes:" |
| 137 | git remote -v |
| 138 | |
| 139 | echo "Branches:" |
| 140 | git branch -v |
| 141 | |
| 142 | echo "Existing git tags:" |
| 143 | git tag -n |
| 144 | |
| 145 | read_version |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 146 | check_if_releaseversion |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 147 | |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 148 | # perform checks if a released version |
| 149 | if [ "$releaseversion" -eq "1" ] |
Zack Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 150 | then |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 151 | 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 Williams | 12783ac | 2018-06-12 15:13:12 -0700 | [diff] [blame] | 160 | fi |
| 161 | |
Zack Williams | 8e69efd | 2018-06-13 15:05:18 -0700 | [diff] [blame] | 162 | exit $fail_validation |