From 2a56bbd0e8450f5ec18d8b3a800bae2e27f48cfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Mattm=C3=BCller?= Date: Mon, 4 Sep 2023 12:17:38 +0200 Subject: [PATCH] jobs/updateSrcRevisions: added functionality to the skeleton MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marc Mattmüller --- jobs/Jenkinsfile_UpdateSrcRevisions | 81 ++++++++++++++++++++++++++++- 1 file changed, 79 insertions(+), 2 deletions(-) diff --git a/jobs/Jenkinsfile_UpdateSrcRevisions b/jobs/Jenkinsfile_UpdateSrcRevisions index 000bbf9..932bbdf 100644 --- a/jobs/Jenkinsfile_UpdateSrcRevisions +++ b/jobs/Jenkinsfile_UpdateSrcRevisions @@ -10,6 +10,7 @@ pipeline { 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 { @@ -24,7 +25,13 @@ pipeline { stage('Prepare') { steps { script { - println "ToDo: prepare stage" + printJobParameters() + setDisplayName() + + // load common file + common = load "./jobs/Jenkinsfile_Common" + + handleCleanBuild(common) } } } @@ -32,10 +39,80 @@ pipeline { stage('Update') { steps { script { - println "ToDo: Update stage" + updateTheSourceRevisions(common, "${params.BUILD_BRANCH}") + commitAndPushTheChanges(common) } } + post { + always { + script { + common.cleanupRepository("./") + } + } + } + } } // 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, theBranch) { + commonHelpers.gitUpdateSubmodulesCheckoutBranch(theBranch, true, true) + commonHelpers.printSubmoduleStatus(true) +} +//--------------------------------------------------------------------------------------------------------------------- +def commitAndPushTheChanges(commonHelpers) { + def commitMsg = "srcrev: updated source revisions by Jenkins Job" + + 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 + git checkout * > /dev/null + """) + } + else { + println "ToDo: commit and push the changes" + //NOTE: The following code snippet may help... + //def theCredentials = getGitCredentialID() + //sshagent (credentials: [theCredentials]) { + // sh(label: "Commit and push changes", returnStdout: true, script: """ + // git commit -m "${commitMsg}" + // git push + // """) + //} + } + } +}