This script helps create a global credential in jenkins from a key source that is located in jenkins master.
You can change the name of key to whatever key exists on master.
Run this script either:
i) Jenkins-> manage Jenkins -> script console
ii) add to init.groovy to make it run during jenkins start
alright heres the script(here i assume my key already exists on master as /root/.ssh/id_rsa
:
import com.cloudbees.plugins.credentials.*
import com.cloudbees.plugins.credentials.common.*
import com.cloudbees.plugins.credentials.domains.*
import com.cloudbees.jenkins.plugins.sshcredentials.impl.*
String keyfile = "/root/.ssh/id_rsa"
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.FileOnMasterPrivateKeySource(keyfile),
"",
"")
credentials_store.addCredentials(global_domain, credentials)
After you run through script console, the result should look like:
Result: true
cheers :)