Forked from ivan-pinatti/jenkins-add-ssh-keypair-with-password-credential.groovy
Created
December 11, 2019 04:14
-
-
Save imranansari/1290ebe4fe65bf70ceea3d67b6faeea1 to your computer and use it in GitHub Desktop.
Jenkins - Add SSH keypair with password credential via groovy script - #jenkins #groovy #ssh #credential
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!groovy | |
// imports | |
import com.cloudbees.jenkins.plugins.sshcredentials.impl.* | |
import com.cloudbees.plugins.credentials.* | |
import com.cloudbees.plugins.credentials.common.* | |
import com.cloudbees.plugins.credentials.domains.Domain | |
import com.cloudbees.plugins.credentials.impl.* | |
import hudson.util.Secret | |
import java.nio.file.Files | |
import jenkins.model.Jenkins | |
import net.sf.json.JSONObject | |
import org.jenkinsci.plugins.plaincredentials.impl.* | |
// parameters | |
def jenkinsMasterKeyParameters = [ | |
description: 'Jenkins Master SSH Key', | |
id: 'jenkins-master-key', | |
secret: 'PleaseUseOnePasswordStrongEnough!', | |
userName: '[email protected]', | |
key: new BasicSSHUserPrivateKey.DirectEntryPrivateKeySource('''-----BEGIN RSA PRIVATE KEY----- | |
MIICXQIBAAKBgQCNEn6kcPiFHyJo3cO+KiUZ21kG2ePPnYq0DnX+ZACllJUHA6Fn | |
gbuRK48UVGpcuZ2OPHlDb+fYXBZu5MVewsbqgOO1B0eDX5GV4Fy7qJx8d1BFyvQA | |
KpCc9c/DDUIG5yFhPCGpLCSit4CA8soVH7NeCOk1lzU18mMlWaaMT8HMfQIDAQAB | |
AoGAOiUZVaXKiPPgNuDQwRyV1iZ2d0BviS8h8DzVnViSe6zWD+ILMKJkMN2HR5XT | |
kQxgSDPct1L0eFTcWjCouPoHChhOXCWMOxHxXv+r5Gbt6kRmRr4DOGsz2a2vZwEJ | |
MI4c67olZsLtLAeEG/y9dHP+i7YrGkoMXM2aZZkvANWypvECQQDsnWT1VS+TDFGB | |
UuDQQuCFkF/g3nW4wqKML/hxaoFOJzKYLTf7ja2ovp56Clq0w/VzQXaiKnapXSjN | |
gZoBH5qTAkEAmKFAmqSlUhukuerEyw9VfMHNO5uj28u7skkf8BynJ1mLQtvoCBp5 | |
RyotXzjzOszWlwf+JAYrzOD0+DenizeWrwJBAN7lMlruIX/rpcgm88McjPclVzy1 | |
M76WE5vuAKOOyjp+MGoshsVA5OvGjfG3WVVaGBm3/HKtf9Tx/mMBiLswM2MCQA0H | |
vzc0lTSUTZTduR1I2tiCxx2upOeP1h9bZNGf8JlIaL41ffKrJ+1uaV82wnUjpbJR | |
KV4z9KtSDTffsHsPLNsCQQCvx7SPjGCrZwqeZBM6QBYi3r6q0TXO1avgGhAx6N9b | |
YE4W1Ve8Pwkl0DCb9IaWu8DVthllS6tBSL+KrrfOZIvO | |
-----END RSA PRIVATE KEY-----''') | |
] | |
// get Jenkins instance | |
Jenkins jenkins = Jenkins.getInstance() | |
// get credentials domain | |
def domain = Domain.global() | |
// get credentials store | |
def store = jenkins.getExtensionList('com.cloudbees.plugins.credentials.SystemCredentialsProvider')[0].getStore() | |
// define private key | |
def privateKey = new BasicSSHUserPrivateKey( | |
CredentialsScope.GLOBAL, | |
jenkinsMasterKeyParameters.id, | |
jenkinsMasterKeyParameters.userName, | |
jenkinsMasterKeyParameters.key, | |
jenkinsMasterKeyParameters.secret, | |
jenkinsMasterKeyParameters.description | |
) | |
// add credential to store | |
store.addCredentials(domain, privateKey) | |
// save to disk | |
jenkins.save() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment