381 lines
13 KiB
Groovy
381 lines
13 KiB
Groovy
markunstable = false
|
|
unstablereason = ""
|
|
|
|
summaryfolder = "${WORKSPACE}/summary"
|
|
summaryfile = "${summaryfolder}/summary.txt"
|
|
summarystring = ""
|
|
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")
|
|
}
|
|
|
|
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 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
|
|
def previousreleasemeta = buildmeta.getNewMetaObject(this)
|
|
dir(releaserepofolder) {
|
|
sh("git clone --depth 1 ${releaserepo} ${checkoutfolder}")
|
|
}
|
|
previousreleasemeta = getpreviousreleasemeta(buildmeta, checkoutfolder)
|
|
dir(checkoutfolder) {
|
|
// Clear all existing files
|
|
sh("git rm -r *")
|
|
}
|
|
return previousreleasemeta
|
|
}
|
|
|
|
def checkoutsourcerepo(buildmeta, String checkoutfolder) {
|
|
def String sourcerepo = buildmeta.gitinfo.remoteurl
|
|
if (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}")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
def getcomponentartifacts(buildinfo, String repofolder, String tempfolder) {
|
|
// Copy relevant artifacts to temp folder
|
|
summaryappend("Artifacts: ")
|
|
for (artifactfilter in buildinfo.meta.releaseinfo.releaseitems) {
|
|
def filterpath = artifactfilter
|
|
if (buildinfo.offsetpath) {
|
|
filterpath = buildinfo.offsetpath + '/' + artifactfilter
|
|
}
|
|
// Copy relevant artifacts to temp folder
|
|
echo "Copying artifacts: ${filterpath} to ${tempfolder}"
|
|
copyArtifacts(
|
|
filter: filterpath,
|
|
projectName: BUILD_JOB,
|
|
selector: [
|
|
$class: 'ParameterizedBuildSelector',
|
|
parameterName: BUILD_JOB_SELECTOR
|
|
],
|
|
target: tempfolder
|
|
)
|
|
// Move relevant artifacts from temp folder to release position
|
|
def originalrootdir = tempfolder + "/" + buildinfo.offsetpath
|
|
dir(originalrootdir) {
|
|
sh("mv * ${repofolder}")
|
|
}
|
|
summaryappend("${artifactfilter},")
|
|
}
|
|
}
|
|
|
|
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 boolean releasegittag(String postcommitshacode, 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 != postcommitshacode) { // 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 tag ${tag} has been used at another commit other than HEAD - You must decide what to do with it - Not good practice to delete it or move it")
|
|
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)) {
|
|
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")
|
|
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 ) {
|
|
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 ) {
|
|
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 gitrelease(buildmeta, previousreleasemeta, String releaserepo, String sourcerepo, String otherreleasetags, String dryrunpush) {
|
|
summaryappend(" Git operations: ")
|
|
def String buildstamp = buildmeta.buildstampinfo.buildstamp
|
|
def String buildstamptag = "${buildstamp}_${buildmeta.jenkinsbuildinfo.buildnumber}"
|
|
|
|
dir(releaserepo) {
|
|
def String precommitshacode = sh(script: "git rev-parse --verify HEAD", returnStdout: true).trim()
|
|
echo "Pre commit sha code: ${precommitshacode}"
|
|
|
|
releasegitadd()
|
|
|
|
def boolean changed = checkforchange(buildmeta, previousreleasemeta)
|
|
if (changed) {
|
|
releasegitcommit(buildstamptag)
|
|
}
|
|
else {
|
|
releasegitreset()
|
|
summaryappend("No-commit,")
|
|
}
|
|
|
|
def String 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.productinfo.productname}_${buildmeta.productinfo.subcomponentname}_${buildstamptag}"
|
|
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(buildinfo, String otherreleasetags, String dryrunpush) {
|
|
def buildmeta = buildinfo.meta
|
|
def String uniquefolder = "${buildmeta.productinfo.productname}_${buildmeta.productinfo.subcomponentname}"
|
|
def String uniquefolderforrepo = "${releaserepofolder}/${uniquefolder}"
|
|
def String uniquefoldertemp = "${tempfolder}/${uniquefolder}"
|
|
def String uniquefolderforsourcerepo = "${sourcerepofolder}/${uniquefolder}"
|
|
|
|
summaryappend("${buildmeta.productinfo.productname} ${buildmeta.productinfo.subcomponentname}: ")
|
|
def previousreleasemeta = checkoutreleaserepo(buildmeta, uniquefolderforrepo)
|
|
|
|
checkoutsourcerepo(buildmeta, uniquefolderforsourcerepo)
|
|
|
|
getcomponentartifacts(buildinfo, uniquefolderforrepo, uniquefoldertemp)
|
|
|
|
gitrelease(buildmeta, previousreleasemeta, uniquefolderforrepo, uniquefolderforsourcerepo, otherreleasetags, dryrunpush)
|
|
|
|
summaryappend("\n")
|
|
}
|
|
|
|
def releasehierarchy(buildmeta, String maxdepth, String otherreleasetags, String dryrunpush) {
|
|
int md = maxdepth.toInteger()
|
|
resetfolders()
|
|
preconditions()
|
|
|
|
// Walk the meta information to a max depth, looking for components to release
|
|
def componentReleaseInfo = buildmeta.releasewalkerunique(md)
|
|
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)
|
|
echo "Version unique components:"
|
|
def String versionuniquecomponent = ""
|
|
for (key in componentReleaseInfoVersionUnique.keySet()) {
|
|
versionuniquecomponent += "${key}\n"
|
|
}
|
|
echo versionuniquecomponent
|
|
|
|
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?"
|
|
}
|
|
}
|
|
|
|
def toplevelid = buildmeta.componentidentifier()
|
|
// For each component
|
|
for (key in componentReleaseInfo.keySet()) {
|
|
def buildinfo = componentReleaseInfo[key]
|
|
if (toplevelid != key) {
|
|
releasecomponent(buildinfo, otherreleasetags, dryrunpush)
|
|
}
|
|
}
|
|
// Do the top level last
|
|
def toplevelbuildinfo = componentReleaseInfo[toplevelid]
|
|
releasecomponent(toplevelbuildinfo, otherreleasetags, dryrunpush)
|
|
|
|
return [summarystring, markunstable, unstablereason]
|
|
}
|
|
|
|
return this;
|