blob: d435b17cf9330cdfab4be05d7be18e875a8d66ae [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
Carlos Sanchez7fd3f212017-02-15 08:53:31 +01004# Arguments:
5# -n dry run, do not build or publish images
Carlos Sanchez09f1e502017-03-07 20:06:42 +01006# -d debug
Carlos Sanchez0f763d42017-02-06 10:55:11 +01007
8set -o pipefail
9
10sort-versions() {
11 if [ "$(uname)" == 'Darwin' ]; then
12 gsort --version-sort
13 else
14 sort --version-sort
15 fi
16}
17
18# Try tagging with and without -f to support all versions of docker
19docker-tag() {
20 local from="jenkinsci/jenkins:$1"
21 local to="jenkinsci/jenkins:$2"
22 local out
Carlos Sanchez4d0468a2017-03-07 19:37:18 +010023
24 docker pull "$from"
Carlos Sanchez0f763d42017-02-06 10:55:11 +010025 if out=$(docker tag -f "$from" "$to" 2>&1); then
26 echo "$out"
27 else
28 docker tag "$from" "$to"
29 fi
30}
31
Carlos Sanchez3ffc4e42017-02-06 13:20:44 +010032get-variant() {
Carlos Sanchezd43f4292017-02-06 13:50:34 +010033 local branch
Carlos Sancheze43b7f92017-02-06 14:12:03 +010034 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 +010035 if [ -z "$branch" ]; then
36 >&2 echo "Could not get the current branch name for commit, not in a branch?: $(git rev-list -n 1 HEAD)"
37 return 1
38 fi
Carlos Sanchez3ffc4e42017-02-06 13:20:44 +010039 case "$branch" in
40 master) echo "" ;;
41 *) echo "-${branch}" ;;
42 esac
43}
44
Carlos Sanchez881d1432017-02-15 10:02:16 +010045login-token() {
46 # could use jq .token
47 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
48}
49
50is-published() {
Carlos Sanchezef7d4ef2017-03-02 11:12:08 +053051 get-manifest "$1" &> /dev/null
Carlos Sanchez881d1432017-02-15 10:02:16 +010052}
53
54get-manifest() {
55 local tag=$1
Carlos Sanchez09f1e502017-03-07 20:06:42 +010056 local opts=""
57 if [ "$debug" = true ]; then
58 opts="-v"
59 fi
60 curl $opts -q -fsSL -H "Accept: application/vnd.docker.distribution.manifest.v2+json" -H "Authorization: Bearer $TOKEN" "https://index.docker.io/v2/jenkinsci/jenkins/manifests/$tag"
Carlos Sanchez881d1432017-02-15 10:02:16 +010061}
62
63get-digest() {
Carlos Sanchez09f1e502017-03-07 20:06:42 +010064 local manifest
65 manifest=$(get-manifest "$1")
Carlos Sanchez881d1432017-02-15 10:02:16 +010066 #get-manifest "$1" | jq .config.digest
Carlos Sanchez09f1e502017-03-07 20:06:42 +010067 if [ "$debug" = true ]; then
68 >&2 echo "DEBUG: Manifest for $1: $manifest"
69 fi
70 echo "$manifest" | grep -A 10 -o '"config".*' | grep digest | head -1 | cut -d':' -f 2,3 | xargs echo
Carlos Sanchez881d1432017-02-15 10:02:16 +010071}
72
Carlos Sanchez0f763d42017-02-06 10:55:11 +010073get-latest-versions() {
74 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
75}
76
77publish() {
78 local version=$1
Carlos Sanchez3ffc4e42017-02-06 13:20:44 +010079 local variant=$2
80 local tag="${version}${variant}"
Carlos Sanchez0f763d42017-02-06 10:55:11 +010081 local sha
82 local build_opts="--no-cache --pull"
83
Carlos Sanchezdf48ce72017-03-07 18:15:06 +010084 if [ "$dry_run" = true ]; then
85 build_opts=""
86 else
87 build_opts="--no-cache --pull"
88 fi
89
Carlos Sanchez48c86a32017-03-07 17:56:42 +010090 local dir=war
91 # lts is in a different dir
92 if [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
93 dir=war-stable
94 fi
95 sha=$(curl -q -fsSL "http://mirrors.jenkins.io/${dir}/${version}/jenkins.war.sha256" | cut -d' ' -f 1)
Carlos Sanchez0f763d42017-02-06 10:55:11 +010096
Carlos Sanchez7b8382f2017-02-06 12:00:52 +010097 docker build --build-arg "JENKINS_VERSION=$version" \
98 --build-arg "JENKINS_SHA=$sha" \
Carlos Sanchez3ffc4e42017-02-06 13:20:44 +010099 --tag "jenkinsci/jenkins:${tag}" ${build_opts} .
Carlos Sanchez0f763d42017-02-06 10:55:11 +0100100
Carlos Sanchezdf48ce72017-03-07 18:15:06 +0100101 if [ "$dry_run" = true ]; then
102 docker push "jenkinsci/jenkins:${tag}"
103 fi
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +0100104}
105
Carlos Sanchez881d1432017-02-15 10:02:16 +0100106tag-and-push() {
107 local source=$1
108 local target=$2
Carlos Sanchezdbdcfe42017-03-07 09:42:40 +0100109 local digest_source
110 local digest_target
111
Carlos Sanchez09f1e502017-03-07 20:06:42 +0100112 if [ "$debug" = true ]; then
113 >&2 echo "DEBUG: Getting digest for ${source}"
114 fi
Carlos Sanchezdbdcfe42017-03-07 09:42:40 +0100115 # if tag doesn't exist yet, ie. dry run
Carlos Sanchez09f1e502017-03-07 20:06:42 +0100116 if ! digest_source=$(get-digest "${source}"); then
117 echo "Unable to get digest for ${source} ${digest_source}"
Carlos Sanchezdbdcfe42017-03-07 09:42:40 +0100118 digest_source=""
119 fi
Carlos Sanchez09f1e502017-03-07 20:06:42 +0100120
121 if [ "$debug" = true ]; then
122 >&2 echo "DEBUG: Getting digest for ${target}"
123 fi
124 if ! digest_target=$(get-digest "${target}"); then
125 echo "Unable to get digest for ${target} ${digest_target}"
Carlos Sanchezb857bb22017-03-07 19:28:02 +0100126 digest_target=""
127 fi
Carlos Sanchez09f1e502017-03-07 20:06:42 +0100128
Carlos Sanchezb857bb22017-03-07 19:28:02 +0100129 if [ "$digest_source" == "$digest_target" ] && [ -n "${digest_target}" ]; then
Carlos Sanchezef7d4ef2017-03-02 11:12:08 +0530130 echo "Images ${source} [$digest_source] and ${target} [$digest_target] are already the same, not updating tags"
Carlos Sanchezb108a532017-02-06 14:30:56 +0100131 else
Carlos Sanchez881d1432017-02-15 10:02:16 +0100132 echo "Creating tag ${target} pointing to ${source}"
Carlos Sanchezac44f2a2017-03-07 18:39:29 +0100133 docker-tag "${source}" "${target}"
Carlos Sanchez7fd3f212017-02-15 08:53:31 +0100134 if [ ! "$dry_run" = true ]; then
Carlos Sanchezdc869152017-03-07 20:21:30 +0100135 echo "Pushing jenkinsci/jenkins:${target}"
136 docker push "jenkinsci/jenkins:${target}"
137 else
138 echo "Would push jenkinsci/jenkins:${target}"
Carlos Sanchez7fd3f212017-02-15 08:53:31 +0100139 fi
Carlos Sanchezb108a532017-02-06 14:30:56 +0100140 fi
Carlos Sanchez0f763d42017-02-06 10:55:11 +0100141}
142
Carlos Sanchez881d1432017-02-15 10:02:16 +0100143publish-latest() {
144 local version=$1
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +0100145 local variant=$2
Carlos Sanchez881d1432017-02-15 10:02:16 +0100146
147 # push latest (for master) or the name of the branch (for other branches)
148 if [ -z "${variant}" ]; then
149 tag-and-push "${version}${variant}" "latest"
150 else
151 tag-and-push "${version}${variant}" "${variant#-}"
Carlos Sanchez7fd3f212017-02-15 08:53:31 +0100152 fi
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +0100153}
154
Carlos Sanchez881d1432017-02-15 10:02:16 +0100155publish-lts() {
156 local version=$1
157 local variant=$2
158 tag-and-push "${version}" "lts${variant}"
159}
160
Carlos Sanchez09f1e502017-03-07 20:06:42 +0100161# Process arguments
162
Carlos Sanchez7fd3f212017-02-15 08:53:31 +0100163dry_run=false
Carlos Sanchez09f1e502017-03-07 20:06:42 +0100164debug=false
165
166while [[ $# -gt 0 ]]; do
167 key="$1"
168 case $key in
169 -n)
170 dry_run=true
171 ;;
172 -d)
173 debug=true
174 ;;
175 *)
176 echo "Unknown option: $key"
177 return 1
178 ;;
179 esac
180 shift
181done
182
183
Carlos Sanchez7fd3f212017-02-15 08:53:31 +0100184if [ "$dry_run" = true ]; then
Carlos Sanchezdf48ce72017-03-07 18:15:06 +0100185 echo "Dry run, will not publish images"
Carlos Sanchez7fd3f212017-02-15 08:53:31 +0100186fi
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +0100187
Carlos Sanchez881d1432017-02-15 10:02:16 +0100188TOKEN=$(login-token)
189
Carlos Sanchez3ffc4e42017-02-06 13:20:44 +0100190variant=$(get-variant)
191
Carlos Sanchez881d1432017-02-15 10:02:16 +0100192lts_version=""
193version=""
Carlos Sanchez0f763d42017-02-06 10:55:11 +0100194for version in $(get-latest-versions); do
Carlos Sanchez881d1432017-02-15 10:02:16 +0100195 if is-published "$version$variant"; then
196 echo "Tag is already published: $version$variant"
Carlos Sanchez0f763d42017-02-06 10:55:11 +0100197 else
Carlos Sanchez881d1432017-02-15 10:02:16 +0100198 echo "Publishing version: $version$variant"
Carlos Sanchezdf48ce72017-03-07 18:15:06 +0100199 publish "$version" "$variant"
Carlos Sanchez0f763d42017-02-06 10:55:11 +0100200 fi
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +0100201
202 # Update lts tag
203 if [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
Carlos Sanchez881d1432017-02-15 10:02:16 +0100204 lts_version="${version}"
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +0100205 fi
Carlos Sanchez0f763d42017-02-06 10:55:11 +0100206done
207
Carlos Sanchez881d1432017-02-15 10:02:16 +0100208publish-latest "${version}" "${variant}"
209if [ -n "${lts_version}" ]; then
210 publish-lts "${lts_version}" "${variant}"
Carlos Sanchez4b0dcec2017-02-14 00:10:14 +0100211fi