nwl-ci/jobs/Jenkinsfile_UpdateSrcRevisions

120 lines
4.2 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
// 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: 'DRY_RUN', defaultValue: true, description: 'do a dry run, i.e. without committing and pushing')
}
options {
timeout(time: 1, unit: 'HOURS')
buildDiscarder(
logRotator(numToKeepStr: '10')
)
}
stages {
stage('Prepare') {
steps {
script {
printJobParameters()
setDisplayName()
// load common file
common = load "./jobs/Jenkinsfile_Common"
handleCleanBuild(common)
common.gitCheckout("${env.YOCTO_REPO_URL}", "${params.BUILD_BRANCH}", "${env.YOCTO_REPO_DIR}", true, false)
}
}
}
stage('Update') {
steps {
script {
updateTheSourceRevisions(common, "${env.YOCTO_REPO_DIR}", "${params.BUILD_BRANCH}")
commitAndPushTheChanges(common, "${env.YOCTO_REPO_DIR}")
}
}
post {
always {
script {
common.cleanupRepository("${env.YOCTO_REPO_DIR}")
}
}
}
}
} // stages
}
//-----------------------------------------------------------------------------
def printJobParameters() {
println "----------------------------------\n\
Job Parameters:\n\
----------------------------------\n\
BUILD_BRANCH = ${params.BUILD_BRANCH}\n\
CLEAN_BUILD = ${params.CLEAN_BUILD}\n\
DRY_RUN = ${params.DRY_RUN}\n\
----------------------------------\n"
}
//---------------------------------------------------------------------------------------------------------------------
def setDisplayName() {
def buildName = "#${env.BUILD_NUMBER}"
def theBranch = "${params.BUILD_BRANCH}"
def cleaning = ("${params.CLEAN_BUILD}" == true) ? "-clean" : ""
currentBuild.displayName = "${buildName}-${theBranch}${cleaning}"
}
//---------------------------------------------------------------------------------------------------------------------
def handleCleanBuild(commonHelpers) {
if(params.CLEAN_BUILD) {
println "CLEAN BUILD REQUESTED, cleaning..."
commonHelpers.cleaningClonedRepoDir()
}
}
//---------------------------------------------------------------------------------------------------------------------
def updateTheSourceRevisions(commonHelpers, repoDir, theBranch) {
dir(repoDir) {
commonHelpers.gitUpdateSubmodulesCheckoutBranch(theBranch, true, true)
commonHelpers.printSubmoduleStatus(true)
}
}
//---------------------------------------------------------------------------------------------------------------------
def commitAndPushTheChanges(commonHelpers, repoDir) {
def commitMsg = "srcrev: updated source revisions by Jenkins Job"
dir(repoDir) {
if(sh(returnStdout: true, script: "git status | grep \"modified:\" | grep -v coreos | wc -l").toInteger() != 0) {
def itemFindCmd = "git status | grep \"modified:\" | grep -v coreos | cut -d':' -f2 | sed -e 's/^[ ]*//' | cut -d' ' -f1"
def changedItems = sh(returnStdout: true, script: "${itemFindCmd}")
sh(label: "Stage tracked and changed git files", returnStdout: true, script: "git add ${changedItems}")
if(params.DRY_RUN) {
println "DRY RUN: commit message = '${commitMsg}'\n changes = ${changedItems}"
sh(script: "git reset HEAD > /dev/null")
}
else {
def theCredentials = commonHelpers.getGitCredentialID()
sshagent (credentials: [theCredentials]) {
println "Commit and Push changes; message = '${commitMsg}'\nchanges = ${changedItems}"
sh(label: "Commit and push changes", returnStdout: true, script: """
git commit -m "${commitMsg}"
git push
""")
}
}
}
}
}