[Jenkins] fatal: could not read Username for ‘Your_Git_Server’: No such file or directory

Error

[Pipeline] powershell
git : key not valid for use in specified state
At D:\Build-Package\JENKIN_AGENTS\JENKIN_AGENTS\workspace\INVEST_APP_NET6@tmp\durable-1b20e676\powershellScript.ps1:3 char:77
+ ...             git ls-remote origin "origin/develop".replace('origin/',' ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (fatal: Unable t...edential store.:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

bash: line 1: /dev/tty: No such device or address
error: failed to execute prompt script (exit code 1)
fatal: could not read Username for 'Your_Git_Server': No such file or directory
[Pipeline] }

Cause

  • Git is trying to ask for a credential (username, password) in the non-interactive environment or in Jenkins Agents from some command such as:
git push --set-upstream origin <<YOUR_BRACH>>

git ls-remote origin <<YOUR_BRACH>>

Solution

  • Store your git credential in Jenkins credentials. You can do this by going to 
    - Manage Jenkins > Credentials > System > Global credentials > Add Credentials.
    - Select “Username with password”
    - Enter your Git username and Git Token (PAT or Access Token) as Password
  • In your Jenkins pipeline, You need to use the withCredentials step
    - SH
    - PowerShell
//Shell Example
withCredentials([usernamePassword(credentialsId: 'git-jenkins', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD')]) {
    sh '''
    git config --global user.name "${GIT_USERNAME}"
    git config --global user.password "${GIT_PASSWORD}"
    git push --set-upstream origin <<YOUR_BRACH>>
    '''
}
//Power Shell Example
withCredentials([usernamePassword(credentialsId: 'git-jenkins', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD')]) {
   def commitId = powershell(script: """ 
                      git config --global user.name "${GIT_USERNAME}"
                      git config --global user.password "${GIT_PASSWORD}"
                      git ls-remote origin "${params.BRANCH_ON_GIT}" | %{ \$_.Split('')[0] }
                      """, returnStdout: true).trim()
} 

Note: from Windows Agents. If you get an error

git : fatal: Unable to persist credentials with the 'wincredman' credential store.

your need to set credentialStore by using this command

git config --global credential.credentialStore dpapi

Reference


Discover more from naiwaen@DebuggingSoft

Subscribe to get the latest posts sent to your email.