blob: 6b08b85eb8402eff1236dd0948d3907c171add2c [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
22 if out=$(docker tag -f "$from" "$to" 2>&1); then
23 echo "$out"
24 else
25 docker tag "$from" "$to"
26 fi
27}
28
Carlos Sanchez3ffc4e42017-02-06 13:20:44 +010029get-variant() {
Carlos Sanchezd43f4292017-02-06 13:50:34 +010030 local branch
Carlos Sancheze43b7f92017-02-06 14:12:03 +010031 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 +010032 if [ -z "$branch" ]; then
33 >&2 echo "Could not get the current branch name for commit, not in a branch?: $(git rev-list -n 1 HEAD)"
34 return 1
35 fi
Carlos Sanchez3ffc4e42017-02-06 13:20:44 +010036 case "$branch" in
37 master) echo "" ;;
38 *) echo "-${branch}" ;;
39 esac
40}
41
Carlos Sanchez881d1432017-02-15 10:02:16 +010042login-token() {
43 # could use jq .token
44 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
45}
46
47is-published() {
Carlos Sanchezef7d4ef2017-03-02 11:12:08 +053048 get-manifest "$1" &> /dev/null
Carlos Sanchez881d1432017-02-15 10:02:16 +010049}
50
51get-manifest() {
52 local tag=$1
53 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"
54}
55
56get-digest() {
57 #get-manifest "$1" | jq .config.digest
58 get-manifest "$1" | grep -A 10 -o '"config".*' | grep digest | head -1 | cut -d':' -f 2,3 | xargs echo
59}
60
Carlos Sanchez0f763d42017-02-06 10:55:11 +010061get-latest-versions() {
62 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
63}
64
65publish() {
66 local version=$1
Carlos Sanchez3ffc4e42017-02-06 13:20:44 +010067 local variant=$2
68 local tag="${version}${variant}"
Carlos Sanchez0f763d42017-02-06 10:55:11 +010069 local sha
70 local build_opts="--no-cache --pull"
71
Carlos Sanchezdf48ce72017-03-07 18:15:06 +010072 if [ "$dry_run" = true ]; then
73 build_opts=""
74 else
75 build_opts="--no-cache --pull"
76 fi
77
Carlos Sanchez48c86a32017-03-07 17:56:42 +010078 local dir=war
79 # lts is in a different dir
80 if [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
81 dir=war-stable
82 fi
83 sha=$(curl -q -fsSL "http://mirrors.jenkins.io/${dir}/${version}/jenkins.war.sha256" | cut -d' ' -f 1)
Carlos Sanchez0f763d42017-02-06 10:55:11 +010084
Carlos Sanchez7b8382f2017-02-06 12:00:52 +010085 docker build --build-arg "JENKINS_VERSION=$version" \
86 --build-arg "JENKINS_SHA=$sha" \
Carlos Sanchez3ffc4e42017-02-06 13:20:44 +010087 --tag "jenkinsci/jenkins:${tag}" ${build_opts} .
Carlos Sanchez0f763d42017-02-06 10:55:11 +010088
Carlos Sanchezdf48ce72017-03-07 18:15:06 +010089 if [ "$dry_run" = true ]; then
90 docker push "jenkinsci/jenkins:${tag}"
91 fi
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +010092}
93
Carlos Sanchez881d1432017-02-15 10:02:16 +010094tag-and-push() {
95 local source=$1
96 local target=$2
Carlos Sanchezdbdcfe42017-03-07 09:42:40 +010097 local digest_source
98 local digest_target
99
100 # if tag doesn't exist yet, ie. dry run
101 if ! digest_source=$(get-digest "${source}" 2>/dev/null); then
102 digest_source=""
103 fi
104 digest_target=$(get-digest "${target}")
Carlos Sanchezef7d4ef2017-03-02 11:12:08 +0530105 if [ "$digest_source" == "$digest_target" ]; then
106 echo "Images ${source} [$digest_source] and ${target} [$digest_target] are already the same, not updating tags"
Carlos Sanchezb108a532017-02-06 14:30:56 +0100107 else
Carlos Sanchez881d1432017-02-15 10:02:16 +0100108 echo "Creating tag ${target} pointing to ${source}"
Carlos Sanchezdf48ce72017-03-07 18:15:06 +0100109 docker-tag "jenkinsci/jenkins:${source}" "jenkinsci/jenkins:${target}"
Carlos Sanchez7fd3f212017-02-15 08:53:31 +0100110 if [ ! "$dry_run" = true ]; then
Carlos Sanchez881d1432017-02-15 10:02:16 +0100111 docker push "jenkinsci/jenkins:${source}"
Carlos Sanchez7fd3f212017-02-15 08:53:31 +0100112 fi
Carlos Sanchezb108a532017-02-06 14:30:56 +0100113 fi
Carlos Sanchez0f763d42017-02-06 10:55:11 +0100114}
115
Carlos Sanchez881d1432017-02-15 10:02:16 +0100116publish-latest() {
117 local version=$1
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +0100118 local variant=$2
Carlos Sanchez881d1432017-02-15 10:02:16 +0100119
120 # push latest (for master) or the name of the branch (for other branches)
121 if [ -z "${variant}" ]; then
122 tag-and-push "${version}${variant}" "latest"
123 else
124 tag-and-push "${version}${variant}" "${variant#-}"
Carlos Sanchez7fd3f212017-02-15 08:53:31 +0100125 fi
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +0100126}
127
Carlos Sanchez881d1432017-02-15 10:02:16 +0100128publish-lts() {
129 local version=$1
130 local variant=$2
131 tag-and-push "${version}" "lts${variant}"
132}
133
Carlos Sanchez7fd3f212017-02-15 08:53:31 +0100134dry_run=false
135if [ "-n" == "${1:-}" ]; then
136 dry_run=true
137fi
138if [ "$dry_run" = true ]; then
Carlos Sanchezdf48ce72017-03-07 18:15:06 +0100139 echo "Dry run, will not publish images"
Carlos Sanchez7fd3f212017-02-15 08:53:31 +0100140fi
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +0100141
Carlos Sanchez881d1432017-02-15 10:02:16 +0100142TOKEN=$(login-token)
143
Carlos Sanchez3ffc4e42017-02-06 13:20:44 +0100144variant=$(get-variant)
145
Carlos Sanchez881d1432017-02-15 10:02:16 +0100146lts_version=""
147version=""
Carlos Sanchez0f763d42017-02-06 10:55:11 +0100148for version in $(get-latest-versions); do
Carlos Sanchez881d1432017-02-15 10:02:16 +0100149 if is-published "$version$variant"; then
150 echo "Tag is already published: $version$variant"
Carlos Sanchez0f763d42017-02-06 10:55:11 +0100151 else
Carlos Sanchez881d1432017-02-15 10:02:16 +0100152 echo "Publishing version: $version$variant"
Carlos Sanchezdf48ce72017-03-07 18:15:06 +0100153 publish "$version" "$variant"
Carlos Sanchez0f763d42017-02-06 10:55:11 +0100154 fi
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +0100155
156 # Update lts tag
157 if [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
Carlos Sanchez881d1432017-02-15 10:02:16 +0100158 lts_version="${version}"
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +0100159 fi
Carlos Sanchez0f763d42017-02-06 10:55:11 +0100160done
161
Carlos Sanchez881d1432017-02-15 10:02:16 +0100162publish-latest "${version}" "${variant}"
163if [ -n "${lts_version}" ]; then
164 publish-lts "${lts_version}" "${variant}"
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +0100165fi