2

I am trying to connect to github using Jenkins Credential plugin

withCredentials([usernamePassword(credentialsId: gitCredential, passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) {
        sh("git push https://${GIT_USERNAME}:${GIT_PASSWORD}@stash.abc.com:656/rad/abl/optical.git --tags")
    }

I am trying to push into Git, but it is failing with the below error, since the password contains @. Since there is no way we could urlEncode on the password obtained from the Jenkins credential plugin. I am looking for a way to resolve the username and password correctly.

[Tagging] Running shell script
+ git push 'https://****:****@stash.abc.com:656/rad/abl/optical.git' --tags
fatal: unable to access 'https://****:ZxmP*K@v6iO/?w4ms@stash.abc.com:656/rad/abl/optical.git': Couldn't resolve host 'v6iO'
[Pipeline] }
[Pipeline] // withCredentials

Any input would be helpful.

Thanks !

user1993412
  • 672
  • 2
  • 8
  • 22
  • Oh special chars in password of credentials seems to be a common annoying issue. If you set the origin to your push URL and use a git credential helper, does that give you any mileage on it? I haven't tried this so don't feel I can put it as an answer. There are some details here worth looking at: https://alanedwardes.com/blog/posts/git-username-password-environment-variables/ – macg33zr Sep 26 '18 at 23:43
  • Thanks @macg33zr , that certainly did help :) – user1993412 Sep 27 '18 at 21:03

1 Answers1

1

This question was answered in a later question. The answer describes how to use the URLEncoder command to encode the special characters in the password.

In your case it would be something like this:

withCredentials([usernamePassword(credentialsId: gitCredential, passwordVariable: "GIT_PASSWORD", usernameVariable: "GIT_USERNAME")]) {
    script {
        env.URL_ENCODED_GIT_PASSWORD=URLEncoder.encode(GIT_PASSWORD, "UTF-8")
    }
        sh "git push https://${GIT_USERNAME}:${URL_ENCODED_GIT_PASSWORD}@stash.abc.com:656/rad/abl/optical.git --tags"
}

I think there is just one problem with this solution. After you have encoded the password jenkins will output its value without the mask (****). One possible solution to this problem is to disable the command echoing. Another solution that I have seen is to use the MaskPasswords plugin, but I don't know if that works because I wasn't able to test it.

Pedro Pinheiro
  • 866
  • 12
  • 30