Jenkins/vars/utils.groovy

75 lines
2.3 KiB
Groovy

import org.jenkinsci.plugins.pipeline.modeldefinition.Utils
void when(Boolean condition, body) {
Map config = [:]
body.resolveStrategy = Closure.OWNER_FIRST
body.delegate = config
if (condition) {
body()
} else {
Utils.markStageSkippedForConditional(STAGE_NAME)
}
}
@NonCPS
String getBuildUser() {
def build = currentBuild.rawBuild
String buildUser = ''
// On essaie de récupérer l'utilisateur à l'origine de l'exécution du job
try {
def cause = build.getCause(hudson.model.Cause.UserIdCause.class)
buildUser = cause.getUserName()
} catch (Exception ex) {
// On ignore l'erreur
}
if (buildUser == '') {
// Si on a pas réussi à retrouver l'utilisateur, on récupère celui du commit courant
try {
def committerUsername = sh(script: 'git --no-pager show -s --format=\'%ae\' | cut -d\'@\' -f1', returnStdout: true).trim()
buildUser = committerUsername
} catch (Exception ex) {
// On ignore l'erreur
}
}
if (buildUser == '') {
// Par défaut, on considère que jenkins est à l'origine du job
buildUser = 'jenkins'
}
return buildUser
}
String getProjectVersionDefaultChannel() {
return env.BRANCH_NAME.toLowerCase().replaceAll('(_|-| )+', '')
}
String getProjectVersionShortChannel(String channel) {
switch (channel) {
case 'develop':
return 'dev'
case 'testing':
return 'tst'
case 'stable':
return 'stb'
default:
throw new Exception("No rule to compute short channel for '${channel}' !")
}
}
List<String> getProjectVersionTags(String overrideChannel = '') {
String channel = overrideChannel ? overrideChannel : getProjectVersionDefaultChannel()
String shortChannel = getProjectVersionShortChannel(channel)
String dateVersion = sh(script: 'date +%Y.%-m.%-d', returnStdout: true)
String timestamp = sh(script: 'date +%-H%M', returnStdout: true)
String shortCommit = sh(script: 'git rev-parse --short HEAD', returnStdout: true)
String longTag = "${dateVersion}-${channel}.${timestamp}.${shortCommit}"
String shortTag = "${dateVersion}-${shortChannel}.${timestamp}"
return [ longTag, shortTag ]
}