Add a meta job to trigger docker-publish and deploy jobs on omec PR merge

Change-Id: I758cd07e7bfa3b1c9cba90723b3ba4ff7319bc57
diff --git a/jjb/cord-macros.yaml b/jjb/cord-macros.yaml
index cc12b36..e16096f 100644
--- a/jjb/cord-macros.yaml
+++ b/jjb/cord-macros.yaml
@@ -152,11 +152,17 @@
                   key: merged
                   value: $.pull_request.merged
                 - type: JSONPath
+                  key: repoUrl
+                  value: $.pull_request.base.repo.html_url
+                - type: JSONPath
+                  key: repoName
+                  value: $.pull_request.base.repo.name
+                - type: JSONPath
                   key: branchName
                   value: $.pull_request.base.ref
                 - type: JSONPath
-                  key: gitRef
-                  value: $.pull_request.base.ref
+                  key: commitHash
+                  value: $.pull_request.merge_commit_sha
             regex-filter-text: $action,$merged
             regex-filter-expression: ^(closed,true)$
             cause: Generic Cause
diff --git a/jjb/omec-ci.yaml b/jjb/omec-ci.yaml
index be1c053..e1e5dee 100644
--- a/jjb/omec-ci.yaml
+++ b/jjb/omec-ci.yaml
@@ -16,6 +16,13 @@
       - 'omec-tc1'
       - 'omec-tc2'
       - 'omec-deploy-staging'
+      - 'omec-postmerge':
+          project: '{name}'
+          build-node: 'omec-qa'
+          c3po-branch-name: 'multiUpfs'
+          ngic-branch-name: 'master'
+          openmme-branch-name: 'paging'
+          pipeline-file: 'omec-postmerge.groovy'
 
 # for ngic-rtc
 - project:
@@ -509,3 +516,76 @@
     concurrent: false
 
     dsl: !include-raw-escape: pipeline/omec-deploy-staging.groovy
+
+# Post-merge job
+# Triggered by Github pull request merge
+- job-template:
+    id: omec-postmerge
+    name: '{project}_postmerge'
+    description: |
+      Created by {id} job-template from ci-management/jjb/omec-ci.yaml
+
+    properties:
+      - cord-infra-properties:
+          build-days-to-keep: '{build-days-to-keep}'
+          artifact-num-to-keep: '{artifact-num-to-keep}'
+
+    triggers:
+      - cord-infra-github-pr-trigger-merge:
+          project: '{project}'
+
+    wrappers:
+      - lf-infra-wrappers:
+          build-timeout: '{build-timeout}'
+          jenkins-ssh-credential: '{jenkins-ssh-credential}'
+
+    parameters:
+      - string:
+          name: buildNode
+          default: '{build-node}'
+          description: 'Name of the Jenkins node to run the job on'
+
+      - string:
+          name: repoName
+          default: ''
+          description: 'Name of the git repo. Populated by Generic Webhook Trigger'
+
+      - string:
+          name: repoUrl
+          default: ''
+          description: 'URL to the git repo. Populated by Generic Webhook Trigger'
+
+      - string:
+          name: branchName
+          default: ''
+          description: 'Branch of the project. Populated by Generic Webhook Trigger'
+
+      - string:
+          name: commitHash
+          default: ''
+          description: 'SHA string of the merged commit. Populated by Generic Webhook Trigger'
+
+      - string:
+          name: c3poBranchName
+          default: '{c3po-branch-name}'
+          description: 'Branch name of c3po repo which we run tests against'
+
+      - string:
+          name: ngicBranchName
+          default: '{ngic-branch-name}'
+          description: 'Branch name of ngic-rtc repo which we run tests against'
+
+      - string:
+          name: openmmeBranchName
+          default: '{openmme-branch-name}'
+          description: 'Branch name of openmme repo which we run tests against'
+
+      - string:
+          name: maintainers
+          default: 'jeremyr@opennetworking.org, you@opennetworking.org'
+          description: "The person that sould be notified if this job fails"
+
+    project-type: pipeline
+    concurrent: true
+
+    dsl: !include-raw-escape: pipeline/{pipeline-file}
diff --git a/jjb/pipeline/omec-postmerge.groovy b/jjb/pipeline/omec-postmerge.groovy
new file mode 100644
index 0000000..9ba9398
--- /dev/null
+++ b/jjb/pipeline/omec-postmerge.groovy
@@ -0,0 +1,80 @@
+// Copyright 2020-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.
+
+// omec-postmerge.groovy
+// Combines docker-publish and deploy-staging pipelines into one job that can be triggered by a GitHub PR merge
+
+def hssdb_tag = ""
+def hss_tag = ""
+def mme_tag = ""
+def spgwc_tag = ""
+def spgwu_tag = ""
+
+pipeline {
+
+  agent {
+    label "${params.buildNode}"
+  }
+
+  stages {
+
+    stage('Publish') {
+      steps {
+        build job: "docker-publish-github_$repoName", parameters: [
+              string(name: 'gitUrl', value: "${repoUrl}"),
+              string(name: 'gitRef', value: "${branchName}"),
+              string(name: 'branchName', value: "${branchName}-${commitHash}"),
+              string(name: 'projectName', value: "${repoName}"),
+            ]
+      }
+    }
+
+    stage('Deploy') {
+      steps {
+        script {
+          hssdb_tag = sh returnStdout: true, script: """curl -s 'https://registry.hub.docker.com/v2/repositories/omecproject/c3po-hssdb/tags/' | jq '.results[] | select(.name | contains("${c3poBranchName}")).name' | head -1 | tr -d \\\""""
+          hss_tag = sh returnStdout: true, script: """curl -s 'https://registry.hub.docker.com/v2/repositories/omecproject/c3po-hss/tags/' | jq '.results[] | select(.name | contains("${c3poBranchName}")).name' | head -1 | tr -d \\\""""
+          mme_tag = sh returnStdout: true, script: """curl -s 'https://registry.hub.docker.com/v2/repositories/omecproject/openmme/tags/' | jq '.results[] | select(.name | contains("${openmmeBranchName}")).name' | head -1 | tr -d \\\""""
+          spgwc_tag = sh returnStdout: true, script: """curl -s 'https://registry.hub.docker.com/v2/repositories/omecproject/ngic-cp/tags/' | jq '.results[] | select(.name | contains("${ngicBranchName}")).name' | head -1 | tr -d \\\""""
+          spgwu_tag = sh returnStdout: true, script: """curl -s 'https://registry.hub.docker.com/v2/repositories/omecproject/ngic-dp/tags/' | jq '.results[] | select(.name | contains("${ngicBranchName}")).name' | head -1 | tr -d \\\""""
+          switch("${params.repoName}") {
+          case "c3po":
+            hssdb_tag = "${branchName}-${commitHash}"
+            hss_tag = "${branchName}-${commitHash}"
+            break
+          case "ngic-rtc":
+            spgwc_tag = "${branchName}-${commitHash}"
+            spgwu_tag = "${branchName}-${commitHash}"
+            break
+          case "openmme":
+            mme_tag = "${branchName}-${commitHash}"
+            break
+          }
+        }
+        build job: "omec-deploy-staging", parameters: [
+              string(name: 'hssdb_tag', value: "${hssdb_tag}"),
+              string(name: 'mme_tag', value: "${hss_tag}"),
+              string(name: 'hss_tag', value: "${mme_tag}"),
+              string(name: 'spgwc_tag', value: "${spgwc_tag}"),
+              string(name: 'spgwu_tag', value: "${spgwu_tag}"),
+            ]
+      }
+    }
+  }
+  post {
+    failure {
+      step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: "${params.maintainers}", sendToIndividuals: false])
+    }
+  }
+}