nwl-ci/jobs/Jenkinsfile_BuildTarget

169 lines
5.8 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"
def nodeHostname = sh(returnStdout: true, script: "set +x && hostname | cut -d '_' -f1").trim()
def cloneCredentials = ("${nodeHostname}" == "nwl") ? 'nmgit_credentials' : 'gitCredentials'
dir(repoPreCloneDir) {
// clone the repository to get the file with the target list
sshagent (credentials: [cloneCredentials]) {
sh "set +x && git clone --depth 1 --branch main ssh://git@bitbucket.gad.local:7999/nm-nsp/nwl-ci.git ."
}
// get the list of the targets:
targetList = sh(returnStdout: true, script: "set +x && 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: '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')
buildDiscarder(
logRotator(numToKeepStr: '20', daysToKeepStr: '7')
)
}
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-${env.MACHINE}.zip"
common.collectingPackageArtifacts("${env.MACHINE}")
common.packAndArchiveArtifacts("${env.MACHINE}", "${env.ARTIFACT_NAME}")
}
}
}
post {
always {
script {
common.cleanupRepository("${env.YOCTO_REPO_DIR}")
}
}
}
}
stage('Update SSTATE-CACHE MIRROR') {
when { expression { return (!params.SKIP_SSTATE_UPLOAD && common.isCurrentJobSuccess()) } }
steps {
script {
common.syncSources("${env.SSTATE_CACHE}", "${env.SSTATE_STORAGE_URL}")
}
}
}
stage('Deploy') {
when { expression { return (params.DEPLOY_TO_NEXUS && 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\
DEPLOY_TO_NEXUS = ${params.DEPLOY_TO_NEXUS}\n\
SKIP_SSTATE_UPLOAD = ${params.SKIP_SSTATE_UPLOAD}\n\
DEBUGGING = ${params.DEBUGGING}\n\
----------------------------------\n"
}
//---------------------------------------------------------------------------------------------------------------------
def checkJobParameters() {
def selectedTarget = "${params.TARGET}"
if("${selectedTarget}" == "select...") {
currentBuild.result = 'ABORTED'
error("Missing build target --> select parameter TARGET for a proper build")
}
env.TARGET = "${selectedTarget}"
}
//---------------------------------------------------------------------------------------------------------------------
def setDisplayName() {
def buildName = "#${env.BUILD_NUMBER}"
currentBuild.displayName = "${buildName}-${env.TARGET}"
}
//---------------------------------------------------------------------------------------------------------------------
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)
}