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 |
Carlos Sanchez | 7fd3f21 | 2017-02-15 08:53:31 +0100 | [diff] [blame] | 4 | # Arguments: |
| 5 | # -n dry run, do not build or publish images |
Carlos Sanchez | 0f763d4 | 2017-02-06 10:55:11 +0100 | [diff] [blame] | 6 | |
| 7 | set -o pipefail |
| 8 | |
| 9 | sort-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 |
| 18 | docker-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 Sanchez | 3ffc4e4 | 2017-02-06 13:20:44 +0100 | [diff] [blame] | 29 | get-variant() { |
Carlos Sanchez | d43f429 | 2017-02-06 13:50:34 +0100 | [diff] [blame] | 30 | local branch |
Carlos Sanchez | e43b7f9 | 2017-02-06 14:12:03 +0100 | [diff] [blame] | 31 | 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] | 32 | 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 Sanchez | 3ffc4e4 | 2017-02-06 13:20:44 +0100 | [diff] [blame] | 36 | case "$branch" in |
| 37 | master) echo "" ;; |
| 38 | *) echo "-${branch}" ;; |
| 39 | esac |
| 40 | } |
| 41 | |
Carlos Sanchez | 881d143 | 2017-02-15 10:02:16 +0100 | [diff] [blame] | 42 | login-token() { |
| 43 | # could use jq .token |
| 44 | curl -q -sSL https://auth.docker.io/token\?service\=registry.docker.io\&scope\=repository:jenkinsci/jenkins:pull | grep -o '"token":"[^"]*"' | cut -d':' -f 2 | xargs echo |
| 45 | } |
| 46 | |
| 47 | is-published() { |
Carlos Sanchez | ef7d4ef | 2017-03-02 11:12:08 +0530 | [diff] [blame] | 48 | get-manifest "$1" &> /dev/null |
Carlos Sanchez | 881d143 | 2017-02-15 10:02:16 +0100 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | get-manifest() { |
| 52 | local tag=$1 |
| 53 | curl -q -fsSL -H "Accept: application/vnd.docker.distribution.manifest.v2+json" -H "Authorization: Bearer $TOKEN" "https://index.docker.io/v2/jenkinsci/jenkins/manifests/$tag" |
| 54 | } |
| 55 | |
| 56 | get-digest() { |
| 57 | #get-manifest "$1" | jq .config.digest |
| 58 | get-manifest "$1" | grep -A 10 -o '"config".*' | grep digest | head -1 | cut -d':' -f 2,3 | xargs echo |
| 59 | } |
| 60 | |
Carlos Sanchez | 0f763d4 | 2017-02-06 10:55:11 +0100 | [diff] [blame] | 61 | get-latest-versions() { |
| 62 | 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 |
| 63 | } |
| 64 | |
| 65 | publish() { |
| 66 | local version=$1 |
Carlos Sanchez | 3ffc4e4 | 2017-02-06 13:20:44 +0100 | [diff] [blame] | 67 | local variant=$2 |
| 68 | local tag="${version}${variant}" |
Carlos Sanchez | 0f763d4 | 2017-02-06 10:55:11 +0100 | [diff] [blame] | 69 | local sha |
| 70 | local build_opts="--no-cache --pull" |
| 71 | |
Carlos Sanchez | 48c86a3 | 2017-03-07 17:56:42 +0100 | [diff] [blame^] | 72 | local dir=war |
| 73 | # lts is in a different dir |
| 74 | if [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 75 | dir=war-stable |
| 76 | fi |
| 77 | sha=$(curl -q -fsSL "http://mirrors.jenkins.io/${dir}/${version}/jenkins.war.sha256" | cut -d' ' -f 1) |
Carlos Sanchez | 0f763d4 | 2017-02-06 10:55:11 +0100 | [diff] [blame] | 78 | |
Carlos Sanchez | 7b8382f | 2017-02-06 12:00:52 +0100 | [diff] [blame] | 79 | docker build --build-arg "JENKINS_VERSION=$version" \ |
| 80 | --build-arg "JENKINS_SHA=$sha" \ |
Carlos Sanchez | 3ffc4e4 | 2017-02-06 13:20:44 +0100 | [diff] [blame] | 81 | --tag "jenkinsci/jenkins:${tag}" ${build_opts} . |
Carlos Sanchez | 0f763d4 | 2017-02-06 10:55:11 +0100 | [diff] [blame] | 82 | |
Carlos Sanchez | 3ffc4e4 | 2017-02-06 13:20:44 +0100 | [diff] [blame] | 83 | docker push "jenkinsci/jenkins:${tag}" |
Carlos Sanchez | 4b0dcec | 2017-02-14 00:10:14 +0100 | [diff] [blame] | 84 | } |
| 85 | |
Carlos Sanchez | 881d143 | 2017-02-15 10:02:16 +0100 | [diff] [blame] | 86 | tag-and-push() { |
| 87 | local source=$1 |
| 88 | local target=$2 |
Carlos Sanchez | dbdcfe4 | 2017-03-07 09:42:40 +0100 | [diff] [blame] | 89 | local digest_source |
| 90 | local digest_target |
| 91 | |
| 92 | # if tag doesn't exist yet, ie. dry run |
| 93 | if ! digest_source=$(get-digest "${source}" 2>/dev/null); then |
| 94 | digest_source="" |
| 95 | fi |
| 96 | digest_target=$(get-digest "${target}") |
Carlos Sanchez | ef7d4ef | 2017-03-02 11:12:08 +0530 | [diff] [blame] | 97 | if [ "$digest_source" == "$digest_target" ]; then |
| 98 | echo "Images ${source} [$digest_source] and ${target} [$digest_target] are already the same, not updating tags" |
Carlos Sanchez | b108a53 | 2017-02-06 14:30:56 +0100 | [diff] [blame] | 99 | else |
Carlos Sanchez | 881d143 | 2017-02-15 10:02:16 +0100 | [diff] [blame] | 100 | echo "Creating tag ${target} pointing to ${source}" |
Carlos Sanchez | 7fd3f21 | 2017-02-15 08:53:31 +0100 | [diff] [blame] | 101 | if [ ! "$dry_run" = true ]; then |
Carlos Sanchez | 881d143 | 2017-02-15 10:02:16 +0100 | [diff] [blame] | 102 | docker-tag "jenkinsci/jenkins:${source}" "jenkinsci/jenkins:${target}" |
| 103 | docker push "jenkinsci/jenkins:${source}" |
Carlos Sanchez | 7fd3f21 | 2017-02-15 08:53:31 +0100 | [diff] [blame] | 104 | fi |
Carlos Sanchez | b108a53 | 2017-02-06 14:30:56 +0100 | [diff] [blame] | 105 | fi |
Carlos Sanchez | 0f763d4 | 2017-02-06 10:55:11 +0100 | [diff] [blame] | 106 | } |
| 107 | |
Carlos Sanchez | 881d143 | 2017-02-15 10:02:16 +0100 | [diff] [blame] | 108 | publish-latest() { |
| 109 | local version=$1 |
Carlos Sanchez | 4b0dcec | 2017-02-14 00:10:14 +0100 | [diff] [blame] | 110 | local variant=$2 |
Carlos Sanchez | 881d143 | 2017-02-15 10:02:16 +0100 | [diff] [blame] | 111 | |
| 112 | # push latest (for master) or the name of the branch (for other branches) |
| 113 | if [ -z "${variant}" ]; then |
| 114 | tag-and-push "${version}${variant}" "latest" |
| 115 | else |
| 116 | tag-and-push "${version}${variant}" "${variant#-}" |
Carlos Sanchez | 7fd3f21 | 2017-02-15 08:53:31 +0100 | [diff] [blame] | 117 | fi |
Carlos Sanchez | 4b0dcec | 2017-02-14 00:10:14 +0100 | [diff] [blame] | 118 | } |
| 119 | |
Carlos Sanchez | 881d143 | 2017-02-15 10:02:16 +0100 | [diff] [blame] | 120 | publish-lts() { |
| 121 | local version=$1 |
| 122 | local variant=$2 |
| 123 | tag-and-push "${version}" "lts${variant}" |
| 124 | } |
| 125 | |
Carlos Sanchez | 7fd3f21 | 2017-02-15 08:53:31 +0100 | [diff] [blame] | 126 | dry_run=false |
| 127 | if [ "-n" == "${1:-}" ]; then |
| 128 | dry_run=true |
| 129 | fi |
| 130 | if [ "$dry_run" = true ]; then |
| 131 | echo "Dry run, will not build or publish images" |
| 132 | fi |
Carlos Sanchez | 4b0dcec | 2017-02-14 00:10:14 +0100 | [diff] [blame] | 133 | |
Carlos Sanchez | 881d143 | 2017-02-15 10:02:16 +0100 | [diff] [blame] | 134 | TOKEN=$(login-token) |
| 135 | |
Carlos Sanchez | 3ffc4e4 | 2017-02-06 13:20:44 +0100 | [diff] [blame] | 136 | variant=$(get-variant) |
| 137 | |
Carlos Sanchez | 881d143 | 2017-02-15 10:02:16 +0100 | [diff] [blame] | 138 | lts_version="" |
| 139 | version="" |
Carlos Sanchez | 0f763d4 | 2017-02-06 10:55:11 +0100 | [diff] [blame] | 140 | for version in $(get-latest-versions); do |
Carlos Sanchez | 881d143 | 2017-02-15 10:02:16 +0100 | [diff] [blame] | 141 | if is-published "$version$variant"; then |
| 142 | echo "Tag is already published: $version$variant" |
Carlos Sanchez | 0f763d4 | 2017-02-06 10:55:11 +0100 | [diff] [blame] | 143 | else |
Carlos Sanchez | 881d143 | 2017-02-15 10:02:16 +0100 | [diff] [blame] | 144 | echo "Publishing version: $version$variant" |
Carlos Sanchez | 7fd3f21 | 2017-02-15 08:53:31 +0100 | [diff] [blame] | 145 | if [ ! "$dry_run" = true ]; then |
| 146 | publish "$version" "$variant" |
| 147 | fi |
Carlos Sanchez | 0f763d4 | 2017-02-06 10:55:11 +0100 | [diff] [blame] | 148 | fi |
Carlos Sanchez | 4b0dcec | 2017-02-14 00:10:14 +0100 | [diff] [blame] | 149 | |
| 150 | # Update lts tag |
| 151 | if [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
Carlos Sanchez | 881d143 | 2017-02-15 10:02:16 +0100 | [diff] [blame] | 152 | lts_version="${version}" |
Carlos Sanchez | 4b0dcec | 2017-02-14 00:10:14 +0100 | [diff] [blame] | 153 | fi |
Carlos Sanchez | 0f763d4 | 2017-02-06 10:55:11 +0100 | [diff] [blame] | 154 | done |
| 155 | |
Carlos Sanchez | 881d143 | 2017-02-15 10:02:16 +0100 | [diff] [blame] | 156 | publish-latest "${version}" "${variant}" |
| 157 | if [ -n "${lts_version}" ]; then |
| 158 | publish-lts "${lts_version}" "${variant}" |
Carlos Sanchez | 4b0dcec | 2017-02-14 00:10:14 +0100 | [diff] [blame] | 159 | fi |