Perhaps the simplest way of adding the global ssh creddentials is to just select "from a file on master ~/.ssh" and jenkins will automatically scan for is_rsa files in that folder..
but we like to do stuff from scripts.. UI is for lazy folks :):D so in groovy, i was wondering how would i approach it.. well i have to admit there was UI involved in initial help. I went to manage credentials > global credentials> add ssh username private key Option Now i had no idea how to approach to this page using groovy. ofcourse there is a plugin for ssh creds.. that i need to work with :) so here i made use of UI 's help :
- I selected the text "Username" , or you can select "password" ..doesnt matter , we need to know where there params are coming from
- so select the text, right click and select " Inspect element " ..every web developers wet dream :)
- scroll up, open up the divs , and there i saw " com.cloudbees.jenkins.plugins.sshcredentials"
- bam , got it..
- next step , google that long plugin bla bla and add groovy at the end of search string to narrow google results :)
ok at this point i was finger crossed there might be some gentle guy who already did some stuff.. luckily found this awesome awesome post .. https://groups.google.com/forum/#!topic/jenkinsci-users/Ou3k6Mqr3vg this dude was tryna implement through puppet..the guy had almost the solution i needed,hy but he added his problem of why its not perfectly working.. i simply took his code, compared to a puppet cookbook full of groovy scripts , https://github.com/jenkinsci/puppet-jenkins/blob/master/files/puppet_helper.groovy and wrote my own code... this will just select the option "from master ~/.ssh" ..you dont need to pass any key or password .. remember, i passed root, you need to pass your own username of host where jenkins is running.. why my username is root? well im running jenkins in a container which is in privileged mode. all containers in privileged mode have username " root " . i know bad idea to run some CI chicken like jenkins under privileged mode, but meh...there are some dark secrets :/ anyway here is the code ..run it directly or as part of jenkins init HOOK ..watever .. enjoy :)
import jenkins.model.*
import com.cloudbees.plugins.credentials.*
import com.cloudbees.plugins.credentials.common.*
import com.cloudbees.plugins.credentials.domains.*
import com.cloudbees.jenkins.plugins.sshcredentials.impl.*
import hudson.plugins.sshslaves.*;
global_domain = Domain.global()
credentials_store =
Jenkins.instance.getExtensionList(
'com.cloudbees.plugins.credentials.SystemCredentialsProvider'
)[0].getStore()
credentials = new BasicSSHUserPrivateKey(
CredentialsScope.GLOBAL,
null,
"root",
new BasicSSHUserPrivateKey.UsersPrivateKeySource(),
"",
""
)
credentials_store.addCredentials(global_domain, credentials)