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