|
| 1 | +#!/usr/bin/env groovy |
| 2 | + |
| 3 | +// Pipeline variables |
| 4 | +def isPR=false // true if the branch being tested belongs to a PR |
| 5 | +def project="" // project where build and deploy will occur |
| 6 | +def projectCreated=false // true if a project was created by this build and needs to be cleaned up |
| 7 | +def repoUrl="" // the URL of this project's repository |
| 8 | +def appName="openshift-docs" // name of application to create |
| 9 | +def approved=false // true if the preview was approved |
| 10 | + |
| 11 | +// uniqueName returns a name with a 16-character random character suffix |
| 12 | +def uniqueName = { String prefix -> |
| 13 | + sh "cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 16 | head -n 1 > suffix" |
| 14 | + suffix = readFile("suffix").trim() |
| 15 | + return prefix + suffix |
| 16 | +} |
| 17 | + |
| 18 | +// setBuildStatus sets a status item on a GitHub commit |
| 19 | +def setBuildStatus = { String url, String context, String message, String state, String backref -> |
| 20 | + step([ |
| 21 | + $class: "GitHubCommitStatusSetter", |
| 22 | + reposSource: [$class: "ManuallyEnteredRepositorySource", url: url ], |
| 23 | + contextSource: [$class: "ManuallyEnteredCommitContextSource", context: context ], |
| 24 | + errorHandlers: [[$class: "ChangingBuildStatusErrorHandler", result: "UNSTABLE"]], |
| 25 | + statusBackrefSource: [ $class: "ManuallyEnteredBackrefSource", backref: backref ], |
| 26 | + statusResultSource: [ $class: "ConditionalStatusResultSource", results: [ |
| 27 | + [$class: "AnyBuildResult", message: message, state: state]] ] |
| 28 | + ]); |
| 29 | +} |
| 30 | + |
| 31 | +// getRepoURL retrieves the origin URL of the current source repository |
| 32 | +def getRepoURL = { |
| 33 | + sh "git config --get remote.origin.url > originurl" |
| 34 | + return readFile("originurl").trim() |
| 35 | +} |
| 36 | + |
| 37 | +// getRouteHostname retrieves the host name from the given route in an |
| 38 | +// OpenShift namespace |
| 39 | +def getRouteHostname = { String routeName, String projectName -> |
| 40 | + sh "oc get route ${routeName} -n ${projectName} -o jsonpath='{ .spec.host }' > apphost" |
| 41 | + return readFile("apphost").trim() |
| 42 | +} |
| 43 | + |
| 44 | +// setPreviewStatus sets a status item for each openshift-docs release |
| 45 | +def setPreviewStatus = { String url, String message, String state, String host, boolean includeLink -> |
| 46 | + setBuildStatus(url, "ci/app-preview/origin", message, state, includeLink ? "http://${host}/openshift-origin/latest/welcome/index.html" : "") |
| 47 | + setBuildStatus(url, "ci/app-preview/enterprise", message, state, includeLink ? "http://${host}/openshift-enterprise/master/welcome/index.html" : "") |
| 48 | + setBuildStatus(url, "ci/app-preview/online", message, state, includeLink ? "http://${host}/openshift-online/master/welcome/index.html" : "") |
| 49 | + setBuildStatus(url, "ci/app-preview/dedicated", message, state, includeLink ? "http://${host}/openshift-dedicated/master/welcome/index.html" : "") |
| 50 | + setBuildStatus(url, "ci/app-preview/registry", message, state, includeLink ? "http://${host}/atomic-registry/latest/registry_quickstart/index.html" : "") |
| 51 | +} |
| 52 | + |
| 53 | +try { // Use a try block to perform cleanup in a finally block when the build fails |
| 54 | + |
| 55 | + node { |
| 56 | + // Initialize variables in default node context |
| 57 | + isPR = env.BRANCH_NAME ? env.BRANCH_NAME.startsWith("PR") : false |
| 58 | + baseProject = env.PROJECT_NAME |
| 59 | + project = env.PROJECT_NAME |
| 60 | + |
| 61 | + stage ('Checkout') { |
| 62 | + checkout scm |
| 63 | + repoUrl = getRepoURL() |
| 64 | + } |
| 65 | + |
| 66 | + // When testing a PR, create a new project to perform the build |
| 67 | + // and deploy artifacts. |
| 68 | + if (isPR) { |
| 69 | + stage ('Create PR Project') { |
| 70 | + setPreviewStatus(repoUrl, "Building application", "PENDING", "", false) |
| 71 | + setBuildStatus(repoUrl, "ci/approve", "Aprove after testing", "PENDING", "") |
| 72 | + project = uniqueName("${appName}-") |
| 73 | + sh "oc new-project ${project}" |
| 74 | + projectCreated=true |
| 75 | + sh "oc policy add-role-to-group view system:authenticated -n ${project}" |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + stage ('Apply object configurations') { |
| 80 | + sh "oc process -f _openshift/docs-template.yaml -n ${project} | oc apply -f - -n ${project}" |
| 81 | + } |
| 82 | + |
| 83 | + stage ('Build') { |
| 84 | + sh "oc start-build ${appName} -n ${project} --from-repo=. --follow" |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + if (isPR) { |
| 89 | + stage ('Verify Service') { |
| 90 | + openshiftVerifyService serviceName: appName, namespace: project |
| 91 | + } |
| 92 | + def appHostName = getRouteHostname(appName, project) |
| 93 | + setPreviewStatus(repoUrl, "The application is available", "SUCCESS", "${appHostName}", true) |
| 94 | + setBuildStatus(repoUrl, "ci/approve", "Approve after testing", "PENDING", "${env.BUILD_URL}input/") |
| 95 | + stage ('Manual Test') { |
| 96 | + timeout(time:2, unit:'DAYS') { |
| 97 | + input "Is everything OK?" |
| 98 | + } |
| 99 | + } |
| 100 | + approved = true |
| 101 | + setPreviewStatus(repoUrl, "Application previewed", "SUCCESS", "", false) |
| 102 | + setBuildStatus(repoUrl, "ci/approve", "Manually approved", "SUCCESS", "") |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | +finally { |
| 107 | + if (projectCreated) { |
| 108 | + node { |
| 109 | + stage('Delete PR Project') { |
| 110 | + if (!approved) { |
| 111 | + setPreviewStatus(repoUrl, "Application previewed", "FAILURE", "", false) |
| 112 | + setBuildStatus(repoUrl, "ci/approve", "Rejected", "FAILURE", "") |
| 113 | + } |
| 114 | + sh "oc delete project ${project}" |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments