194 lines
6 KiB
Groovy
194 lines
6 KiB
Groovy
import hudson.plugins.git.*
|
|
import hudson.plugins.git.extensions.*
|
|
import hudson.plugins.git.extensions.impl.*
|
|
|
|
clh = load("${autobuilder.commonscripts()}/commandline/commandline.groovy")
|
|
|
|
String getGitInfo(String workingSetRootPath) {
|
|
dir(workingSetRootPath) {
|
|
return clh.getCommandOutput('git rev-parse HEAD')
|
|
}
|
|
}
|
|
|
|
@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
|
|
"""
|
|
}
|
|
}
|
|
|
|
def gitCheckout(String gitURL, String revision, String targetDir, String repoRootSuffix = "", int cloneDepth = 1, boolean shallowClone = true, ArrayList<String> sparseitemlist = new ArrayList<String>()) {
|
|
checkgitlfsinstall()
|
|
def reporootpath = checkouttoreporoot(gitURL)
|
|
if (!sparseitemlist.isEmpty()) {
|
|
def itemstring = "Items in sparse item list:\n"
|
|
for (item in sparseitemlist) {
|
|
itemstring += item + "\n"
|
|
}
|
|
echo itemstring
|
|
}
|
|
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: [],
|
|
])
|
|
}
|
|
|
|
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
|
|
"""
|
|
} else {
|
|
echo "Not supporting git stripping in Windows at the moment"
|
|
}
|
|
}
|
|
}
|
|
|
|
return this
|