Jenkins/vars/utils.groovy

78 lines
2.4 KiB
Groovy
Raw Normal View History

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 sh(script: 'git rev-parse --abbrev-ref HEAD', returnStdout: true).trim().toLowerCase().replaceAll('(_|-| )+', '')
}
String getProjectVersionDefaultShortChannel() {
String channel = getProjectVersionDefaultChannel()
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}' !")
}
}
String getProjectVersionTag(String overrideChannel = '') {
String channel = overrideChannel ? overrideChannel : getProjectVersionDefaultChannel()
String versionTag = sh(script:"echo \$(date +%Y.%-m.%-d)-${channel}.\$(date +%-H%M).\$(git rev-parse --short HEAD)", returnStdout: true).trim()
return versionTag
}
String getProjectVersionShortTag(String overrideShortChannel = '') {
String shortChannel = overrideShortChannel ? overrideShortChannel : getProjectVersionDefaultShortChannel()
String versionTag = sh(script:"echo \$(date +%Y.%-m.%-d)-${shortChannel}.\$(date +%-H%M)", returnStdout: true).trim()
return versionTag
}