| |
@@ -0,0 +1,278 @@
|
| |
+ library identifier: "c3i@{{ c3i_lib_branch }}", changelog: false,
|
| |
+ retriever: modernSCM([$class: 'GitSCMSource', remote: "{{ c3i_lib_url }}"])
|
| |
+ import static org.apache.commons.lang.StringEscapeUtils.escapeHtml;
|
| |
+ pipeline {
|
| |
+ {% if "job-updater" in job_vars.name %}
|
| |
+ {% include c3i_default_agent_snippet %}
|
| |
+ {% else %}
|
| |
+ {% include c3i_build_agent_snippet %}
|
| |
+ {% endif %}
|
| |
+
|
| |
+ options {
|
| |
+ timestamps()
|
| |
+ timeout(time: 30, unit: 'MINUTES')
|
| |
+ }
|
| |
+ environment {
|
| |
+ SERVICE_ACCOUNT_TOKEN = readFile(file: '/run/secrets/kubernetes.io/serviceaccount/token').trim()
|
| |
+ // needed by c3i pagure client
|
| |
+ TRIGGER_NAMESPACE = readFile('/run/secrets/kubernetes.io/serviceaccount/namespace').trim()
|
| |
+ PAGURE_REPO_NAME = env.GIT_REPO.split('/')[3..-1].join('/').replace('forks/', '').replaceAll(/.git$/, '')
|
| |
+ PAGURE_URL = env.GIT_REPO.split('/')[0..2].join('/')
|
| |
+ PAGURE_API = "${env.PAGURE_URL}/api/0"
|
| |
+ PAGURE_REPO_IS_FORK = "${env.GIT_REPO.contains('/forks/') ? 'true': 'false'}"
|
| |
+ }
|
| |
+ {% if "postmerge" not in job_vars.name %}
|
| |
+ triggers {
|
| |
+ ciBuildTrigger(
|
| |
+ noSquash: true,
|
| |
+ providerList: [
|
| |
+ {% if "premerge" in job_vars.name %}
|
| |
+ {% for topic in job_vars.messaging_fedmsg_topics %}
|
| |
+ fedmsgSubscriber(
|
| |
+ name: params.MESSAGING_FEDMSG_PROVIDER,
|
| |
+ overrides: [topic: "{{ topic }}"],
|
| |
+ checks: [
|
| |
+ [field: '$.pullrequest.project.url_path', expectedValue: params.GIT_REPO.split('/')[3..-1].join('/').replace('forks/', 'fork/').replaceAll(/.git$/, '')],
|
| |
+ [field: '$.pullrequest.branch', expectedValue: params.GIT_MAIN_BRANCH],
|
| |
+ ]
|
| |
+ ),
|
| |
+ {% endfor %}
|
| |
+ {% elif "job-updater" in job_vars.name %}
|
| |
+ {% for topic in job_vars.messaging_fedmsg_topics %}
|
| |
+ fedmsgSubscriber(
|
| |
+ name: params.MESSAGING_FEDMSG_PROVIDER,
|
| |
+ overrides: [topic: "{{ topic }}"],
|
| |
+ checks: [
|
| |
+ [field: '$.repo.url_path', expectedValue: params.GIT_REPO.split('/')[3..-1].join('/').replace('forks/', 'fork/').replaceAll(/.git$/, '')],
|
| |
+ [field: '$.branch', expectedValue: params.GIT_MAIN_BRANCH],
|
| |
+ ]
|
| |
+ )
|
| |
+ {% endfor %}
|
| |
+ {% endif %}
|
| |
+ ]
|
| |
+ )
|
| |
+ }
|
| |
+ {% endif %}
|
| |
+ stages {
|
| |
+ stage('Proceeding CI_MESSAGE') {
|
| |
+ when {
|
| |
+ expression { env.CI_MESSAGE }
|
| |
+ }
|
| |
+ steps {
|
| |
+ script {
|
| |
+ def message = readJSON text: params.CI_MESSAGE
|
| |
+ if (env.GIT_REPO_REF != params.GIT_MAIN_BRANCH) {
|
| |
+ env.GIT_REPO_REF = "pull/${message.pullrequest.id}/head"
|
| |
+ }
|
| |
+ }
|
| |
+ }
|
| |
+ }
|
| |
+ stage('Update Build Info') {
|
| |
+ steps {
|
| |
+ script {
|
| |
+ if (!env.GIT_REPO_REF) {
|
| |
+ error("This build is not started by a CI message and GIT_REPO_REF is empty. Only configurations were done.")
|
| |
+ }
|
| |
+
|
| |
+ // FIXME: Due to a bug described in https://issues.jenkins-ci.org/browse/JENKINS-45489
|
| |
+ checkout([$class: 'GitSCM',
|
| |
+ branches: [[name: env.GIT_REPO_REF]],
|
| |
+ userRemoteConfigs: [[url: params.GIT_REPO, refspec: '+refs/heads/*:refs/remotes/origin/* +refs/pull/*/head:refs/remotes/origin/pull/*/head']],
|
| |
+ ])
|
| |
+ env.GIT_COMMIT = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
|
| |
+ echo "Build ${env.GIT_REPO_REF}, commit=${env.GIT_COMMIT}"
|
| |
+
|
| |
+ // Set friendly display name and description
|
| |
+ def pagure_repo_home = env.GIT_REPO.replace('/forks/', '/fork/').replaceAll(/.git$/,'')
|
| |
+ {% if "premerge" in job_vars.name %}
|
| |
+ // Is the current branch a pull-request? If no, env.PR_NO will be empty.
|
| |
+ env.PR_NO = env.GIT_REPO_REF.split('/')[1] // return X from pull/X/head
|
| |
+ env.PR_URL = "${pagure_repo_home}/pull-request/${env.PR_NO}"
|
| |
+ echo "Building PR #${env.PR_NO}: ${env.PR_URL}"
|
| |
+ // NOTE: Old versions of OpenShift Client Jenkins plugin are buggy to handle arguments
|
| |
+ // with special bash characters (like whitespaces, #, etc).
|
| |
+ // https://bugzilla.redhat.com/show_bug.cgi?id=1625518
|
| |
+ currentBuild.displayName = "PR#${env.PR_NO}"
|
| |
+ // To enable HTML syntax in build description, go to `Jenkins/Global Security/Markup Formatter` and select 'Safe HTML'.
|
| |
+ def pagureLink = """<a href="${env.PR_URL}">${currentBuild.displayName}</a>"""
|
| |
+ try {
|
| |
+ def prInfo = pagure.getPR(env.PR_NO)
|
| |
+ pagureLink = """<a href="${env.PR_URL}">PR#${env.PR_NO}: ${escapeHtml(prInfo.title)}</a>"""
|
| |
+ // set PR status to Pending
|
| |
+ if (params.PAGURE_API_KEY_SECRET_NAME)
|
| |
+ pagure.setBuildStatusOnPR(null, 'Building...')
|
| |
+ } catch (Exception e) {
|
| |
+ echo "Error using pagure API: ${e}"
|
| |
+ }
|
| |
+ currentBuild.description = pagureLink
|
| |
+ {% else %}
|
| |
+ currentBuild.displayName = "${env.GIT_REPO_REF}: ${env.GIT_COMMIT.substring(0, 7)}"
|
| |
+ currentBuild.description = """<a href="${pagure_repo_home}/c/${env.GIT_COMMIT}">${currentBuild.displayName}</a>"""
|
| |
+ if (params.PAGURE_API_KEY_SECRET_NAME) {
|
| |
+ try {
|
| |
+ pagure.flagCommit('pending', null, 'Building...')
|
| |
+ echo "Updated commit ${env.GIT_COMMIT} status to PENDING."
|
| |
+ } catch (e) {
|
| |
+ echo "Error updating commit ${env.GIT_COMMIT} status to PENDING: ${e}"
|
| |
+ }
|
| |
+ }
|
| |
+ {% endif %}
|
| |
+ {% include "get_paas_domain.groovy" %}
|
| |
+ }
|
| |
+ }
|
| |
+ }
|
| |
+ {% if "job-updater" in job_vars.name %}
|
| |
+ stage('Update pipeline jobs') {
|
| |
+ steps {
|
| |
+ script {
|
| |
+ dir("{{ c3i_definition_dir }}") {
|
| |
+ sh '{{ c3i_definition_update_script }}'
|
| |
+ }
|
| |
+ }
|
| |
+ }
|
| |
+ }
|
| |
+ stage('Trigger postmerge') {
|
| |
+ steps {
|
| |
+ script {
|
| |
+ openshift.withCluster() {
|
| |
+ def build = c3i.build(script: this, objs: "bc/${env.POSTMERGE_JOB}")
|
| |
+ 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."
|
| |
+ }
|
| |
+ }
|
| |
+ }
|
| |
+ }
|
| |
+ {% else %}
|
| |
+ {{ task_var_build_and_test }}
|
| |
+ stage('Push container') {
|
| |
+ when {
|
| |
+ expression {
|
| |
+ return env.GIT_REPO_REF == params.GIT_MAIN_BRANCH
|
| |
+ }
|
| |
+ }
|
| |
+ steps {
|
| |
+ script {
|
| |
+ dir ("${env.HOME}/.docker") {
|
| |
+ // for the OpenShift internal registry
|
| |
+ def dockerConfig = readJSON text: '{ "auths": {} }'
|
| |
+ dockerConfig.auths['docker-registry.default.svc:5000'] = [
|
| |
+ 'email': '',
|
| |
+ 'auth': sh(returnStdout: true, script: 'set +x; echo -n "serviceaccount:$SERVICE_ACCOUNT_TOKEN" | base64 -').trim()
|
| |
+ ]
|
| |
+ // merging user specified credentials
|
| |
+ 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
|
| |
+ }
|
| |
+ env.RESULTING_IMAGE_REPOS.tokenize(',').each {
|
| |
+ def sourceImage = it + ":" + env.RESULTING_TAG
|
| |
+ def imageName = it.split('/').last()
|
| |
+ def destImage = "${env.IMAGE_DESTINATION_NAMESPACE}/${imageName}:${env.IMAGE_TAG}"
|
| |
+ // copy between registies
|
| |
+ echo "Copying container from ${sourceImage} to ${destImage}"
|
| |
+ sh "skopeo copy --src-cert-dir=/var/run/secrets/kubernetes.io/serviceaccount/ docker://${sourceImage} docker://${destImage}"
|
| |
+ }
|
| |
+ }
|
| |
+ }
|
| |
+ }
|
| |
+ }
|
| |
+ post {
|
| |
+ cleanup {
|
| |
+ script {
|
| |
+ if (env.RESULTING_TAG) {
|
| |
+ echo "Removing tag ${env.RESULTING_TAG} from the ImageStream..."
|
| |
+ openshift.withCluster() {
|
| |
+ openshift.withProject("${params.IMAGESTREAM_NAMESPACE}") {
|
| |
+ openshift.tag("${params.IMAGESTREAM_NAME}:${env.RESULTING_TAG}",
|
| |
+ "-d")
|
| |
+ }
|
| |
+ }
|
| |
+ }
|
| |
+ }
|
| |
+ }
|
| |
+ success {
|
| |
+ script {
|
| |
+ // on pre-merge workflow success
|
| |
+ if (params.PAGURE_API_KEY_SECRET_NAME && env.PR_NO) {
|
| |
+ try {
|
| |
+ pagure.setBuildStatusOnPR(100, 'Build passed.')
|
| |
+ echo "Updated PR #${env.PR_NO} status to PASS."
|
| |
+ } catch (e) {
|
| |
+ echo "Error updating PR #${env.PR_NO} status to PASS: ${e}"
|
| |
+ }
|
| |
+ }
|
| |
+ // on post-merge workflow success
|
| |
+ if (params.PAGURE_API_KEY_SECRET_NAME && !env.PR_NO) {
|
| |
+ try {
|
| |
+ pagure.flagCommit('success', 100, 'Build passed.')
|
| |
+ echo "Updated commit ${env.GIT_COMMIT} status to PASS."
|
| |
+ } catch (e) {
|
| |
+ echo "Error updating commit ${env.GIT_COMMIT} status to PASS: ${e}"
|
| |
+ }
|
| |
+ }
|
| |
+ }
|
| |
+ }
|
| |
+ failure {
|
| |
+ script {
|
| |
+ // on pre-merge workflow failure
|
| |
+ if (params.PAGURE_API_KEY_SECRET_NAME && env.PR_NO) {
|
| |
+ // updating Pagure PR flag
|
| |
+ try {
|
| |
+ pagure.setBuildStatusOnPR(0, 'Build failed.')
|
| |
+ echo "Updated PR #${env.PR_NO} status to FAILURE."
|
| |
+ } catch (e) {
|
| |
+ echo "Error updating PR #${env.PR_NO} status to FAILURE: ${e}"
|
| |
+ }
|
| |
+ // making a comment
|
| |
+ try {
|
| |
+ pagure.commentOnPR("""
|
| |
+ Build ${env.GIT_COMMIT} [FAILED](${env.BUILD_URL})!
|
| |
+ Rebase or make new commits to rebuild.
|
| |
+ """.stripIndent(), env.PR_NO)
|
| |
+ echo "Comment made."
|
| |
+ } catch (e) {
|
| |
+ echo "Error making a comment on PR #${env.PR_NO}: ${e}"
|
| |
+ }
|
| |
+ }
|
| |
+ // on post-merge workflow failure
|
| |
+ if (!env.PR_NO) {
|
| |
+ // updating Pagure commit flag
|
| |
+ if (params.PAGURE_API_KEY_SECRET_NAME) {
|
| |
+ try {
|
| |
+ pagure.flagCommit('failure', 0, 'Build failed.')
|
| |
+ echo "Updated commit ${env.GIT_COMMIT} status to FAILURE."
|
| |
+ } catch (e) {
|
| |
+ echo "Error updating commit ${env.GIT_COMMIT} status to FAILURE: ${e}"
|
| |
+ }
|
| |
+ }
|
| |
+ // sending email
|
| |
+ if (params.MAIL_ADDRESS){
|
| |
+ try {
|
| |
+ def recipient = params.MAIL_ADDRESS
|
| |
+ def subject = "Jenkins job ${env.JOB_NAME} #${env.BUILD_NUMBER} failed."
|
| |
+ def body = "Build URL: ${env.BUILD_URL}"
|
| |
+ if (env.PR_NO) {
|
| |
+ subject = "Jenkins job ${env.JOB_NAME}, PR #${env.PR_NO} ${status}."
|
| |
+ body += "\nPull Request: ${env.PR_URL}"
|
| |
+ }
|
| |
+ emailext to: recipient, subject: subject, body: body
|
| |
+ } catch (e) {
|
| |
+ echo "Error sending email: ${e}"
|
| |
+ }
|
| |
+ }
|
| |
+ }
|
| |
+ }
|
| |
+ }
|
| |
+ {% endif %}
|
| |
+ }
|
| |
+ }
|
| |
Providing unified workflows for components in pagure. Jobs are triggered
by messages from UMB or fedmsg.