457 lines
17 KiB
Groovy
457 lines
17 KiB
Groovy
gh = load("${autobuilder.commonscripts()}/versioncontrol/git.groovy")
|
|
|
|
markunstable = false
|
|
unstablereason = ""
|
|
|
|
summaryfolder = "${WORKSPACE}/summary"
|
|
summaryfile = "${summaryfolder}/summary.txt"
|
|
summarystring = ""
|
|
repoandtagsfile = "${summaryfolder}/repoandtags.txt"
|
|
repoandtagsstring = ""
|
|
releaserepofolder = "${WORKSPACE}/releaserepos"
|
|
sourcerepofolder = "${WORKSPACE}/sourcerepos"
|
|
tempfolder = "${WORKSPACE}/temp"
|
|
gitrevparseoutputfile = "${tempfolder}/gitrevparse.out"
|
|
|
|
metascript = autobuilder.getmetafolderscript()
|
|
|
|
def resetfolders() {
|
|
dir(summaryfolder) {
|
|
deleteDir()
|
|
}
|
|
dir(releaserepofolder) {
|
|
deleteDir()
|
|
}
|
|
dir(tempfolder) {
|
|
deleteDir()
|
|
}
|
|
}
|
|
|
|
def preconditions() {
|
|
markunstable = false
|
|
unstablereason = ""
|
|
summarystring = ""
|
|
writeFile(file: summaryfile, text: "Release summary: \n")
|
|
repoandtagsstring = ""
|
|
writeFile(file: repoandtagsfile, text: "Repository summary: \n")
|
|
}
|
|
|
|
def setunstable(String reason) {
|
|
markunstable = true
|
|
unstablereason += reason + "\n"
|
|
echo "Marked build as unstable: Reason: ${reason}"
|
|
}
|
|
|
|
def summaryappend(String summary) {
|
|
def String currentsummarycontents = readFile(file: summaryfile)
|
|
writeFile(file: summaryfile, text: currentsummarycontents + summary)
|
|
summarystring += summary
|
|
}
|
|
|
|
def repoandtagsappend(String summary) {
|
|
def String repoandtagscontents = readFile(file: repoandtagsfile)
|
|
writeFile(file: repoandtagsfile, text: repoandtagscontents + summary)
|
|
repoandtagsstring += summary
|
|
}
|
|
|
|
def getpreviousreleasemeta(buildmeta, String checkoutfolder) {
|
|
def previousreleasemeta = buildmeta.getNewMetaObject(this)
|
|
// See if there is meta data file from the current release
|
|
def String releasemetafolderpath = "${checkoutfolder}/${buildmeta.folder()}"
|
|
if (fileExists(releasemetafolderpath)) {
|
|
previousreleasemeta = metascript.metafromfolder(checkoutfolder)
|
|
}
|
|
return previousreleasemeta
|
|
}
|
|
|
|
def checkoutreleaserepo(buildmeta, String checkoutfolder) {
|
|
def String releaserepo = buildmeta.releaseinfo.releaserepoURL
|
|
// Get the latest head revision of the release repo without checking out working copy; Check out to a folder unique to component; Extract any meta
|
|
repoandtagsappend(" Release repo: ${releaserepo}")
|
|
def previousreleasemeta = buildmeta.getNewMetaObject(this)
|
|
dir(releaserepofolder) {
|
|
sh("git clone ${releaserepo} ${checkoutfolder}")
|
|
}
|
|
previousreleasemeta = getpreviousreleasemeta(buildmeta, checkoutfolder)
|
|
dir(checkoutfolder) {
|
|
// Clear all existing files - Note that this does not remove .git*
|
|
sh("git rm -r *")
|
|
}
|
|
return previousreleasemeta
|
|
}
|
|
|
|
def checkoutsourcerepo(buildmeta, String checkoutfolder) {
|
|
def String sourcerepo = buildmeta.gitinfo.remoteurl
|
|
if (sourcerepo) {
|
|
repoandtagsappend(" Source repo: ${sourcerepo}")
|
|
// Get the latest revision of the source repo without checking out working copy; Check out to a folder unique to component
|
|
if (fileExists(checkoutfolder)) {
|
|
dir(checkoutfolder) {
|
|
sh("""
|
|
git reset --hard
|
|
git clean -fdx
|
|
git pull
|
|
# Remove all tags that are not already on the remote
|
|
git tag -l | xargs git tag -d >/dev/null
|
|
git fetch -t >/dev/null
|
|
"""
|
|
)
|
|
}
|
|
}
|
|
else {
|
|
dir(sourcerepofolder) {
|
|
sh("git clone ${sourcerepo} ${checkoutfolder}")
|
|
}
|
|
}
|
|
} else {
|
|
repoandtagsappend(" Source repo: None")
|
|
}
|
|
}
|
|
|
|
def boolean getcomponentartifacts(toplevelbuildmeta, buildinfo, String repofolder, String tempfolder) {
|
|
// Copy relevant artifacts to temp folder
|
|
summaryappend("Artifacts: ")
|
|
def boolean result = true
|
|
for (artifactfilter in buildinfo.metainfo.releaseinfo.releaseitems) {
|
|
def filterpath = artifactfilter
|
|
if (buildinfo.accumulatedpath) {
|
|
filterpath = buildinfo.accumulatedpath + '/' + artifactfilter
|
|
}
|
|
// Copy relevant artifacts to temp folder
|
|
echo "Copying artifacts: Filter path: ${filterpath} to ${tempfolder} from job: ${toplevelbuildmeta.jenkinsbuildinfo.buildjobname} #${toplevelbuildmeta.jenkinsbuildinfo.buildnumber}"
|
|
try {
|
|
copyArtifacts(
|
|
filter: filterpath,
|
|
projectName: toplevelbuildmeta.jenkinsbuildinfo.buildjobname,
|
|
selector: [
|
|
$class: 'ParameterizedBuildSelector',
|
|
parameterName: "<SpecificBuildSelector plugin='copyartifact@1.46.2'><buildNumber>${toplevelbuildmeta.jenkinsbuildinfo.buildnumber}</buildNumber></SpecificBuildSelector>"
|
|
],
|
|
target: tempfolder
|
|
)
|
|
} catch (Exception e) {
|
|
echo "Exception occurred while copying artifacts from ${toplevelbuildmeta.jenkinsbuildinfo.buildjobname}: ${e.getMessage()}"
|
|
result = false
|
|
summaryappend("Does not exist:")
|
|
}
|
|
if (result) {
|
|
// Move relevant artifacts from temp folder to release position
|
|
def originalrootdir = tempfolder + "/" + buildinfo.accumulatedpath
|
|
dir(originalrootdir) {
|
|
sh("mv -v * ${repofolder}")
|
|
}
|
|
}
|
|
summaryappend("${artifactfilter},")
|
|
}
|
|
return result
|
|
}
|
|
|
|
def releasegitadd() {
|
|
// Add anything new
|
|
sh("git add .")
|
|
summaryappend("Add,")
|
|
}
|
|
|
|
def releasegitreset() {
|
|
// Add anything new
|
|
sh("git reset --hard")
|
|
sh("git clean -fdx")
|
|
summaryappend("Reset,")
|
|
}
|
|
|
|
def releasegitcommit(String buildstamp) {
|
|
sh("git commit -a -m ${buildstamp}")
|
|
summaryappend("Commit,")
|
|
}
|
|
|
|
// Determine if there has been a change by comparing the sha code in previous release meta and the build meta
|
|
def boolean checkforchange(buildmeta, previousreleasemeta) {
|
|
String buildsha = buildmeta.gitinfo.gitrevision
|
|
String previoussha = previousreleasemeta.gitinfo.gitrevision
|
|
|
|
def boolean shachanged = (buildsha != previoussha)
|
|
echo "buildsha: ${buildsha} previoussha: ${previoussha} changed: ${shachanged}"
|
|
|
|
String buildstamp = buildmeta.buildstampinfo.buildstamp
|
|
String previousbuildstamp = previousreleasemeta.buildstampinfo.buildstamp
|
|
|
|
def boolean buildstampchanged = (buildstamp != previousbuildstamp)
|
|
echo "buildstamp: ${buildstamp} previousbuildstamp: ${previousbuildstamp} changed: ${buildstampchanged}"
|
|
|
|
def boolean changed = (shachanged | buildstampchanged)
|
|
echo "changed: ${changed}"
|
|
|
|
return changed
|
|
}
|
|
|
|
def ArrayList<String> maketaglist(buildmeta, String buildstamp, String otherreleasetags) {
|
|
def ArrayList<String> taglist = new ArrayList<String>()
|
|
taglist.add(buildstamp)
|
|
|
|
def ArrayList<String> othertaglist = otherreleasetags.split(',')
|
|
for (othertagitem in othertaglist) {
|
|
if (othertagitem?.trim()) {
|
|
def componentothertagitem = "${othertagitem.trim()}_${buildmeta.productinfo.productname}_${buildmeta.productinfo.subcomponentname}"
|
|
taglist.add(componentothertagitem)
|
|
}
|
|
}
|
|
return taglist
|
|
}
|
|
|
|
def String releasegitgetshacode(String tag) {
|
|
def existingshacode = ""
|
|
// Check if tag exists already
|
|
def int gitrevparsestatus = sh(script: "git rev-parse \"${tag}\" >\"${gitrevparseoutputfile}\" 2>/dev/null", returnStatus: true)
|
|
if (gitrevparsestatus == 0) { // Tag exists
|
|
existingshacode = readFile(file: gitrevparseoutputfile).trim()
|
|
}
|
|
return existingshacode
|
|
}
|
|
|
|
def boolean releasegittag(String postcommitshacode, String tag) {
|
|
def boolean tagadded = false
|
|
// Check if tag exists already
|
|
def String existingshacode = releasegitgetshacode(tag)
|
|
if (existingshacode) { // Tag exists
|
|
if (existingshacode != postcommitshacode) { // If the existing tag is not associated with the one expected
|
|
// This is a problem - The tag is already at another position in git repo
|
|
setunstable("The release tag ${tag} has been used at another commit other than one expected - You must decide what to do with it - Not good practice to delete it or move it (Existing sha code: ${existingshacode} Post commit sha code: ${postcommitshacode})")
|
|
summaryappend("(${tag} - Could not add!)")
|
|
} else {
|
|
summaryappend("(${tag} - Already added)")
|
|
}
|
|
} else { // If tag does not exist already
|
|
// Tag
|
|
sh("git tag \"${tag}\"")
|
|
summaryappend("(${tag})")
|
|
tagadded = true
|
|
}
|
|
return tagadded
|
|
}
|
|
|
|
def boolean releasegittags(String postcommitshacode, ArrayList<String> taglist) {
|
|
boolean tagadded = false
|
|
|
|
summaryappend("Rel Tag ")
|
|
for (tag in taglist) {
|
|
if (releasegittag(postcommitshacode, tag)) {
|
|
echo "Tag added: ${tag} with sha: ${postcommitshacode}"
|
|
tagadded = true
|
|
}
|
|
}
|
|
return tagadded
|
|
}
|
|
|
|
def boolean sourcegittag(String shacode, String tag) {
|
|
def boolean tagadded = false
|
|
// Check if tag exists already
|
|
def int gitrevparsestatus = sh(script: "git rev-parse \"${tag}\" >\"${gitrevparseoutputfile}\" 2>/dev/null", returnStatus: true)
|
|
if (gitrevparsestatus == 0) { // Tag exists
|
|
def existingshacode = readFile(file: gitrevparseoutputfile).trim()
|
|
if (existingshacode != shacode) { // If the existing tag is not associated with HEAD
|
|
// This is a problem - The tag is already at another position in git repo
|
|
setunstable("The source tag ${tag} has been used at another commit (${existingshacode}) - You must decide what to do with it - Not good practice to delete it or move it (Existing sha code: ${existingshacode} Post commit sha code: ${shacode})")
|
|
summaryappend("(${tag} - Could not add!)")
|
|
}
|
|
else {
|
|
summaryappend("(${tag} - Already added)")
|
|
}
|
|
}
|
|
else { // If tag does not exist already
|
|
// Tag
|
|
sh("git tag \"${tag}\" ${shacode}")
|
|
summaryappend("(${tag})")
|
|
tagadded = true
|
|
}
|
|
return tagadded
|
|
}
|
|
|
|
def sourcegittags(String buildsha, List taglist) {
|
|
boolean tagadded = false
|
|
|
|
summaryappend(",Src Tag ")
|
|
for (tag in taglist) {
|
|
if (sourcegittag(buildsha, tag)) {
|
|
tagadded = true
|
|
}
|
|
}
|
|
return tagadded
|
|
}
|
|
|
|
def releasegitpush(String dryrunpush ) {
|
|
sh("git remote -v")
|
|
if (dryrunpush == "true") {
|
|
sh("git push -n --tags origin master")
|
|
summaryappend(",Dry-Run-Push")
|
|
setunstable("The artifact(s) have not been pushed/saved (\"Dry run push\" parameter/option is selected)")
|
|
}
|
|
else {
|
|
sh("git push --tags origin master")
|
|
summaryappend(",Push")
|
|
}
|
|
}
|
|
|
|
def sourcegitpush(String dryrunpush ) {
|
|
sh("git remote -v")
|
|
if (dryrunpush == "true") {
|
|
sh("git push -n --tags")
|
|
summaryappend(",Src-Dry-Run-Push")
|
|
setunstable("The source tag(s) have not been pushed/saved (\"Dry run push\" parameter/option is selected)")
|
|
}
|
|
else {
|
|
sh("git push --tags")
|
|
summaryappend(",Src Push")
|
|
}
|
|
}
|
|
|
|
def boolean isalreadyreleased(buildmeta, tag) {
|
|
def boolean alreadyreleased = buildmeta.releaseinfo.released
|
|
if (alreadyreleased) {
|
|
echo "Component is already marked as released: ${alreadyreleased}"
|
|
} else {
|
|
String commitshacode = releasegitgetshacode(tag)
|
|
if (commitshacode) {
|
|
echo "Tag: ${tag} already exists in this repo therefore marking as already released"
|
|
alreadyreleased = true
|
|
} else {
|
|
alreadyreleased = false
|
|
}
|
|
}
|
|
return alreadyreleased
|
|
}
|
|
|
|
def gitrelease(buildmeta, previousreleasemeta, String releaserepo, String sourcerepo, String otherreleasetags, String dryrunpush) {
|
|
summaryappend(" Git operations: ")
|
|
def String buildstamptag = buildmeta.releasetag()
|
|
|
|
dir(releaserepo) {
|
|
def String precommitshacode = sh(script: "git rev-parse --verify HEAD", returnStdout: true).trim()
|
|
echo "Pre commit sha code: ${precommitshacode}"
|
|
|
|
def boolean alreadyreleased = isalreadyreleased(buildmeta, buildstamptag)
|
|
|
|
def String postcommitshacode
|
|
def boolean changed = false
|
|
if (alreadyreleased) {
|
|
summaryappend("Already-released,")
|
|
postcommitshacode = releasegitgetshacode(buildstamptag)
|
|
echo "Previous release sha code: ${postcommitshacode}"
|
|
} else {
|
|
releasegitadd()
|
|
|
|
changed = checkforchange(buildmeta, previousreleasemeta)
|
|
if (changed) {
|
|
releasegitcommit(buildstamptag)
|
|
} else {
|
|
releasegitreset()
|
|
summaryappend("No-commit,")
|
|
}
|
|
|
|
postcommitshacode = sh(script: "git rev-parse --verify HEAD", returnStdout: true).trim()
|
|
echo "Post commit sha code: ${postcommitshacode}"
|
|
}
|
|
|
|
ArrayList<String> taglist = maketaglist(buildmeta, buildstamptag, otherreleasetags)
|
|
def tagadded = releasegittags(postcommitshacode, taglist)
|
|
|
|
if (changed || tagadded) {
|
|
releasegitpush(dryrunpush)
|
|
}
|
|
}
|
|
dir(sourcerepo) {
|
|
def String srcbuildstamptag = buildmeta.globalreleasetag()
|
|
def String buildsha = buildmeta.gitinfo.gitrevision
|
|
|
|
if (buildsha) {
|
|
def ArrayList<String> srctaglist = maketaglist(buildmeta, srcbuildstamptag, otherreleasetags)
|
|
def tagadded = sourcegittags(buildsha, srctaglist)
|
|
if (tagadded) {
|
|
sourcegitpush(dryrunpush)
|
|
}
|
|
} else {
|
|
summaryappend(",No Src ")
|
|
}
|
|
}
|
|
}
|
|
|
|
def releasecomponent(toplevelbuildmeta, buildinfo, String otherreleasetags, String dryrunpush) {
|
|
def buildmeta = buildinfo.metainfo
|
|
def uriconverted = gh.uniquereporootname(buildmeta.gitinfo.remoteurl)
|
|
def String uniquefolder = "${buildmeta.productinfo.productname}_${buildmeta.productinfo.subcomponentname}_${uriconverted}"
|
|
def String uniquefolderforrepo = "${releaserepofolder}/${uniquefolder}"
|
|
def String uniquefoldertemp = "${tempfolder}/${uniquefolder}"
|
|
def String uniquefolderforsourcerepo = "${sourcerepofolder}/${uniquefolder}"
|
|
|
|
summaryappend("${buildmeta.productinfo.productname} ${buildmeta.productinfo.subcomponentname}: ")
|
|
repoandtagsappend("${buildmeta.productinfo.productname} ${buildmeta.productinfo.subcomponentname}: ")
|
|
def previousreleasemeta = checkoutreleaserepo(buildmeta, uniquefolderforrepo)
|
|
|
|
checkoutsourcerepo(buildmeta, uniquefolderforsourcerepo)
|
|
|
|
if (getcomponentartifacts(toplevelbuildmeta, buildinfo, uniquefolderforrepo, uniquefoldertemp)) {
|
|
gitrelease(buildmeta, previousreleasemeta, uniquefolderforrepo, uniquefolderforsourcerepo, otherreleasetags, dryrunpush)
|
|
}
|
|
|
|
summaryappend("\n")
|
|
repoandtagsappend("\n")
|
|
}
|
|
|
|
def releasehierarchy(String buildfolder, String metafolder, buildmeta, String maxdepth, String otherreleasetags, String dryrunpush) {
|
|
def int md = maxdepth.toInteger()
|
|
resetfolders()
|
|
preconditions()
|
|
|
|
// Walk the meta information to a max depth, looking for components to release
|
|
def componentReleaseInfo = buildmeta.releasewalkerunique(md, buildfolder, metafolder)
|
|
echo "Unique components:"
|
|
def String versioncomponent = ""
|
|
for (key in componentReleaseInfo.keySet()) {
|
|
versioncomponent += "${key}\n"
|
|
}
|
|
echo versioncomponent
|
|
|
|
// Walk the meta information to a max depth, looking for components to release
|
|
def componentReleaseInfoVersionUnique = buildmeta.releasewalkerversionunique(md, buildfolder, metafolder)
|
|
def String versionuniquecomponent = ''
|
|
def String versionwithpathscomponent = ''
|
|
for (key in componentReleaseInfoVersionUnique.keySet()) {
|
|
versionuniquecomponent += "${key}\n"
|
|
versionwithpathscomponent += "${key}: path: ${componentReleaseInfoVersionUnique[key].accumulatedpath} meta path: ${componentReleaseInfoVersionUnique[key].accumulatedmetapath}\n"
|
|
}
|
|
echo 'Version unique components:'
|
|
echo versionuniquecomponent
|
|
|
|
echo 'Version unique components with paths:'
|
|
echo versionwithpathscomponent
|
|
|
|
if (DO_INTEGRITY_CHECK == "true") {
|
|
if (componentReleaseInfo.size() != componentReleaseInfoVersionUnique.size()) {
|
|
error "There are 2 or more versions of the same component being used in this hierarchy: Is this correct? (If you ignore this issue, you will get an error much later because the system cannot tag 2 different commits of the associated source code with the same label)"
|
|
}
|
|
}
|
|
|
|
for (key in componentReleaseInfo.keySet()) {
|
|
def buildinfo = componentReleaseInfo[key]
|
|
if (!buildinfo.metainfo.jenkinsbuildinfo.isFullBuild()) {
|
|
error "The subcomponent: ${buildinfo.componentidentifier()} is not a full build (Some artifacts are missing)"
|
|
}
|
|
}
|
|
|
|
def toplevelid = buildmeta.componentidentifier()
|
|
// For each component
|
|
for (key in componentReleaseInfo.keySet()) {
|
|
def buildinfo = componentReleaseInfo[key]
|
|
if (buildinfo.metainfo.componentidentifier() != '___') {
|
|
if (toplevelid != key) {
|
|
releasecomponent(buildmeta, buildinfo, otherreleasetags, dryrunpush)
|
|
}
|
|
}
|
|
}
|
|
// Do the top level last
|
|
def toplevelbuildinfo = componentReleaseInfo[toplevelid]
|
|
releasecomponent(buildmeta, toplevelbuildinfo, otherreleasetags, dryrunpush)
|
|
|
|
return [summarystring, markunstable, unstablereason]
|
|
}
|
|
|
|
return this;
|