| |
@@ -0,0 +1,252 @@
|
| |
+ import static org.apache.commons.lang.StringEscapeUtils.escapeHtml;
|
| |
+
|
| |
+ library identifier: "c3i@master", changelog: false,
|
| |
+ retriever: modernSCM([$class: 'GitSCMSource', remote: "https://pagure.io/c3i-library.git"])
|
| |
+ pipeline {
|
| |
+
|
| |
+ {% include 'default-agent.groovy' %}
|
| |
+
|
| |
+ 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'}"
|
| |
+ }
|
| |
+ triggers {
|
| |
+ ciBuildTrigger(
|
| |
+ noSquash: true,
|
| |
+ providerList: [
|
| |
+ fedmsgSubscriber(
|
| |
+ name: params.MESSAGING_FEDMSG_PROVIDER,
|
| |
+ overrides: [topic: "io.pagure.prod.pagure.pull-request.new"],
|
| |
+ 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],
|
| |
+ ]
|
| |
+ ),
|
| |
+ fedmsgSubscriber(
|
| |
+ name: params.MESSAGING_FEDMSG_PROVIDER,
|
| |
+ overrides: [topic: "io.pagure.prod.pagure.pull-request.update"],
|
| |
+ 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],
|
| |
+ ]
|
| |
+ ),
|
| |
+ fedmsgSubscriber(
|
| |
+ name: params.MESSAGING_FEDMSG_PROVIDER,
|
| |
+ overrides: [topic: "io.pagure.prod.pagure.pull-request.reopened"],
|
| |
+ 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],
|
| |
+ ]
|
| |
+ ),
|
| |
+ fedmsgSubscriber(
|
| |
+ name: params.MESSAGING_FEDMSG_PROVIDER,
|
| |
+ overrides: [topic: "io.pagure.prod.pagure.pull-request.rebased"],
|
| |
+ 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],
|
| |
+ ]
|
| |
+ ),
|
| |
+ ]
|
| |
+ )
|
| |
+ }
|
| |
+ stages {
|
| |
+ stage('Proceeding CI_MESSAGE') {
|
| |
+ when {
|
| |
+ expression { env.CI_MESSAGE }
|
| |
+ }
|
| |
+ steps {
|
| |
+ script {
|
| |
+ def message = readJSON text: params.CI_MESSAGE
|
| |
+ 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.")
|
| |
+ }
|
| |
+
|
| |
+ c3i.clone(repo: params.GIT_REPO,
|
| |
+ branch: env.GIT_REPO_REF)
|
| |
+ 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$/,'')
|
| |
+ // 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
|
| |
+ def prInfo = pagure.getPR(env.PR_NO)
|
| |
+ env.PAGURE_REQUEST_BRANCH = prInfo.branch_from
|
| |
+ env.PAGURE_REQUEST_REPO = "${env.PAGURE_URL}/${prInfo.repo_from.fullname}"
|
| |
+ }
|
| |
+ }
|
| |
+ }
|
| |
+ stage('Allocate C3IaaS project') {
|
| |
+ steps {
|
| |
+ script {
|
| |
+ env.PIPELINE_ID = "c3i-verification-pr-${env.PR_NO}-git${env.GIT_COMMIT.take(8)}-${currentBuild.id}"
|
| |
+ echo "Requesting new OpenShift project ${env.PIPELINE_ID}..."
|
| |
+ openshift.withCluster() {
|
| |
+ openshift.withProject(params.C3IAAS_NAMESPACE) {
|
| |
+ c3i.buildAndWait(script: this, objs: "bc/${params.C3IAAS_JOB_NAME}",
|
| |
+ '-e', "PROJECT_NAME=${env.PIPELINE_ID}",
|
| |
+ '-e', "ADMIN_GROUPS=system:serviceaccounts:${env.TRIGGER_NAMESPACE},system:serviceaccounts:${env.C3IAAS_NAMESPACE}",
|
| |
+ '-e', "LIFETIME_IN_MINUTES=60"
|
| |
+ )
|
| |
+ }
|
| |
+ }
|
| |
+ }
|
| |
+ }
|
| |
+ post {
|
| |
+ success {
|
| |
+ echo "Allocated project ${env.PIPELINE_ID}"
|
| |
+ }
|
| |
+ failure {
|
| |
+ echo "Failed to allocate ${env.PIPELINE_ID} project"
|
| |
+ }
|
| |
+ }
|
| |
+ }
|
| |
+ stage('Copy Secrets') {
|
| |
+ steps {
|
| |
+ script {
|
| |
+ openshift.withCluster() {
|
| |
+ factorySecret = openshift.selector('secret', 'factory2-c3i-verification-secret').object(exportable:true)
|
| |
+ openshift.withProject(env.PIPELINE_ID) {
|
| |
+ openshift.create(factorySecret)
|
| |
+ }
|
| |
+ }
|
| |
+ }
|
| |
+ }
|
| |
+ }
|
| |
+ stage('Install Jenkins master') {
|
| |
+ steps {
|
| |
+ script {
|
| |
+ sh "oc project ${env.PIPELINE_ID}"
|
| |
+ dir('setup') {
|
| |
+ sh "./install-jenkins --jcasc ${env.PAGURE_REQUEST_REPO.replace('/forks/', '/fork/')}/raw/${env.PAGURE_REQUEST_BRANCH}/f/setup/jcasc.yml"
|
| |
+ }
|
| |
+ openshift.withCluster() {
|
| |
+ openshift.withProject(env.PIPELINE_ID) {
|
| |
+ c3i.waitForPods(script: this, objs: [name: "jenkins"], num: 1)
|
| |
+ }
|
| |
+ }
|
| |
+ }
|
| |
+ }
|
| |
+ }
|
| |
+ stage('Setup test project') {
|
| |
+ steps {
|
| |
+ script {
|
| |
+ dir('verification/test-project') {
|
| |
+ sh 'ln -s ../../roles roles'
|
| |
+ sh "ansible-playbook deploy.yml -e c3i_ocp_namespace=${env.PIPELINE_ID} -e c3i_lib_branch=${env.PAGURE_REQUEST_BRANCH} -e c3i_lib_url=${env.PAGURE_REQUEST_REPO} -e c3i_git_repo=${env.PAGURE_REQUEST_REPO} -e c3i_git_main_branch=${env.PAGURE_REQUEST_BRANCH}"
|
| |
+ }
|
| |
+ }
|
| |
+ }
|
| |
+ }
|
| |
+ stage('postmerge') {
|
| |
+ steps {
|
| |
+ script {
|
| |
+ openshift.withCluster() {
|
| |
+ openshift.withProject(env.PIPELINE_ID) {
|
| |
+ c3i.buildAndWait(script: this, objs: "bc/c3i-library-test-postmerge",
|
| |
+ '-e', "IMAGE=registry.access.redhat.com/rhscl/postgresql-95-rhel7:latest",
|
| |
+ '-e', "IMAGE_TAG=latest-PR${env.PR_NO}-${env.BUILD_NUMBER}",
|
| |
+ '-e', "PAGURE_API_KEY_SECRET_NAME=", // disable for pagure communication
|
| |
+ '-e', "GIT_REPO_REF=${env.PAGURE_REQUEST_BRANCH}",
|
| |
+ '-e', "LIFETIME_IN_MINUTES=30"
|
| |
+ )
|
| |
+ }
|
| |
+ }
|
| |
+ }
|
| |
+ }
|
| |
+ }
|
| |
+ stage('Parallel trigger') {
|
| |
+ steps {
|
| |
+ script {
|
| |
+ openshift.withCluster() {
|
| |
+ openshift.withProject(env.PIPELINE_ID) {
|
| |
+ parallel(
|
| |
+ "trigger-on-latest-tag": {
|
| |
+ c3i.buildAndWait(script: this, objs: "bc/c3i-library-test-trigger-on-latest-tag",
|
| |
+ '-e', "IMAGE=quay.io/factory2/c3i-library-test:latest-PR${env.PR_NO}-${env.BUILD_NUMBER}",
|
| |
+ '-e', "TARGET_TAG=stage-PR${env.PR_NO}-${env.BUILD_NUMBER}"
|
| |
+ )
|
| |
+ },
|
| |
+ "greenwave-promote-to-stage": {
|
| |
+ c3i.buildAndWait(script: this, objs: "bc/c3i-library-test-greenwave-promote-to-stage",
|
| |
+ '-e', "IMAGE=quay.io/factory2/c3i-library-test:latest-PR${env.PR_NO}-${env.BUILD_NUMBER}",
|
| |
+ '-e', "TARGET_TAG=stage-PR${env.PR_NO}-${env.BUILD_NUMBER}"
|
| |
+ )
|
| |
+ }
|
| |
+ )
|
| |
+ }
|
| |
+ }
|
| |
+ }
|
| |
+ }
|
| |
+ }
|
| |
+ }
|
| |
+ post {
|
| |
+ success {
|
| |
+ script {
|
| |
+ if (env.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}"
|
| |
+ }
|
| |
+ }
|
| |
+ }
|
| |
+ }
|
| |
+ failure {
|
| |
+ script {
|
| |
+ if (params.PAGURE_API_KEY_SECRET_NAME && env.PR_NO) {
|
| |
+ 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}"
|
| |
+ }
|
| |
+ 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}"
|
| |
+ }
|
| |
+ }
|
| |
+ }
|
| |
+ }
|
| |
+ }
|
| |
+ }
|
| |
I am not sure you need this anymore