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 | 0f763d4 | 2017-02-06 10:55:11 +0100 | [diff] [blame] | 6 | |
| 7 | set -o pipefail |
| 8 | |
| 9 | sort-versions() { |
| 10 | if [ "$(uname)" == 'Darwin' ]; then |
| 11 | gsort --version-sort |
| 12 | else |
| 13 | sort --version-sort |
| 14 | fi |
| 15 | } |
| 16 | |
| 17 | # Try tagging with and without -f to support all versions of docker |
| 18 | docker-tag() { |
| 19 | local from="jenkinsci/jenkins:$1" |
| 20 | local to="jenkinsci/jenkins:$2" |
| 21 | local out |
Carlos Sanchez | 4d0468a | 2017-03-07 19:37:18 +0100 | [diff] [blame^] | 22 | |
| 23 | docker pull "$from" |
Carlos Sanchez | 0f763d4 | 2017-02-06 10:55:11 +0100 | [diff] [blame] | 24 | if out=$(docker tag -f "$from" "$to" 2>&1); then |
| 25 | echo "$out" |
| 26 | else |
| 27 | docker tag "$from" "$to" |
| 28 | fi |
| 29 | } |
| 30 | |
Carlos Sanchez | 3ffc4e4 | 2017-02-06 13:20:44 +0100 | [diff] [blame] | 31 | get-variant() { |
Carlos Sanchez | d43f429 | 2017-02-06 13:50:34 +0100 | [diff] [blame] | 32 | local branch |
Carlos Sanchez | e43b7f9 | 2017-02-06 14:12:03 +0100 | [diff] [blame] | 33 | 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] | 34 | if [ -z "$branch" ]; then |
| 35 | >&2 echo "Could not get the current branch name for commit, not in a branch?: $(git rev-list -n 1 HEAD)" |
| 36 | return 1 |
| 37 | fi |
Carlos Sanchez | 3ffc4e4 | 2017-02-06 13:20:44 +0100 | [diff] [blame] | 38 | case "$branch" in |
| 39 | master) echo "" ;; |
| 40 | *) echo "-${branch}" ;; |
| 41 | esac |
| 42 | } |
| 43 | |
Carlos Sanchez | 881d143 | 2017-02-15 10:02:16 +0100 | [diff] [blame] | 44 | login-token() { |
| 45 | # could use jq .token |
| 46 | 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 |
| 47 | } |
| 48 | |
| 49 | is-published() { |
Carlos Sanchez | ef7d4ef | 2017-03-02 11:12:08 +0530 | [diff] [blame] | 50 | get-manifest "$1" &> /dev/null |
Carlos Sanchez | 881d143 | 2017-02-15 10:02:16 +0100 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | get-manifest() { |
| 54 | local tag=$1 |
| 55 | curl -q -fsSL -H "Accept: application/vnd.docker.distribution.manifest.v2+json" -H "Authorization: Bearer $TOKEN" "https://index.docker.io/v2/jenkinsci/jenkins/manifests/$tag" |
| 56 | } |
| 57 | |
| 58 | get-digest() { |
| 59 | #get-manifest "$1" | jq .config.digest |
| 60 | get-manifest "$1" | grep -A 10 -o '"config".*' | grep digest | head -1 | cut -d':' -f 2,3 | xargs echo |
| 61 | } |
| 62 | |
Carlos Sanchez | 0f763d4 | 2017-02-06 10:55:11 +0100 | [diff] [blame] | 63 | get-latest-versions() { |
| 64 | 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 |
| 65 | } |
| 66 | |
| 67 | publish() { |
| 68 | local version=$1 |
Carlos Sanchez | 3ffc4e4 | 2017-02-06 13:20:44 +0100 | [diff] [blame] | 69 | local variant=$2 |
| 70 | local tag="${version}${variant}" |
Carlos Sanchez | 0f763d4 | 2017-02-06 10:55:11 +0100 | [diff] [blame] | 71 | local sha |
| 72 | local build_opts="--no-cache --pull" |
| 73 | |
Carlos Sanchez | df48ce7 | 2017-03-07 18:15:06 +0100 | [diff] [blame] | 74 | if [ "$dry_run" = true ]; then |
| 75 | build_opts="" |
| 76 | else |
| 77 | build_opts="--no-cache --pull" |
| 78 | fi |
| 79 | |
Carlos Sanchez | 48c86a3 | 2017-03-07 17:56:42 +0100 | [diff] [blame] | 80 | local dir=war |
| 81 | # lts is in a different dir |
| 82 | if [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 83 | dir=war-stable |
| 84 | fi |
| 85 | 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] | 86 | |
Carlos Sanchez | 7b8382f | 2017-02-06 12:00:52 +0100 | [diff] [blame] | 87 | docker build --build-arg "JENKINS_VERSION=$version" \ |
| 88 | --build-arg "JENKINS_SHA=$sha" \ |
Carlos Sanchez | 3ffc4e4 | 2017-02-06 13:20:44 +0100 | [diff] [blame] | 89 | --tag "jenkinsci/jenkins:${tag}" ${build_opts} . |
Carlos Sanchez | 0f763d4 | 2017-02-06 10:55:11 +0100 | [diff] [blame] | 90 | |
Carlos Sanchez | df48ce7 | 2017-03-07 18:15:06 +0100 | [diff] [blame] | 91 | if [ "$dry_run" = true ]; then |
| 92 | docker push "jenkinsci/jenkins:${tag}" |
| 93 | fi |
Carlos Sanchez | 4b0dcec | 2017-02-14 00:10:14 +0100 | [diff] [blame] | 94 | } |
| 95 | |
Carlos Sanchez | 881d143 | 2017-02-15 10:02:16 +0100 | [diff] [blame] | 96 | tag-and-push() { |
| 97 | local source=$1 |
| 98 | local target=$2 |
Carlos Sanchez | dbdcfe4 | 2017-03-07 09:42:40 +0100 | [diff] [blame] | 99 | local digest_source |
| 100 | local digest_target |
| 101 | |
| 102 | # if tag doesn't exist yet, ie. dry run |
Carlos Sanchez | b857bb2 | 2017-03-07 19:28:02 +0100 | [diff] [blame] | 103 | if ! digest_source=$(get-digest "${source}" 2>&1); then |
| 104 | echo "Unable to get digest for ${source}: ${digest_source}" |
Carlos Sanchez | dbdcfe4 | 2017-03-07 09:42:40 +0100 | [diff] [blame] | 105 | digest_source="" |
| 106 | fi |
Carlos Sanchez | b857bb2 | 2017-03-07 19:28:02 +0100 | [diff] [blame] | 107 | if ! digest_target=$(get-digest "${target}" 2>&1); then |
| 108 | echo "Unable to get digest for ${target}: ${digest_target}" |
| 109 | digest_target="" |
| 110 | fi |
| 111 | if [ "$digest_source" == "$digest_target" ] && [ -n "${digest_target}" ]; then |
Carlos Sanchez | ef7d4ef | 2017-03-02 11:12:08 +0530 | [diff] [blame] | 112 | 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] | 113 | else |
Carlos Sanchez | 881d143 | 2017-02-15 10:02:16 +0100 | [diff] [blame] | 114 | echo "Creating tag ${target} pointing to ${source}" |
Carlos Sanchez | ac44f2a | 2017-03-07 18:39:29 +0100 | [diff] [blame] | 115 | docker-tag "${source}" "${target}" |
Carlos Sanchez | 7fd3f21 | 2017-02-15 08:53:31 +0100 | [diff] [blame] | 116 | if [ ! "$dry_run" = true ]; then |
Carlos Sanchez | 881d143 | 2017-02-15 10:02:16 +0100 | [diff] [blame] | 117 | docker push "jenkinsci/jenkins:${source}" |
Carlos Sanchez | 7fd3f21 | 2017-02-15 08:53:31 +0100 | [diff] [blame] | 118 | fi |
Carlos Sanchez | b108a53 | 2017-02-06 14:30:56 +0100 | [diff] [blame] | 119 | fi |
Carlos Sanchez | 0f763d4 | 2017-02-06 10:55:11 +0100 | [diff] [blame] | 120 | } |
| 121 | |
Carlos Sanchez | 881d143 | 2017-02-15 10:02:16 +0100 | [diff] [blame] | 122 | publish-latest() { |
| 123 | local version=$1 |
Carlos Sanchez | 4b0dcec | 2017-02-14 00:10:14 +0100 | [diff] [blame] | 124 | local variant=$2 |
Carlos Sanchez | 881d143 | 2017-02-15 10:02:16 +0100 | [diff] [blame] | 125 | |
| 126 | # push latest (for master) or the name of the branch (for other branches) |
| 127 | if [ -z "${variant}" ]; then |
| 128 | tag-and-push "${version}${variant}" "latest" |
| 129 | else |
| 130 | tag-and-push "${version}${variant}" "${variant#-}" |
Carlos Sanchez | 7fd3f21 | 2017-02-15 08:53:31 +0100 | [diff] [blame] | 131 | fi |
Carlos Sanchez | 4b0dcec | 2017-02-14 00:10:14 +0100 | [diff] [blame] | 132 | } |
| 133 | |
Carlos Sanchez | 881d143 | 2017-02-15 10:02:16 +0100 | [diff] [blame] | 134 | publish-lts() { |
| 135 | local version=$1 |
| 136 | local variant=$2 |
| 137 | tag-and-push "${version}" "lts${variant}" |
| 138 | } |
| 139 | |
Carlos Sanchez | 7fd3f21 | 2017-02-15 08:53:31 +0100 | [diff] [blame] | 140 | dry_run=false |
| 141 | if [ "-n" == "${1:-}" ]; then |
| 142 | dry_run=true |
| 143 | fi |
| 144 | if [ "$dry_run" = true ]; then |
Carlos Sanchez | df48ce7 | 2017-03-07 18:15:06 +0100 | [diff] [blame] | 145 | echo "Dry run, will not publish images" |
Carlos Sanchez | 7fd3f21 | 2017-02-15 08:53:31 +0100 | [diff] [blame] | 146 | fi |
Carlos Sanchez | 4b0dcec | 2017-02-14 00:10:14 +0100 | [diff] [blame] | 147 | |
Carlos Sanchez | 881d143 | 2017-02-15 10:02:16 +0100 | [diff] [blame] | 148 | TOKEN=$(login-token) |
| 149 | |
Carlos Sanchez | 3ffc4e4 | 2017-02-06 13:20:44 +0100 | [diff] [blame] | 150 | variant=$(get-variant) |
| 151 | |
Carlos Sanchez | 881d143 | 2017-02-15 10:02:16 +0100 | [diff] [blame] | 152 | lts_version="" |
| 153 | version="" |
Carlos Sanchez | 0f763d4 | 2017-02-06 10:55:11 +0100 | [diff] [blame] | 154 | for version in $(get-latest-versions); do |
Carlos Sanchez | 881d143 | 2017-02-15 10:02:16 +0100 | [diff] [blame] | 155 | if is-published "$version$variant"; then |
| 156 | echo "Tag is already published: $version$variant" |
Carlos Sanchez | 0f763d4 | 2017-02-06 10:55:11 +0100 | [diff] [blame] | 157 | else |
Carlos Sanchez | 881d143 | 2017-02-15 10:02:16 +0100 | [diff] [blame] | 158 | echo "Publishing version: $version$variant" |
Carlos Sanchez | df48ce7 | 2017-03-07 18:15:06 +0100 | [diff] [blame] | 159 | publish "$version" "$variant" |
Carlos Sanchez | 0f763d4 | 2017-02-06 10:55:11 +0100 | [diff] [blame] | 160 | fi |
Carlos Sanchez | 4b0dcec | 2017-02-14 00:10:14 +0100 | [diff] [blame] | 161 | |
| 162 | # Update lts tag |
| 163 | if [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
Carlos Sanchez | 881d143 | 2017-02-15 10:02:16 +0100 | [diff] [blame] | 164 | lts_version="${version}" |
Carlos Sanchez | 4b0dcec | 2017-02-14 00:10:14 +0100 | [diff] [blame] | 165 | fi |
Carlos Sanchez | 0f763d4 | 2017-02-06 10:55:11 +0100 | [diff] [blame] | 166 | done |
| 167 | |
Carlos Sanchez | 881d143 | 2017-02-15 10:02:16 +0100 | [diff] [blame] | 168 | publish-latest "${version}" "${variant}" |
| 169 | if [ -n "${lts_version}" ]; then |
| 170 | publish-lts "${lts_version}" "${variant}" |
Carlos Sanchez | 4b0dcec | 2017-02-14 00:10:14 +0100 | [diff] [blame] | 171 | fi |