Skip to content

Instantly share code, notes, and snippets.

@mbiarnes
Last active July 19, 2023 06:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbiarnes/205ae76c987ad17b54358f71ac372aec to your computer and use it in GitHub Desktop.
Save mbiarnes/205ae76c987ad17b54358f71ac372aec to your computer and use it in GitHub Desktop.
used pipeline (copied from Jenkinsfle.buildchain)
@Library('jenkins-pipeline-shared-libraries')_
import org.kie.jenkins.MavenCommand
import org.kie.jenkins.MavenSettingsUtils
agentLabel = 'kie-rhel8-priority && !built-in'
timeoutValue = env.ADDITIONAL_TIMEOUT?.trim() ?: '180'
jdkTool = env.BUILD_JDK_TOOL
mavenTool = env.BUILD_MAVEN_TOOL
mavenDeployArtifacts = env.ENABLE_DEPLOY
mavenDeployRepository = env.MAVEN_DEPLOY_REPOSITORY ?: ''
mavenDeployRepositoryCredsId = env.MAVEN_DEPLOY_REPOSITORY_CREDS_ID ?: ''
mavenDeployLocalDir = env.MAVEN_DEPLOY_LOCAL_DIR ?: ''
buildChainType = env.BUILDCHAIN_TYPE?.trim() ?: 'cross_pr'
buildChainProject = env.BUILDCHAIN_PROJECT?.trim()
defaultSettingsXmlId = isPRBuildChainType() || isFDBBuildChainType() ? 'kogito_pr_settings' : 'kogito_release_settings'
settingsXmlId = env.MAVEN_SETTINGS_FILE_ID ?: defaultSettingsXmlId
enableSonarCloudAnalysis = env.ENABLE_SONARCLOUD ? env.ENABLE_SONARCLOUD.toBoolean() : false
downstreamBuild = env.DOWNSTREAM_BUILD ? env.DOWNSTREAM_BUILD.toBoolean() : false
buildChainConfigRepo = env.BUILDCHAIN_CONFIG_REPO ?: 'kogito-pipelines'
buildChainConfigBranch = env.BUILDCHAIN_CONFIG_BRANCH ?: '\${BRANCH:main}'
buildChainConfigGitAuthor = env.BUILDCHAIN_CONFIG_AUTHOR ?: '\${AUTHOR:kiegroup}'
buildChainConfigDefinitionFilePath = env.BUILDCHAIN_CONFIG_FILE_PATH ?: '.ci/pull-request-config.yaml'
buildChainAction = isFDBBuildChainType() ? 'full_downstream' : buildChainType
buildChainAdditionalArguments = buildChainProject ? [ "-p ${buildChainProject}" ] : []
skipTests = params.SKIP_TESTS ?: false
skipIntegrationTests = params.SKIP_INTEGRATION_TESTS ?: false
notificationJobName = env.NOTIFICATION_JOB_NAME ?: 'PR'
pipeline {
agent {
label agentLabel
}
tools {
nodejs 'nodejs-16.2.0'
}
options {
timestamps()
timeout(time: timeoutValue, unit: 'MINUTES')
}
environment {
FIREFOX_FOLDER = '/opt/tools/firefox-91esr'
MAVEN_OPTS = '-Xms1024m -Xmx12g'
}
stages {
stage('Initialize') {
steps {
script {
cleanWs()
sh 'git clone https://github.com/kiegroup/droolsjbpm-build-bootstrap ./'
sh 'printenv'
sh 'printenv > env_props'
archiveArtifacts artifacts: 'env_props'
if (mavenDeployArtifacts) {
assert mavenDeployRepository : 'Please provide the maven deploy repository'
assert mavenDeployRepositoryCredsId : 'Please provide the maven deploy repository credentials'
}
currentBuild.displayName = params.DISPLAY_NAME ?: env.GIT_BRANCH_NAME ?: currentBuild.displayName
// Needed by Kogito Apps
setupCypressEnv('9.7.0')
}
}
}
stage('check space before build') {
steps {
script {
try {
util.spaceLeft()
} catch (err) {
echo "Error when checking the space on node ... ${err}"
}
}
}
}
stage('CI_MESSAGE'){
steps{
script{
echo "CI_MESSAGE: ${CI_MESSAGE}"
}
}
}
stage('get missing params'){
steps{
script{
prInfoObject = readJSON text: "${CI_MESSAGE}"
env['ghprbPullId'] = prInfoObject.issue.number
env['ghprbGhRepository'] = prInfoObject.repository.full_name
}
}
}
stage('Map missing params'){
steps{
script {
umbToGHPRB("${CI_MESSAGE}", env.ghprbGhRepository, env.ghprbPullId)
if (isBranchBuildChainType()) {
buildChainAdditionalArguments.add("-b ${env.GIT_BRANCH_NAME.trim()}")
buildChainAdditionalArguments.add("-g ${env.GIT_AUTHOR.trim()}")
if(env.BUILDCHAIN_FULL_BRANCH_DOWNSTREAM_BUILD) {
buildChainAdditionalArguments.add("--fullProjectDependencyTree")
}
} else {
buildChainAdditionalArguments.add("-u ${ghprbPullLink}")
}
}
}
}
stage('Install build-chain tool') {
steps {
script {
println '[INFO] Getting build-chain version from composite action file'
def buildChainVersion = buildChain.getBuildChainVersionFromCompositeActionFile()
if ([null, 'null'].contains(buildChainVersion)) {
def errorMessage = "[ERROR] The build-chain version can't be recovered. Please contact administrator"
println errorMessage
error(errorMessage)
}
println "[INFO] build-chain version recovered '${buildChainVersion}'"
sh "npm install -g @kie/build-chain-action@${buildChainVersion}${env.NPM_REGISTRY_URL ? " -registry=${NPM_REGISTRY_URL}" : ''}"
sh "npm list -g | grep build-chain"
}
}
}
stage('Build projects') {
tools {
jdk jdkTool
maven mavenTool
}
steps {
script {
env.LD_LIBRARY_PATH = "${env.LD_LIBRARY_PATH ? "${env.LD_LIBRARY_PATH}:": ''}${GRAALVM_HOME}/lib/server"
echo "LD_LIBRARY_PATH = ${LD_LIBRARY_PATH}"
env.BUILD_MVN_OPTS_CURRENT = "${env.BUILD_MVN_OPTS_CURRENT ?: ''} ${getBuildMavenOptsCurrent()}"
echo "BUILD_MVN_OPTS_CURRENT = ${BUILD_MVN_OPTS_CURRENT}"
if (mavenDeployArtifacts) {
env.DEPLOY_MVN_OPTS = "${env.DEPLOY_MVN_OPTS ?: ''} -DaltDeploymentRepository=local::default::file://${getMavenDeployLocalDir()}"
echo "DEPLOY_MVN_OPTS = ${DEPLOY_MVN_OPTS}"
}
configFileProvider([configFile(fileId: settingsXmlId, variable: 'MAVEN_SETTINGS_FILE')]) {
withCredentials([string(credentialsId: "${GIT_AUTHOR_TOKEN_CREDENTIALS_ID}", variable: 'GITHUB_TOKEN')]) {
env.BUILD_MVN_OPTS = "${env.BUILD_MVN_OPTS ?: ''} -s ${MAVEN_SETTINGS_FILE} -Dmaven.wagon.http.ssl.insecure=true -Dmaven.test.failure.ignore=true"
echo "BUILD_MVN_OPTS = ${BUILD_MVN_OPTS}"
util.runWithPythonVirtualEnv("${getBuildChainCommandline(true)}", 'swf')
}
}
}
}
post {
always {
script {
// Remove `node_modules` to avoid heap space issues with junit command thereafter
// Related to https://github.com/jenkinsci/junit-plugin/issues/478 and https://github.com/jenkinsci/junit-plugin/issues/467
sh 'find . -type d -name node_modules -exec rm -rf {} \\; || true'
junit(testResults: '**/junit.xml, **/target/surefire-reports/**/*.xml, **/target/failsafe-reports/**/*.xml, **/target/invoker-reports/**/*.xml', allowEmptyResults: true)
archiveArtifacts(artifacts: '**/cypress/screenshots/**,**/cypress/videos/**', fingerprint: false, allowEmptyArchive: true)
}
}
unsuccessful {
script {
util.archiveConsoleLog('', 300)
}
}
}
}
stage('Sonar analysis') {
when {
expression { return enableSonarCloudAnalysis }
}
tools {
jdk jdkTool
maven mavenTool
}
steps {
script {
dir(getProjectFolder()) {
maven.runMavenWithSettingsSonar(settingsXmlId, "-e -nsu validate -Psonarcloud-analysis -Denforcer.skip=true ${env.SONARCLOUD_ANALYSIS_MVN_OPTS ?: ''}", 'SONARCLOUD_TOKEN', 'sonar_analysis.maven.log')
}
}
}
}
stage('Upload artifacts to repository') {
when {
expression { return mavenDeployArtifacts }
}
tools {
jdk jdkTool
maven mavenTool
}
steps {
script {
// Upload to specific repository with credentials
String mavenDeployRepositoryZipUrl = "${mavenDeployRepository.replaceAll('/content/', '/service/local/').replaceFirst('/*$', '')}/content-compressed"
maven.uploadLocalArtifacts(mavenDeployRepositoryCredsId, getMavenDeployLocalDir(), mavenDeployRepositoryZipUrl)
}
}
}
stage('check space after build') {
steps {
script {
util.spaceLeft()
}
}
}
}
/*post {
unsuccessful {
script {
if (isPRBuildChainType()) {
pullrequest.postComment(util.getMarkdownTestSummary(notificationJobName, getReproducer(true), "${BUILD_URL}", 'GITHUB'), "${GIT_AUTHOR_TOKEN_CREDENTIALS_ID}")
} else if (shouldNotify()) {
withCredentials([string(credentialsId: "${JENKINS_EMAIL_CREDS_ID}", variable: 'KOGITO_CI_EMAIL_TO')]) {
mailer.sendMarkdownTestSummaryNotification(env.NOTIFICATION_JOB_NAME ?: '', "[${env.GIT_BRANCH_NAME}] ${getRepoNameCamelCase(env.REPO_NAME)}", [env.KOGITO_CI_EMAIL_TO], getReproducer())
}
}
}
}
cleanup {
script {
// Clean also docker in case of usage of testcontainers lib
util.cleanNode('docker')
sh 'rm -rf /tmp/quarkus'
}
}
}*/
}
void setupCypressEnv(String cypressVersion) {
if (env.CYPRESS_BINARY_URL) {
env.CYPRESS_INSTALL_BINARY = "${CYPRESS_BINARY_URL}/cypress-${cypressVersion}.zip"
}
}
boolean isPRBuildChainType() {
return buildChainType == 'cross_pr'
}
boolean isFDBBuildChainType() {
return buildChainType == 'full_downstream'
}
boolean isSingleBuildChainType() {
return buildChainType == 'single'
}
boolean isBranchBuildChainType() {
return buildChainType == 'branch'
}
String getBuildMavenOptsCurrent() {
List opts_current = []
enableSonarCloudAnalysis ? opts_current.add('-Prun-code-coverage') : null
skipTests ? opts_current.add('-DskipTests') : null
skipIntegrationTests ? opts_current.add('-DskipITs') : null
return opts_current.join(' ')
}
boolean shouldNotify() {
return env.ENABLE_NOTIFICATION
}
String getProjectFolder() {
def project = (buildChainProject ? util.getProjectGroupName(buildChainProject) : util.getProjectTriggeringJob())[1]
// First guessing whether there is a clone defined into the buildchain config
// If not, fallback to simple folder structure
String projectFolder = "bc/kiegroup_${project}/${project}"
if (!fileExists(projectFolder)) {
projectFolder = "bc/kiegroup_${project}"
}
return projectFolder
}
String getBuildChainCommandline(boolean includeToken=false) {
return "build-chain build ${buildChainAction} ${includeToken ? "--token ${GITHUB_TOKEN} " : ''}-f 'https://raw.githubusercontent.com/${buildChainConfigGitAuthor}/${buildChainConfigRepo}/${buildChainConfigBranch}/${buildChainConfigDefinitionFilePath}' -o 'bc' ${buildChainAdditionalArguments.join(' ')} --skipParallelCheckout"
}
String getReproducer(boolean isGH = false) {
String reproducer = """
${env.QUARKUS_BRANCH ? "export QUARKUS_BRANCH=${env.QUARKUS_BRANCH}" : ''}
${env.BUILD_MVN_OPTS_CURRENT ? "export BUILD_MVN_OPTS_CURRENT=${env.BUILD_MVN_OPTS_CURRENT}" : ''}
${getBuildChainCommandline()}
NOTE: To install the build-chain tool, please refer to https://github.com/kiegroup/github-action-build-chain#local-execution
"""
if(isGH) {
return """
<details>
<summary><b>Reproducer</b></summary>
${reproducer}
</details>
"""
} else {
return """
```spoiler Reproducer
${reproducer}
```
"""
}
}
String getRepoNameCamelCase(String repo) {
List words = repo.split('-') as List
return words.collect { it.isEmpty() ? it : it.substring(0, 1).toUpperCase() + it.substring(1).toLowerCase() }.join(' ')
}
String getMavenDeployLocalDir() {
return mavenDeployLocalDir ?: "${WORKSPACE}/maven_deploy_dir"
}
void umbToGHPRB(String umbMessage, String repositoryName, String pullRequestId) {
if(umbMessage?.trim()) {
def ghrprbToGHAPIPRMap = [
'ghprbPullId' : 'number',
'ghprbPullLink' : 'html_url',
'ghprbPullTitle': 'title',
'ghprbCommentBody' : 'body',
'ghprbActualCommit': 'head.sha',
'ghprbSourceBranch' : 'head.ref',
'ghprbTargetBranch' : 'base.ref',
'ghprbPullLongDescription' : 'head.repo.description',
'ghprbAuthorRepoGitUrl' : 'head.repo.clone_url',
'ghprbGhRepository' : 'base.repo.full_name'
]
def curlResult = sh(returnStdout: true, script: "curl -L https://api.github.com/repos/${repositoryName}/pulls/${pullRequestId}")?.trim()
if (curlResult) {
def prInfoObject = readJSON text: curlResult
ghrprbToGHAPIPRMap.each { entry ->
def keys = entry.value.split('\\.')
def newValue = prInfoObject
keys.each { nestedKey ->
newValue = newValue["${nestedKey}"]
}
env["${entry.key}"] = newValue
println "[INFO] ${entry.value}:${newValue} mapped to ${entry.key}"
}
}
} else {
println "[WARN] Nothing to map grom UMB message. The content is empty."
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment