blob: 5af76832c21a65e579c4a4f954d6fb5c82841de4 [file] [log] [blame]
Carlos Sanchez0f763d42017-02-06 10:55:11 +01001#!/bin/bash -eu
2
3# Publish any versions of the docker image not yet pushed to jenkinsci/jenkins
Carlos Sanchez7fd3f212017-02-15 08:53:31 +01004# Arguments:
5# -n dry run, do not build or publish images
Carlos Sanchez0f763d42017-02-06 10:55:11 +01006
7set -o pipefail
8
9sort-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
18docker-tag() {
19 local from="jenkinsci/jenkins:$1"
20 local to="jenkinsci/jenkins:$2"
21 local out
Carlos Sanchez4d0468a2017-03-07 19:37:18 +010022
23 docker pull "$from"
Carlos Sanchez0f763d42017-02-06 10:55:11 +010024 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 Sanchez3ffc4e42017-02-06 13:20:44 +010031get-variant() {
Carlos Sanchezd43f4292017-02-06 13:50:34 +010032 local branch
Carlos Sancheze43b7f92017-02-06 14:12:03 +010033 branch=$(git show-ref | grep $(git rev-list -n 1 HEAD) | tail -1 | rev | cut -d/ -f 1 | rev)
Carlos Sanchezd43f4292017-02-06 13:50:34 +010034 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 Sanchez3ffc4e42017-02-06 13:20:44 +010038 case "$branch" in
39 master) echo "" ;;
40 *) echo "-${branch}" ;;
41 esac
42}
43
Carlos Sanchez881d1432017-02-15 10:02:16 +010044login-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
49is-published() {
Carlos Sanchezef7d4ef2017-03-02 11:12:08 +053050 get-manifest "$1" &> /dev/null
Carlos Sanchez881d1432017-02-15 10:02:16 +010051}
52
53get-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
58get-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 Sanchez0f763d42017-02-06 10:55:11 +010063get-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
67publish() {
68 local version=$1
Carlos Sanchez3ffc4e42017-02-06 13:20:44 +010069 local variant=$2
70 local tag="${version}${variant}"
Carlos Sanchez0f763d42017-02-06 10:55:11 +010071 local sha
72 local build_opts="--no-cache --pull"
73
Carlos Sanchezdf48ce72017-03-07 18:15:06 +010074 if [ "$dry_run" = true ]; then
75 build_opts=""
76 else
77 build_opts="--no-cache --pull"
78 fi
79
Carlos Sanchez48c86a32017-03-07 17:56:42 +010080 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 Sanchez0f763d42017-02-06 10:55:11 +010086
Carlos Sanchez7b8382f2017-02-06 12:00:52 +010087 docker build --build-arg "JENKINS_VERSION=$version" \
88 --build-arg "JENKINS_SHA=$sha" \
Carlos Sanchez3ffc4e42017-02-06 13:20:44 +010089 --tag "jenkinsci/jenkins:${tag}" ${build_opts} .
Carlos Sanchez0f763d42017-02-06 10:55:11 +010090
Carlos Sanchezdf48ce72017-03-07 18:15:06 +010091 if [ "$dry_run" = true ]; then
92 docker push "jenkinsci/jenkins:${tag}"
93 fi
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +010094}
95
Carlos Sanchez881d1432017-02-15 10:02:16 +010096tag-and-push() {
97 local source=$1
98 local target=$2
Carlos Sanchezdbdcfe42017-03-07 09:42:40 +010099 local digest_source
100 local digest_target
101
102 # if tag doesn't exist yet, ie. dry run
Carlos Sanchezb857bb22017-03-07 19:28:02 +0100103 if ! digest_source=$(get-digest "${source}" 2>&1); then
104 echo "Unable to get digest for ${source}: ${digest_source}"
Carlos Sanchezdbdcfe42017-03-07 09:42:40 +0100105 digest_source=""
106 fi
Carlos Sanchezb857bb22017-03-07 19:28:02 +0100107 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 Sanchezef7d4ef2017-03-02 11:12:08 +0530112 echo "Images ${source} [$digest_source] and ${target} [$digest_target] are already the same, not updating tags"
Carlos Sanchezb108a532017-02-06 14:30:56 +0100113 else
Carlos Sanchez881d1432017-02-15 10:02:16 +0100114 echo "Creating tag ${target} pointing to ${source}"
Carlos Sanchezac44f2a2017-03-07 18:39:29 +0100115 docker-tag "${source}" "${target}"
Carlos Sanchez7fd3f212017-02-15 08:53:31 +0100116 if [ ! "$dry_run" = true ]; then
Carlos Sanchez881d1432017-02-15 10:02:16 +0100117 docker push "jenkinsci/jenkins:${source}"
Carlos Sanchez7fd3f212017-02-15 08:53:31 +0100118 fi
Carlos Sanchezb108a532017-02-06 14:30:56 +0100119 fi
Carlos Sanchez0f763d42017-02-06 10:55:11 +0100120}
121
Carlos Sanchez881d1432017-02-15 10:02:16 +0100122publish-latest() {
123 local version=$1
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +0100124 local variant=$2
Carlos Sanchez881d1432017-02-15 10:02:16 +0100125
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 Sanchez7fd3f212017-02-15 08:53:31 +0100131 fi
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +0100132}
133
Carlos Sanchez881d1432017-02-15 10:02:16 +0100134publish-lts() {
135 local version=$1
136 local variant=$2
137 tag-and-push "${version}" "lts${variant}"
138}
139
Carlos Sanchez7fd3f212017-02-15 08:53:31 +0100140dry_run=false
141if [ "-n" == "${1:-}" ]; then
142 dry_run=true
143fi
144if [ "$dry_run" = true ]; then
Carlos Sanchezdf48ce72017-03-07 18:15:06 +0100145 echo "Dry run, will not publish images"
Carlos Sanchez7fd3f212017-02-15 08:53:31 +0100146fi
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +0100147
Carlos Sanchez881d1432017-02-15 10:02:16 +0100148TOKEN=$(login-token)
149
Carlos Sanchez3ffc4e42017-02-06 13:20:44 +0100150variant=$(get-variant)
151
Carlos Sanchez881d1432017-02-15 10:02:16 +0100152lts_version=""
153version=""
Carlos Sanchez0f763d42017-02-06 10:55:11 +0100154for version in $(get-latest-versions); do
Carlos Sanchez881d1432017-02-15 10:02:16 +0100155 if is-published "$version$variant"; then
156 echo "Tag is already published: $version$variant"
Carlos Sanchez0f763d42017-02-06 10:55:11 +0100157 else
Carlos Sanchez881d1432017-02-15 10:02:16 +0100158 echo "Publishing version: $version$variant"
Carlos Sanchezdf48ce72017-03-07 18:15:06 +0100159 publish "$version" "$variant"
Carlos Sanchez0f763d42017-02-06 10:55:11 +0100160 fi
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +0100161
162 # Update lts tag
163 if [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
Carlos Sanchez881d1432017-02-15 10:02:16 +0100164 lts_version="${version}"
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +0100165 fi
Carlos Sanchez0f763d42017-02-06 10:55:11 +0100166done
167
Carlos Sanchez881d1432017-02-15 10:02:16 +0100168publish-latest "${version}" "${variant}"
169if [ -n "${lts_version}" ]; then
170 publish-lts "${lts_version}" "${variant}"
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +0100171fi