Carlos Sanchez | 0f763d4 | 2017-02-06 10:55:11 +0100 | [diff] [blame] | 1 | #!/bin/bash -eu |
| 2 | |
| 3 | # Publish any versions of the docker image not yet pushed to jenkinsci/jenkins |
| 4 | |
| 5 | set -o pipefail |
| 6 | |
| 7 | sort-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 |
| 16 | docker-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 Sanchez | 3ffc4e4 | 2017-02-06 13:20:44 +0100 | [diff] [blame] | 27 | get-variant() { |
Carlos Sanchez | d43f429 | 2017-02-06 13:50:34 +0100 | [diff] [blame] | 28 | local branch |
Carlos Sanchez | e43b7f9 | 2017-02-06 14:12:03 +0100 | [diff] [blame] | 29 | branch=$(git show-ref | grep $(git rev-list -n 1 HEAD) | tail -1 | rev | cut -d/ -f 1 | rev) |
Carlos Sanchez | d43f429 | 2017-02-06 13:50:34 +0100 | [diff] [blame] | 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 Sanchez | 3ffc4e4 | 2017-02-06 13:20:44 +0100 | [diff] [blame] | 34 | case "$branch" in |
| 35 | master) echo "" ;; |
| 36 | *) echo "-${branch}" ;; |
| 37 | esac |
| 38 | } |
| 39 | |
Carlos Sanchez | 0f763d4 | 2017-02-06 10:55:11 +0100 | [diff] [blame] | 40 | get-published-versions() { |
Carlos Sanchez | 3ffc4e4 | 2017-02-06 13:20:44 +0100 | [diff] [blame] | 41 | 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 Sanchez | 0f763d4 | 2017-02-06 10:55:11 +0100 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | get-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 | |
| 49 | publish() { |
| 50 | local version=$1 |
Carlos Sanchez | 3ffc4e4 | 2017-02-06 13:20:44 +0100 | [diff] [blame] | 51 | local variant=$2 |
| 52 | local tag="${version}${variant}" |
Carlos Sanchez | 0f763d4 | 2017-02-06 10:55:11 +0100 | [diff] [blame] | 53 | 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 Sanchez | 7b8382f | 2017-02-06 12:00:52 +0100 | [diff] [blame] | 58 | docker build --build-arg "JENKINS_VERSION=$version" \ |
| 59 | --build-arg "JENKINS_SHA=$sha" \ |
Carlos Sanchez | 3ffc4e4 | 2017-02-06 13:20:44 +0100 | [diff] [blame] | 60 | --tag "jenkinsci/jenkins:${tag}" ${build_opts} . |
Carlos Sanchez | 0f763d4 | 2017-02-06 10:55:11 +0100 | [diff] [blame] | 61 | |
Carlos Sanchez | 3ffc4e4 | 2017-02-06 13:20:44 +0100 | [diff] [blame] | 62 | docker push "jenkinsci/jenkins:${tag}" |
Carlos Sanchez | b108a53 | 2017-02-06 14:30:56 +0100 | [diff] [blame] | 63 | |
| 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 Sanchez | 0f763d4 | 2017-02-06 10:55:11 +0100 | [diff] [blame] | 72 | |
| 73 | # Update lts tag |
Carlos Sanchez | 7b8382f | 2017-02-06 12:00:52 +0100 | [diff] [blame] | 74 | if [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
Carlos Sanchez | 3ffc4e4 | 2017-02-06 13:20:44 +0100 | [diff] [blame] | 75 | echo "Updating lts${variant} tag to ${tag}" |
Carlos Sanchez | b108a53 | 2017-02-06 14:30:56 +0100 | [diff] [blame] | 76 | docker-tag "${tag}" "lts${variant}" |
Carlos Sanchez | 3ffc4e4 | 2017-02-06 13:20:44 +0100 | [diff] [blame] | 77 | docker push "jenkinsci/jenkins:lts${variant}" |
Carlos Sanchez | 0f763d4 | 2017-02-06 10:55:11 +0100 | [diff] [blame] | 78 | fi |
| 79 | } |
| 80 | |
Carlos Sanchez | 3ffc4e4 | 2017-02-06 13:20:44 +0100 | [diff] [blame] | 81 | variant=$(get-variant) |
| 82 | |
Carlos Sanchez | 0f763d4 | 2017-02-06 10:55:11 +0100 | [diff] [blame] | 83 | published_versions="$(get-published-versions)" |
| 84 | |
| 85 | for version in $(get-latest-versions); do |
Carlos Sanchez | 3ffc4e4 | 2017-02-06 13:20:44 +0100 | [diff] [blame] | 86 | tag="${version}${variant}" |
| 87 | if echo "${published_versions}" | grep -q "^${tag}$"; then |
| 88 | echo "Tag is already published: $tag" |
Carlos Sanchez | 0f763d4 | 2017-02-06 10:55:11 +0100 | [diff] [blame] | 89 | else |
Carlos Sanchez | 3ffc4e4 | 2017-02-06 13:20:44 +0100 | [diff] [blame] | 90 | echo "Publishing tag: $tag" |
| 91 | publish "$version" "$variant" |
Carlos Sanchez | 0f763d4 | 2017-02-06 10:55:11 +0100 | [diff] [blame] | 92 | fi |
| 93 | done |
| 94 | |
| 95 | |