Add CD job for tost-onos

Change-Id: I529c37181d2584f49f7b4c1623f0f1b1c21a6096
diff --git a/jjb/defaults.yaml b/jjb/defaults.yaml
index 16471ab..7b5200c 100644
--- a/jjb/defaults.yaml
+++ b/jjb/defaults.yaml
@@ -87,6 +87,11 @@
     # tests or test output generation may not yet exist.
     junit-allow-empty-results: false
 
+    # Name of the Docker repository (usually on DockerHub) that is the
+    # destination for images to be pushed to after building.
+    # Default is invalid - must be specified on every job.
+    docker-repo: 'default-and-invalid'
+
     # URL of the Docker registry (server running a docker registry) that is the
     # destination for images to be pushed to after building. If set, must
     # include trailing slash as a separator before the repo name.
@@ -95,3 +100,7 @@
 
     # ONF docker credentials for onfbuilder user
     docker-credentials-id: 'docker-artifact-push-credentials'
+
+    # maintainers
+    # Used to notify users in supported tests
+    maintainers: 'zdw@opennetworking.org'
diff --git a/jjb/pipeline/docker-publish.groovy b/jjb/pipeline/docker-publish.groovy
new file mode 100644
index 0000000..316d429
--- /dev/null
+++ b/jjb/pipeline/docker-publish.groovy
@@ -0,0 +1,126 @@
+// Copyright 2017-present Open Networking Foundation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+/* docker-publish pipeline */
+pipeline {
+
+  /* executor is determined by JJB */
+  agent {
+    label "${params.buildNode}"
+  }
+  stages {
+
+    stage('checkout') {
+      steps {
+        checkout([
+          $class: 'GitSCM',
+          userRemoteConfigs: [[ url: "${params.gitUrl}", ]],
+          branches: [[ name: "${params.gitRef}", ]],
+          extensions: [
+            [$class: 'WipeWorkspace'],
+            [$class: 'RelativeTargetDirectory', relativeTargetDir: "${params.projectName}"],
+            [$class: 'CloneOption', depth: 0, noTags: false, reference: '', shallow: false],
+            [$class: 'SubmoduleOption', recursiveSubmodules: true],
+          ],
+        ])
+        script {
+          git_tags = sh(script:"cd $projectName; git tag -l --points-at HEAD", returnStdout: true).trim()
+        }
+      }
+    }
+
+    stage('build'){
+      steps {
+        sh( script: """
+          #!/usr/bin/env bash
+          set -eu -o pipefail
+
+          # checked out in a subdir so the log can be in WORKSPACE
+          cd "$projectName"
+
+          # set registry/repository variables
+          export DOCKER_REGISTRY="$dockerRegistry"
+          export DOCKER_REPOSITORY="$dockerRepo/"
+
+          # Build w/branch
+          echo "Building image with branch"
+          $extraEnvironmentVars DOCKER_TAG="$branchName" make docker-build 2>&1 | tee "$WORKSPACE/docker-build.log"
+
+          # Build w/tags if they exist
+          if [ -n "$git_tags" ]
+          echo "Tags found in git, building:"
+          echo "$git_tags"
+
+          then
+            for tag in $git_tags
+            do
+              # remove leading 'v' on funky golang tags
+              clean_tag=\$(echo \$tag | sed 's/^v//g')
+              echo "Building image with tag: \$clean_tag (should reuse cached layers)"
+              $extraEnvironmentVars DOCKER_TAG="\$clean_tag" make docker-build
+            done
+          fi
+        """)
+      }
+    }
+
+    stage('push'){
+      steps {
+        script {
+          withDockerRegistry([credentialsId: 'docker-artifact-push-credentials']) {
+            sh( script:"""
+              #!/usr/bin/env bash
+              set -eu -o pipefail
+
+              # checked out in a subdir so the log can be in WORKSPACE
+              cd "$projectName"
+
+              # set registry/repository variables
+              export DOCKER_REGISTRY="$dockerRegistry"
+              export DOCKER_REPOSITORY="$dockerRepo/"
+
+              # Push w/branch
+              echo "Pushing image with branch"
+              $extraEnvironmentVars DOCKER_TAG="$branchName" make docker-push 2>&1 | tee "$WORKSPACE/docker-push.log"
+
+              # Push w/tags if they exist
+              if [ -n "$git_tags" ]
+              echo "Tags found in git, pushing:"
+              echo "$git_tags"
+              then
+                for tag in $git_tags
+                do
+                  # remove leading 'v' on funky golang tags
+                  clean_tag=\$(echo \$tag | sed 's/^v//g')
+                  echo "Pushing image with tag: \$clean_tag (should reuse cached layers)"
+                  $extraEnvironmentVars DOCKER_TAG="\$clean_tag" make docker-push
+                done
+              fi
+            """)
+          }
+        }
+      }
+    }
+  }
+
+  post {
+    always {
+      archiveArtifacts artifacts: 'docker-*.log', fingerprint: true
+      deleteDir()
+    }
+    failure {
+      step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: "${params.maintainers}", sendToIndividuals: false])
+    }
+  }
+}
diff --git a/jjb/repos/tost-onos.yaml b/jjb/repos/tost-onos.yaml
index be32dfc..2097a9c 100644
--- a/jjb/repos/tost-onos.yaml
+++ b/jjb/repos/tost-onos.yaml
@@ -20,16 +20,8 @@
       - 'tag-check':
           dependency-jobs: 'license-check_tost-onos'
       - 'make-test':
-          name-extension: 'onos-2.2'
           make-test-vars: |
-            ONOS_BRANCH={name-extension}
-          make-test-targets: 'docker-build'
-          junit-allow-empty-results: true
-          build-node: '{bigger-build-node}'
-      - 'make-test':
-          name-extension: 'master'
-          make-test-vars: |
-            ONOS_BRANCH={name-extension}
+            ONOS_BRANCH=onos-2.2
           make-test-targets: 'docker-build'
           junit-allow-empty-results: true
           build-node: '{bigger-build-node}'
@@ -37,4 +29,10 @@
 - job-group:
     name: 'publish-tost-onos-jobs'
     jobs:
-      - 'version-tag'
+      - 'docker-publish':
+          docker-repo: 'onosproject'
+          maintainers: 'charles@opennetworking.org, pier@opennetworking.org'
+          build-timeout: 30
+          extraEnvironmentVars: |
+            ONOS_BRANCH=onos-2.2
+          dependency-jobs: 'version-tag'
diff --git a/jjb/templates/docker-publish.yaml b/jjb/templates/docker-publish.yaml
new file mode 100644
index 0000000..906f9a4
--- /dev/null
+++ b/jjb/templates/docker-publish.yaml
@@ -0,0 +1,84 @@
+---
+# generic docker image building + dockerhub publishing tasks
+
+- job-template:
+    id: docker-publish
+    name: 'docker-publish_{project}'
+    description: |
+      Created by {id} job-template from ci-management/jjb/docker-publish.yaml, script pipeline/docker-publish.groovy
+
+    triggers:
+      - onf-infra-gerrit-trigger-merge:
+          gerrit-server-name: '{gerrit-server-name}'
+          project-regexp: '{project}'
+          branch-regexp: '{branch-regexp}'
+          file-include-regexp: '{all-files-regexp}'
+          dependency-jobs: '{dependency-jobs}'
+
+    properties:
+      - onf-infra-properties:
+          build-days-to-keep: '{build-days-to-keep}'
+          artifact-num-to-keep: '{artifact-num-to-keep}'
+
+    wrappers:
+      - lf-infra-wrappers:
+          build-timeout: '{build-timeout}'
+          jenkins-ssh-credential: '{jenkins-ssh-credential}'
+
+    parameters:
+      - string:
+          name: buildNode
+          default: '{bigger-build-node}'
+          description: 'Name of the Jenkins build executor to run the job on'
+
+      - string:
+          name: gitUrl
+          default: '$GIT_URL/$GERRIT_PROJECT'
+          description: 'URL to the git repo'
+
+      - string:
+          name: gitRef
+          default: '$GERRIT_PATCHSET_REVISION'
+          description: 'git ref to build (commit hash or tag)'
+
+      - string:
+          name: projectName
+          default: '$GERRIT_PROJECT'
+          description: 'Name of the project in Gerrit'
+
+      - string:
+          name: branchName
+          default: '$GERRIT_BRANCH'
+          description: 'Branch of the project in Gerrit'
+
+      - string:
+          name: dockerRepo
+          default: '{docker-repo}'
+          description: "Docker repository to push to ('onosproject', etc.)"
+
+      - string:
+          name: dockerRegistry
+          default: '{docker-registry}'
+          description: "Docker registry to push to (blank for DockerHub)"
+
+      # AWS CPU arch names: `x86_64` `arm64` (which don't align to vendor names... *sigh*)
+      - string:
+          name: dockerArchList
+          default: 'x86_64'
+          description: "List of architectures to build containers on, pipe separated (nonfunctional currently)"
+
+      - string:
+          name: maintainers
+          default: '{maintainers}'
+          description: "The person that should be notified if this job fails"
+
+      - string:
+          name: extraEnvironmentVars
+          default: '{extraEnvironmentVars}'
+          description: "Provide extra environment variables to the build"
+
+    project-type: pipeline
+    concurrent: true
+    extraEnvironmentVars: ""
+
+    dsl: !include-raw-escape: ../pipeline/docker-publish.groovy