2022-10-17 22:34:17 +02:00
|
|
|
import java.util.regex.Matcher
|
|
|
|
|
2020-03-19 09:45:07 +01:00
|
|
|
// 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 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
|
2021-09-09 15:45:34 +02:00
|
|
|
def gitCredentialsType = params.gitCredentialsType ? params.gitCredentialsType : 'http'
|
2020-03-19 09:45:07 +01:00
|
|
|
def gitEmail = params.gitEmail ? params.gitEmail : 'jenkins@cadoles.com'
|
|
|
|
def gitUsername = params.gitUsername ? params.gitUsername : 'Jenkins'
|
2020-03-24 10:00:48 +01:00
|
|
|
def skipCi = params.containsKey('skipCi') ? params.skipCi : false
|
2022-10-17 22:34:17 +02:00
|
|
|
def skipPush = params.containsKey('skipPush') ? params.skipPush : true
|
2020-03-19 09:45:07 +01:00
|
|
|
|
|
|
|
// 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
|
2022-10-17 22:34:17 +02:00
|
|
|
sh('git fetch --all')
|
2020-03-19 09:45:07 +01:00
|
|
|
|
|
|
|
// Merge currentRef into distBranch and push
|
|
|
|
sh("git checkout -b '${distBranch}' 'origin/${distBranch}'")
|
2020-03-24 10:36:07 +01:00
|
|
|
|
|
|
|
// Add git username/email
|
|
|
|
sh("git config user.email '${gitEmail}'")
|
|
|
|
sh("git config user.username '${gitUsername}'")
|
|
|
|
|
2020-03-19 09:45:07 +01:00
|
|
|
sh("git merge ${currentRef}")
|
|
|
|
|
|
|
|
if (!skipPush) {
|
2022-10-17 22:34:17 +02:00
|
|
|
sh('git push')
|
2020-03-19 09:45:07 +01:00
|
|
|
} else {
|
|
|
|
println("Skipping push. Set skipPush param to 'true' to enable remote repository update.")
|
|
|
|
}
|
2022-09-08 10:57:30 +02:00
|
|
|
|
2020-03-19 09:45:07 +01:00
|
|
|
// 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,
|
|
|
|
)
|
|
|
|
|
2020-03-24 10:52:32 +01:00
|
|
|
result['previousTag'] = lastTag.trim()
|
2020-03-19 09:45:07 +01:00
|
|
|
|
2020-03-24 10:52:32 +01:00
|
|
|
println("Last tag is '${result['previousTag']}'")
|
2020-03-19 09:45:07 +01:00
|
|
|
|
|
|
|
// Extract version number from last tag
|
2020-03-24 10:52:32 +01:00
|
|
|
def lastVersionNumber = lastTag.split('/').last().trim()
|
2020-03-19 09:45:07 +01:00
|
|
|
result['previousVersionNumber'] = lastVersionNumber
|
|
|
|
|
|
|
|
println("Last version number is '${lastVersionNumber}'")
|
|
|
|
|
2022-10-17 22:34:17 +02:00
|
|
|
String versionRoot = extractVersionRoot(lastVersionNumber)
|
|
|
|
String versionNumber = ''
|
|
|
|
|
|
|
|
if (versionRoot) {
|
|
|
|
versionNumber = versionRoot
|
|
|
|
} else {
|
|
|
|
versionNumber = sh(
|
|
|
|
script: "git describe --always ${currentRef}",
|
|
|
|
returnStdout: true,
|
|
|
|
).split('/').last().trim()
|
2022-09-08 10:57:30 +02:00
|
|
|
|
2022-10-17 22:34:17 +02:00
|
|
|
Boolean isCommitRef = !versionNumber.matches(/^[0-9]+\.[0-9]+\.[0-9]+.*$/)
|
2022-09-08 10:57:30 +02:00
|
|
|
|
2022-10-17 22:34:17 +02:00
|
|
|
if (isCommitRef) {
|
|
|
|
versionNumber = "0.0.0-${versionNumber}"
|
|
|
|
}
|
2022-09-08 10:57:30 +02:00
|
|
|
}
|
2020-03-19 09:45:07 +01:00
|
|
|
|
2022-10-26 14:48:36 +02:00
|
|
|
versionNumber = "${versionNumber}-${env.BUILD_NUMBER}"
|
2022-09-12 18:37:32 +02:00
|
|
|
|
2020-03-19 09:45:07 +01:00
|
|
|
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
|
|
|
|
|
2022-10-17 22:34:17 +02:00
|
|
|
def tagComment = "Build ${versionNumber} ${distRepo} package for ${dist}-${distVersion}."
|
2020-03-19 09:45:07 +01:00
|
|
|
if (skipCi) {
|
|
|
|
tagComment += ' [ci skip]'
|
|
|
|
}
|
|
|
|
|
2022-09-12 18:18:22 +02:00
|
|
|
sh("git tag -f -a '${tag}' -m '${tagComment}'")
|
2020-03-19 09:45:07 +01:00
|
|
|
|
|
|
|
// Push tag
|
|
|
|
if (!skipPush) {
|
2022-10-17 22:34:17 +02:00
|
|
|
sh('git push --tags -f')
|
2020-03-19 09:45:07 +01:00
|
|
|
} 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) {
|
2021-09-09 15:45:34 +02:00
|
|
|
if (gitCredentialsType == 'http') {
|
|
|
|
git.withHTTPCredentials(gitCredentials) {
|
|
|
|
proc.call()
|
|
|
|
}
|
|
|
|
} else if (gitCredentialsType == 'ssh') {
|
|
|
|
git.withSSHCredentials(gitCredentials) {
|
|
|
|
proc.call()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new Exception("Unknown git credentials type '${gitCredentialsType}' ! Expected 'ssh' or 'http' (default).")
|
2020-03-19 09:45:07 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
proc.call()
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
2022-10-17 22:34:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@NonCPS
|
|
|
|
String extractVersionRoot(String fullVersion) {
|
|
|
|
Matcher fullVersionMatcher = fullVersion =~ /^([0-9]+\.[0-9]+\.[0-9]+).*$/
|
|
|
|
|
|
|
|
if (!fullVersionMatcher.matches()) {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return fullVersionMatcher.group(1)
|
|
|
|
}
|