Jenkins/vars/git.groovy

30 lines
939 B
Groovy
Raw Permalink Normal View History

2020-03-20 17:15:15 +01:00
def withHTTPCredentials(String credentialsId, Closure fn) {
withCredentials([
usernamePassword(
credentialsId: credentialsId,
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: "chmod +x '${tmpAskPassScript}'")
withEnv(["GIT_ASKPASS=${tmpAskPassScript}"]) {
fn.call()
}
} finally {
sh(script: "rm -f '${tmpAskPassScript}'")
}
}
}