From ac776c5e82588e75ccc01c114d51fe7f6834385b Mon Sep 17 00:00:00 2001
From: Leonardo Rossetti <lrossett@redhat.com>
Date: Dec 22 2021 17:21:32 +0000
Subject: image build and push job


---

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..5c13f97
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,6 @@
+# IDE/editor section
+.idea
+
+# project specific
+*.ini
+venv/
diff --git a/README.md b/README.md
index 07d5ad8..f27bc50 100644
--- a/README.md
+++ b/README.md
@@ -40,4 +40,4 @@ jenkins-jobs update ./defs
 
 ## License
 
-MIT (See license file)
+MIT (See LICENSE file for details)
diff --git a/defs/jobs/images/container-image/Jenkinsfile b/defs/jobs/images/container-image/Jenkinsfile
index 62c2d49..b699515 100644
--- a/defs/jobs/images/container-image/Jenkinsfile
+++ b/defs/jobs/images/container-image/Jenkinsfile
@@ -1,16 +1,73 @@
 #!groovy
 
+def git_refs = params.GIT_REFS
+def git_repo = params.GIT_REPO
 def chdir = params.CHDIR
-def image = params.IMAGE_NAME
+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'
+         }
+    }
 
-podTemplate(cloud: 'openshift', envVars: [secretEnvVar(key: 'DUFFY_API_KEY', optional: false, secretKey: 'jey', secretName: ' duffy-api-key')], inheritFrom: 'cico-workspace', name: 'myname', namespace: 'fedora-kube-sig', volumes: [secretVolume(defaultMode: '', mountPath: '/mnt/duffy-ssh-key', optional: false, secretName: 'duffy-ssh-key')]) {
-    
     stage('prepare') {
-        sh 'mkdir -p $HOME/.docker'
+        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('done') {
-        // cleanup tasks      
+    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
index a5e182a..af09bba 100644
--- a/defs/jobs/images/container-image/job.yaml
+++ b/defs/jobs/images/container-image/job.yaml
@@ -10,22 +10,23 @@
               - master
             clean:
               after: true
-      script-path: deploy/jenkins/defs/jobs/images/container-image-build/Jenkinsfile
+      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"
-          default: "master"
+          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: "full qualified image name to use, example: quay.io/fedora/myimage:latest"
+          description: "fully qualified image name to use, example: quay.io/fedora/myimage:latest"
       - string:
-          name: "QUAY_SECRET"
+          name: "QUAY_SECRET_NAME"
           description: "quay secret name to use"
-          default: "something"
+          default: "quay-secret"

