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}'") } } }