gitjenkinsgit-credential-winstore

Git Credential Manager for Windows and credentials in a file


How can I get the Git Credential Manager (GCM) to read credentials from a file for HTTPS Git URLs? We need this in order to facilitate automatic cloning operations for Jenkins jobs.

Background

We have been successfully using the store credential store in Git 1.7 on Windows prior to upgrading to Git for Windows v2.9.0. Now the CGM always asks for credentials causing the Jenkins build to hang.

I notice the GCM docs mention a credential.interactive never setting but how can I tell it which file to read the credentials from? And what format is it expecting in that file?


Solution

  • After asking on the GCM Github issues page it turns out that the GCM does not support reading credentials from a file.

    But my goal is to allow non-interactive population of credentials and it does support programmatically adding credentials to the Windows Credential Store that GCM uses under the hood. By using the bundled libraries (binaries here) I was able to put together a Powershell script that allowed us to add credentials during machine provisioning by Chef :

    Add-Type -Path 'c:\path\to\gcm-v1.4.0\Microsoft.Alm.Authentication.dll'
    
    $credentials = New-Object -TypeName Microsoft.Alm.Authentication.Credential('someuser', 'secret')
    $targetUri = New-Object -TypeName Microsoft.Alm.Authentication.TargetUri('https://git.example.com/projects')
    $namespace = "git"
    $secretStore = New-Object -TypeName Microsoft.Alm.Authentication.SecretStore($namespace, $null, $null, $null)
    
    $foundCredentials = $null
    $secretStore.ReadCredentials($targetUri, [ref] $foundCredentials)
    if ($foundCredentials -ne $null) {
        echo "Credentials already found, not inserting"
    } else {
        echo "Inserting stored credentials"
        $secretStore.WriteCredentials($targetUri, $credentials)
    }
    

    This allows the Jenkins slave to perform Git clones without user interaction.

    Note: You will need to run the Powershell script with the "Unrestricted" execution policy as well as unblock the DLLs included in the GCM otherwise they won't load.