-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
kie-issues#821: Setup weekly cloud jobs #1177
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,260 @@ | ||
import org.jenkinsci.plugins.workflow.libs.Library | ||
|
||
@Library('jenkins-pipeline-shared-libraries')_ | ||
|
||
// Deploy jobs | ||
IMAGES_DEPLOY = 'kogito-images.weekly-deploy' | ||
SEVERLESS_OPERATOR_DEPLOY = 'kogito-serverless-operator.weekly-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' | ||
|
||
// Should be multibranch pipeline | ||
pipeline { | ||
agent { | ||
label 'ubuntu' | ||
} | ||
|
||
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_WEEKLY_SUFFIX = 'nightly' | ||
|
||
// Use branch name in weekly tag as we may have parallel main and release branch builds | ||
WEEKLY_TAG = """${getBuildBranch()}-${getCurrentDate()}""" | ||
} | ||
|
||
stages { | ||
stage('Initialize') { | ||
steps { | ||
script { | ||
echo "weekly tag is ${env.WEEKLY_TAG}" | ||
|
||
currentBuild.displayName = env.WEEKLY_TAG | ||
} | ||
} | ||
} | ||
|
||
stage('Build & Deploy Images') { | ||
when { | ||
expression { return isImagesDeploy() } | ||
} | ||
steps { | ||
script { | ||
def buildParams = getDefaultBuildParams() | ||
addImageBuildParams(buildParams, env.WEEKLY_TAG) | ||
addDeployImageWithLatestTagParam(buildParams) | ||
|
||
// For building | ||
addAppsParam(buildParams) | ||
|
||
// For testing | ||
addSkipTestsParam(buildParams) | ||
addExamplesParam(buildParams) | ||
|
||
buildJob(IMAGES_DEPLOY, buildParams) | ||
} | ||
} | ||
post { | ||
failure { | ||
addFailedStage(IMAGES_DEPLOY) | ||
} | ||
} | ||
} | ||
|
||
stage('Build & Deploy Serverless Operator') { | ||
when { | ||
expression { return isOperatorDeploy() } | ||
} | ||
steps { | ||
script { | ||
def buildParams = getDefaultBuildParams() | ||
addSkipTestsParam(buildParams) | ||
addImageBuildParams(buildParams, env.WEEKLY_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 weekly 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.WEEKLY_TAG}") | ||
addStringParam(params, 'GIT_CHECKOUT_DATETIME', getCheckoutDatetime()) | ||
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()}/incubator-kie-kogito-apps") | ||
} | ||
|
||
void addExamplesParam(buildParams) { | ||
addStringParam(buildParams, 'EXAMPLES_URI', "https://github.com/${getGitAuthor()}/incubator-kie-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_WEEKLY_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 isOperatorDeploy() { | ||
return !params.SKIP_OPERATOR | ||
} | ||
|
||
String getCurrentDate() { | ||
return sh(returnStdout: true, script: 'date -u "+%Y-%m-%d"').trim() | ||
} | ||
|
||
String getCheckoutDatetime() { | ||
return params.GIT_CHECKOUT_DATETIME | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please consider using util.getLabel('ubuntu'), so we can easily avoid faulty nodes in the future
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated. Thanks @cimbalek