If your git password contains special characters such as "%", ":", "@", or "/", passing ${env.GIT_PASSWORD}
as part of the git url ie https://${env.GIT_USERNAME}:${env.GIT_PASSWORD}@<REPO>
without doing any encoding is likely to result in an Invalid username or password
error.
To save any hassle using an inline credential.helper is a better way to go however the suggestion of !echo password=\$GIT_PASSWORD; echo'
will result in a warning in your build logs warning: invalid credential line: get
as the credential.helper is passed an argument to indicate the required operation (get,store,erase). In this case the credential helper is trying to interpret the get
operation as a credential input. Valid inputs are protocol,host,path,username,password,url. See https://git-scm.com/docs/git-credential#IOFMT
A better inline credential.helper would be !f() { echo password=\$GIT_PASSWORD; }; f
This way the credential.helper operation get
is ignored.
Full example:
try {
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'MyID', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD']]) {
sh("${git} config credential.username ${env.GIT_USERNAME}")
sh("${git} config credential.helper '!f() { echo password=\$GIT_PASSWORD; }; f'")
sh("GIT_ASKPASS=true ${git} push origin --tags")
}
} finally {
sh("${git} config --unset credential.username")
sh("${git} config --unset credential.helper")
}