fatal could not read Username for 'https://git.xxxxxx.com' terminal prompts disabled
On a Jenkins pipeline, a necessary step is to clone a Git repository before executing any commands. Handling Git credentials when accessing non-public repositories can be a bit tricky. This post addresses one error you may encounter.
Error
fatal: could not read Username for 'https://git.xxxxxx.com': terminal prompts disabled
This error message can appear when setting up a Go project (and potentially in other scenarios as well). It occurs when the dependency download process encounters a private Git repository. The go get
command attempts to clone the repository, prompting for the username and password that Git needs to access it. However, go get
"disables the terminal prompt by default" (as mentioned in the official documentation on direct access to private modules). The sequence of events leading to this error is, therefore, as follows:
- You try to set up a Go project.
- You install the project dependencies.
- One of the dependencies is from a private Git repository.
- The
go get
command tries to clone the repository. - The Git command requires a username and password to access the private repository.
- No mechanism is in place to provide the username and password.
- The terminal prompt is disabled, so Git cannot prompt for the username and password.
- The error message is displayed.
Solution
You might think you can simply enable the terminal prompt by setting the GIT_TERMINAL_PROMPT
environment variable to 1
. However, this is unhelpful, as you likely don’t want to remote into the machine to manually enter the username and password in the now-visible Git credentials prompt. A typical solution is to set up "gitaskpass", allowing Git to invoke a user-defined script whenever credentials are needed. According to the Git documentation:
Without any credential helpers defined, Git will try the following strategies to ask the user for usernames and passwords:
- If the GIT_ASKPASS environment variable is set, the program specified by the variable is invoked. A suitable prompt is provided to the program on the command line, and the user’s input is read from its standard output.
- Otherwise, if the core.askPass configuration variable is set, its value is used as above.
- Otherwise, if the SSH_ASKPASS environment variable is set, its value is used as above.
- Otherwise, the user is prompted on the terminal.
The reason why a UI prompt may appear when enabling the terminal prompt is that you might already have a Git credential helper set up when installing Git. This credential helper typically requires a one-time manual input of the username and password, after which credentials are stored in the system's credential store (or cache). This is why you may not have encountered the prompt before/that often.
To fix the error, you can set up a credential helper or configure your askpass program. The simplest solution when using Jenkins is to use the withCredentials
helper, which provides a Git credentials binding that essentially sets up Git ask pass for you (as described in this post). Here is an example of how to use it in a Jenkins pipeline:
withCredentials([gitUsernamePassword(credentialsId: 'my-credentials-id', gitToolName: 'git')]) {
setupGoProject()
}
Note that you will need to configure the credentials in Jenkins before using them in the pipeline. The my-credentials-id
refers to the ID of the credentials you set up in Jenkins (see this documentation). Whenever you need to execute a function that requires Git credentials, you can wrap it in the withCredentials
block. This ensures that the credentials are available to the function.
Conclusion
To address Jenkins errors related to Git credentials, consider using the gitUsernamePassword
binding of the Jenkins Credentials Binding Plugin.