Add git.withHTTPCredentials() helper
This commit is contained in:
parent
63bb0dcf08
commit
7ac2319ae1
30
vars/git.groovy
Normal file
30
vars/git.groovy
Normal 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}'")
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user