How to set git credentials using credentials parameter

Duane picture Duane · Apr 1, 2016 · Viewed 8.4k times · Source

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')
    }
}

Answer

luka5z picture luka5z · Apr 3, 2016

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') 
        } 
    } 
}