From ff15480fec78ffefd8bf16f2f647e3f13470925e Mon Sep 17 00:00:00 2001 From: Michal Kovarik Date: Jan 22 2020 13:34:53 +0000 Subject: C3I - template usage Use jinja2 templates for: c3i-library include agent include - secret is taken using openshift, not as variable include jenkinsfiles directly not as a refence to repository --- diff --git a/openshift/pipelines/Makefile b/openshift/pipelines/Makefile index 993653c..beb43ad 100644 --- a/openshift/pipelines/Makefile +++ b/openshift/pipelines/Makefile @@ -22,7 +22,7 @@ install: @for job in $(JOBS); do \ echo "[PIPELINE] Updating pipeline job \"$${job}\"..." ; \ template_file=$$(cat ./$(JOBS_DIR)/$${job}.tmpl); \ - $(OC_CMD) process --local -f ./$(TEMPLATES_DIR)/$${template_file} \ + jinja2 ./$(TEMPLATES_DIR)/$${template_file} | $(OC_CMD) process --local -f - \ --param-file ./$(JOBS_DIR)/$${job}.env | $(OC_CMD) apply -f -; \ echo "[PIPELINE] Pipeline job \"$${job}\" updated" ; \ done diff --git a/openshift/pipelines/templates/snippets/c3i-library-parameters.yaml b/openshift/pipelines/templates/snippets/c3i-library-parameters.yaml new file mode 100644 index 0000000..5f96d60 --- /dev/null +++ b/openshift/pipelines/templates/snippets/c3i-library-parameters.yaml @@ -0,0 +1,8 @@ +- name: C3I_LIB_URL + displayName: C3I library git url + required: true + value: "https://pagure.io/c3i-library.git" +- name: C3I_LIB_BRANCH + displayName: C3I library branch + required: true + value: "master" diff --git a/openshift/pipelines/templates/snippets/c3i-library.groovy b/openshift/pipelines/templates/snippets/c3i-library.groovy new file mode 100644 index 0000000..e752c81 --- /dev/null +++ b/openshift/pipelines/templates/snippets/c3i-library.groovy @@ -0,0 +1,2 @@ +library identifier: "c3i@${C3I_LIB_BRANCH}", changelog: false, + retriever: modernSCM([$class: 'GitSCMSource', remote: "${C3I_LIB_URL}"]) diff --git a/openshift/pipelines/templates/snippets/default-agent.groovy b/openshift/pipelines/templates/snippets/default-agent.groovy new file mode 100644 index 0000000..c6f5180 --- /dev/null +++ b/openshift/pipelines/templates/snippets/default-agent.groovy @@ -0,0 +1,29 @@ +agent { + kubernetes { + cloud "${params.JENKINS_AGENT_CLOUD_NAME}" + label "jenkins-slave-${UUID.randomUUID().toString()}" + serviceAccount "${params.JENKINS_AGENT_SERVICE_ACCOUNT}" + defaultContainer 'jnlp' + yaml """ + apiVersion: v1 + kind: Pod + metadata: + labels: + app: "${env.JOB_BASE_NAME}" + factory2-pipeline-build-number: "${env.BUILD_NUMBER}" + spec: + containers: + - name: jnlp + image: "${params.JENKINS_AGENT_IMAGE}" + imagePullPolicy: Always + tty: true + resources: + requests: + memory: 512Mi + cpu: 200m + limits: + memory: 768Mi + cpu: 300m + """ + } +} diff --git a/openshift/pipelines/templates/waiverdb-build-template.yaml b/openshift/pipelines/templates/waiverdb-build-template.yaml index 6a7ec15..ab6f6b6 100644 --- a/openshift/pipelines/templates/waiverdb-build-template.yaml +++ b/openshift/pipelines/templates/waiverdb-build-template.yaml @@ -114,6 +114,7 @@ parameters: value: 'pagure-api-key' - name: MAIL_ADDRESS displayName: If set, build failure messages to this mail address. +{% include "snippets/c3i-library-parameters.yaml" %} labels: template: waiverdb-build objects: @@ -179,10 +180,6 @@ objects: spec: runPolicy: "Serial" completionDeadlineSeconds: 1800 - source: - git: - uri: "${WAIVERDB_GIT_REPO}" - ref: "${WAIVERDB_MAIN_BRANCH}" strategy: type: JenkinsPipeline jenkinsPipelineStrategy: @@ -233,4 +230,5 @@ objects: value: "${PAGURE_API_KEY_SECRET_NAME}" - name: MAIL_ADDRESS value: "${MAIL_ADDRESS}" - jenkinsfilePath: openshift/pipelines/templates/waiverdb-build.Jenkinsfile + jenkinsfile: | + {% filter indent(width=10) %}{% include "waiverdb-build.Jenkinsfile" %}{% endfilter %} diff --git a/openshift/pipelines/templates/waiverdb-build.Jenkinsfile b/openshift/pipelines/templates/waiverdb-build.Jenkinsfile index b16dfaa..95cc2aa 100644 --- a/openshift/pipelines/templates/waiverdb-build.Jenkinsfile +++ b/openshift/pipelines/templates/waiverdb-build.Jenkinsfile @@ -1,5 +1,4 @@ -library identifier: 'c3i@master', changelog: false, - retriever: modernSCM([$class: 'GitSCMSource', remote: 'https://pagure.io/c3i-library.git']) +{% include "snippets/c3i-library.groovy" %} import static org.apache.commons.lang.StringEscapeUtils.escapeHtml; pipeline { agent { @@ -23,11 +22,6 @@ pipeline { imagePullPolicy: Always tty: true env: - - name: REGISTRY_CREDENTIALS - valueFrom: - secretKeyRef: - name: "${params.CONTAINER_REGISTRY_CREDENTIALS}" - key: '.dockerconfigjson' # Required by unit tests: Set up NSS Wrapper to generate a fake user name for the random UID assigned by OpenShift - name: LD_PRELOAD value: '/usr/lib64/libnss_wrapper.so' @@ -355,10 +349,11 @@ pipeline { env.WAIVERDB_DEV_IMAGE_DESTINATIONS.split(',') : [] openshift.withCluster() { def sourceImage = env.RESULTING_IMAGE_REPO + ":" + env.RESULTING_TAG - if (env.REGISTRY_CREDENTIALS) { - dir ("${env.HOME}/.docker") { - writeFile file:'config.json', text: env.REGISTRY_CREDENTIALS - } + if (params.CONTAINER_REGISTRY_CREDENTIALS) { + dir ("${env.HOME}/.docker") { + def dockerconf = openshift.selector('secret', params.CONTAINER_REGISTRY_CREDENTIALS).object().data['.dockerconfigjson'] + writeFile file: 'config.json', text: dockerconf, encoding: "Base64" + } } // pull the built image from imagestream echo "Pulling container from ${sourceImage}..." diff --git a/openshift/pipelines/templates/waiverdb-full-integration-test-template.yaml b/openshift/pipelines/templates/waiverdb-full-integration-test-template.yaml index 51673ea..33f7a7e 100644 --- a/openshift/pipelines/templates/waiverdb-full-integration-test-template.yaml +++ b/openshift/pipelines/templates/waiverdb-full-integration-test-template.yaml @@ -94,10 +94,6 @@ objects: spec: runPolicy: "Parallel" completionDeadlineSeconds: 1800 - source: - git: - uri: "${WAIVERDB_GIT_REPO}" - ref: "${WAIVERDB_GIT_REF}" strategy: type: JenkinsPipeline jenkinsPipelineStrategy: @@ -136,4 +132,5 @@ objects: value: "${BACKEND_INTEGRATION_TEST_REPO_BRANCH}" - name: BACKEND_INTEGRATION_TEST_FILE value: "${BACKEND_INTEGRATION_TEST_FILE}" - jenkinsfilePath: openshift/pipelines/templates/waiverdb-full-integration-test.Jenkinsfile + jenkinsfile: | + {% filter indent(width=10) %}{% include "waiverdb-full-integration-test.Jenkinsfile" %}{% endfilter %} diff --git a/openshift/pipelines/templates/waiverdb-full-integration-test.Jenkinsfile b/openshift/pipelines/templates/waiverdb-full-integration-test.Jenkinsfile index cc205bd..adedde9 100644 --- a/openshift/pipelines/templates/waiverdb-full-integration-test.Jenkinsfile +++ b/openshift/pipelines/templates/waiverdb-full-integration-test.Jenkinsfile @@ -1,36 +1,7 @@ -library identifier: 'c3i@master', changelog: false, - retriever: modernSCM([$class: 'GitSCMSource', remote: 'https://pagure.io/c3i-library.git']) +{% include "snippets/c3i-library.groovy" %} def pipeline_data pipeline { - agent { - kubernetes { - cloud "${params.JENKINS_AGENT_CLOUD_NAME}" - label "jenkins-slave-${UUID.randomUUID().toString()}" - serviceAccount "${params.JENKINS_AGENT_SERVICE_ACCOUNT}" - defaultContainer 'jnlp' - yaml """ - apiVersion: v1 - kind: Pod - metadata: - labels: - app: "${env.JOB_BASE_NAME}" - factory2-pipeline-kind: "waiverdb-integration-test-pipeline" - factory2-pipeline-build-number: "${env.BUILD_NUMBER}" - spec: - containers: - - name: jnlp - image: "${params.JENKINS_AGENT_IMAGE}" - imagePullPolicy: Always - resources: - requests: - memory: 512Mi - cpu: 200m - limits: - memory: 768Mi - cpu: 300m - """ - } - } + {% include "snippets/default-agent.groovy" %} options { timestamps() timeout(time: 30, unit: 'MINUTES') diff --git a/openshift/pipelines/templates/waiverdb-greenwave-trigger.Jenkinsfile b/openshift/pipelines/templates/waiverdb-greenwave-trigger.Jenkinsfile index 221ec41..0de83ec 100644 --- a/openshift/pipelines/templates/waiverdb-greenwave-trigger.Jenkinsfile +++ b/openshift/pipelines/templates/waiverdb-greenwave-trigger.Jenkinsfile @@ -1,7 +1,5 @@ // Use scripted syntax because CIBuildTrigger currently doesn't support the declarative syntax -library identifier: 'c3i@master', changelog: false, - retriever: modernSCM([$class: 'GitSCMSource', remote: 'https://pagure.io/c3i-library.git']) - +{% include "snippets/c3i-library.groovy" %} properties([ disableConcurrentBuilds(), pipelineTriggers([ diff --git a/openshift/pipelines/templates/waiverdb-greenwave-trigger.yaml b/openshift/pipelines/templates/waiverdb-greenwave-trigger.yaml index 77ae946..9796a91 100644 --- a/openshift/pipelines/templates/waiverdb-greenwave-trigger.yaml +++ b/openshift/pipelines/templates/waiverdb-greenwave-trigger.yaml @@ -47,6 +47,7 @@ parameters: - name: OPENSHIFT_CLOUD_NAME displayName: Name of OpenShift cloud in Jenkins master configuration value: openshift +{% include "snippets/c3i-library-parameters.yaml" %} objects: - kind: ServiceAccount apiVersion: v1 @@ -74,10 +75,6 @@ objects: spec: runPolicy: "Serial" completionDeadlineSeconds: 1800 - source: - git: - uri: "${WAIVERDB_GIT_REPO}" - ref: "${WAIVERDB_GIT_REF}" strategy: type: JenkinsPipeline jenkinsPipelineStrategy: @@ -111,4 +108,5 @@ objects: value: - name: MESSAGE_HEADERS value: - jenkinsfilePath: openshift/pipelines/templates/waiverdb-greenwave-trigger.Jenkinsfile + jenkinsfile: | + {% filter indent(width=10) %}{% include "waiverdb-greenwave-trigger.Jenkinsfile" %}{% endfilter %} diff --git a/openshift/pipelines/templates/waiverdb-image-promotion-template.yaml b/openshift/pipelines/templates/waiverdb-image-promotion-template.yaml index 2575933..53c2447 100644 --- a/openshift/pipelines/templates/waiverdb-image-promotion-template.yaml +++ b/openshift/pipelines/templates/waiverdb-image-promotion-template.yaml @@ -63,6 +63,7 @@ parameters: displayName: Name of OpenShift cloud in Jenkins master configuration required: true value: openshift +{% include "snippets/c3i-library-parameters.yaml" %} objects: - kind: ServiceAccount apiVersion: v1 @@ -90,10 +91,6 @@ objects: spec: runPolicy: "Serial" # FIXME: Parallel is supported, but we have limited quota in UpShift. completionDeadlineSeconds: 1800 - source: - git: - uri: "${WAIVERDB_GIT_REPO}" - ref: "${WAIVERDB_GIT_REF}" strategy: type: JenkinsPipeline source: @@ -120,4 +117,5 @@ objects: value: "${JENKINS_AGENT_CLOUD_NAME}" - name: JENKINS_AGENT_SERVICE_ACCOUNT value: "${NAME}-jenkins-slave" - jenkinsfilePath: openshift/pipelines/templates/waiverdb-image-promotion.Jenkinsfile + jenkinsfile: | + {% filter indent(width=10) %}{% include "waiverdb-image-promotion.Jenkinsfile" %}{% endfilter %} diff --git a/openshift/pipelines/templates/waiverdb-image-promotion.Jenkinsfile b/openshift/pipelines/templates/waiverdb-image-promotion.Jenkinsfile index c76272d..5b7c230 100644 --- a/openshift/pipelines/templates/waiverdb-image-promotion.Jenkinsfile +++ b/openshift/pipelines/templates/waiverdb-image-promotion.Jenkinsfile @@ -1,40 +1,5 @@ pipeline { - agent { - kubernetes { - cloud "${params.JENKINS_AGENT_CLOUD_NAME}" - label "jenkins-slave-${UUID.randomUUID().toString()}" - serviceAccount "${params.JENKINS_AGENT_SERVICE_ACCOUNT}" - defaultContainer 'jnlp' - yaml """ - apiVersion: v1 - kind: Pod - metadata: - labels: - app: "jenkins-${env.JOB_BASE_NAME}" - factory2-pipeline-kind: "waiverdb-image-promotion-pipeline" - factory2-pipeline-build-number: "${env.BUILD_NUMBER}" - spec: - containers: - - name: jnlp - image: "${params.JENKINS_AGENT_IMAGE}" - imagePullPolicy: Always - tty: true - env: - - name: REGISTRY_CREDENTIALS - valueFrom: - secretKeyRef: - name: "${params.CONTAINER_REGISTRY_CREDENTIALS}" - key: '.dockerconfigjson' - resources: - requests: - memory: 512Mi - cpu: 200m - limits: - memory: 768Mi - cpu: 500m - """ - } - } + {% include "snippets/default-agent.groovy" %} options { timestamps() timeout(time: 30, unit: 'MINUTES') @@ -56,9 +21,13 @@ pipeline { 'auth': sh(returnStdout: true, script: 'set +x; echo -n "serviceaccount:$SERVICE_ACCOUNT_TOKEN" | base64 -').trim() ] // merging user specified credentials - if (env.REGISTRY_CREDENTIALS) { - toBeMerged = readJSON text: env.REGISTRY_CREDENTIALS - dockerConfig.auths.putAll(toBeMerged.auths) + if (params.CONTAINER_REGISTRY_CREDENTIALS) { + openshift.withCluster() { + def dockerconf = openshift.selector('secret', params.CONTAINER_REGISTRY_CREDENTIALS).object().data['.dockerconfigjson'] + def dockerString = new String(dockerconf.decodeBase64()) + toBeMerged = readJSON text: dockerString + dockerConfig.auths.putAll(toBeMerged.auths) + } } // writing to ~/.docker/config.json writeJSON file: 'config.json', json: dockerConfig diff --git a/openshift/pipelines/templates/waiverdb-integration-test-template.yaml b/openshift/pipelines/templates/waiverdb-integration-test-template.yaml index 696f9e4..c309f04 100644 --- a/openshift/pipelines/templates/waiverdb-integration-test-template.yaml +++ b/openshift/pipelines/templates/waiverdb-integration-test-template.yaml @@ -46,6 +46,7 @@ parameters: - name: MESSAGING_PROVIDER displayName: Name of the JMS messaging provider value: Red Hat UMB +{% include "snippets/c3i-library-parameters.yaml" %} objects: - kind: ServiceAccount apiVersion: v1 @@ -73,10 +74,6 @@ objects: spec: runPolicy: "Serial" # FIXME: Parallel is supported, but we have limited quota in UpShift. completionDeadlineSeconds: 1800 - source: - git: - uri: "${WAIVERDB_GIT_REPO}" - ref: "${WAIVERDB_GIT_REF}" strategy: type: JenkinsPipeline jenkinsPipelineStrategy: @@ -103,4 +100,5 @@ objects: value: "${ENVIRONMENT}" - name: MESSAGING_PROVIDER value: "${MESSAGING_PROVIDER}" - jenkinsfilePath: openshift/pipelines/templates/waiverdb-integration-test.Jenkinsfile + jenkinsfile: | + {% filter indent(width=10) %}{% include "waiverdb-integration-test.Jenkinsfile" %}{% endfilter %} diff --git a/openshift/pipelines/templates/waiverdb-integration-test.Jenkinsfile b/openshift/pipelines/templates/waiverdb-integration-test.Jenkinsfile index 8d4c7da..1de5da9 100644 --- a/openshift/pipelines/templates/waiverdb-integration-test.Jenkinsfile +++ b/openshift/pipelines/templates/waiverdb-integration-test.Jenkinsfile @@ -1,44 +1,8 @@ import java.text.* -library identifier: 'c3i@master', changelog: false, - retriever: modernSCM([$class: 'GitSCMSource', remote: 'https://pagure.io/c3i-library.git']) +{% include "snippets/c3i-library.groovy" %} pipeline { - agent { - kubernetes { - cloud "${params.JENKINS_AGENT_CLOUD_NAME}" - label "jenkins-slave-${UUID.randomUUID().toString()}" - serviceAccount "${params.JENKINS_AGENT_SERVICE_ACCOUNT}" - defaultContainer 'jnlp' - yaml """ - apiVersion: v1 - kind: Pod - metadata: - labels: - app: "${env.JOB_BASE_NAME}" - factory2-pipeline-kind: "waiverdb-integration-test-pipeline" - factory2-pipeline-build-number: "${env.BUILD_NUMBER}" - spec: - containers: - - name: jnlp - image: "${params.JENKINS_AGENT_IMAGE}" - imagePullPolicy: Always - tty: true - env: - - name: REGISTRY_CREDENTIALS - valueFrom: - secretKeyRef: - name: "${params.CONTAINER_REGISTRY_CREDENTIALS}" - key: '.dockerconfigjson' - resources: - requests: - memory: 512Mi - cpu: 200m - limits: - memory: 768Mi - cpu: 300m - """ - } - } + {% include "snippets/default-agent.groovy" %} options { timestamps() timeout(time: 30, unit: 'MINUTES') diff --git a/openshift/pipelines/templates/waiverdb-polling-pagure.Jenkinsfile b/openshift/pipelines/templates/waiverdb-polling-pagure.Jenkinsfile new file mode 100644 index 0000000..dec4684 --- /dev/null +++ b/openshift/pipelines/templates/waiverdb-polling-pagure.Jenkinsfile @@ -0,0 +1,136 @@ +{% include "snippets/c3i-library.groovy" %} +pipeline { + agent { + kubernetes { + cloud "${OPENSHIFT_CLOUD_NAME}" + label "jenkins-slave-${UUID.randomUUID().toString()}" + serviceAccount "${NAME}-jenkins-slave" + defaultContainer 'jnlp' + yaml """ + apiVersion: v1 + kind: Pod + metadata: + labels: + app: "jenkins-${env.JOB_BASE_NAME}" + factory2-pipeline-kind: "waiverdb-polling-to-pagure-pipeline" + factory2-pipeline-build-number: "${env.BUILD_NUMBER}" + spec: + containers: + - name: jnlp + image: "${JENKINS_AGENT_IMAGE}" + imagePullPolicy: Always + tty: true + resources: + requests: + memory: 378Mi + cpu: 200m + limits: + memory: 768Mi + cpu: 500m + """ + } + } + options { + timestamps() + } + environment { + PIPELINE_NAMESPACE = readFile('/run/secrets/kubernetes.io/serviceaccount/namespace').trim() + PAGURE_URL = "${PAGURE_URL}" + PAGURE_REPO_IS_FORK = "${PAGURE_REPO_IS_FORK}" + PAGURE_POLLING_FOR_PR = "${PAGURE_POLLING_FOR_PR}" + PAGURE_REPO_HOME = "${env.PAGURE_URL}${env.PAGURE_REPO_IS_FORK == 'true' ? '/fork' : ''}/${PAGURE_REPO_NAME}" + GIT_URL = "${env.PAGURE_URL}/${env.PAGURE_REPO_IS_FORK == 'true' ? 'forks/' : ''}${PAGURE_REPO_NAME}.git" + PREMERGE_JOB_NAME = "${PREMERGE_JOB_NAME}" + POSTMERGE_JOB_NAME = "${POSTMERGE_JOB_NAME}" + } + triggers { pollSCM("${PAGURE_POLLING_SCHEDULE}") } + stages { + stage('Prepare') { + agent { label 'master' } + steps { + script { + // checking out the polled branch + def polledBranch = env.PAGURE_POLLING_FOR_PR == 'true' ? 'origin/pull/*/head' : "origin/${PAGURE_POLLED_BRANCH}" + def scmVars = checkout([$class: 'GitSCM', + branches: [[name: polledBranch]], + userRemoteConfigs: [ + [ + name: 'origin', + url: env.GIT_URL, + refspec: '+refs/heads/*:refs/remotes/origin/* +refs/pull/*/head:refs/remotes/origin/pull/*/head', + ], + ], + extensions: [[$class: 'CleanBeforeCheckout']], + ]) + env.WAIVERDB_GIT_COMMIT = scmVars.GIT_COMMIT + // setting build display name + def prefix = 'origin/' + def branch = scmVars.GIT_BRANCH.startsWith(prefix) ? scmVars.GIT_BRANCH.substring(prefix.size()) + : scmVars.GIT_BRANCH // origin/pull/1234/head -> pull/1234/head, origin/master -> master + env.WAIVERDB_GIT_BRANCH = branch + echo "Build on branch=${env.WAIVERDB_GIT_BRANCH}, commit=${env.WAIVERDB_GIT_COMMIT}" + if (env.PAGURE_POLLING_FOR_PR == 'false') { + currentBuild.displayName = "${env.WAIVERDB_GIT_BRANCH}: ${env.WAIVERDB_GIT_COMMIT.substring(0, 7)}" + currentBuild.description = """${currentBuild.displayName}""" + } + else if (env.PAGURE_POLLING_FOR_PR == 'true' && branch ==~ /^pull\/[0-9]+\/head$/) { + env.PR_NO = branch.split('/')[1] + env.PR_URL = "${env.PAGURE_REPO_HOME}/pull-request/${env.PR_NO}" + // To HTML syntax in build description, go to `Jenkins/Global Security/Markup Formatter` and select 'Safe HTML'. + def pagureLink = """PR#${env.PR_NO}""" + echo "Building PR #${env.PR_NO}: ${env.PR_URL}" + currentBuild.displayName = "PR#${env.PR_NO}" + currentBuild.description = pagureLink + } else { // This shouldn't happen. + error("Build is aborted due to unexpected polling trigger actions.") + } + } + } + } + stage('Update pipeline jobs') { + when { + expression { + return "${PIPELINE_UPDATE_JOBS_DIR}" && env.PAGURE_POLLING_FOR_PR == 'false' && env.WAIVERDB_GIT_BRANCH == "${PAGURE_POLLED_BRANCH}" + } + } + steps { + checkout([$class: 'GitSCM', + branches: [[name: env.WAIVERDB_GIT_COMMIT]], + userRemoteConfigs: [ + [ + name: 'origin', + url: env.GIT_URL, + refspec: '+refs/heads/*:refs/remotes/origin/* +refs/pull/*/head:refs/remotes/origin/pull/*/head', + ], + ], + extensions: [[$class: 'CleanBeforeCheckout']], + ]) + script { + dir('openshift/pipelines') { + sh ''' + make install JOBS_DIR="${PIPELINE_UPDATE_JOBS_DIR}" + ''' + } + } + } + } + stage('Build') { + steps { + script { + openshift.withCluster() { + def bc = env.PAGURE_POLLING_FOR_PR == 'true' ? env.PREMERGE_JOB_NAME : env.POSTMERGE_JOB_NAME + def build = c3i.build(script: this, objs: "bc/${bc}", + '-e', "WAIVERDB_GIT_REF=${env.WAIVERDB_GIT_BRANCH}", + ) + c3i.waitForBuildStart(script: this, build: build) + def devBuildInfo = build.object() + def downstreamBuildName = devBuildInfo.metadata.name + def downstreamBuildUrl = devBuildInfo.metadata.annotations['openshift.io/jenkins-build-uri'] + echo "Downstream build ${downstreamBuildName}(${downstreamBuildUrl}) started." + } + } + } + } + } +} + diff --git a/openshift/pipelines/templates/waiverdb-polling-pagure.yaml b/openshift/pipelines/templates/waiverdb-polling-pagure.yaml index 06b6293..194abf3 100644 --- a/openshift/pipelines/templates/waiverdb-polling-pagure.yaml +++ b/openshift/pipelines/templates/waiverdb-polling-pagure.yaml @@ -58,6 +58,7 @@ parameters: displayName: Name of OpenShift cloud in Jenkins master configuration required: true value: openshift +{% include "snippets/c3i-library-parameters.yaml" %} objects: - kind: ServiceAccount apiVersion: v1 @@ -88,141 +89,5 @@ objects: strategy: type: JenkinsPipeline jenkinsPipelineStrategy: - jenkinsfile: |- - // Don't use external Jenkinsfile here, or Jenkins will also poll on that repo and branch - library identifier: 'c3i@master', changelog: false, - retriever: modernSCM([$class: 'GitSCMSource', remote: 'https://pagure.io/c3i-library.git']) - pipeline { - agent { - kubernetes { - cloud "${OPENSHIFT_CLOUD_NAME}" - label "jenkins-slave-${UUID.randomUUID().toString()}" - serviceAccount "${NAME}-jenkins-slave" - defaultContainer 'jnlp' - yaml """ - apiVersion: v1 - kind: Pod - metadata: - labels: - app: "jenkins-${env.JOB_BASE_NAME}" - factory2-pipeline-kind: "waiverdb-polling-to-pagure-pipeline" - factory2-pipeline-build-number: "${env.BUILD_NUMBER}" - spec: - containers: - - name: jnlp - image: "${JENKINS_AGENT_IMAGE}" - imagePullPolicy: Always - tty: true - resources: - requests: - memory: 378Mi - cpu: 200m - limits: - memory: 768Mi - cpu: 500m - """ - } - } - options { - timestamps() - } - environment { - PIPELINE_NAMESPACE = readFile('/run/secrets/kubernetes.io/serviceaccount/namespace').trim() - PAGURE_URL = "${PAGURE_URL}" - PAGURE_REPO_IS_FORK = "${PAGURE_REPO_IS_FORK}" - PAGURE_POLLING_FOR_PR = "${PAGURE_POLLING_FOR_PR}" - PAGURE_REPO_HOME = "${env.PAGURE_URL}${env.PAGURE_REPO_IS_FORK == 'true' ? '/fork' : ''}/${PAGURE_REPO_NAME}" - GIT_URL = "${env.PAGURE_URL}/${env.PAGURE_REPO_IS_FORK == 'true' ? 'forks/' : ''}${PAGURE_REPO_NAME}.git" - PREMERGE_JOB_NAME = "${PREMERGE_JOB_NAME}" - POSTMERGE_JOB_NAME = "${POSTMERGE_JOB_NAME}" - } - triggers { pollSCM("${PAGURE_POLLING_SCHEDULE}") } - stages { - stage('Prepare') { - agent { label 'master' } - steps { - script { - // checking out the polled branch - def polledBranch = env.PAGURE_POLLING_FOR_PR == 'true' ? 'origin/pull/*/head' : "origin/${PAGURE_POLLED_BRANCH}" - def scmVars = checkout([$class: 'GitSCM', - branches: [[name: polledBranch]], - userRemoteConfigs: [ - [ - name: 'origin', - url: env.GIT_URL, - refspec: '+refs/heads/*:refs/remotes/origin/* +refs/pull/*/head:refs/remotes/origin/pull/*/head', - ], - ], - extensions: [[$class: 'CleanBeforeCheckout']], - ]) - env.WAIVERDB_GIT_COMMIT = scmVars.GIT_COMMIT - // setting build display name - def prefix = 'origin/' - def branch = scmVars.GIT_BRANCH.startsWith(prefix) ? scmVars.GIT_BRANCH.substring(prefix.size()) - : scmVars.GIT_BRANCH // origin/pull/1234/head -> pull/1234/head, origin/master -> master - env.WAIVERDB_GIT_BRANCH = branch - echo "Build on branch=${env.WAIVERDB_GIT_BRANCH}, commit=${env.WAIVERDB_GIT_COMMIT}" - if (env.PAGURE_POLLING_FOR_PR == 'false') { - currentBuild.displayName = "${env.WAIVERDB_GIT_BRANCH}: ${env.WAIVERDB_GIT_COMMIT.substring(0, 7)}" - currentBuild.description = """${currentBuild.displayName}""" - } - else if (env.PAGURE_POLLING_FOR_PR == 'true' && branch ==~ /^pull\/[0-9]+\/head$/) { - env.PR_NO = branch.split('/')[1] - env.PR_URL = "${env.PAGURE_REPO_HOME}/pull-request/${env.PR_NO}" - // To HTML syntax in build description, go to `Jenkins/Global Security/Markup Formatter` and select 'Safe HTML'. - def pagureLink = """PR#${env.PR_NO}""" - echo "Building PR #${env.PR_NO}: ${env.PR_URL}" - currentBuild.displayName = "PR#${env.PR_NO}" - currentBuild.description = pagureLink - } else { // This shouldn't happen. - error("Build is aborted due to unexpected polling trigger actions.") - } - } - } - } - stage('Update pipeline jobs') { - when { - expression { - return "${PIPELINE_UPDATE_JOBS_DIR}" && env.PAGURE_POLLING_FOR_PR == 'false' && env.WAIVERDB_GIT_BRANCH == "${PAGURE_POLLED_BRANCH}" - } - } - steps { - checkout([$class: 'GitSCM', - branches: [[name: env.WAIVERDB_GIT_COMMIT]], - userRemoteConfigs: [ - [ - name: 'origin', - url: env.GIT_URL, - refspec: '+refs/heads/*:refs/remotes/origin/* +refs/pull/*/head:refs/remotes/origin/pull/*/head', - ], - ], - extensions: [[$class: 'CleanBeforeCheckout']], - ]) - script { - dir('openshift/pipelines') { - sh ''' - make install JOBS_DIR="${PIPELINE_UPDATE_JOBS_DIR}" - ''' - } - } - } - } - stage('Build') { - steps { - script { - openshift.withCluster() { - def bc = env.PAGURE_POLLING_FOR_PR == 'true' ? env.PREMERGE_JOB_NAME : env.POSTMERGE_JOB_NAME - def build = c3i.build(script: this, objs: "bc/${bc}", - '-e', "WAIVERDB_GIT_REF=${env.WAIVERDB_GIT_BRANCH}", - ) - c3i.waitForBuildStart(script: this, build: build) - def devBuildInfo = build.object() - def downstreamBuildName = devBuildInfo.metadata.name - def downstreamBuildUrl = devBuildInfo.metadata.annotations['openshift.io/jenkins-build-uri'] - echo "Downstream build ${downstreamBuildName}(${downstreamBuildUrl}) started." - } - } - } - } - } - } + jenkinsfile: | + {% filter indent(width=10) %}{% include "waiverdb-polling-pagure.Jenkinsfile" %}{% endfilter %} diff --git a/openshift/pipelines/templates/waiverdb-repotracker-trigger.Jenkinsfile b/openshift/pipelines/templates/waiverdb-repotracker-trigger.Jenkinsfile index 1dacd5f..e66e89e 100644 --- a/openshift/pipelines/templates/waiverdb-repotracker-trigger.Jenkinsfile +++ b/openshift/pipelines/templates/waiverdb-repotracker-trigger.Jenkinsfile @@ -1,7 +1,5 @@ // Use scripted syntax because CIBuildTrigger currently doesn't support the declarative syntax -library identifier: 'c3i@master', changelog: false, - retriever: modernSCM([$class: 'GitSCMSource', remote: 'https://pagure.io/c3i-library.git']) - +{% include "snippets/c3i-library.groovy" %} properties([ disableConcurrentBuilds(), pipelineTriggers([ diff --git a/openshift/pipelines/templates/waiverdb-repotracker-trigger.yaml b/openshift/pipelines/templates/waiverdb-repotracker-trigger.yaml index 0b0caa0..d7b231c 100644 --- a/openshift/pipelines/templates/waiverdb-repotracker-trigger.yaml +++ b/openshift/pipelines/templates/waiverdb-repotracker-trigger.yaml @@ -41,6 +41,7 @@ parameters: - name: TEST_JOB_NAME displayName: Name of integration test job to trigger required: true +{% include "snippets/c3i-library-parameters.yaml" %} objects: - kind: ServiceAccount apiVersion: v1 @@ -68,10 +69,6 @@ objects: spec: runPolicy: "Serial" completionDeadlineSeconds: 1800 - source: - git: - uri: "${WAIVERDB_GIT_REPO}" - ref: "${WAIVERDB_GIT_REF}" strategy: type: JenkinsPipeline jenkinsPipelineStrategy: @@ -97,4 +94,5 @@ objects: value: - name: MESSAGE_HEADERS value: - jenkinsfilePath: openshift/pipelines/templates/waiverdb-repotracker-trigger.Jenkinsfile + jenkinsfile: | + {% filter indent(width=10) %}{% include "waiverdb-repotracker-trigger.Jenkinsfile" %}{% endfilter %}