From 844ab5840ee649b0c1a4facbb04fe3121b170137 Mon Sep 17 00:00:00 2001
From: Leonardo Rossetti <lrossett@redhat.com>
Date: May 20 2022 18:04:20 +0000
Subject: image-build template job


---

diff --git a/defs/jobs/gating-services/gating-services-images-pagure-ci/Jenkinsfile b/defs/jobs/gating-services/gating-services-images-pagure-ci/Jenkinsfile
new file mode 100644
index 0000000..171636e
--- /dev/null
+++ b/defs/jobs/gating-services/gating-services-images-pagure-ci/Jenkinsfile
@@ -0,0 +1,40 @@
+def repo = params.REPO
+def branch = params.BRANCH
+def branch_to = params.BRANCH_TO
+
+def push_secret_name = 'quay-secret'
+def image_push = branch_to == 'None'
+
+node('cico-workspace') {
+    def jobName = 'container-image-build-and-push'   
+    def jobs = [
+        [
+            'chdir': 'images/resultsdb-backend',
+            'image_name': 'quay.io/fedora-kube-sig/resultsdb-backend:latest-f35',
+            'extra_args': 'OS_NAME=fedora OS_VERSION=35',
+            'push_secret_name': push_secret_name,
+            'image_push': image_push
+        ],
+        [
+            'chdir': 'images/resultsdb-frontend',
+            'image_name': 'quay.io/fedora-kube-sig/resultsdb-frontend:latest-f35',
+            'extra_args': 'OS_NAME=fedora OS_VERSION=35',
+            'push_secret_name': push_secret_name,
+            'image_push': image_push
+        ]
+    ]
+
+    jobs.each { job ->
+        def jobParams = [
+            string(name: 'GIT_URL', value: repo),
+            string(name: 'GIT_REFS', value: branch),
+            string(name: 'CHDIR', value: job.chdir),
+            string(name: 'IMAGE_NAME', value: job.image_name),
+            string(name: 'IMAGE_BUILD_EXTRA_ARGS', value: job.extra_args),
+            string(name: 'PUSH_SECRET_NAME', value: job.push_secret_name),
+            booleanParam(name: 'IMAGE_PUSH', value: job.image_push)
+        ]
+    
+        build job: jobName, parameters: jobParams 
+    }
+}
diff --git a/defs/jobs/gating-services/gating-services-images-pagure-ci/job.yaml b/defs/jobs/gating-services/gating-services-images-pagure-ci/job.yaml
new file mode 100644
index 0000000..2439a4f
--- /dev/null
+++ b/defs/jobs/gating-services/gating-services-images-pagure-ci/job.yaml
@@ -0,0 +1,40 @@
+- job-template:
+    name: gating-services-images-pagure-ci
+    project-type: pipeline
+    display-name: Gating Services Images Pagure CI
+    auth-token: "{auth_token}"
+    notifications:
+      - raw:
+          xml: |-
+            <com.tikal.hudson.plugins.notification.Endpoint>
+              <protocol>HTTP</protocol>
+              <format>JSON</format>
+              <urlInfo>
+                <urlOrId>gating-services-images-pagure-ci-url</urlOrId>
+                <urlType>SECRET</urlType>
+              </urlInfo>
+              <event>all</event>
+              <timeout>60000</timeout>
+              <loglines>0</loglines>
+              <buildNotes/>
+              <retries>0</retries>
+            </com.tikal.hudson.plugins.notification.Endpoint>
+    pipeline-scm:
+      scm:
+        - git:
+            url: https://pagure.io/kube-sig/jenkins.git
+            branches:
+              - master
+            clean:
+              after: true
+      script-path: defs/jobs/gating-services/gating-services-images-pagure-ci/Jenkinsfile
+    parameters:
+      - string:
+          name: "REPO"
+          description: "The GIT repository to pull from"
+      - string:
+          name: "BRANCH"
+          description: "GIT branch to  source from"
+      - string:
+          name: "BRANCH_TO"
+          description: "GIT branch merge to"
diff --git a/defs/jobs/images/container-image/Jenkinsfile b/defs/jobs/images/container-image/Jenkinsfile
deleted file mode 100644
index b699515..0000000
--- a/defs/jobs/images/container-image/Jenkinsfile
+++ /dev/null
@@ -1,73 +0,0 @@
-#!groovy
-
-def git_refs = params.GIT_REFS
-def git_repo = params.GIT_REPO
-def chdir = params.CHDIR
-def image_name = params.IMAGE_NAME
-def quay_secret_name = params.QUAY_SECRET_NAME
-
-def node_sh(cmd) {
-    def key_path = '/duffy-ssh-key/ssh-privatekey'
-    def vars = [
-        [var: 'node_hostname', password: node_hostname]
-    ]
-    def regexes = [
-        [regex: ',([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})\'']
-    ]
-
-    wrap([$class: 'MaskPasswordsBuildWrapper', varPasswordPairs: vars, varMaskRegexes: regexes]) {
-        sh "ssh -o StrictHostKeyChecking=no root@${node_hostname} -i ${key_path} '${cmd}'"
-    }
-}
-
-node('cico-workspace') {
-    stage('cico') {
-        node = sh(script: "cico --debug node get -f value -c hostname -c comment", returnStdout: true).trim().tokenize(' ')
-        env.node_hostname = "${node[0]}.ci.centos.org"
-        env.node_ssid = "${node[1]}"
-
-         wrap([$class: 'MaskPasswordsBuildWrapper', varPasswordPairs: [[var: 'node_hostname', password: node_hostname]], varMaskRegexes: []]) {
-            node_sh 'cat /etc/centos-release'
-         }
-    }
-
-    stage('prepare') {
-        node_sh 'yum install -y podman'
-
-        withCredentials([file(credentialsId: quay_secret_name, variable: 'quay_secret_json')]) {
-            node_sh 'mkdir -p /.docker'
-
-            dir("/tmp/${env.node_hostname}") {
-                writeFile file: 'config.json', text: readFile(quay_secret_json)
-                wrap([$class: 'MaskPasswordsBuildWrapper', varPasswordPairs: [[var: 'node_hostname', password: node_hostname]], varMaskRegexes: []]) {
-                    sh "scp -o StrictHostKeyChecking=no -i /duffy-ssh-key/ssh-privatekey ./config.json root@${node_hostname}:/.docker"
-                }
-                deleteDir()
-            }
-        }
-    }
-
-    stage('checkout') {
-        dir('koji-operator') {
-            git branch: git_refs, url: git_url
-
-            node_sh "mkdir -p /tmp/${chdir}"
-
-            wrap([$class: 'MaskPasswordsBuildWrapper', varPasswordPairs: [[var: 'node_hostname', password: node_hostname]], varMaskRegexes: []]) {
-                sh "scp -o StrictHostKeyChecking=no -i /duffy-ssh-key/ssh-privatekey -r ${chdir}/* root@${node_hostname}:/tmp/${chdir}"
-            }
-        }
-    }
-
-    stage('build') {
-        node_sh "podman build -t ${image_name} -f /tmp/${chdir}/Dockerfile /tmp/${chdir}"
-    }
-
-    stage('push') {
-            node_sh "podman push --authfile /.docker/config.json ${image_name}"
-        }
-
-    stage('cleanup') {
-        sh "cico node done ${node_ssid}"
-    }
-}
diff --git a/defs/jobs/images/container-image/job.yaml b/defs/jobs/images/container-image/job.yaml
deleted file mode 100644
index af09bba..0000000
--- a/defs/jobs/images/container-image/job.yaml
+++ /dev/null
@@ -1,32 +0,0 @@
-- job:
-    name: container-image
-    project-type: pipeline
-    display-name: Container Image Build n Push
-    pipeline-scm:
-      scm:
-        - git:
-            url: https://pagure.io/kube-sig/jenkins.git
-            branches:
-              - master
-            clean:
-              after: true
-      script-path: defs/jobs/images/container-image/Jenkinsfile
-    parameters:
-      - string:
-          name: "GIT_URL"
-          description: "GIT repository url to pull from"
-      - string:
-          name: "GIT_REFS"
-          description: "GIT refs to use, such as branch, tag, commit, etc"
-          default: "main"
-      - string:
-          name: "CHDIR"
-          description: "Dockerfile directory to use"
-          default: "."
-      - string:
-          name: "IMAGE_NAME"
-          description: "fully qualified image name to use, example: quay.io/fedora/myimage:latest"
-      - string:
-          name: "QUAY_SECRET_NAME"
-          description: "quay secret name to use"
-          default: "quay-secret"
diff --git a/defs/jobs/library/container-image-build-and-push/Jenkinsfile b/defs/jobs/library/container-image-build-and-push/Jenkinsfile
new file mode 100644
index 0000000..a531177
--- /dev/null
+++ b/defs/jobs/library/container-image-build-and-push/Jenkinsfile
@@ -0,0 +1,88 @@
+def git_refs = params.GIT_REFS
+def git_repo = params.GIT_REPO
+def chdir = params.CHDIR
+def image_name = params.IMAGE_NAME
+def image_build_extra_args = params.IMAGE_BUILD_EXTRA_ARGS
+def image_push = params.IMAGE_PUSH
+def push_secret_name = params.PUSH_SECRET_NAME
+
+def node_sh(cmd) {
+    def key_path = '/duffy-ssh-key/ssh-privatekey'
+    def vars = [
+        [var: 'node_hostname', password: node_hostname]
+    ]
+    def regexes = [
+        [regex: ',([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})\'']
+    ]
+
+    wrap([$class: 'MaskPasswordsBuildWrapper', varPasswordPairs: vars, varMaskRegexes: regexes]) {
+        sh "ssh -o StrictHostKeyChecking=no root@${node_hostname} -i ${key_path} '${cmd}'"
+    }
+}
+
+node('cico-workspace') {
+    try {
+        stage('cico') {
+            node = sh(script: "cico --debug node get --release 9-stream -f value -c hostname -c comment", returnStdout: true).trim().tokenize(' ')
+            env.node_hostname = "${node[0]}.ci.centos.org"
+            env.node_ssid = "${node[1]}"
+
+             wrap([$class: 'MaskPasswordsBuildWrapper', varPasswordPairs: [[var: 'node_hostname', password: node_hostname]], varMaskRegexes: []]) {
+                node_sh 'cat /etc/centos-release'
+             }
+        }
+
+        stage('prepare') {
+            node_sh 'yum install -y make podman'
+
+            if (image_push) {
+                withCredentials([file(credentialsId: push_secret_name, variable: 'push_secret_json')]) {
+                    node_sh 'mkdir -p /.docker'
+
+                    dir("/tmp/${env.node_hostname}") {
+                        writeFile file: 'config.json', text: readFile(push_secret_json)
+                        wrap([$class: 'MaskPasswordsBuildWrapper', varPasswordPairs: [[var: 'node_hostname', password: node_hostname]], varMaskRegexes: []]) {
+                            sh "scp -o StrictHostKeyChecking=no -i /duffy-ssh-key/ssh-privatekey ./config.json root@${node_hostname}:/.docker"
+                        }
+                        deleteDir()
+                    }
+                }
+            }
+        }
+
+        stage('checkout') {
+            dir('repository') {
+                git branch: git_refs, url: git_url
+
+                node_sh "mkdir -p /tmp/${chdir}"
+
+                wrap([$class: 'MaskPasswordsBuildWrapper', varPasswordPairs: [[var: 'node_hostname', password: node_hostname]], varMaskRegexes: []]) {
+                    sh "scp -o StrictHostKeyChecking=no -i /duffy-ssh-key/ssh-privatekey -r ${chdir}/* root@${node_hostname}:/tmp/${chdir}"
+                }
+            }
+        }
+
+        stage('build') {
+            node_sh "(cd /tmp/${chdir}; make image/build IMAGE=${image_name} ${image_build_extra_args};)"
+        }
+
+        stage('push') {
+            if (image_push) {
+                node_sh "podman push --authfile /.docker/config.json ${image_name}"
+            }
+        }
+
+        stage('cleanup') {
+            wrap([$class: 'MaskPasswordsBuildWrapper', varPasswordPairs: [[var: 'node_hostname', password: node_hostname]], varMaskRegexes: []]) {
+                sh "cico -q node done ${node_ssid}"
+            }
+        }
+
+    } catch (Exception e) {
+        stage('fail') {
+            sh "cico node done ${node_ssid}"
+            error '[ERROR] ' + e.toString()
+        }
+    }
+} 
+
diff --git a/defs/jobs/library/container-image-build-and-push/job.yaml b/defs/jobs/library/container-image-build-and-push/job.yaml
new file mode 100644
index 0000000..2dea522
--- /dev/null
+++ b/defs/jobs/library/container-image-build-and-push/job.yaml
@@ -0,0 +1,37 @@
+- job:
+    name: "container-image-build-and-push"
+    project-type: pipeline
+    display-name: Container Image Build and Push
+    pipeline-scm:
+      scm:
+        - git:
+            url: https://pagure.io/kube-sig/jenkins.git
+            branches:
+              - master
+            clean:
+              after: true
+      script-path: defs/jobs/library/container-image-build-and-push/Jenkinsfile
+    parameters:
+      - string:
+          name: "GIT_URL"
+          description: "GIT repository url to pull from"
+      - string:
+          name: "GIT_REFS"
+          description: "GIT refs to use, such as branch, tag, commit, etc"
+      - string:
+          name: "CHDIR"
+          description: "Dockerfile directory to use"
+      - string:
+          name: "IMAGE_NAME"
+          description: "fully qualified image name to use, example: quay.io/fedora/myimage:latest"
+      - string:
+          name: "IMAGE_BUILD_EXTRA_ARGS"
+          description: "extra build arguments to be used when building the image (KEY1=VALUE1 KEY2=VALUE2)"
+      - string:
+          name: "PUSH_SECRET_NAME"
+          description: "push secret name to use"
+      - bool:
+          name: "IMAGE_PUSH"
+          description: "boolean value to indicate if the built image should also be pushed to a registry"
+          default: true
+
diff --git a/defs/projects.yaml b/defs/projects.yaml
index 3a0803e..2ee22ed 100644
--- a/defs/projects.yaml
+++ b/defs/projects.yaml
@@ -4,3 +4,9 @@
       - koji-operator-pagure-ci:
           auth_token: "{job_koji_operator_pagure_ci_auth_token}"
 
+
+- project:
+    name: gating-services
+    jobs:
+      - gating-services-images-pagure-ci:
+          auth_token: "{job_gating_services_images_pagure_ci_auth_token}"
diff --git a/defs/views.yaml b/defs/views.yaml
index 9959f52..58d85cf 100644
--- a/defs/views.yaml
+++ b/defs/views.yaml
@@ -1,8 +1,8 @@
 - view:
-    name: images
+    name: library
     view-type: list
     job-name:
-      - container-image
+      - container-image-build-and-push
     columns:
       - status
       - weather
@@ -34,4 +34,17 @@
       - job
       - last-success
       - last-failure
-      - last-duration
\ No newline at end of file
+      - last-duration
+
+- view:
+    name: gating-services
+    view-type: list
+    job-name:
+      - gating-services-images-pagure-ci
+    columns:
+      - status
+      - weather
+      - job
+      - last-success
+      - last-failure
+      - last-duration

