ci-dockerfiles-sanity-check: new job

Job to verify if new dockerfiles build succeed/fail

Signed-off-by: Riku Voipio <riku.voipio@linaro.org>
Change-Id: Id1405c19604a63af503dc268f5a86bd9fa086d70
diff --git a/ci-dockerfiles-deployment.yaml b/ci-dockerfiles-deployment.yaml
index 0c4b45f..28e034c 100644
--- a/ci-dockerfiles-deployment.yaml
+++ b/ci-dockerfiles-deployment.yaml
@@ -10,6 +10,11 @@
         - build-discarder:
             days-to-keep: 90
             num-to-keep: 200
+        - build-blocker:
+            use-build-blocker: true
+            blocking-jobs:
+              - "ci-docker*"
+            block-level: 'NODE'
     disabled: false
     node: build-amd64
     concurrent: false
@@ -64,6 +69,8 @@
             file: true
         - shell:
             !include-raw: ci-dockerfiles-deployment/builders.sh
+        - shell:
+            !include-raw: ci-dockerfiles-deployment/publish.sh
     publishers:
         - email:
-            recipients: 'ben.copeland@linaro.org riku.voipio@linaro.org kelley.spoon@linaro.org fathi.boudra@linaro.org'
+            recipients: linaro-infrastructure-errors@lists.linaro.org
diff --git a/ci-dockerfiles-deployment/builders.sh b/ci-dockerfiles-deployment/builders.sh
index e4f58e0..fea77b3 100644
--- a/ci-dockerfiles-deployment/builders.sh
+++ b/ci-dockerfiles-deployment/builders.sh
@@ -2,33 +2,20 @@
 
 set -ex
 
-trap cleanup_exit INT TERM EXIT
-
-cleanup_exit()
-{
-    rm -rf ${HOME}/.docker
-    rm -f ${WORKSPACE}/{log,config.json,version.txt}
-}
-
-mkdir -p ${HOME}/.docker
-wget https://ci.trustedfirmware.org/userContent/config.json
-sed -e "s|\${DOCKER_AUTH}|${DOCKER_AUTH}|" < ${WORKSPACE}/config.json > ${HOME}/.docker/config.json
-chmod 0600 ${HOME}/.docker/config.json
-
 echo ""
 echo "########################################################################"
 echo "    Gerrit Environment"
 env |grep '^GERRIT'
 echo "########################################################################"
 
-rm -f ${WORKSPACE}/{log,config.json,version.txt}
+rm -f ${WORKSPACE}/log
 cd dockerfiles/
 
 git_previous_commit=$(git rev-parse HEAD~1)
 git_commit=$(git rev-parse HEAD)
 files=$(git diff --name-only ${git_previous_commit} ${git_commit})
 echo Changes in: ${files}
-changed_dirs=$(dirname ${files})
+changed_dirs=$(dirname ${files}|sort -u)
 
 update_images=""
 for dir in ${changed_dirs}; do
@@ -45,6 +32,9 @@
       # images to the list.
       update_images="${update_images} $(dirname $(find . -path "*-${dir_basename}*/build.sh" | sed -e "s#^\./##g"))"
       ;;
+    ".")
+      continue
+      ;;
     *)
       update_images="${update_images} $(dirname $(find ${dir} -name build.sh))"
       ;;
@@ -80,21 +70,6 @@
       echo "Skipping: can't build for ${image_arch} on ${host_arch}"
       ;;
   esac
-  if [ -r .docker-tag ]; then
-    docker_tag=$(cat .docker-tag)
-    if [ x"${GERRIT_BRANCH}" != x"master" ]; then
-      new_tag=${docker_tag}-${GERRIT_BRANCH}
-      docker tag ${docker_tag} ${new_tag}
-      docker_tag=${new_tag}
-    fi
-    docker push ${docker_tag}
-  fi
   )||echo $image failed >> ${WORKSPACE}/log
 done
 
-if [ -e ${WORKSPACE}/log ]
-then
-    echo "some images failed:"
-    cat ${WORKSPACE}/log
-    exit 1
-fi
diff --git a/ci-dockerfiles-deployment/publish.sh b/ci-dockerfiles-deployment/publish.sh
new file mode 100644
index 0000000..9d71f3e
--- /dev/null
+++ b/ci-dockerfiles-deployment/publish.sh
@@ -0,0 +1,45 @@
+#!/bin/bash
+
+set -ex
+
+trap cleanup_exit INT TERM EXIT
+
+cleanup_exit()
+{
+    rm -rf ${HOME}/.docker
+    rm -f ${WORKSPACE}/{log,config.json,version.txt}
+}
+
+docker_log_in()
+{
+    mkdir -p ${HOME}/.docker
+    sed -e "s|\${DOCKER_AUTH}|${DOCKER_AUTH}|" < ${WORKSPACE}/config.json > ${HOME}/.docker/config.json
+    chmod 0600 ${HOME}/.docker/config.json
+}
+
+update_images=$(find -type f -name .docker-tag)
+docker_log_in
+for imagename in ${update_images}; do
+  (
+    docker_tag=$(cat $imagename)
+    if [ x"${GERRIT_BRANCH}" != x"master" ]; then
+      new_tag=${docker_tag}-${GERRIT_BRANCH}
+      docker tag ${docker_tag} ${new_tag}
+      docker_tag=${new_tag}
+    fi
+    for i in 30 60 120;
+    do
+        docker push ${docker_tag} && exit 0 || true
+        sleep $i
+        docker_log_in
+    done
+    exit 1
+  )||echo $imagename push failed >> ${WORKSPACE}/log
+done
+
+if [ -e ${WORKSPACE}/log ]
+then
+    echo "some images failed:"
+    cat ${WORKSPACE}/log
+    exit 1
+fi
diff --git a/ci-dockerfiles-deployment/report.sh b/ci-dockerfiles-deployment/report.sh
new file mode 100644
index 0000000..fe0ac53
--- /dev/null
+++ b/ci-dockerfiles-deployment/report.sh
@@ -0,0 +1,32 @@
+#!/bin/bash
+
+set -ex
+
+trap cleanup_exit INT TERM EXIT
+
+cleanup_exit()
+{
+    rm -rf ${HOME}/.docker
+    rm -f ${WORKSPACE}/{log,config.json,version.txt}
+}
+
+update_images=$(find -type f -name .docker-tag)
+
+for imagename in ${update_images}; do
+  (
+    docker_tag=$(cat $imagename)
+    if [ x"${GERRIT_BRANCH}" != x"master" ]; then
+      new_tag=${docker_tag}-${GERRIT_BRANCH}
+      docker tag ${docker_tag} ${new_tag}
+      docker_tag=${new_tag}
+    fi
+    echo successful build ${docker_tag}
+  )
+done
+
+if [ -e ${WORKSPACE}/log ]
+then
+    echo "some images failed:"
+    cat ${WORKSPACE}/log
+    exit 1
+fi
diff --git a/ci-dockerfiles-sanity-check.yaml b/ci-dockerfiles-sanity-check.yaml
new file mode 100644
index 0000000..4699910
--- /dev/null
+++ b/ci-dockerfiles-sanity-check.yaml
@@ -0,0 +1,74 @@
+- job:
+    name: ci-dockerfiles-sanity-check
+    project-type: matrix
+    defaults: global
+    properties:
+        - authorization:
+            anonymous:
+                - job-discover
+            linaro:
+                - job-read
+                - job-extended-read
+        - build-discarder:
+            days-to-keep: 90
+            num-to-keep: 200
+        - build-blocker:
+            use-build-blocker: true
+            blocking-jobs:
+              - "ci-docker*"
+            block-level: 'NODE'
+    disabled: false
+    node: build-amd64
+    concurrent: false
+    display-name: 'CI Dockerfiles sanity check'
+    scm:
+        - git:
+            url: https://review.trustedfirmware.org/${GERRIT_PROJECT}
+            refspec: ${GERRIT_REFSPEC}
+            branches:
+                - ${GERRIT_BRANCH}
+            skip-tag: true
+            clean:
+                before: true
+            choosing-strategy: gerrit
+            basedir: dockerfiles
+    triggers:
+        - gerrit:
+            server-name: 'review.trustedfirmware.org'
+            trigger-on:
+                - patchset-created-event
+            projects:
+                - project-compare-type: 'PLAIN'
+                  project-pattern: 'ci/dockerfiles'
+                  branches:
+                    - branch-compare-type: 'PLAIN'
+                      branch-pattern: 'master'
+            silent-start: true
+    axes:
+        - axis:
+            type: slave
+            name: label
+            values:
+                - build-amd64
+    execution-strategy:
+        sequential: false
+    wrappers:
+        - timestamps
+        - copy-to-slave:
+            includes:
+                - config.json
+            relative-to: 'somewhereElse'
+    builders:
+        - shell: |
+            #!/bin/bash -e
+            echo "#${BUILD_NUMBER}-${GERRIT_PATCHSET_REVISION:0:8}" > ${WORKSPACE}/version.txt
+        - build-name-setter:
+            name: 'version.txt'
+            file: true
+        - shell:
+            !include-raw: ci-dockerfiles-deployment/builders.sh
+        - shell:
+            !include-raw: ci-dockerfiles-deployment/report.sh
+    publishers:
+        - email:
+            recipients: linaro-infrastructure-errors@lists.linaro.org