[SEBA-497] v2

Fixes to the standalone docker-publish job

Change-Id: Ie89a15bd3b315e0fb31e58edb5059d9eb5d0ebc4
diff --git a/jjb/defaults.yaml b/jjb/defaults.yaml
index b1f4948..b6e6ada 100644
--- a/jjb/defaults.yaml
+++ b/jjb/defaults.yaml
@@ -71,7 +71,7 @@
     seba-branches-regexp: '^(master|seba-1.0)$'
 
     # for matching repos that build docker images with imagebuilder
-    imagebuilder-projects-regexp: '^(xos.*|cord-tester|chameleon|rcord|mcord|ecord|acordion|addressmanager|epc-service|exampleservice|fabric|fabric-crossconnect|globalxos|hippie-oss|hss_db|hypercache|internetemulator|kubernetes-service|monitoring|olt-service|onos-service|openstack|progran|sdn-controller|simpleexampleservice|templateservice|vEE|vEG|vBBU|venb|vHSS|vMME|vnaas|vPGWC|vPGWU|vrouter|vsg|vsg-hw|vSGW|vSM|vspgwc|vspgwu|vtn-service|vtr|att-workflow-driver|tt-workflow-driver)$'
+    imagebuilder-projects-regexp: '^(xos.*|cord-tester|chameleon|rcord|mcord|ecord|acordion|addressmanager|epc-service|exampleservice|fabric|fabric-crossconnect|globalxos|hippie-oss|hss_db|hypercache|internetemulator|monitoring|olt-service|onos-service|openstack|progran|sdn-controller|simpleexampleservice|templateservice|vEE|vEG|vBBU|venb|vHSS|vMME|vnaas|vPGWC|vPGWU|vrouter|vsg|vsg-hw|vSGW|vSM|vspgwc|vspgwu|vtn-service|vtr|att-workflow-driver|tt-workflow-driver)$'
 
     # strictness of version checks
     semver-strict: 0
@@ -97,6 +97,12 @@
     # 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.
+    # Default is blank, which maps to DockerHub.
+    docker-registry: ''
+
     # for matching files with file-include-regexp
     all-files-regexp: '.*'
     doc-files-regexp: '^docs/.*'
diff --git a/jjb/docker-publish.yaml b/jjb/docker-publish.yaml
index 9495c04..1c7d190 100644
--- a/jjb/docker-publish.yaml
+++ b/jjb/docker-publish.yaml
@@ -37,10 +37,20 @@
           description: 'URL to the git repo'
 
       - string:
+          name: gitRef
+          default: '$GERRIT_PATCHSET_REVISION'
+          description: 'git ref to build (commit hash or tag)'
+
+      - string:
           name: dockerRepo
           default: '{docker-repo}'
           description: "Docker repository to push to ('opencord', 'xosproject', 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
diff --git a/jjb/pipeline/docker-publish.groovy b/jjb/pipeline/docker-publish.groovy
index 89eef34..e9f05a3 100644
--- a/jjb/pipeline/docker-publish.groovy
+++ b/jjb/pipeline/docker-publish.groovy
@@ -13,7 +13,7 @@
           $class: 'GitSCM',
           userRemoteConfigs: [[
             url: "${params.gitUrl}",
-            name: "${params.GERRIT_PATCHSET_REVISION}",
+            name: "${params.gitRef}",
           ]],
           extensions: [
             [$class: 'WipeWorkspace'],
@@ -22,36 +22,41 @@
           ],
         ])
         script {
-          def git_tags={ shell "git tag -l --points-at HEAD" }
+          git_tags = sh(script:"cd $GERRIT_PROJECT; git tag -l --points-at HEAD", returnStdout: true).trim()
         }
       }
     }
 
     stage('build'){
       steps {
-        sh """
+        sh( script: """
           #!/usr/bin/env bash
           set -eu -o pipefail
 
           # checked out in a subdir so the log can be in WORKSPACE
           cd "$GERRIT_PROJECT"
 
+          # set registry/repository variables
+          export DOCKER_REGISTRY="$dockerRegistry"
           export DOCKER_REPOSITORY="$dockerRepo/"
 
           # Build w/branch
           echo "Building image with branch"
-          make DOCKER_TAG="$GERRIT_BRANCH" docker-build 2>&1 | tee > "$WORKSPACE/docker-build.log"
+          make DOCKER_TAG="$GERRIT_BRANCH" 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
-              echo "Building image with tag (should reuse cached layers)"
-              make DOCKER_TAG="$tag" docker-build
+              echo "Building image with tag: \$tag (should reuse cached layers)"
+              make DOCKER_TAG="\$tag" docker-build
             done
           fi
-        """
+        """)
       }
     }
 
@@ -59,29 +64,33 @@
       steps {
         script {
           withDockerRegistry([credentialsId: 'docker-artifact-push-credentials']) {
-            sh """
+            sh( script:"""
               #!/usr/bin/env bash
               set -eu -o pipefail
 
               # checked out in a subdir so the log can be in WORKSPACE
               cd "$GERRIT_PROJECT"
 
+              # set registry/repository variables
+              export DOCKER_REGISTRY="$dockerRegistry"
               export DOCKER_REPOSITORY="$dockerRepo/"
 
               # Push w/branch
               echo "Pushing image with branch"
-              make DOCKER_TAG="$GERRIT_BRANCH" docker-push 2>&1 | tee > "$WORKSPACE/docker-push.log"
+              make DOCKER_TAG="$GERRIT_BRANCH" 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
-                  echo "Pushing image with tag (should reuse cached layers)"
-                  make DOCKER_TAG="$tag" docker-push
+                  echo "Pushing image with tag: \$tag (should reuse cached layers)"
+                  make DOCKER_TAG="\$tag" docker-push
                 done
               fi
-            """
+            """)
           }
         }
       }
@@ -90,7 +99,7 @@
 
   post {
     always {
-      archiveArtifacts artifacts: 'docker-build.log', fingerprint: true
+      archiveArtifacts artifacts: 'docker-*.log', fingerprint: true
       deleteDir()
     }
   }
diff --git a/jjb/verify/alpine-grpc-base.yaml b/jjb/verify/alpine-grpc-base.yaml
index 7197480..219065d 100644
--- a/jjb/verify/alpine-grpc-base.yaml
+++ b/jjb/verify/alpine-grpc-base.yaml
@@ -21,4 +21,5 @@
     name: 'publish-alpine-grpc-base-jobs'
     jobs:
       - 'docker-publish':
-          docker-repo: "xosproject"
+          docker-repo: 'xosproject'
+          dependency-jobs: 'version-tag'
diff --git a/jjb/verify/kubernetes-service.yaml b/jjb/verify/kubernetes-service.yaml
index ab163eb..7ee0e97 100644
--- a/jjb/verify/kubernetes-service.yaml
+++ b/jjb/verify/kubernetes-service.yaml
@@ -8,6 +8,7 @@
     jobs:
       - 'verify-kubernetes-service-jobs':
           branch-regexp: '{supported-branches-regexp}'
+      - 'publish-kubernetes-service-jobs'
 
 - job-group:
     name: 'verify-kubernetes-service-jobs'
@@ -22,3 +23,10 @@
       - 'api-test':
           dependency-jobs: 'verify_kubernetes-service_unit-test'
           pipeline_script: 'all-xos-api-test-helm.groovy'
+
+- job-group:
+    name: 'publish-kubernetes-service-jobs'
+    jobs:
+      - 'docker-publish':
+          docker-repo: 'xosproject'
+          dependency-jobs: 'version-tag'