Jenkins/vars/git.groovy

44 lines
1.4 KiB
Groovy
Raw 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}'")
}
}
}
def withSSHCredentials(String credentialsId, Closure fn) {
def randomUUID = UUID.randomUUID().toString()
withCredentials([
sshUserPrivateKey(
credentialsId: credentialsId,
keyFileVariable: 'GIT_SSH_IDENTITY_FILE',
)
]) {
withEnv(['GIT_SSH_VARIANT=ssh', 'GIT_SSH_COMMAND=ssh -i $GIT_SSH_IDENTITY_FILE -o IdentitiesOnly=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null']) {
fn.call()
}
}
2020-03-20 17:15:15 +01:00
}