371 lines
13 KiB
Groovy
371 lines
13 KiB
Groovy
import hudson.plugins.git.*
|
|
import hudson.plugins.git.extensions.*
|
|
import hudson.plugins.git.extensions.impl.*
|
|
|
|
clh = load("${autobuilder.commonscripts()}/commandline/commandline.groovy")
|
|
|
|
def boolean isShallowClonedRepo(String gitrootpath) {
|
|
def boolean isshallowclone = true
|
|
|
|
dir( gitrootpath) {
|
|
if (isUnix()) {
|
|
isshallowclone = sh(returnStdout: true, script: "git rev-parse --is-shallow-repository").trim().toBoolean()
|
|
}
|
|
else {
|
|
isshallowclone = bat(returnStdout: true, script: "@git rev-parse --is-shallow-repository").trim().toBoolean()
|
|
}
|
|
echo "Repo at ${gitrootpath} is shallow cloned: ${isshallowclone}"
|
|
}
|
|
return isshallowclone
|
|
}
|
|
|
|
String getGitInfo(String workingSetRootPath) {
|
|
dir(workingSetRootPath) {
|
|
return clh.getCommandOutput('git rev-parse HEAD')
|
|
}
|
|
}
|
|
|
|
List<String> gettaglist(String workingSetRootPath) {
|
|
dir(workingSetRootPath) {
|
|
return clh.getCommandOutput('git tag --sort=-version:refname')
|
|
}
|
|
}
|
|
|
|
@NonCPS
|
|
def ArrayList<SparseCheckoutPath> sparsecheckoutconfig(ArrayList<String> paths) {
|
|
ArrayList<SparseCheckoutPath> sparsecheckoutpaths = new ArrayList<SparseCheckoutPath>()
|
|
for (path in paths) {
|
|
sparsecheckoutpaths.add(new SparseCheckoutPath(path))
|
|
}
|
|
return sparsecheckoutpaths
|
|
}
|
|
|
|
def String uniquereporootname(String gitURL) {
|
|
def uniquename = gitURL.replaceAll("[^A-Za-z0-9]", "_")
|
|
echo "Unique reporoot name for URL: ${gitURL} is: ${uniquename}"
|
|
return uniquename
|
|
}
|
|
|
|
// The REPO_ROOT is a folder where a cache of git checkouts can be kept - each subfolder name is unique to a particular git repo
|
|
def String checkouttoreporoot(String gitURL) {
|
|
def String reporootsubfolder
|
|
def String reporootenv = env.REPO_ROOT
|
|
if (!reporootenv) {
|
|
reporootsubfolder = ""
|
|
echo "To improve Git performance: Define the REPO_ROOT enviroment to a path where a cache of git repositories can be placed on this slave"
|
|
} else {
|
|
def reporootname = uniquereporootname(gitURL)
|
|
reporootsubfolder = "${reporootenv}/${reporootname}"
|
|
|
|
if (!fileExists(reporootsubfolder)) {
|
|
echo "Checking out ${gitURL} to ${reporootsubfolder}"
|
|
checkout([
|
|
$class: 'GitSCM',
|
|
userRemoteConfigs: [
|
|
[url: gitURL]
|
|
],
|
|
branches: [
|
|
[name: 'origin/master']
|
|
],
|
|
doGenerateSubmoduleConfigurations: false,
|
|
extensions: [
|
|
[$class: 'GitLFSPull'],
|
|
[$class: 'CleanBeforeCheckout'],
|
|
[$class: 'CheckoutOption', timeout: 180],
|
|
[$class: 'RelativeTargetDirectory', relativeTargetDir: reporootsubfolder],
|
|
],
|
|
submoduleCfg: [],
|
|
])
|
|
} else {
|
|
echo "Found ${gitURL} checked out at ${reporootsubfolder} ... checking status"
|
|
dir(reporootsubfolder) {
|
|
if (isUnix()) {
|
|
sh """
|
|
git status
|
|
if [ \$? -ne 0 ]; then exit 1; fi
|
|
"""
|
|
} else {
|
|
bat """
|
|
git status
|
|
if errorlevel 1 EXIT /b 1
|
|
"""
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return reporootsubfolder
|
|
}
|
|
|
|
def checkgitlfsinstall() {
|
|
if (isUnix()) {
|
|
sh """
|
|
git lfs install
|
|
if [ \$? -ne 0 ]; then exit 1; fi
|
|
"""
|
|
|
|
} else {
|
|
bat """
|
|
git lfs install
|
|
if errorlevel 1 EXIT /b 1
|
|
"""
|
|
}
|
|
}
|
|
|
|
//No credentials specified
|
|
//Cloning the remote Git repository
|
|
//Cloning repository git@hydra:print-server-releases/print_server_whole.git
|
|
//> git init /home/jenkins_user/jenkins/jobs/Print_Server_publish_to_customer/ws/whole # timeout=10
|
|
//Using reference repository: /home/jenkins_user/reporoot/git_hydra_print_server_releases_print_server_whole_git
|
|
//Fetching upstream changes from git@hydra:print-server-releases/print_server_whole.git
|
|
// > git --version # timeout=10
|
|
// > git fetch --tags --progress git@hydra:print-server-releases/print_server_whole.git +refs/heads/*:refs/remotes/origin/* # timeout=120
|
|
// > git config remote.origin.url git@hydra:print-server-releases/print_server_whole.git # timeout=10
|
|
// > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
|
|
// > git config remote.origin.url git@hydra:print-server-releases/print_server_whole.git # timeout=10
|
|
// Cleaning workspace
|
|
// > git rev-parse --verify HEAD # timeout=10
|
|
// No valid HEAD. Skipping the resetting
|
|
// > git clean -fdx # timeout=10
|
|
// Fetching upstream changes from git@hydra:print-server-releases/print_server_whole.git
|
|
// > git fetch --tags --progress git@hydra:print-server-releases/print_server_whole.git +refs/heads/*:refs/remotes/origin/* # timeout=120
|
|
// > git rev-parse refs/tags/2.10180.7389.21125_7^{commit} # timeout=10
|
|
// > git rev-parse refs/remotes/origin/refs/tags/2.10180.7389.21125_7^{commit} # timeout=10
|
|
// Checking out Revision 64cac35e9a7603c060135f61f91b3f809bbdf160 (refs/tags/2.10180.7389.21125_7)
|
|
// Enabling Git LFS pull
|
|
// > git config core.sparsecheckout # timeout=10
|
|
// > git checkout -f 64cac35e9a7603c060135f61f91b3f809bbdf160 # timeout=120
|
|
// > git config --get remote.origin.url # timeout=10
|
|
// > git lfs pull origin # timeout=120
|
|
// Commit message: "2.10180.7389.21125_7"
|
|
// > git rev-list --no-walk 64cac35e9a7603c060135f61f91b3f809bbdf160 # timeout=10
|
|
|
|
|
|
// > git rev-parse --is-inside-work-tree # timeout=10
|
|
// Fetching changes from the remote Git repository
|
|
// > git config remote.origin.url git@hydra:releases/print-server-installer.git # timeout=10
|
|
// Cleaning workspace
|
|
// > git rev-parse --verify HEAD # timeout=10
|
|
// Resetting working tree
|
|
// > git reset --hard # timeout=10
|
|
// > git clean -fdx # timeout=10
|
|
// Using shallow fetch with depth 1
|
|
// Fetching upstream changes from git@hydra:releases/print-server-installer.git
|
|
// > git --version # timeout=10
|
|
// > git fetch --tags --progress git@hydra:releases/print-server-installer.git +refs/heads/*:refs/remotes/origin/* --depth=1 # timeout=120
|
|
// > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
|
|
// > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
|
|
// Checking out Revision d0788d22622862e441c5995a22629846573c39f3 (refs/remotes/origin/master)
|
|
// Enabling Git LFS pull
|
|
// > git config core.sparsecheckout # timeout=10
|
|
// > git read-tree -mu HEAD # timeout=10
|
|
// > git checkout -f d0788d22622862e441c5995a22629846573c39f3 # timeout=120
|
|
// > git config --get remote.origin.url # timeout=10
|
|
// > git lfs pull origin # timeout=120
|
|
// Commit message: "2.10180.7389.21125_3"
|
|
// > git rev-list --no-walk d0788d22622862e441c5995a22629846573c39f3 # timeout=10
|
|
|
|
def String lfsinstallandreporoot(String gitURL) {
|
|
checkgitlfsinstall()
|
|
return checkouttoreporoot(gitURL)
|
|
}
|
|
|
|
def showsparseitemlist(ArrayList<String> sparseitemlist = new ArrayList<String>()) {
|
|
if (!sparseitemlist.isEmpty()) {
|
|
def itemstring = "Items in sparse item list:\n"
|
|
for (item in sparseitemlist) {
|
|
itemstring += item + "\n"
|
|
}
|
|
echo itemstring
|
|
}
|
|
}
|
|
|
|
def docheckout(String gitURL, String revision, int cloneDepth, String reporootpath, boolean shallowClone, String targetDir, sparseitemlist) {
|
|
checkout([
|
|
$class: 'GitSCM',
|
|
userRemoteConfigs: [
|
|
[url: gitURL]
|
|
],
|
|
branches: [
|
|
[name: revision]
|
|
],
|
|
doGenerateSubmoduleConfigurations: false,
|
|
extensions: [
|
|
[$class: 'GitLFSPull'],
|
|
[$class: 'CleanBeforeCheckout'],
|
|
[$class: 'CloneOption', depth: cloneDepth, noTags: false, reference: reporootpath, shallow: shallowClone, timeout: 120],
|
|
[$class: 'CheckoutOption', timeout: 120],
|
|
[$class: 'RelativeTargetDirectory', relativeTargetDir: targetDir],
|
|
[$class: 'SparseCheckoutPaths', sparseCheckoutPaths: sparsecheckoutconfig(sparseitemlist)],
|
|
],
|
|
submoduleCfg: [],
|
|
])
|
|
}
|
|
|
|
// gitCheckout (below) cannot handle sparse checkout and lfs at the moment - see https://issues.jenkins-ci.org/browse/JENKINS-43427 - see gitCheckoutAlt()
|
|
def gitCheckout(String gitURL, String revision, String targetDir, String repoRootSuffix = "", int cloneDepth = 1, boolean shallowClone = true, ArrayList<String> sparseitemlist = new ArrayList<String>()) {
|
|
def String reporootpath = lfsinstallandreporoot(gitURL)
|
|
showsparseitemlist(sparseitemlist)
|
|
docheckout(gitURL, revision, cloneDepth, reporootpath, shallowClone, targetDir, sparseitemlist)
|
|
}
|
|
|
|
def gitinit(String targetDir, String reporootpath) {
|
|
if (!fileExists("${targetDir}/.git")) { // Git folder does not already exist
|
|
echo "git init ${targetDir}"
|
|
if (isUnix()) {
|
|
sh """
|
|
git init "${targetDir}"
|
|
"""
|
|
} else {
|
|
bat """
|
|
git init "${targetDir}"
|
|
"""
|
|
}
|
|
}
|
|
echo "Writing ${reporootpath}/.git/objects to ${targetDir}/.git/objects/info/alternates"
|
|
dir("${targetDir}/.git/objects/info") {
|
|
writeFile(file: 'alternates', text: "${reporootpath}/.git/objects")
|
|
}
|
|
}
|
|
|
|
def setlfsfetchinclude(String targetDir, ArrayList<String> sparseitemlist) {
|
|
def String sparseitemliststring = ""
|
|
if (!sparseitemlist.isEmpty()) {
|
|
for (item in sparseitemlist) {
|
|
sparseitemliststring += "${item},"
|
|
}
|
|
echo "git config lfs.fetchinclude ${sparseitemliststring}"
|
|
dir(targetDir) {
|
|
if (isUnix()) {
|
|
sh """
|
|
git config lfs.fetchinclude "${sparseitemliststring}"
|
|
"""
|
|
} else {
|
|
bat """
|
|
git config lfs.fetchinclude "${sparseitemliststring}"
|
|
"""
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
def gitsparserestore(String targetDir) {
|
|
echo "Clean out full fetch; Force a sparse fetch"
|
|
dir(targetDir) {
|
|
if (isUnix()) {
|
|
sh """
|
|
git rm -rf . > /dev/null
|
|
git clean -fxd > /dev/null
|
|
git reset --hard > /dev/null
|
|
"""
|
|
} else {
|
|
bat """
|
|
git rm -rf . 2> nul
|
|
git clean -fxd 2> nul
|
|
git reset --hard 2> nul
|
|
"""
|
|
}
|
|
}
|
|
}
|
|
|
|
def List convertSparseItemsString(String itemstopublish) {
|
|
def List sparsepaths = []
|
|
|
|
boolean nonempty = itemstopublish?.trim()
|
|
if (nonempty) {
|
|
def String[] items = itemstopublish.trim().split(',')
|
|
def String allpaths = ""
|
|
for(String item : items) {
|
|
sparsepaths.add(item)
|
|
allpaths += "${item}\n"
|
|
}
|
|
echo "Selected item to publish:\n${allpaths}"
|
|
}
|
|
return sparsepaths
|
|
}
|
|
|
|
def gitCheckoutAlt(String gitURL, String revision, String targetDir, String repoRootSuffix = "", int cloneDepth = 1, boolean shallowClone = true, ArrayList<String> sparseitemlist = new ArrayList<String>()) {
|
|
echo "Performing alternative checkout"
|
|
def String reporootpath = lfsinstallandreporoot(gitURL)
|
|
showsparseitemlist(sparseitemlist)
|
|
if (!sparseitemlist.isEmpty()) {
|
|
gitinit(targetDir, reporootpath)
|
|
setlfsfetchinclude(targetDir, sparseitemlist)
|
|
}
|
|
docheckout(gitURL, revision, cloneDepth, reporootpath, shallowClone, targetDir, sparseitemlist)
|
|
if (!sparseitemlist.isEmpty()) {
|
|
gitsparserestore(targetDir)
|
|
}
|
|
}
|
|
|
|
def checkRepoExists(String gitURL) {
|
|
def int result = 0
|
|
if (isUnix()) {
|
|
result = sh( script: """
|
|
git ls-remote "${gitURL}" CHECK_GIT_REMOTE_URL_REACHABILITY
|
|
""",
|
|
returnStatus: true
|
|
)
|
|
} else {
|
|
result = bat( script: """
|
|
@git ls-remote "${gitURL}" CHECK_GIT_REMOTE_URL_REACHABILITY
|
|
""",
|
|
returnStatus: true
|
|
)
|
|
}
|
|
echo "git ls-remote return result: ${result}"
|
|
if (result > 0) {
|
|
error("The release repo: ${gitURL} cannot be found. Please ensure this exists before building")
|
|
} else {
|
|
echo "The release repo: ${gitURL} exists"
|
|
}
|
|
}
|
|
|
|
String getSHAFromVersion(String versionFolder) {
|
|
dir (versionFolder) {
|
|
srcRevision = readFile("gitrevision.txt").trim()
|
|
}
|
|
return srcRevision
|
|
}
|
|
|
|
boolean checkAllVersionsAgree(java.util.LinkedHashMap versions) {
|
|
def keys = versions.keySet()
|
|
if (keys.size() < 1) {
|
|
return false
|
|
}
|
|
if (keys.size() == 1) {
|
|
return true
|
|
}
|
|
else {
|
|
version = ""
|
|
for (key in keys) {
|
|
nextversion = versions.get(key)
|
|
if (version == "") {
|
|
version = nextversion
|
|
}
|
|
if (nextversion != version) {
|
|
return false
|
|
}
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
def stripgithistory(String rootfolder) {
|
|
echo "Stripping git history out of ${rootfolder}"
|
|
dir(rootfolder) {
|
|
if (isUnix()) {
|
|
sh """#!/bin/bash
|
|
pwd
|
|
find . -type d -name ".git" | xargs -iXX rm -rf XX
|
|
find . -name ".gitignore" | xargs -iXX rm -rf XX
|
|
find . -name ".gitmodules" | xargs -iXX rm -rf XX
|
|
find . -iname ".gitattributes" | xargs -iXX rm -rf XX
|
|
find . -iname "*@tmp" | xargs -iXX rm -rf XX
|
|
"""
|
|
} else {
|
|
echo "Not supporting git stripping in Windows at the moment"
|
|
}
|
|
}
|
|
}
|
|
|
|
return this
|