Skip to content

Instantly share code, notes, and snippets.

@MarkEWaite
Created October 1, 2019 20:15
Show Gist options
  • Save MarkEWaite/4b8f1672cd16f98639f78cc4c6de5798 to your computer and use it in GitHub Desktop.
Save MarkEWaite/4b8f1672cd16f98639f78cc4c6de5798 to your computer and use it in GitHub Desktop.
import com.cloudbees.jenkins.plugins.sshcredentials.impl.*
import com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey.DirectEntryPrivateKeySource
import com.cloudbees.plugins.credentials.*
import com.cloudbees.plugins.credentials.domains.*
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl
String username = System.getenv('JENKINS_ADMIN_USER') ?: 'adminuser'
String password = System.getenv('JENKINS_ADMIN_PASSWORD') ?: 'adminpassword'
//
// Create adminuser-repo-private-key-id private key credential
//
String privateKeyText = new File('/ssh-keys/adminuser-repo-private-key').text
DirectEntryPrivateKeySource privateKeySource = new DirectEntryPrivateKeySource(privateKeyText)
Credentials privateKeyCredential = new BasicSSHUserPrivateKey(
CredentialsScope.GLOBAL, // scope
'admin-repo-private-key-id', // id
username, // username
privateKeySource, // private key source
'', // passphrase
'Admin user SSH key' // description
)
SystemCredentialsProvider.getInstance().getStore().addCredentials(Domain.global(), privateKeyCredential)
//
// Create admin-username-password-id username/password credential
//
Credentials usernamePasswordCredential = new UsernamePasswordCredentialsImpl(
CredentialsScope.GLOBAL, // scope
'admin-username-password-id', // id
'Admin username/password', // description
username, // username
password // password
)
SystemCredentialsProvider.getInstance().getStore().addCredentials(Domain.global(), usernamePasswordCredential)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment