jobs/common: refactored gitCheckout() for using more flexible

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 <marc.mattmueller@netmodule.com>
This commit is contained in:
Marc Mattmüller 2023-08-29 14:40:31 +02:00
parent 54161e2b5c
commit be603724d2
1 changed files with 34 additions and 16 deletions

View File

@ -118,8 +118,31 @@ def removePreExistingYoctoConfigs(confPath) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
def gitCheckout(gitUrl, branchTag, repoDir, hasSubmodules) { def printLatestGitHistory() {
println "checking out ${gitUrl} to ${repoDir}..." 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() def gitCredentials = getGitCredentialID()
if(!fileExists("./${repoDir}")) { if(!fileExists("./${repoDir}")) {
sshagent (credentials: [gitCredentials]) { sshagent (credentials: [gitCredentials]) {
@ -127,21 +150,16 @@ def gitCheckout(gitUrl, branchTag, repoDir, hasSubmodules) {
sh(script: "git clone ${inclSubmodulesOpt} ${gitUrl} ${repoDir}") sh(script: "git clone ${inclSubmodulesOpt} ${gitUrl} ${repoDir}")
} }
} }
dir("${repoDir}") { }
def updateSubmodulesCmd = hasSubmodules ? " && git submodule update --init --recursive" : "" //-----------------------------------------------------------------------------
sshagent (credentials: [gitCredentials]) { def gitCheckout(gitUrl, branchTag, repoDir, hasSubmodules, doUpdatePackageSrcRevisions = true) {
sh(script: "git fetch -ap && git fetch -t") println "checking out git repository ${gitUrl} to ${repoDir}..."
sh(script: "git checkout ${branchTag} && git pull --rebase ${updateSubmodulesCmd}") cloneGitRepoUnlessExisting(gitUrl, repoDir, hasSubmodules)
sh(label: "Temporary task - updating source revisions (sub-repositories)", dir("${repoDir}") {
script: "./scripts/update-source-revisions.sh") gitUpdateSubmodulesCheckoutBranch(branchTag, hasSubmodules, doUpdatePackageSrcRevisions)
} printSubmoduleStatus(hasSubmodules)
if(hasSubmodules) { printLatestGitHistory()
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}"
} }
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------