Permalink
Cannot retrieve contributors at this time
Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign up
Fetching contributors…
| apiVersion: v1 | |
| kind: Template | |
| metadata: | |
| creationTimestamp: null | |
| name: ${APP_NAME}-base-image | |
| objects: | |
| - apiVersion: v1 | |
| kind: BuildConfig | |
| metadata: | |
| name: "${APP_NAME}-base-image" | |
| spec: | |
| strategy: | |
| type: | |
| jenkinsPipelineStrategy: | |
| jenkinsfile: |- | |
| pipeline { | |
| agent any | |
| environment { | |
| currentBaseImg = 'null' | |
| latestBaseImg = 'null' | |
| rebuild = 'null' | |
| baseIsTag = "${BASE_IMAGE_TAG}" | |
| baseImgNamespace = "${BASE_IMAGE_NAMESPACE}" | |
| userInput = 'null' | |
| appName = "${APP_NAME}" | |
| appBcName = "${APP_BC}" | |
| appNamespace = "${APP_NAMESPACE}" | |
| } | |
| stages { | |
| stage('Check base image version') { | |
| steps { | |
| script { | |
| openshift.withCluster() { | |
| def isTag | |
| openshift.withProject("${baseImgNamespace}") { | |
| isTag = openshift.selector( "istag/${baseIsTag}" ) | |
| isTag = isTag.object() | |
| } | |
| openshift.withProject("${appNamespace}") { | |
| if (openshift.selector( "bc/${appBcName}" ).exists() ) { | |
| def appBc = openshift.selector( "bc/${appBcName}" ) | |
| appBc = appBc.object() | |
| latestBaseImg = "${isTag.image.dockerImageReference}" | |
| def buildNumber = "${appBc.status.lastVersion}" | |
| def appBuild = openshift.selector( "build/${appBcName}-${buildNumber}" ) | |
| appBuild = appBuild.object() | |
| currentBaseImg = appBuild.spec.strategy.sourceStrategy.from.name | |
| } | |
| else { | |
| echo "The OpenShift BuildConfig ${appBcName} does not exist" | |
| return | |
| } | |
| } | |
| } | |
| } | |
| script { | |
| if ( latestBaseImg == currentBaseImg ){ | |
| echo "Current base image and latest base image are the same, ${currentBaseImg}." | |
| } | |
| else{ | |
| echo "Consider rebuilding using 'latest' base image.\n\nCurrent deployed base image is ${currentBaseImg}\nLatest available base image is ${latestBaseImg}" | |
| rebuild = 'true' | |
| } | |
| } | |
| } | |
| } | |
| stage('Upgrade prompt') { | |
| steps { | |
| script { | |
| def goAhead | |
| if (rebuild == 'true') { | |
| /* | |
| * Check if the developer wants an updated build using the new | |
| * base image. The 'input' function will wait for an interactive | |
| * answer for 1 minute. If the UPGRADE_WIHTOUT_ASKING parameter | |
| * is set to any value, the build will be triggered automatically | |
| * without the interactive check. | |
| */ | |
| if (params.containsKey('UPGRADE_WITHOUT_ASKING')) { | |
| goAhead = true | |
| } else { | |
| timeout(1) { | |
| goAhead = input( | |
| id: 'userInput', message: 'Upgrade base image?') | |
| } | |
| } | |
| } | |
| if (rebuild == 'true' && goAhead != 'null') { | |
| echo "Starting a build of ${appBcName}" | |
| build job: "${appBcName}", wait: false | |
| } | |
| } | |
| } | |
| } | |
| } | |
| post { | |
| aborted { | |
| script { | |
| if (rebuild == 'true') { | |
| mail to: "${NOTIFY_EMAIL_LIST}", | |
| from: "${NOTIFY_EMAIL_FROM}", replyTo: "${NOTIFY_EMAIL_REPLYTO}", | |
| subject: "New ${appName} base image ${baseIsTag} available for approval", | |
| body: "A new ${appName} base image is available. Visit ${JOB_DISPLAY_URL} to re-run this job ('Build Now') and approve a new build.\n\nCurrent base image: ${currentBaseImg}\nLatest base image: ${latestBaseImg}" | |
| } | |
| } | |
| } | |
| } | |
| } | |
| triggers: | |
| - imageChange: | |
| from: | |
| kind: ImageStreamTag | |
| name: ${BASE_IMAGE_TAG} | |
| namespace: ${BASE_IMAGE_NAMESPACE} | |
| type: ImageChange | |
| parameters: | |
| - description: The base image tag | |
| displayName: Base image tag, e.g. 'jenkins:latest' | |
| name: BASE_IMAGE_TAG | |
| required: true | |
| value: "jenkins:latest" | |
| - description: The base image project namespace | |
| displayName: Base image project namespace, e.g. 'openshift' | |
| name: BASE_IMAGE_NAMESPACE | |
| required: true | |
| value: "openshift" | |
| - description: The application buildconfig name | |
| displayName: The application buildconfig name, e.g. 'jenkins-custom' | |
| name: APP_BC | |
| required: true | |
| value: "jenkins-custom" | |
| - description: Application name | |
| displayName: Application Name, e.g. 'jenkins' | |
| name: APP_NAME | |
| required: true | |
| value: "jenkins" | |
| - description: Application project namespace | |
| displayName: Application project namespace, e.g. 'lifecycle' | |
| name: APP_NAMESPACE | |
| required: true | |
| value: "lifecycle" | |
| - description: Comma-separated email notification recipient list | |
| name: NOTIFY_EMAIL_LIST | |
| required: true | |
| value: "noreply@example.com" | |
| - description: Email notification From header | |
| name: NOTIFY_EMAIL_FROM | |
| required: true | |
| value: "noreply@example.com" | |
| - description: Email notification Reply-To header | |
| name: NOTIFY_EMAIL_REPLYTO | |
| required: true | |
| value: "noreply@example.com" |