Add git.withHTTPCredentials() helper

This commit is contained in:
wpetit 2020-03-20 17:15:15 +01:00
parent 63bb0dcf08
commit 7ac2319ae1

30
vars/git.groovy Normal file
View File

@ -0,0 +1,30 @@
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}'")
}
}
}