Carlos Sanchez | 0f763d4 | 2017-02-06 10:55:11 +0100 | [diff] [blame] | 1 | #!/bin/bash -eu |
| 2 | |
| 3 | # Publish any versions of the docker image not yet pushed to jenkinsci/jenkins |
Carlos Sanchez | 7fd3f21 | 2017-02-15 08:53:31 +0100 | [diff] [blame] | 4 | # Arguments: |
| 5 | # -n dry run, do not build or publish images |
Carlos Sanchez | 09f1e50 | 2017-03-07 20:06:42 +0100 | [diff] [blame] | 6 | # -d debug |
Carlos Sanchez | 0f763d4 | 2017-02-06 10:55:11 +0100 | [diff] [blame] | 7 | |
| 8 | set -o pipefail |
| 9 | |
| 10 | sort-versions() { |
| 11 | if [ "$(uname)" == 'Darwin' ]; then |
| 12 | gsort --version-sort |
| 13 | else |
| 14 | sort --version-sort |
| 15 | fi |
| 16 | } |
| 17 | |
| 18 | # Try tagging with and without -f to support all versions of docker |
| 19 | docker-tag() { |
| 20 | local from="jenkinsci/jenkins:$1" |
| 21 | local to="jenkinsci/jenkins:$2" |
| 22 | local out |
Carlos Sanchez | 4d0468a | 2017-03-07 19:37:18 +0100 | [diff] [blame] | 23 | |
| 24 | docker pull "$from" |
Carlos Sanchez | 0f763d4 | 2017-02-06 10:55:11 +0100 | [diff] [blame] | 25 | if out=$(docker tag -f "$from" "$to" 2>&1); then |
| 26 | echo "$out" |
| 27 | else |
| 28 | docker tag "$from" "$to" |
| 29 | fi |
| 30 | } |
| 31 | |
Carlos Sanchez | 3ffc4e4 | 2017-02-06 13:20:44 +0100 | [diff] [blame] | 32 | get-variant() { |
Carlos Sanchez | d43f429 | 2017-02-06 13:50:34 +0100 | [diff] [blame] | 33 | local branch |
Carlos Sanchez | e43b7f9 | 2017-02-06 14:12:03 +0100 | [diff] [blame] | 34 | branch=$(git show-ref | grep $(git rev-list -n 1 HEAD) | tail -1 | rev | cut -d/ -f 1 | rev) |
Carlos Sanchez | d43f429 | 2017-02-06 13:50:34 +0100 | [diff] [blame] | 35 | if [ -z "$branch" ]; then |
| 36 | >&2 echo "Could not get the current branch name for commit, not in a branch?: $(git rev-list -n 1 HEAD)" |
| 37 | return 1 |
| 38 | fi |
Carlos Sanchez | 3ffc4e4 | 2017-02-06 13:20:44 +0100 | [diff] [blame] | 39 | case "$branch" in |
| 40 | master) echo "" ;; |
| 41 | *) echo "-${branch}" ;; |
| 42 | esac |
| 43 | } |
| 44 | |
Carlos Sanchez | 881d143 | 2017-02-15 10:02:16 +0100 | [diff] [blame] | 45 | login-token() { |
| 46 | # could use jq .token |
| 47 | curl -q -sSL https://auth.docker.io/token\?service\=registry.docker.io\&scope\=repository:jenkinsci/jenkins:pull | grep -o '"token":"[^"]*"' | cut -d':' -f 2 | xargs echo |
| 48 | } |
| 49 | |
| 50 | is-published() { |
Carlos Sanchez | ef7d4ef | 2017-03-02 11:12:08 +0530 | [diff] [blame] | 51 | get-manifest "$1" &> /dev/null |
Carlos Sanchez | 881d143 | 2017-02-15 10:02:16 +0100 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | get-manifest() { |
| 55 | local tag=$1 |
Carlos Sanchez | 09f1e50 | 2017-03-07 20:06:42 +0100 | [diff] [blame] | 56 | local opts="" |
| 57 | if [ "$debug" = true ]; then |
| 58 | opts="-v" |
| 59 | fi |
| 60 | curl $opts -q -fsSL -H "Accept: application/vnd.docker.distribution.manifest.v2+json" -H "Authorization: Bearer $TOKEN" "https://index.docker.io/v2/jenkinsci/jenkins/manifests/$tag" |
Carlos Sanchez | 881d143 | 2017-02-15 10:02:16 +0100 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | get-digest() { |
Carlos Sanchez | 09f1e50 | 2017-03-07 20:06:42 +0100 | [diff] [blame] | 64 | local manifest |
| 65 | manifest=$(get-manifest "$1") |
Carlos Sanchez | 881d143 | 2017-02-15 10:02:16 +0100 | [diff] [blame] | 66 | #get-manifest "$1" | jq .config.digest |
Carlos Sanchez | 09f1e50 | 2017-03-07 20:06:42 +0100 | [diff] [blame] | 67 | if [ "$debug" = true ]; then |
| 68 | >&2 echo "DEBUG: Manifest for $1: $manifest" |
| 69 | fi |
| 70 | echo "$manifest" | grep -A 10 -o '"config".*' | grep digest | head -1 | cut -d':' -f 2,3 | xargs echo |
Carlos Sanchez | 881d143 | 2017-02-15 10:02:16 +0100 | [diff] [blame] | 71 | } |
| 72 | |
Carlos Sanchez | 0f763d4 | 2017-02-06 10:55:11 +0100 | [diff] [blame] | 73 | get-latest-versions() { |
| 74 | curl -q -fsSL https://api.github.com/repos/jenkinsci/jenkins/tags?per_page=20 | grep '"name": "jenkins-' | egrep -o '[0-9]+(\.[0-9]+)+' | sort-versions | uniq |
| 75 | } |
| 76 | |
| 77 | publish() { |
| 78 | local version=$1 |
Carlos Sanchez | 3ffc4e4 | 2017-02-06 13:20:44 +0100 | [diff] [blame] | 79 | local variant=$2 |
| 80 | local tag="${version}${variant}" |
Carlos Sanchez | 0f763d4 | 2017-02-06 10:55:11 +0100 | [diff] [blame] | 81 | local sha |
| 82 | local build_opts="--no-cache --pull" |
| 83 | |
Carlos Sanchez | df48ce7 | 2017-03-07 18:15:06 +0100 | [diff] [blame] | 84 | if [ "$dry_run" = true ]; then |
| 85 | build_opts="" |
| 86 | else |
| 87 | build_opts="--no-cache --pull" |
| 88 | fi |
| 89 | |
Carlos Sanchez | 48c86a3 | 2017-03-07 17:56:42 +0100 | [diff] [blame] | 90 | local dir=war |
| 91 | # lts is in a different dir |
| 92 | if [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 93 | dir=war-stable |
| 94 | fi |
| 95 | sha=$(curl -q -fsSL "http://mirrors.jenkins.io/${dir}/${version}/jenkins.war.sha256" | cut -d' ' -f 1) |
Carlos Sanchez | 0f763d4 | 2017-02-06 10:55:11 +0100 | [diff] [blame] | 96 | |
Carlos Sanchez | 7b8382f | 2017-02-06 12:00:52 +0100 | [diff] [blame] | 97 | docker build --build-arg "JENKINS_VERSION=$version" \ |
| 98 | --build-arg "JENKINS_SHA=$sha" \ |
Carlos Sanchez | 3ffc4e4 | 2017-02-06 13:20:44 +0100 | [diff] [blame] | 99 | --tag "jenkinsci/jenkins:${tag}" ${build_opts} . |
Carlos Sanchez | 0f763d4 | 2017-02-06 10:55:11 +0100 | [diff] [blame] | 100 | |
Carlos Sanchez | df48ce7 | 2017-03-07 18:15:06 +0100 | [diff] [blame] | 101 | if [ "$dry_run" = true ]; then |
| 102 | docker push "jenkinsci/jenkins:${tag}" |
| 103 | fi |
Carlos Sanchez | 4b0dcec | 2017-02-14 00:10:14 +0100 | [diff] [blame] | 104 | } |
| 105 | |
Carlos Sanchez | 881d143 | 2017-02-15 10:02:16 +0100 | [diff] [blame] | 106 | tag-and-push() { |
| 107 | local source=$1 |
| 108 | local target=$2 |
Carlos Sanchez | dbdcfe4 | 2017-03-07 09:42:40 +0100 | [diff] [blame] | 109 | local digest_source |
| 110 | local digest_target |
| 111 | |
Carlos Sanchez | 09f1e50 | 2017-03-07 20:06:42 +0100 | [diff] [blame] | 112 | if [ "$debug" = true ]; then |
| 113 | >&2 echo "DEBUG: Getting digest for ${source}" |
| 114 | fi |
Carlos Sanchez | dbdcfe4 | 2017-03-07 09:42:40 +0100 | [diff] [blame] | 115 | # if tag doesn't exist yet, ie. dry run |
Carlos Sanchez | 09f1e50 | 2017-03-07 20:06:42 +0100 | [diff] [blame] | 116 | if ! digest_source=$(get-digest "${source}"); then |
| 117 | echo "Unable to get digest for ${source} ${digest_source}" |
Carlos Sanchez | dbdcfe4 | 2017-03-07 09:42:40 +0100 | [diff] [blame] | 118 | digest_source="" |
| 119 | fi |
Carlos Sanchez | 09f1e50 | 2017-03-07 20:06:42 +0100 | [diff] [blame] | 120 | |
| 121 | if [ "$debug" = true ]; then |
| 122 | >&2 echo "DEBUG: Getting digest for ${target}" |
| 123 | fi |
| 124 | if ! digest_target=$(get-digest "${target}"); then |
| 125 | echo "Unable to get digest for ${target} ${digest_target}" |
Carlos Sanchez | b857bb2 | 2017-03-07 19:28:02 +0100 | [diff] [blame] | 126 | digest_target="" |
| 127 | fi |
Carlos Sanchez | 09f1e50 | 2017-03-07 20:06:42 +0100 | [diff] [blame] | 128 | |
Carlos Sanchez | b857bb2 | 2017-03-07 19:28:02 +0100 | [diff] [blame] | 129 | if [ "$digest_source" == "$digest_target" ] && [ -n "${digest_target}" ]; then |
Carlos Sanchez | ef7d4ef | 2017-03-02 11:12:08 +0530 | [diff] [blame] | 130 | echo "Images ${source} [$digest_source] and ${target} [$digest_target] are already the same, not updating tags" |
Carlos Sanchez | b108a53 | 2017-02-06 14:30:56 +0100 | [diff] [blame] | 131 | else |
Carlos Sanchez | 881d143 | 2017-02-15 10:02:16 +0100 | [diff] [blame] | 132 | echo "Creating tag ${target} pointing to ${source}" |
Carlos Sanchez | ac44f2a | 2017-03-07 18:39:29 +0100 | [diff] [blame] | 133 | docker-tag "${source}" "${target}" |
Carlos Sanchez | 7fd3f21 | 2017-02-15 08:53:31 +0100 | [diff] [blame] | 134 | if [ ! "$dry_run" = true ]; then |
Carlos Sanchez | dc86915 | 2017-03-07 20:21:30 +0100 | [diff] [blame] | 135 | echo "Pushing jenkinsci/jenkins:${target}" |
| 136 | docker push "jenkinsci/jenkins:${target}" |
| 137 | else |
| 138 | echo "Would push jenkinsci/jenkins:${target}" |
Carlos Sanchez | 7fd3f21 | 2017-02-15 08:53:31 +0100 | [diff] [blame] | 139 | fi |
Carlos Sanchez | b108a53 | 2017-02-06 14:30:56 +0100 | [diff] [blame] | 140 | fi |
Carlos Sanchez | 0f763d4 | 2017-02-06 10:55:11 +0100 | [diff] [blame] | 141 | } |
| 142 | |
Carlos Sanchez | 881d143 | 2017-02-15 10:02:16 +0100 | [diff] [blame] | 143 | publish-latest() { |
| 144 | local version=$1 |
Carlos Sanchez | 4b0dcec | 2017-02-14 00:10:14 +0100 | [diff] [blame] | 145 | local variant=$2 |
Carlos Sanchez | 881d143 | 2017-02-15 10:02:16 +0100 | [diff] [blame] | 146 | |
| 147 | # push latest (for master) or the name of the branch (for other branches) |
| 148 | if [ -z "${variant}" ]; then |
| 149 | tag-and-push "${version}${variant}" "latest" |
| 150 | else |
| 151 | tag-and-push "${version}${variant}" "${variant#-}" |
Carlos Sanchez | 7fd3f21 | 2017-02-15 08:53:31 +0100 | [diff] [blame] | 152 | fi |
Carlos Sanchez | 4b0dcec | 2017-02-14 00:10:14 +0100 | [diff] [blame] | 153 | } |
| 154 | |
Carlos Sanchez | 881d143 | 2017-02-15 10:02:16 +0100 | [diff] [blame] | 155 | publish-lts() { |
| 156 | local version=$1 |
| 157 | local variant=$2 |
| 158 | tag-and-push "${version}" "lts${variant}" |
| 159 | } |
| 160 | |
Carlos Sanchez | 09f1e50 | 2017-03-07 20:06:42 +0100 | [diff] [blame] | 161 | # Process arguments |
| 162 | |
Carlos Sanchez | 7fd3f21 | 2017-02-15 08:53:31 +0100 | [diff] [blame] | 163 | dry_run=false |
Carlos Sanchez | 09f1e50 | 2017-03-07 20:06:42 +0100 | [diff] [blame] | 164 | debug=false |
| 165 | |
| 166 | while [[ $# -gt 0 ]]; do |
| 167 | key="$1" |
| 168 | case $key in |
| 169 | -n) |
| 170 | dry_run=true |
| 171 | ;; |
| 172 | -d) |
| 173 | debug=true |
| 174 | ;; |
| 175 | *) |
| 176 | echo "Unknown option: $key" |
| 177 | return 1 |
| 178 | ;; |
| 179 | esac |
| 180 | shift |
| 181 | done |
| 182 | |
| 183 | |
Carlos Sanchez | 7fd3f21 | 2017-02-15 08:53:31 +0100 | [diff] [blame] | 184 | if [ "$dry_run" = true ]; then |
Carlos Sanchez | df48ce7 | 2017-03-07 18:15:06 +0100 | [diff] [blame] | 185 | echo "Dry run, will not publish images" |
Carlos Sanchez | 7fd3f21 | 2017-02-15 08:53:31 +0100 | [diff] [blame] | 186 | fi |
Carlos Sanchez | 4b0dcec | 2017-02-14 00:10:14 +0100 | [diff] [blame] | 187 | |
Carlos Sanchez | 881d143 | 2017-02-15 10:02:16 +0100 | [diff] [blame] | 188 | TOKEN=$(login-token) |
| 189 | |
Carlos Sanchez | 3ffc4e4 | 2017-02-06 13:20:44 +0100 | [diff] [blame] | 190 | variant=$(get-variant) |
| 191 | |
Carlos Sanchez | 881d143 | 2017-02-15 10:02:16 +0100 | [diff] [blame] | 192 | lts_version="" |
| 193 | version="" |
Carlos Sanchez | 0f763d4 | 2017-02-06 10:55:11 +0100 | [diff] [blame] | 194 | for version in $(get-latest-versions); do |
Carlos Sanchez | 881d143 | 2017-02-15 10:02:16 +0100 | [diff] [blame] | 195 | if is-published "$version$variant"; then |
| 196 | echo "Tag is already published: $version$variant" |
Carlos Sanchez | 0f763d4 | 2017-02-06 10:55:11 +0100 | [diff] [blame] | 197 | else |
Carlos Sanchez | 881d143 | 2017-02-15 10:02:16 +0100 | [diff] [blame] | 198 | echo "Publishing version: $version$variant" |
Carlos Sanchez | df48ce7 | 2017-03-07 18:15:06 +0100 | [diff] [blame] | 199 | publish "$version" "$variant" |
Carlos Sanchez | 0f763d4 | 2017-02-06 10:55:11 +0100 | [diff] [blame] | 200 | fi |
Carlos Sanchez | 4b0dcec | 2017-02-14 00:10:14 +0100 | [diff] [blame] | 201 | |
| 202 | # Update lts tag |
| 203 | if [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
Carlos Sanchez | 881d143 | 2017-02-15 10:02:16 +0100 | [diff] [blame] | 204 | lts_version="${version}" |
Carlos Sanchez | 4b0dcec | 2017-02-14 00:10:14 +0100 | [diff] [blame] | 205 | fi |
Carlos Sanchez | 0f763d4 | 2017-02-06 10:55:11 +0100 | [diff] [blame] | 206 | done |
| 207 | |
Carlos Sanchez | 881d143 | 2017-02-15 10:02:16 +0100 | [diff] [blame] | 208 | publish-latest "${version}" "${variant}" |
| 209 | if [ -n "${lts_version}" ]; then |
| 210 | publish-lts "${lts_version}" "${variant}" |
Carlos Sanchez | 4b0dcec | 2017-02-14 00:10:14 +0100 | [diff] [blame] | 211 | fi |