blob: cc7de19ba426f1f7c3a59f362f5fc02373d4ec42 [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 Sanchez0f763d42017-02-06 10:55:11 +010042get-published-versions() {
Carlos Sanchez3ffc4e42017-02-06 13:20:44 +010043 local regex="[0-9\.]+[a-z\-]*"
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +010044 curl -q -fsSL https://registry.hub.docker.com/v2/repositories/jenkinsci/jenkins/tags?page_size=30 | egrep -o "\"name\": \"${regex}\"" | egrep -o "${regex}"
Carlos Sanchez0f763d42017-02-06 10:55:11 +010045}
46
47get-latest-versions() {
48 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
49}
50
51publish() {
52 local version=$1
Carlos Sanchez3ffc4e42017-02-06 13:20:44 +010053 local variant=$2
54 local tag="${version}${variant}"
Carlos Sanchez0f763d42017-02-06 10:55:11 +010055 local sha
56 local build_opts="--no-cache --pull"
57
58 sha=$(curl -q -fsSL "http://repo.jenkins-ci.org/simple/releases/org/jenkins-ci/main/jenkins-war/${version}/jenkins-war-${version}.war.sha1")
59
Carlos Sanchez7b8382f2017-02-06 12:00:52 +010060 docker build --build-arg "JENKINS_VERSION=$version" \
61 --build-arg "JENKINS_SHA=$sha" \
Carlos Sanchez3ffc4e42017-02-06 13:20:44 +010062 --tag "jenkinsci/jenkins:${tag}" ${build_opts} .
Carlos Sanchez0f763d42017-02-06 10:55:11 +010063
Carlos Sanchez3ffc4e42017-02-06 13:20:44 +010064 docker push "jenkinsci/jenkins:${tag}"
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +010065}
66
67publish-latest() {
68 local tag=$1
69 local variant=$2
Carlos Sanchezb108a532017-02-06 14:30:56 +010070
71 # push latest (for master) or the name of the branch (for other branches)
72 if [ -z "${variant}" ]; then
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +010073 echo "Updating latest tag to ${tag}"
Carlos Sanchez7fd3f212017-02-15 08:53:31 +010074 if [ ! "$dry_run" = true ]; then
75 docker-tag "${tag}" "latest"
76 docker push "jenkinsci/jenkins:latest"
77 fi
Carlos Sanchezb108a532017-02-06 14:30:56 +010078 else
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +010079 echo "Updating ${variant#-} tag to ${tag}"
Carlos Sanchez7fd3f212017-02-15 08:53:31 +010080 if [ ! "$dry_run" = true ]; then
81 docker-tag "${tag}" "${variant#-}"
82 docker push "jenkinsci/jenkins:${variant#-}"
83 fi
Carlos Sanchezb108a532017-02-06 14:30:56 +010084 fi
Carlos Sanchez0f763d42017-02-06 10:55:11 +010085}
86
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +010087publish-lts() {
88 local tag=$1
89 local variant=$2
90 echo "Updating lts${variant} tag to ${lts_tag}"
Carlos Sanchez7fd3f212017-02-15 08:53:31 +010091 if [ ! "$dry_run" = true ]; then
92 docker-tag "${lts_tag}" "lts${variant}"
93 docker push "jenkinsci/jenkins:lts${variant}"
94 fi
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +010095}
96
Carlos Sanchez7fd3f212017-02-15 08:53:31 +010097dry_run=false
98if [ "-n" == "${1:-}" ]; then
99 dry_run=true
100fi
101if [ "$dry_run" = true ]; then
102 echo "Dry run, will not build or publish images"
103fi
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +0100104
Carlos Sanchez3ffc4e42017-02-06 13:20:44 +0100105variant=$(get-variant)
106
Carlos Sanchez0f763d42017-02-06 10:55:11 +0100107published_versions="$(get-published-versions)"
108
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +0100109lts_tag=""
110tag=""
Carlos Sanchez0f763d42017-02-06 10:55:11 +0100111for version in $(get-latest-versions); do
Carlos Sanchez3ffc4e42017-02-06 13:20:44 +0100112 tag="${version}${variant}"
113 if echo "${published_versions}" | grep -q "^${tag}$"; then
114 echo "Tag is already published: $tag"
Carlos Sanchez0f763d42017-02-06 10:55:11 +0100115 else
Carlos Sanchez3ffc4e42017-02-06 13:20:44 +0100116 echo "Publishing tag: $tag"
Carlos Sanchez7fd3f212017-02-15 08:53:31 +0100117 if [ ! "$dry_run" = true ]; then
118 publish "$version" "$variant"
119 fi
Carlos Sanchez0f763d42017-02-06 10:55:11 +0100120 fi
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +0100121
122 # Update lts tag
123 if [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
124 lts_tag="${tag}"
125 fi
Carlos Sanchez0f763d42017-02-06 10:55:11 +0100126done
127
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +0100128publish-latest "${tag}" "${variant}"
129if [ -n "${lts_tag}" ]; then
130 publish-lts "${tag}" "${variant}"
131fi