From be603724d26432b15f3502c920b394536b750e92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Mattm=C3=BCller?= Date: Tue, 29 Aug 2023 14:40:31 +0200 Subject: [PATCH] jobs/common: refactored gitCheckout() for using more flexible MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit the function gitCheckout() holds the following steps: - git clone unless done - checking out a branch/tag/commit including rebase and updating submodules - launching the update source revision script - printing the submodule status - printing the last 3 commits of the git history But so far you could not use the steps separately. Thus, this function was refactored by extracting some steps into own functions so that they can be used easily. Example: the job workspace is the cloned git repository and you want to update the source revision hashes ;-P Signed-off-by: Marc Mattmüller --- jobs/Jenkinsfile_Common | 50 ++++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/jobs/Jenkinsfile_Common b/jobs/Jenkinsfile_Common index 78441d4..02892d9 100644 --- a/jobs/Jenkinsfile_Common +++ b/jobs/Jenkinsfile_Common @@ -118,8 +118,31 @@ def removePreExistingYoctoConfigs(confPath) { //----------------------------------------------------------------------------- -def gitCheckout(gitUrl, branchTag, repoDir, hasSubmodules) { - println "checking out ${gitUrl} to ${repoDir}..." +def printLatestGitHistory() { + gitHistory = sh(returnStdout: true, script: "git log --pretty=oneline -3") + println "Last 3 git commits:\n-----------------------------\n${gitHistory}" +} +//----------------------------------------------------------------------------- +def printSubmoduleStatus(hasSubmodules = true) { + if(hasSubmodules) { + submoduleStatus = sh(script: "git submodule status", returnStdout: true) + println "${submoduleStatus}" + } +} +//----------------------------------------------------------------------------- +def gitUpdateSubmodulesCheckoutBranch(branchTag, hasSubmodules, doUpdatePackageSrcRevisions) { + def gitCredentials = getGitCredentialID() + def updateSubmodulesCmd = hasSubmodules ? " && git submodule update --init --recursive" : "" + sshagent (credentials: [gitCredentials]) { + sh(script: "git checkout ${branchTag} && git pull --rebase ${updateSubmodulesCmd}") + if(doUpdatePackageSrcRevisions) { + sh(label: "Temporary task - updating source revisions (sub-repositories)", + script: "./scripts/update-source-revisions.sh") + } + } +} +//----------------------------------------------------------------------------- +def cloneGitRepoUnlessExisting(gitUrl, repoDir, hasSubmodules) { def gitCredentials = getGitCredentialID() if(!fileExists("./${repoDir}")) { sshagent (credentials: [gitCredentials]) { @@ -127,21 +150,16 @@ def gitCheckout(gitUrl, branchTag, repoDir, hasSubmodules) { sh(script: "git clone ${inclSubmodulesOpt} ${gitUrl} ${repoDir}") } } - dir("${repoDir}") { - def updateSubmodulesCmd = hasSubmodules ? " && git submodule update --init --recursive" : "" - sshagent (credentials: [gitCredentials]) { - sh(script: "git fetch -ap && git fetch -t") - sh(script: "git checkout ${branchTag} && git pull --rebase ${updateSubmodulesCmd}") +} +//----------------------------------------------------------------------------- +def gitCheckout(gitUrl, branchTag, repoDir, hasSubmodules, doUpdatePackageSrcRevisions = true) { + println "checking out git repository ${gitUrl} to ${repoDir}..." + cloneGitRepoUnlessExisting(gitUrl, repoDir, hasSubmodules) - sh(label: "Temporary task - updating source revisions (sub-repositories)", - script: "./scripts/update-source-revisions.sh") - } - if(hasSubmodules) { - submoduleStatus = sh(script: "git submodule status", returnStdout: true) - println "${submoduleStatus}" - } - gitHistory = sh(returnStdout: true, script: "git log --pretty=oneline -3") - println "Last 3 git commits:\n-----------------------------\n${gitHistory}" + dir("${repoDir}") { + gitUpdateSubmodulesCheckoutBranch(branchTag, hasSubmodules, doUpdatePackageSrcRevisions) + printSubmoduleStatus(hasSubmodules) + printLatestGitHistory() } } //-----------------------------------------------------------------------------