-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KOGITO-9676 CI: Promote call setup-branch job
- Loading branch information
Showing
10 changed files
with
695 additions
and
439 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,327 @@ | ||
import org.jenkinsci.plugins.workflow.libs.Library | ||
|
||
@Library('jenkins-pipeline-shared-libraries')_ | ||
|
||
// Deploy jobs | ||
IMAGES_DEPLOY = 'kogito-images-deploy' | ||
EXAMPLES_IMAGES_DEPLOY = 'kogito-examples-images-deploy' | ||
OPERATOR_DEPLOY = 'kogito-operator-deploy' | ||
SEVERLESS_OPERATOR_DEPLOY = 'kogito-serverless-operator-deploy' | ||
|
||
// Map of executed jobs | ||
// See https://javadoc.jenkins.io/plugin/workflow-support/org/jenkinsci/plugins/workflow/support/steps/build/RunWrapper.html | ||
// for more options on built job entity | ||
JOBS = [:] | ||
|
||
FAILED_STAGES = [:] | ||
UNSTABLE_STAGES = [:] | ||
|
||
defaultImageParamsPrefix = 'IMAGE' | ||
kogitoImagesParamsPrefix = 'KOGITO_IMAGES' | ||
examplesImagesParamsPrefix = 'EXAMPLES_IMAGES' | ||
baseImageParamsPrefix = 'BASE_IMAGE' | ||
promoteImageParamsPrefix = 'PROMOTE_IMAGE' | ||
|
||
// Should be multibranch pipeline | ||
pipeline { | ||
agent { | ||
label 'kie-rhel8 && !built-in' | ||
} | ||
|
||
options { | ||
timeout(time: 1380, unit: 'MINUTES') | ||
} | ||
|
||
// parameters { | ||
// For parameters, check into ./dsl/jobs.groovy file | ||
// } | ||
|
||
environment { | ||
// Some generated env is also defined into ./dsl/jobs.groovy file | ||
|
||
KOGITO_CI_EMAIL_TO = credentials("${JENKINS_EMAIL_CREDS_ID}") | ||
|
||
IMAGE_NAME_NIGHTLY_SUFFIX = 'nightly' | ||
|
||
// Use branch name in nightly tag as we may have parallel main and release branch builds | ||
NIGHTLY_TAG = """${getBuildBranch()}-${sh( | ||
returnStdout: true, | ||
script: 'date -u "+%Y-%m-%d"' | ||
).trim()}""" | ||
} | ||
|
||
stages { | ||
stage('Initialize') { | ||
steps { | ||
script { | ||
echo "nightly tag is ${env.NIGHTLY_TAG}" | ||
|
||
currentBuild.displayName = env.NIGHTLY_TAG | ||
} | ||
} | ||
} | ||
|
||
stage('Build & Deploy Images') { | ||
when { | ||
expression { return isImagesDeploy() } | ||
} | ||
steps { | ||
script { | ||
def buildParams = getDefaultBuildParams() | ||
addImageBuildParams(buildParams, env.NIGHTLY_TAG) | ||
addDeployImageWithLatestTagParam(buildParams) | ||
|
||
// For building | ||
addAppsParam(buildParams) | ||
|
||
// For testing | ||
addSkipTestsParam(buildParams) | ||
addExamplesParam(buildParams) | ||
|
||
// Ask to create the PR with changes occuring in this | ||
addBooleanParam(buildParams, 'CREATE_PR', true) | ||
|
||
buildJob(IMAGES_DEPLOY, buildParams) | ||
} | ||
} | ||
post { | ||
failure { | ||
addFailedStage(IMAGES_DEPLOY) | ||
} | ||
} | ||
} | ||
|
||
stage('Build examples images & BDD testing') { | ||
parallel { | ||
stage('Build examples images & BDD testing') { | ||
stages { | ||
stage('Build & Deploy Examples Images') { | ||
when { | ||
expression { return isExamplesImagesDeploy() } | ||
} | ||
steps { | ||
script { | ||
def buildParams = getDefaultBuildParams() | ||
addSkipTestsParam(buildParams) | ||
addImageBuildParams(buildParams, env.NIGHTLY_TAG) | ||
addStringParam(buildParams, constructKey(defaultImageParamsPrefix, 'NAME_PREFIX'), 'examples') | ||
addDeployImageWithLatestTagParam(buildParams) | ||
|
||
// For BDD tests | ||
addImageBuildParams(buildParams, env.NIGHTLY_TAG, kogitoImagesParamsPrefix) | ||
addExamplesParam(buildParams) | ||
|
||
buildJob(EXAMPLES_IMAGES_DEPLOY, buildParams) | ||
} | ||
} | ||
post { | ||
failure { | ||
addFailedStage(EXAMPLES_IMAGES_DEPLOY) | ||
} | ||
} | ||
} | ||
stage('Build & Deploy Operator') { | ||
when { | ||
expression { return isOperatorDeploy() } | ||
} | ||
steps { | ||
script { | ||
def buildParams = getDefaultBuildParams() | ||
addSkipTestsParam(buildParams) | ||
addImageBuildParams(buildParams, env.NIGHTLY_TAG) | ||
addDeployImageWithLatestTagParam(buildParams) | ||
|
||
// For BDD tests | ||
addImageBuildParams(buildParams, env.NIGHTLY_TAG, kogitoImagesParamsPrefix) | ||
addImageBuildParams(buildParams, env.NIGHTLY_TAG, examplesImagesParamsPrefix) | ||
addStringParam(buildParams, constructKey(examplesImagesParamsPrefix, 'CACHE_MODE'), isExamplesImagesDeploy() ? 'always' : 'never') | ||
addStringParam(buildParams, constructKey(examplesImagesParamsPrefix, 'NAME_PREFIX'), 'examples') | ||
addExamplesParam(buildParams) | ||
|
||
buildJob(OPERATOR_DEPLOY, buildParams) | ||
} | ||
} | ||
post { | ||
failure { | ||
addFailedStage(OPERATOR_DEPLOY) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
stage('Build & Deploy Serverless Operator') { | ||
when { | ||
expression { return isOperatorDeploy() } | ||
} | ||
steps { | ||
script { | ||
def buildParams = getDefaultBuildParams() | ||
addSkipTestsParam(buildParams) | ||
addImageBuildParams(buildParams, env.NIGHTLY_TAG) | ||
addDeployImageWithLatestTagParam(buildParams) | ||
|
||
buildJob(SEVERLESS_OPERATOR_DEPLOY, buildParams) | ||
} | ||
} | ||
post { | ||
failure { | ||
addFailedStage(SEVERLESS_OPERATOR_DEPLOY) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
post { | ||
unsuccessful { | ||
sendPipelineErrorNotification() | ||
} | ||
} | ||
} | ||
|
||
def buildJob(String jobName, List buildParams, String jobKey = jobName) { | ||
echo "[${jobKey}] Build ${jobName} with params ${buildParams}" | ||
|
||
def job = build(job: "${jobName}", wait: true, parameters: buildParams, propagate: false) | ||
JOBS[jobKey] = job | ||
|
||
// Set Unstable if job did not succeed | ||
if (!isJobSucceeded(jobKey)) { | ||
addUnstableStage(jobKey) | ||
unstable("Job ${jobName} finished with result ${job.result}") | ||
} | ||
return job | ||
} | ||
|
||
def getJob(String jobKey) { | ||
return JOBS[jobKey] | ||
} | ||
|
||
String getJobUrl(String jobKey) { | ||
echo "getJobUrl for ${jobKey}" | ||
return getJob(jobKey)?.absoluteUrl ?: '' | ||
} | ||
|
||
boolean isJobSucceeded(String jobKey) { | ||
return getJob(jobKey)?.result == 'SUCCESS' | ||
} | ||
|
||
boolean isJobUnstable(String jobKey) { | ||
return getJob(jobKey)?.result == 'UNSTABLE' | ||
} | ||
|
||
void addFailedStage(String jobKey = '') { | ||
FAILED_STAGES.put("${env.STAGE_NAME}", jobKey) | ||
} | ||
void addUnstableStage(String jobKey = '') { | ||
UNSTABLE_STAGES.put("${env.STAGE_NAME}", jobKey) | ||
} | ||
|
||
void sendPipelineErrorNotification() { | ||
String bodyMsg = "Kogito Cloud nightly job #${env.BUILD_NUMBER} was: ${currentBuild.currentResult}" | ||
|
||
paramsStr = '' | ||
if (params.SKIP_TESTS) { | ||
paramsStr += '\n- Tests skipped' | ||
} | ||
if (params.SKIP_IMAGES) { | ||
paramsStr += '\n- Images skipped' | ||
} | ||
if (params.SKIP_OPERATOR) { | ||
paramsStr += '\n- Operator skipped' | ||
} | ||
bodyMsg += paramsStr ? "\n\nConfiguration:${paramsStr}" : '\n' | ||
|
||
if (FAILED_STAGES.size() > 0) { | ||
bodyMsg += '\nFailed stages: \n- ' | ||
bodyMsg += FAILED_STAGES.collect { "${it.key} => ${getJobUrl(it.value)}" }.join('\n- ') | ||
} | ||
bodyMsg += '\n' | ||
if (UNSTABLE_STAGES.size() > 0) { | ||
bodyMsg += '\nUnstable stages: \n- ' | ||
bodyMsg += UNSTABLE_STAGES.collect { "${it.key} => ${getJobUrl(it.value)}" }.join('\n- ') | ||
} | ||
bodyMsg += '\n' | ||
bodyMsg += "\nPlease look here: ${env.BUILD_URL}" | ||
emailext body: bodyMsg, subject: "[${getBuildBranch()}][d] Full Pipeline", | ||
to: env.KOGITO_CI_EMAIL_TO | ||
} | ||
|
||
List getDefaultBuildParams(String buildBranchName = '', String key = '') { | ||
buildBranchName = buildBranchName ?: getBuildBranch() | ||
List params = [] | ||
addStringParam(params, 'DISPLAY_NAME', "${key ? "${key}-" : ''}${env.NIGHTLY_TAG}") | ||
addBooleanParam(params, 'SEND_NOTIFICATION', true) | ||
|
||
return params | ||
} | ||
|
||
void addSkipTestsParam(buildParams) { | ||
addBooleanParam(buildParams, 'SKIP_TESTS', params.SKIP_TESTS) | ||
} | ||
|
||
void addSkipIntegrationTestsParam(buildParams) { | ||
addBooleanParam(buildParams, 'SKIP_INTEGRATION_TESTS', params.SKIP_TESTS) | ||
} | ||
|
||
void addAppsParam(buildParams) { | ||
addStringParam(buildParams, 'APPS_REF', "${getBuildBranch()}") | ||
addStringParam(buildParams, 'APPS_URI', "https://github.com/${getGitAuthor()}/kogito-apps") | ||
} | ||
|
||
void addExamplesParam(buildParams) { | ||
addStringParam(buildParams, 'EXAMPLES_URI', "https://github.com/${getGitAuthor()}/kogito-examples") | ||
addStringParam(buildParams, 'EXAMPLES_REF', "nightly-${getBuildBranch()}") | ||
} | ||
|
||
void addImageBuildParams(List buildParams, String tag, String paramsPrefix = defaultImageParamsPrefix, String extraSuffix = '') { | ||
addStringParam(buildParams, constructKey(paramsPrefix, 'REGISTRY_CREDENTIALS'), env.IMAGE_REGISTRY_CREDENTIALS) | ||
addStringParam(buildParams, constructKey(paramsPrefix, 'REGISTRY'), env.IMAGE_REGISTRY) | ||
addStringParam(buildParams, constructKey(paramsPrefix, 'NAMESPACE'), env.IMAGE_NAMESPACE) | ||
addStringParam(buildParams, constructKey(paramsPrefix, 'NAME_SUFFIX'), (extraSuffix ? "${extraSuffix}-" : '') + env.IMAGE_NAME_NIGHTLY_SUFFIX) | ||
addStringParam(buildParams, constructKey(paramsPrefix, 'TAG'), tag) | ||
} | ||
|
||
void addDeployImageWithLatestTagParam(buildParams) { | ||
addBooleanParam(buildParams, 'DEPLOY_WITH_LATEST_TAG', isDeployImagesLatestTag()) | ||
} | ||
|
||
void addStringParam(List params, String key, String value) { | ||
params.add(string(name: key, value: value)) | ||
} | ||
|
||
void addBooleanParam(List params, String key, boolean value) { | ||
params.add(booleanParam(name: key, value: value)) | ||
} | ||
|
||
String constructKey(String prefix, String paramId) { | ||
return prefix ? "${prefix}_${paramId}" : paramId | ||
} | ||
|
||
String getBuildBranch() { | ||
return env.GIT_BRANCH_NAME | ||
} | ||
|
||
String getGitAuthor() { | ||
return env.GIT_AUTHOR | ||
} | ||
|
||
String getGitAuthorCredsID() { | ||
return env.GIT_AUTHOR_CREDS_ID | ||
} | ||
|
||
boolean isDeployImagesLatestTag() { | ||
return getBuildBranch() == env.BRANCH_FOR_LATEST | ||
} | ||
|
||
boolean isImagesDeploy() { | ||
return !params.SKIP_IMAGES | ||
} | ||
|
||
boolean isExamplesImagesDeploy() { | ||
return !params.SKIP_EXAMPLES_IMAGES | ||
} | ||
|
||
boolean isOperatorDeploy() { | ||
return !params.SKIP_OPERATOR | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.