Merge pull request #426 from rtyler/publish-containers-729

Make sure the docker Pipeline is run on a daily basis
diff --git a/Jenkinsfile b/Jenkinsfile
index f287149..8b07fca 100644
--- a/Jenkinsfile
+++ b/Jenkinsfile
@@ -26,7 +26,7 @@
 
     if (infra.isTrusted()) {
         stage('Publish') {
-            sh './weekly.sh'
+            sh './publish.sh'
         }
     }
 }
diff --git a/publish.sh b/publish.sh
new file mode 100755
index 0000000..5ceacb3
--- /dev/null
+++ b/publish.sh
@@ -0,0 +1,70 @@
+#!/bin/bash -eu
+
+# Publish any versions of the docker image not yet pushed to jenkinsci/jenkins
+
+set -o pipefail
+
+sort-versions() {
+    if [ "$(uname)" == 'Darwin' ]; then
+        gsort --version-sort
+    else
+        sort --version-sort
+    fi
+}
+
+# Try tagging with and without -f to support all versions of docker
+docker-tag() {
+    local from="jenkinsci/jenkins:$1"
+    local to="jenkinsci/jenkins:$2"
+    local out
+    if out=$(docker tag -f "$from" "$to" 2>&1); then
+        echo "$out"
+    else
+        docker tag "$from" "$to"
+    fi
+}
+
+get-published-versions() {
+    curl -q -fsSL https://registry.hub.docker.com/v1/repositories/jenkinsci/jenkins/tags | egrep -o '"name": "[0-9\.]+"' | egrep -o '[0-9\.]+'
+}
+
+get-latest-versions() {
+    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
+}
+
+publish() {
+    local version=$1
+    local sha
+    local build_opts="--no-cache --pull"
+
+    sha=$(curl -q -fsSL "http://repo.jenkins-ci.org/simple/releases/org/jenkins-ci/main/jenkins-war/${version}/jenkins-war-${version}.war.sha1")
+
+    docker build --build-arg "version=$version" \
+                 --build-arg "sha=$sha" \
+                 --tag "jenkinsci/jenkins:$version" ${build_opts} .
+
+    docker-tag $version latest
+
+    docker push "jenkinsci/jenkins:$version"
+    docker push "jenkinsci/jenkins:latest"
+
+    # Update lts tag
+    if [ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]; then
+        echo "Updating lts tag to $version"
+        docker-tag $version lts
+        docker push "jenkinsci/jenkins:lts"
+    fi
+}
+
+published_versions="$(get-published-versions)"
+
+for version in $(get-latest-versions); do
+    if echo "${published_versions}" | grep -q "^${version}$"; then
+        echo "Version is already published: $version"
+    else
+        echo "Publishing version: $version"
+        publish $version
+    fi
+done
+
+
diff --git a/weekly.sh b/weekly.sh
deleted file mode 100755
index da3efff..0000000
--- a/weekly.sh
+++ /dev/null
@@ -1,22 +0,0 @@
-#!/bin/bash
-
-set -e
-set -x
-
-JENKINS_VERSION=$(curl -sq https://api.github.com/repos/jenkinsci/jenkins/tags | grep '"name":' | egrep -o '[0-9]+(\.[0-9]+)+' | sort --version-sort | uniq | tail -1)
-echo "$JENKINS_VERSION"
-
-JENKINS_SHA=$(curl "http://repo.jenkins-ci.org/simple/releases/org/jenkins-ci/main/jenkins-war/${JENKINS_VERSION}/jenkins-war-${JENKINS_VERSION}.war.sha1")
-echo "$JENKINS_SHA"
-
-docker build --build-arg "JENKINS_VERSION=$JENKINS_VERSION" \
-             --build-arg "JENKINS_SHA=$JENKINS_SHA" \
-             --no-cache --pull \
-             --tag "jenkinsci/jenkins:$JENKINS_VERSION" .
-
-docker tag -f "jenkinsci/jenkins:$JENKINS_VERSION" jenkinsci/jenkins:latest
-
-docker push "jenkinsci/jenkins:$JENKINS_VERSION"
-docker push jenkinsci/jenkins:latest
-
-