blob: d59d450a8b940150d57355f0c24ea3011cd6abd9 [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
29 branch=$(git show-ref --heads | grep $(git rev-list -n 1 HEAD) | sed -e 's#.*/heads/##')
30 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-tag "${tag}" "latest${variant}"
Carlos Sanchez0f763d42017-02-06 10:55:11 +010063
Carlos Sanchez3ffc4e42017-02-06 13:20:44 +010064 docker push "jenkinsci/jenkins:${tag}"
65 docker push "jenkinsci/jenkins:latest${variant}"
Carlos Sanchez0f763d42017-02-06 10:55:11 +010066
67 # Update lts tag
Carlos Sanchez7b8382f2017-02-06 12:00:52 +010068 if [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
Carlos Sanchez3ffc4e42017-02-06 13:20:44 +010069 echo "Updating lts${variant} tag to ${tag}"
70 docker-tag "$version" "lts${variant}"
71 docker push "jenkinsci/jenkins:lts${variant}"
Carlos Sanchez0f763d42017-02-06 10:55:11 +010072 fi
73}
74
Carlos Sanchez3ffc4e42017-02-06 13:20:44 +010075variant=$(get-variant)
76
Carlos Sanchez0f763d42017-02-06 10:55:11 +010077published_versions="$(get-published-versions)"
78
79for version in $(get-latest-versions); do
Carlos Sanchez3ffc4e42017-02-06 13:20:44 +010080 tag="${version}${variant}"
81 if echo "${published_versions}" | grep -q "^${tag}$"; then
82 echo "Tag is already published: $tag"
Carlos Sanchez0f763d42017-02-06 10:55:11 +010083 else
Carlos Sanchez3ffc4e42017-02-06 13:20:44 +010084 echo "Publishing tag: $tag"
85 publish "$version" "$variant"
Carlos Sanchez0f763d42017-02-06 10:55:11 +010086 fi
87done
88
89