Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set git credentials using credentials parameter

I'm writing a job-dsl seed job. The seed job needs to be able to generate from either github.com or from my companies github enterprise server. I'd like to keep one job rather than have two.

In each case I would like jenkins to be Authenticated. So to do this, I hardcoded the creds into the script. However I'm not satisfied with this. I would prefer to add a Credentials parameter on the seed job.

The problem is, the Creds parameter seems to add en ENV variable to the script that contains USERID/PASSWORD. http://steve-jansen.github.io/blog/2014/12/16/parsing-jenkins-secrets-in-a-shell-script/

However, the git jobdsl seems to want a Credentials ID, not USERID/PASSWORD.

How to resolve this impasse?

scm {
    git {
        remote {
            name('origin')
                url(repo)
                credentials(myCredential)
        }
        branch('master')
    }
}
like image 837
Duane Avatar asked Apr 01 '16 20:04

Duane


People also ask

How do I add credentials to credential manager in Git?

Or finding it via the Control Panel -> Manage Windows Credentials. Go to Windows Credentials -> Generic Credentials. Here your credential should be listed if everything is working correctly. Git should add it by default the first time you log in to a new repository.

How do I authenticate my credentials in Git?

There are three main approaches you can take: Using a personal authentication token or password. Using an SSH key. Using your GitHub password with 2-factor authentication.

How do I change credentials in credential Manager Git?

To update your credentials, go to Control Panel → Credential Manager → Generic Credentials. Find the credentials related to your Git account and edit them to use the updated password. This should be the accepted answer if git config --list contains credential.


2 Answers

A good introduction to the way Job DSL handles credentials, can be found on official wiki page.

Two examples explaining how it's possible to pass user password to Jenkins job:

// use the github-ci-key credentials for authentication with GitHub 
job('example-1') { 
    scm { 
        git { 
            remote { 
                github('account/repo', 'ssh') 
                credentials('github-ci-key') 
            } 
        } 
    } 
} 

// assign the jarsign-keystore credentials to the PASSWORD build variable job('example-2') { 
    wrappers { 
        credentialsBinding { 
            usernamePassword('PASSWORD', 'jarsign-keystore') 
        } 
    } 
}
like image 187
luka5z Avatar answered Dec 30 '22 10:12

luka5z


Adding to the answer made by Duane above, CredentialID is the unique ID of a credential stored in Jenkins. You can also provide your own ID to identify it in a meaningful way.

Jenkins Credentials

Use this ID shown in the image above in your job-dsl script and it should work for you.

Peace.

like image 39
Surendra Deshpande Avatar answered Dec 30 '22 09:12

Surendra Deshpande