blob: e200ed5852a3011ac9443d5ecf68271095d6fa3f [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\-]*"
42 curl -q -fsSL https://registry.hub.docker.com/v1/repositories/jenkinsci/jenkins/tags | 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 Sanchezb108a532017-02-06 14:30:56 +010063
64 # push latest (for master) or the name of the branch (for other branches)
65 if [ -z "${variant}" ]; then
66 docker-tag "${tag}" "latest"
67 docker push "jenkinsci/jenkins:latest"
68 else
69 docker-tag "${tag}" "${variant#-}"
70 docker push "jenkinsci/jenkins:${variant#-}"
71 fi
Carlos Sanchez0f763d42017-02-06 10:55:11 +010072
73 # Update lts tag
Carlos Sanchez7b8382f2017-02-06 12:00:52 +010074 if [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
Carlos Sanchez3ffc4e42017-02-06 13:20:44 +010075 echo "Updating lts${variant} tag to ${tag}"
Carlos Sanchezb108a532017-02-06 14:30:56 +010076 docker-tag "${tag}" "lts${variant}"
Carlos Sanchez3ffc4e42017-02-06 13:20:44 +010077 docker push "jenkinsci/jenkins:lts${variant}"
Carlos Sanchez0f763d42017-02-06 10:55:11 +010078 fi
79}
80
Carlos Sanchez3ffc4e42017-02-06 13:20:44 +010081variant=$(get-variant)
82
Carlos Sanchez0f763d42017-02-06 10:55:11 +010083published_versions="$(get-published-versions)"
84
85for version in $(get-latest-versions); do
Carlos Sanchez3ffc4e42017-02-06 13:20:44 +010086 tag="${version}${variant}"
87 if echo "${published_versions}" | grep -q "^${tag}$"; then
88 echo "Tag is already published: $tag"
Carlos Sanchez0f763d42017-02-06 10:55:11 +010089 else
Carlos Sanchez3ffc4e42017-02-06 13:20:44 +010090 echo "Publishing tag: $tag"
91 publish "$version" "$variant"
Carlos Sanchez0f763d42017-02-06 10:55:11 +010092 fi
93done
94
95