Improve docker-build-to-jfrog pipeline

- Remove quiet flag from image building
- Add CONTEXT_PATH variable

Change-Id: Ifba291437eda5ab0ace9d33854ff04e72ff86f9d
diff --git a/docker-build-to-jfrog.groovy b/docker-build-to-jfrog.groovy
index 7f91a54..b16dc06 100644
--- a/docker-build-to-jfrog.groovy
+++ b/docker-build-to-jfrog.groovy
@@ -2,6 +2,7 @@
  * Docker image build pipeline with push to JFrog
  * IMAGE_NAME - Image name
  * IMAGE_TAGS - Tag list for image, separated by space
+ * CONTEXT_PATH - Path to build context directory
  * CREDENTIALS_ID - gerrit credentials id
  * DOCKERFILE_PATH - path to dockerfile in repository
  * DOCKER_REGISTRY - url to registry
@@ -21,6 +22,17 @@
   def dockerRepository = DOCKER_REGISTRY
   def docker_dev_repo = "docker-dev-local"
   def docker_prod_repo = "docker-prod-local"
+  def dockerFileOption
+
+  def buildTag = "oss-ci-docker-${BUILD_NUMBER}-${GERRIT_CHANGE_NUMBER}-${GERRIT_PATCHSET_NUMBER}"
+
+  if (DOCKERFILE_PATH.trim() == ''){
+    dockerFileOption = ''
+  }
+  else {
+    dockerFileOption = "--file ${DOCKERFILE_PATH}"
+  }
+  def buildCmd = "docker build --tag ${buildTag} ${dockerFileOption} --rm ${CONTEXT_PATH}"
 
   def imageTagsList = IMAGE_TAGS.tokenize(" ")
   def workspace = common.getWorkspace()
@@ -35,13 +47,10 @@
       ])
     }
     stage("build image"){
-      containerId = sh(
-        script: "docker build -f ${DOCKERFILE_PATH}/Dockerfile -q --rm . | awk -F':' '{print \$2}'",
-        returnStdout: true
-      ).trim().take(12)
+      sh "${buildCmd}"
       imageTagsList << "${GERRIT_CHANGE_NUMBER}_${GERRIT_PATCHSET_NUMBER}"
       for (imageTag in imageTagsList) {
-        sh "docker tag ${containerId} ${dockerRepository}/${projectNamespace}/${IMAGE_NAME}:${imageTag}"
+        sh "docker tag ${buildTag} ${dockerRepository}/${projectNamespace}/${IMAGE_NAME}:${imageTag}"
       }
     }
     stage("publish image"){