Created
May 21, 2018 14:49
-
-
Save ivan-pinatti/830ec918781060df03b12efd4a14096e to your computer and use it in GitHub Desktop.
Jenkins - Add Username with password credential via groovy script - #jenkins #groovy #username #password #credential #usernameWithPassword
This file contains 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.plugins.credentials.* | |
import com.cloudbees.plugins.credentials.domains.Domain | |
import com.cloudbees.plugins.credentials.impl.* | |
import hudson.util.Secret | |
import jenkins.model.Jenkins | |
// parameters | |
def jenkinsKeyUsernameWithPasswordParameters = [ | |
description: 'Description here', | |
id: 'key-id-here', | |
secret: '12345678901234567890', | |
userName: 'your-username-here' | |
] | |
// 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 Bitbucket secret | |
def jenkinsKeyUsernameWithPassword = new UsernamePasswordCredentialsImpl( | |
CredentialsScope.GLOBAL, | |
jenkinsKeyUsernameWithPasswordParameters.id, | |
jenkinsKeyUsernameWithPasswordParameters.description, | |
jenkinsKeyUsernameWithPasswordParameters.userName, | |
jenkinsKeyUsernameWithPasswordParameters.secret | |
) | |
// add credential to store | |
store.addCredentials(domain, jenkinsKeyUsernameWithPassword) | |
// save to disk | |
jenkins.save() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please check this other one @anandasubedi , I believe this is what are you referring to;
https://gist.github.com/ivan-pinatti/de063b610d1bdf2da229c7874968f4d9