nwl-ci/jobs/Jenkinsfile_Build

177 lines
6.3 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
// Preloaded the list of supported targets used for the choice parameters TARGET:
def targetList
// 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"
dir(repoPreCloneDir) {
// clone the repository to get the file with the target list
sshagent (credentials: ['admin_credentials']) {
sh "git clone --depth 1 --branch ${env.BRANCH_NAME} ssh://git@bitbucket.gad.local:7999/nm-nsp/nwl-ci.git ."
}
// get the list of the targets:
targetList = sh(returnStdout: true, script: "cat ./jobs/nwlTargets").trim()
// load common file
common = load "./jobs/Jenkinsfile_Common"
}
sh("rm -rf ${repoPreCloneDir}")
}
// declarative pipeline
pipeline {
agent any
parameters {
choice(name: 'TARGET', choices: "${targetList}", description: 'choose the build target')
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: '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()
setDisplayName()
}
}
}
stage('Prepare') {
steps {
script {
if(params.CLEAN_BUILD) {
println "CLEAN BUILD REQUESTED, cleaning..."
common.cleaningClonedRepoDir()
}
setupEnvironment(common)
}
}
}
stage('Build') {
steps {
script {
dir("${env.YOCTO_REPO_DIR}") {
common.buildTheYoctoPackage()
env.ARTIFACT_NAME = "NWL-${machine}.zip"
common.collectingPackageArtifacts("${env.MACHINE}")
common.packAndArchiveArtifacts("${env.MACHINE}", "${env.ARTIFACT_NAME}")
}
println "TODO: sync sstate-cache to the server"
}
}
}
stage('Deploy') {
when { expression { return common.isCurrentJobSuccess() } }
steps {
script {
deployToArtifactory(common)
}
}
}
} // stages
}
//-----------------------------------------------------------------------------
def printJobParameters() {
println "----------------------------------\n\
Job Parameters:\n\
----------------------------------\n\
TARGET = ${params.TARGET}\n\
BUILD_BRANCH = ${params.BUILD_BRANCH}\n\
CLEAN_BUILD = ${params.CLEAN_BUILD}\n\
DEBUGGING = ${params.DEBUGGING}\n\
----------------------------------\n"
}
//---------------------------------------------------------------------------------------------------------------------
def isJobTriggeredByTimer() {
// The following check is not allowed without having an Administrator approved the script signature
// return (currentBuild.rawBuild.getCause(hudson.triggers.TimerTrigger$TimerTriggerCause) != null)
// Thus we need to find another way round with:
// CAUSE = "${currentBuild.getBuildCauses()[0].shortDescription}"
def jobCause = currentBuild.getBuildCauses()
println "jobCause as information:\n" + jobCause
def jobDescriptionString = "${jobCause[0].shortDescription}"
return jobDescriptionString.contains("timer")
}
//---------------------------------------------------------------------------------------------------------------------
def getDefaultTarget() {
def defaultTarget = sh(returnStdout: true, script: "head -n2 ./jobs/nwlTargets | tail -n1").trim()
return "${defaultTarget}"
}
//---------------------------------------------------------------------------------------------------------------------
def checkJobParameters() {
// Check the selected target and overwrite it with a default one when triggered by a timer
def selectedTarget = "${params.TARGET}"
if("${params.TARGET}" == "select...") {
selectedTarget = ""
if(isJobTriggeredByTimer()) {
selectedTarget = getDefaultTarget()
println "Triggered by Timer --> taking default target = ${selectedTarget}"
}
else {
currentBuild.result = 'ABORTED'
error("Missing build target --> select parameter TARGET for a proper build")
}
}
env.TARGET = "${selectedTarget}"
}
//---------------------------------------------------------------------------------------------------------------------
def setDisplayName() {
def buildName = "#${env.BUILD_NUMBER}"
def postfix = isJobTriggeredByTimer() ? "-nightly" : ""
currentBuild.displayName = "${buildName}-${env.TARGET}${postfix}"
}
//---------------------------------------------------------------------------------------------------------------------
def setupEnvironment(commonHelpers) {
def machine = "${env.TARGET}"
def nwlBranch = "${params.BUILD_BRANCH}"
def nwlRepoDir = "${env.YOCTO_REPO_DIR}"
commonHelpers.setupBuildEnvironment(machine, nwlBranch, nwlRepoDir, params.DEBUGGING)
commonHelpers.printEnvironmentParameters()
}
//---------------------------------------------------------------------------------------------------------------------
def deployToArtifactory(commonHelpers) {
def artifactFilepath = "${env.DEPLOY_CONTENT_DIR}/${env.ARTIFACT_NAME}"
def nexusGroupId = "nwl.${env.CI_IMAGE}"
def nexusArtifactId = "${env.MACHINE}"
def nexusVersion = "latest"
println "deploying ${artifactFilepath} as ${nexusGroupId}.${nexusArtifactId} to Nexus..."
commonHelpers.deployArtifactToNexusArtifactory(artifactFilepath, "zip", nexusGroupId, nexusArtifactId, nexusVersion)
}