Jenkins/vars/cpkg.groovy

141 lines
5.1 KiB
Groovy
Raw Normal View History

// Basic port of https://forge.cadoles.com/Cadoles/cpkg
def call(Map params = [:]) {
def currentRef = sh(script: 'git rev-parse HEAD', returnStdout: true).trim()
def baseRef = params.baseRef ? params.baseRef : currentRef
def distRepo = params.distRepo ? params.distRepo : 'dev'
def dist = params.dist ? params.dist : 'eole'
def distVersion = params.distVersion ? params.distVersion : '2.7.0'
def distBranchName = params.distBranchName ? params.distBranchName : env.GIT_BRANCH
def gitCredentials = params.gitCredentials ? params.gitCredentials : null
def gitEmail = params.gitEmail ? params.gitEmail : 'jenkins@cadoles.com'
def gitUsername = params.gitUsername ? params.gitUsername : 'Jenkins'
def skipCi = params.skipCi ? params.skipCi : false
def skipPush = params.skipPush ? params.skipPush: true
// Define dist branch based on provided informations and base branch name
def distBranch = "dist/${dist}/${distVersion}/${distBranchName}"
def result = [:]
result['distBranch'] = distBranch
result['distVersion'] = distVersion
result['distRepo'] = distRepo
result['distBranchName'] = distBranchName
def proc = {
// Add distBranch to fetched refs
sh("git config --add remote.origin.fetch +refs/heads/${distBranch}:refs/remotes/origin/${distBranch}")
// Update branches
sh("git fetch --all")
// Merge currentRef into distBranch and push
sh("git checkout -b '${distBranch}' 'origin/${distBranch}'")
sh("git merge ${currentRef}")
if (!skipPush) {
sh("git push")
} else {
println("Skipping push. Set skipPush param to 'true' to enable remote repository update.")
}
// Retrieve last tag matching pattern pkg/${distRepo}/${dist}-${distVersion}/*
def lastTag = sh(
script: "git tag -l 'pkg/${distRepo}/${dist}-${distVersion}/*' --sort=v:refname | tail -n 1",
returnStdout: true,
)
result['previousTag'] = lastTag
println("Last tag is '${lastTag}'")
// Extract version number from last tag
def lastVersionNumber = lastTag.split('/').last()
result['previousVersionNumber'] = lastVersionNumber
println("Last version number is '${lastVersionNumber}'")
def versionNumber = incrementVersionNumber(lastVersionNumber)
println("New version number will be '${versionNumber}'")
result['newVersionNumber'] = versionNumber
// Generate tag with incremented version number
def tag = "pkg/${distRepo}/${dist}-${distVersion}/${versionNumber}"
result['newTag'] = tag
def tagComment="Build ${versionNumber} ${distRepo} package for ${dist}-${distVersion}."
if (skipCi) {
tagComment += ' [ci skip]'
}
sh("git tag -a '${tag}' -m '${tagComment}'")
// Push tag
if (!skipPush) {
sh("git push --tags")
} else {
println("Skipping push. Set skipPush param to 'true' to enable remote repository update.")
}
// Switch back to previous ref
sh("git checkout ${currentRef}")
}
if (gitCredentials != null) {
withCredentials([
usernamePassword(
credentialsId: gitCredentials,
usernameVariable: 'GIT_USERNAME',
passwordVariable: 'GIT_PASSWORD'
)
]) {
def randomUUID = UUID.randomUUID().toString()
def tmpAskPassScript = pwd(tmp:true) + "/${randomUUID}"
try {
writeFile(
file: tmpAskPassScript,
text: '''
#!/bin/sh
case "$1" in
Username*) echo $GIT_USERNAME ;;
Password*) echo $GIT_PASSWORD ;;
esac
'''
)
sh(script: "git config user.email '${gitEmail}'")
sh(script: "git config user.name '${gitUsername}'")
sh(script: "chmod +x '${tmpAskPassScript}'")
withEnv(["GIT_ASKPASS=${tmpAskPassScript}"]) {
proc.call()
}
} finally {
sh(script: "rm -f '${tmpAskPassScript}'")
}
}
} else {
proc.call()
}
return result
}
def incrementVersionNumber(String versionNumber) {
// Split versionNumber (typical pattern: <major>.<minor>.<patch>)
def versionNumberParts = versionNumber.split(/\./)
// Extract path number
def patchNumber = versionNumberParts.last()
// Split patch number (typical pattern: <patch>-<build>)
def patchNumberParts = patchNumber.split('-')
// If version number matches pattern <major>.<minor>.<patch>-<build>
if (patchNumberParts.size() > 1) {
return versionNumberParts[0..-2].join('.') + '.' + patchNumberParts[0..-2].join('-') + '-' + (patchNumberParts.last().toInteger() + 1)
} else { // Else version number matches pattern <major>.<minor>.<patch>
return versionNumberParts[0..-2].join('.') + '.' + (patchNumber.toInteger() + 1)
}
}