blob: 8518ffd1158ca92f622af78c47fd39e3a1cace9d [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() {
48 get-manifest "$1" > /dev/null
49}
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
61compare-digest() {
62 local tag1=$1
63 local tag2=$2
64 [ "$(get-digest ${tag1})" == "$(get-digest ${tag2})" ]
Carlos Sanchez0f763d42017-02-06 10:55:11 +010065}
66
67get-latest-versions() {
68 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
69}
70
71publish() {
72 local version=$1
Carlos Sanchez3ffc4e42017-02-06 13:20:44 +010073 local variant=$2
74 local tag="${version}${variant}"
Carlos Sanchez0f763d42017-02-06 10:55:11 +010075 local sha
76 local build_opts="--no-cache --pull"
77
78 sha=$(curl -q -fsSL "http://repo.jenkins-ci.org/simple/releases/org/jenkins-ci/main/jenkins-war/${version}/jenkins-war-${version}.war.sha1")
79
Carlos Sanchez7b8382f2017-02-06 12:00:52 +010080 docker build --build-arg "JENKINS_VERSION=$version" \
81 --build-arg "JENKINS_SHA=$sha" \
Carlos Sanchez3ffc4e42017-02-06 13:20:44 +010082 --tag "jenkinsci/jenkins:${tag}" ${build_opts} .
Carlos Sanchez0f763d42017-02-06 10:55:11 +010083
Carlos Sanchez3ffc4e42017-02-06 13:20:44 +010084 docker push "jenkinsci/jenkins:${tag}"
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +010085}
86
Carlos Sanchez881d1432017-02-15 10:02:16 +010087tag-and-push() {
88 local source=$1
89 local target=$2
90 if compare-digest "${source}" "${target}"; then
91 echo "Images ${source} and ${target} are already the same, not updating tags"
Carlos Sanchezb108a532017-02-06 14:30:56 +010092 else
Carlos Sanchez881d1432017-02-15 10:02:16 +010093 echo "Creating tag ${target} pointing to ${source}"
Carlos Sanchez7fd3f212017-02-15 08:53:31 +010094 if [ ! "$dry_run" = true ]; then
Carlos Sanchez881d1432017-02-15 10:02:16 +010095 docker-tag "jenkinsci/jenkins:${source}" "jenkinsci/jenkins:${target}"
96 docker push "jenkinsci/jenkins:${source}"
Carlos Sanchez7fd3f212017-02-15 08:53:31 +010097 fi
Carlos Sanchezb108a532017-02-06 14:30:56 +010098 fi
Carlos Sanchez0f763d42017-02-06 10:55:11 +010099}
100
Carlos Sanchez881d1432017-02-15 10:02:16 +0100101publish-latest() {
102 local version=$1
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +0100103 local variant=$2
Carlos Sanchez881d1432017-02-15 10:02:16 +0100104
105 # push latest (for master) or the name of the branch (for other branches)
106 if [ -z "${variant}" ]; then
107 tag-and-push "${version}${variant}" "latest"
108 else
109 tag-and-push "${version}${variant}" "${variant#-}"
Carlos Sanchez7fd3f212017-02-15 08:53:31 +0100110 fi
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +0100111}
112
Carlos Sanchez881d1432017-02-15 10:02:16 +0100113publish-lts() {
114 local version=$1
115 local variant=$2
116 tag-and-push "${version}" "lts${variant}"
117}
118
Carlos Sanchez7fd3f212017-02-15 08:53:31 +0100119dry_run=false
120if [ "-n" == "${1:-}" ]; then
121 dry_run=true
122fi
123if [ "$dry_run" = true ]; then
124 echo "Dry run, will not build or publish images"
125fi
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +0100126
Carlos Sanchez881d1432017-02-15 10:02:16 +0100127TOKEN=$(login-token)
128
Carlos Sanchez3ffc4e42017-02-06 13:20:44 +0100129variant=$(get-variant)
130
Carlos Sanchez881d1432017-02-15 10:02:16 +0100131lts_version=""
132version=""
Carlos Sanchez0f763d42017-02-06 10:55:11 +0100133for version in $(get-latest-versions); do
Carlos Sanchez881d1432017-02-15 10:02:16 +0100134 if is-published "$version$variant"; then
135 echo "Tag is already published: $version$variant"
Carlos Sanchez0f763d42017-02-06 10:55:11 +0100136 else
Carlos Sanchez881d1432017-02-15 10:02:16 +0100137 echo "Publishing version: $version$variant"
Carlos Sanchez7fd3f212017-02-15 08:53:31 +0100138 if [ ! "$dry_run" = true ]; then
139 publish "$version" "$variant"
140 fi
Carlos Sanchez0f763d42017-02-06 10:55:11 +0100141 fi
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +0100142
143 # Update lts tag
144 if [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
Carlos Sanchez881d1432017-02-15 10:02:16 +0100145 lts_version="${version}"
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +0100146 fi
Carlos Sanchez0f763d42017-02-06 10:55:11 +0100147done
148
Carlos Sanchez881d1432017-02-15 10:02:16 +0100149publish-latest "${version}" "${variant}"
150if [ -n "${lts_version}" ]; then
151 publish-lts "${lts_version}" "${variant}"
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +0100152fi