| |
@@ -1,348 +0,0 @@
|
| |
- 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 c3i_trigger_update_only %}
|
| |
- {% include "master-agent.groovy" %}
|
| |
- {% elif "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 %}
|
| |
- // pagure
|
| |
- {% if "pagure" in c3i_git_repo %}
|
| |
- 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 %}
|
| |
- ]
|
| |
- )
|
| |
- }
|
| |
- // gerrit trigger
|
| |
- {% elif "gerrit" in c3i_git_repo %}
|
| |
- triggers {
|
| |
- gerrit customUrl: '',
|
| |
- gerritProjects:
|
| |
- [[branches: [[compareType: 'PLAIN', pattern: params.GIT_MAIN_BRANCH ]],
|
| |
- compareType: 'ANT',
|
| |
- disableStrictForbiddenFileVerification: false,
|
| |
- pattern: env.GIT_REPO.split('/')[-1].replaceAll(/.git$/, '')]],
|
| |
- serverName: env.GERRIT_SERVER_NAME,
|
| |
- // there are variables in job-updater.yml and premerge.yml which change the value of trigger
|
| |
- triggerOnEvents: [{{ job_vars.gerrit_trigger }}]
|
| |
- }
|
| |
- {% endif %}
|
| |
- {% endif %}
|
| |
- {% if c3i_trigger_update_only %}
|
| |
- {% include "trigger-update-stages.groovy" %}
|
| |
- {% else %}
|
| |
- stages {
|
| |
- stage('Proceeding CI_MESSAGE from UMB') {
|
| |
- 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('Proceeding Gerrit trigger') {
|
| |
- when {
|
| |
- expression { env.GERRIT_REFSPEC }
|
| |
- }
|
| |
- steps {
|
| |
- script {
|
| |
- // figure out how look the message when it is merged
|
| |
- if (env.GERRIT_EVENT_TYPE == "patchset-created") {
|
| |
- env.GIT_REPO_REF = env.GERRIT_REFSPEC
|
| |
- env.PR_NO = env.GERRIT_CHANGE_NUMBER
|
| |
- }
|
| |
- }
|
| |
- }
|
| |
- }
|
| |
- stage('Clone git repo') {
|
| |
- 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
|
| |
- 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}"
|
| |
- }
|
| |
- }
|
| |
- }
|
| |
- // this is not needed for Gerrit
|
| |
- {% if "pagure" in c3i_git_repo %}
|
| |
- stage('Update Build Info') {
|
| |
- steps {
|
| |
- script {
|
| |
- // 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" %}
|
| |
- }
|
| |
- }
|
| |
- }
|
| |
- {% endif %}
|
| |
- // Update jenkins files through oc command
|
| |
- // run all jobs from templates/jobs directory
|
| |
- // job updater is on the same lever as postmerge/premerge
|
| |
- {% 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 - build, test and upload container') {
|
| |
- 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 %}
|
| |
- stage('Allocate C3IaaS project') {
|
| |
- steps {
|
| |
- script {
|
| |
- if (env.PR_NO) {
|
| |
- env.PIPELINE_ID = "c3i-{{ c3i_component }}-pr-${env.PR_NO}-git${env.GIT_COMMIT.take(8)}-${currentBuild.id}"
|
| |
- } else {
|
| |
- env.PIPELINE_ID = "c3i-{{ c3i_component }}-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=${params.C3IAAS_LIFETIME}"
|
| |
- )
|
| |
- }
|
| |
- }
|
| |
- namespaceLink = """<a href="{{ c3i_ocp_host }}/console/project/${env.PIPELINE_ID}">Openshift</a>"""
|
| |
- currentBuild.description = "${currentBuild.description}<br/>${namespaceLink}"
|
| |
- }
|
| |
- }
|
| |
- post {
|
| |
- success {
|
| |
- echo "Allocated project ${env.PIPELINE_ID}"
|
| |
- }
|
| |
- failure {
|
| |
- echo "Failed to allocate ${env.PIPELINE_ID} project"
|
| |
- }
|
| |
- }
|
| |
- }
|
| |
- // run component snippet to run and test the component
|
| |
- {{ 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}"
|
| |
- retry(5) {
|
| |
- sh "skopeo copy --src-cert-dir=/var/run/secrets/kubernetes.io/serviceaccount/ docker://${sourceImage} docker://${destImage}"
|
| |
- }
|
| |
- }
|
| |
- }
|
| |
- }
|
| |
- }
|
| |
- stage('Triggering Tower deployment') {
|
| |
- when {
|
| |
- // env.GIT_REPO_REF == params.GIT_MAIN_BRANCH it means that this is a postmerge
|
| |
- expression { env.GIT_REPO_REF == params.GIT_MAIN_BRANCH && params.TOWER_TEMPLATE_ID && params.TOWER_SECRET }
|
| |
- }
|
| |
- {% include "triggering-tower-steps.groovy" %}
|
| |
- }
|
| |
- }
|
| |
- post {
|
| |
- 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}"
|
| |
- emailext to: recipient, subject: subject, body: body
|
| |
- } catch (e) {
|
| |
- echo "Error sending email: ${e}"
|
| |
- }
|
| |
- }
|
| |
- }
|
| |
- }
|
| |
- }
|
| |
- {% endif %}
|
| |
- }
|
| |
- {% endif %}
|
| |
- }
|
| |