Select Page

How to put git credentials in Jenkins pipeline.

fatal: could not read Username for, No such device

There some cases where you need to initialize in git, but cannot use git checkout or other plugins. For example, we have a situation: inside Jenkins pipeline, we running terraform code. Unfortunately, terraform code contains modules that loading source from another repository via ‘https’, not ‘ssh’. In this case, you need to authorize in git. If it was your computer – it’s a pop-up line “Put your credentials”. But in the pipeline, you cannot resolve just sending username and password via arguments. As a result, you’ve got an error: “fatal: could not read Username for, No such device”. So, how to resolve this problem.

For your information, GIT has a credential helper tool, and via credential helper, we can paste our password. Just put next to lines in your pipeline and it resolves your problems.

git config --global credential.username {USERNAME}
git config --global credential.helper "!echo password={PASSWORD}; echo"

You can read more about credential helper there https://git-scm.com/docs/gitcredentials
And this is a stage of pipeline as an example:


stage("Git creds"){
withCredentials(usernamePassword(credentialsId: GIT_CREDS, passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME'){
sh("""
git config --global credential.username {GIT_USERNAME}
git config --global credential.helper "!echo password={GITPASSWORD}; echo"
git clone {your_repository}
""")
}
}
In the console output, you’ll see that USERNAME and PASSWORD shows as secret text, so nobody knows the credentials details.