blob: 886ed4f189cc37a3f5d03cdc29edfce84ee5a038 [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
4
5set -o pipefail
6
7sort-versions() {
8 if [ "$(uname)" == 'Darwin' ]; then
9 gsort --version-sort
10 else
11 sort --version-sort
12 fi
13}
14
15# Try tagging with and without -f to support all versions of docker
16docker-tag() {
17 local from="jenkinsci/jenkins:$1"
18 local to="jenkinsci/jenkins:$2"
19 local out
20 if out=$(docker tag -f "$from" "$to" 2>&1); then
21 echo "$out"
22 else
23 docker tag "$from" "$to"
24 fi
25}
26
Carlos Sanchez3ffc4e42017-02-06 13:20:44 +010027get-variant() {
Carlos Sanchezd43f4292017-02-06 13:50:34 +010028 local branch
Carlos Sancheze43b7f92017-02-06 14:12:03 +010029 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 +010030 if [ -z "$branch" ]; then
31 >&2 echo "Could not get the current branch name for commit, not in a branch?: $(git rev-list -n 1 HEAD)"
32 return 1
33 fi
Carlos Sanchez3ffc4e42017-02-06 13:20:44 +010034 case "$branch" in
35 master) echo "" ;;
36 *) echo "-${branch}" ;;
37 esac
38}
39
Carlos Sanchez0f763d42017-02-06 10:55:11 +010040get-published-versions() {
Carlos Sanchez3ffc4e42017-02-06 13:20:44 +010041 local regex="[0-9\.]+[a-z\-]*"
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +010042 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 +010043}
44
45get-latest-versions() {
46 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
47}
48
49publish() {
50 local version=$1
Carlos Sanchez3ffc4e42017-02-06 13:20:44 +010051 local variant=$2
52 local tag="${version}${variant}"
Carlos Sanchez0f763d42017-02-06 10:55:11 +010053 local sha
54 local build_opts="--no-cache --pull"
55
56 sha=$(curl -q -fsSL "http://repo.jenkins-ci.org/simple/releases/org/jenkins-ci/main/jenkins-war/${version}/jenkins-war-${version}.war.sha1")
57
Carlos Sanchez7b8382f2017-02-06 12:00:52 +010058 docker build --build-arg "JENKINS_VERSION=$version" \
59 --build-arg "JENKINS_SHA=$sha" \
Carlos Sanchez3ffc4e42017-02-06 13:20:44 +010060 --tag "jenkinsci/jenkins:${tag}" ${build_opts} .
Carlos Sanchez0f763d42017-02-06 10:55:11 +010061
Carlos Sanchez3ffc4e42017-02-06 13:20:44 +010062 docker push "jenkinsci/jenkins:${tag}"
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +010063}
64
65publish-latest() {
66 local tag=$1
67 local variant=$2
Carlos Sanchezb108a532017-02-06 14:30:56 +010068
69 # push latest (for master) or the name of the branch (for other branches)
70 if [ -z "${variant}" ]; then
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +010071 echo "Updating latest tag to ${tag}"
Carlos Sanchezb108a532017-02-06 14:30:56 +010072 docker-tag "${tag}" "latest"
73 docker push "jenkinsci/jenkins:latest"
74 else
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +010075 echo "Updating ${variant#-} tag to ${tag}"
Carlos Sanchezb108a532017-02-06 14:30:56 +010076 docker-tag "${tag}" "${variant#-}"
77 docker push "jenkinsci/jenkins:${variant#-}"
78 fi
Carlos Sanchez0f763d42017-02-06 10:55:11 +010079}
80
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +010081publish-lts() {
82 local tag=$1
83 local variant=$2
84 echo "Updating lts${variant} tag to ${lts_tag}"
85 docker-tag "${lts_tag}" "lts${variant}"
86 docker push "jenkinsci/jenkins:lts${variant}"
87}
88
89
Carlos Sanchez3ffc4e42017-02-06 13:20:44 +010090variant=$(get-variant)
91
Carlos Sanchez0f763d42017-02-06 10:55:11 +010092published_versions="$(get-published-versions)"
93
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +010094lts_tag=""
95tag=""
Carlos Sanchez0f763d42017-02-06 10:55:11 +010096for version in $(get-latest-versions); do
Carlos Sanchez3ffc4e42017-02-06 13:20:44 +010097 tag="${version}${variant}"
98 if echo "${published_versions}" | grep -q "^${tag}$"; then
99 echo "Tag is already published: $tag"
Carlos Sanchez0f763d42017-02-06 10:55:11 +0100100 else
Carlos Sanchez3ffc4e42017-02-06 13:20:44 +0100101 echo "Publishing tag: $tag"
102 publish "$version" "$variant"
Carlos Sanchez0f763d42017-02-06 10:55:11 +0100103 fi
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +0100104
105 # Update lts tag
106 if [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
107 lts_tag="${tag}"
108 fi
Carlos Sanchez0f763d42017-02-06 10:55:11 +0100109done
110
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +0100111publish-latest "${tag}" "${variant}"
112if [ -n "${lts_tag}" ]; then
113 publish-lts "${tag}" "${variant}"
114fi