nwl-ci/jobs/Jenkinsfile_BuildAll

183 lines
6.7 KiB
Plaintext

// Loading code requires a NODE context
// But we want the code accessible outside the node Context
// So declare common (object created by the LOAD operation) outside the Node block.
def common
// This step is necessary to get the files directly from the git repo, as for the
// first build it is not yet cloned into the workspace. Normally we use an agent
// of a certain label (like core-os_buildagent) to have define credentials to get
// the files. In the current case we use any agent.
node() {
def repoPreCloneDir = "repo-preclone"
def nodeHostname = sh(returnStdout: true, script: "hostname | cut -d '_' -f1").trim()
def cloneCredentials = ("${nodeHostname}" == "nwl") ? 'admin_credentials' : 'gitCredentials'
dir(repoPreCloneDir) {
// clone the repository to get the file with the target list
sshagent (credentials: [cloneCredentials]) {
sh "git clone --depth 1 --branch main ssh://git@bitbucket.gad.local:7999/nm-nsp/nwl-ci.git ."
}
// load common file
common = load "./jobs/Jenkinsfile_Common"
}
sh("rm -rf ${repoPreCloneDir}")
}
// declarative pipeline
pipeline {
agent any
parameters {
string(name: 'BUILD_BRANCH', defaultValue: 'main', description: 'Enter the branch of the NWL to build (default = main), will skip deployment if not main')
booleanParam(name: 'CLEAN_BUILD', defaultValue: false, description: 'do a clean build, i.e. remove the yocto directory and start from scratch')
booleanParam(name: 'DEPLOY_TO_NEXUS', defaultValue: true, description: 'deploy the built artifact to Nexus')
booleanParam(name: 'SKIP_SSTATE_UPLOAD', defaultValue: false, description: 'skip uploading/synchronizing the sstate-cache to the mirror')
booleanParam(name: 'DEBUGGING', defaultValue: false, description: 'debugging mode, removes quiet mode for bitbake')
}
options {
timeout(time: 5, unit: 'HOURS')
disableConcurrentBuilds()
buildDiscarder(
logRotator(numToKeepStr: '20', daysToKeepStr: '7')
)
}
triggers {
cron('H H(5-6) * * 1-5')
}
stages {
stage('Check Parameters') {
steps {
script {
printJobParameters()
checkJobParameters(common)
setDisplayName(common)
}
}
}
stage('Prepare') {
steps {
script {
if(params.CLEAN_BUILD) {
println "CLEAN BUILD REQUESTED, cleaning..."
common.cleaningClonedRepoDir()
}
setupEnvironment(common)
}
}
}
stage('Build') {
steps {
script {
def listOfTargets = getTargetsFromList()
def isBuildSuccess = true
def firstMachineFailing = ""
catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
for (machine in listOfTargets) {
if(isMachineSane(machine)) {
println "BUILDING ${machine}"
catchError(buildResult: 'UNSTABLE', stageResult: 'FAILURE') {
if(!runBuildJob(common, machine, "${params.BUILD_BRANCH}")) {
println "Building ${machine} failed"
if(isBuildSuccess) {
isBuildSuccess = false
firstMachineFailing = machine
}
}
}
} // end isMachineSane
} // end for
if(!isBuildSuccess) {
error("Build error, first machine failing = ${firstMachineFailing}")
}
} // end catchError
}
}
}
} // stages
}
//-----------------------------------------------------------------------------
def printJobParameters() {
println "----------------------------------\n\
Job Parameters:\n\
----------------------------------\n\
BUILD_BRANCH = ${params.BUILD_BRANCH}\n\
CLEAN_BUILD = ${params.CLEAN_BUILD}\n\
DEPLOY_TO_NEXUS = ${params.DEPLOY_TO_NEXUS}\n\
SKIP_SSTATE_UPLOAD = ${params.SKIP_SSTATE_UPLOAD}\n\
DEBUGGING = ${params.DEBUGGING}\n\
----------------------------------\n"
}
//---------------------------------------------------------------------------------------------------------------------
def checkJobParameters(commonHelpers) {
// Check the selected target and overwrite it with a default one when triggered by a timer
if(commonHelpers.isJobTriggeredByTimer()) {
println "INFO: Triggered by Timer"
}
}
//---------------------------------------------------------------------------------------------------------------------
def setDisplayName(commonHelpers) {
def buildName = "#${env.BUILD_NUMBER}"
def postfix = commonHelpers.isJobTriggeredByTimer() ? "-nightly" : ""
currentBuild.displayName = "${buildName}${postfix}"
}
//---------------------------------------------------------------------------------------------------------------------
def setupEnvironment(commonHelpers) {
def nwlBranch = "${params.BUILD_BRANCH}"
def nwlRepoDir = "${env.YOCTO_REPO_DIR}"
commonHelpers.gitCheckout("${env.YOCTO_REPO_URL}", nwlBranch, nwlRepoDir, true)
env.BUILD_IMG_JOB = "/build-yocto-target-pipeline/${env.BRANCH_NAME}"
}
//-----------------------------------------------------------------------------
def getTargetsFromList() {
def theTargets = sh(returnStdout: true, script: "cat ./jobs/nwlTargets | grep -v select").trim().split("\n")
return theTargets
}
//-----------------------------------------------------------------------------
def isMachineSane(machine) {
// ToDo: place here any exclusions if necessary
// adapt the line below as soon as all other target are ready:
return ("${machine}" == "cn9130-cf-pro")
}
//-----------------------------------------------------------------------------
def runBuildJob(commonHelpers, buildTarget, buildBranch) {
def buildJob = null
try {
buildJob = build(job: "${env.BUILD_IMG_JOB}",
quietPeriod: 0,
propagate: true,
wait: true,
parameters: [string(name: 'TARGET', value: buildTarget),
string(name: 'BUILD_BRANCH', value: buildBranch),
booleanParam(name: 'CLEAN_BUILD', value: params.CLEAN_BUILD),
booleanParam(name: 'DEPLOY_TO_NEXUS', value: params.DEPLOY_TO_NEXUS),
booleanParam(name: 'SKIP_SSTATE_UPLOAD', value: params.SKIP_SSTATE_UPLOAD),
booleanParam(name: 'DEBUGGING', value: params.DEBUGGING)]
)
}
catch(Exception e) {
println "Exception caught: " + e.toString()
return
}
// assert to be sure
if(buildJob == null) {
error("Something went really wrong with ${env.BUILD_IMG_JOB} (TARGET=${buildTarget})")
return
}
return (buildJob.getResult() != 'SUCCESS')
}