From 7ac2319ae1804a17126019a0e148c36d7fe5fb42 Mon Sep 17 00:00:00 2001 From: William Petit Date: Fri, 20 Mar 2020 17:15:15 +0100 Subject: [PATCH] Add git.withHTTPCredentials() helper --- vars/git.groovy | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 vars/git.groovy diff --git a/vars/git.groovy b/vars/git.groovy new file mode 100644 index 0000000..461cd15 --- /dev/null +++ b/vars/git.groovy @@ -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}'") + } + } +} \ No newline at end of file