Getting credentials and other parameters into Jenkins' ActiveChoice plugin

36 Views Asked by At

The Active Choices Jenkins plugin (https://plugins.jenkins.io/uno-choice/) allows someone to write a groovy script to return some dynamic options.

My script is something like this:

static String getDockerHubToken(String username, String password) {
  return ...;
}

static List getDockerHubTags(String jwtToken) {
  return ...;
}

def token = getDockerHubToken('myusername', 'mypassword')
def tagNames = getDockerHubTags(token)
return tagNames

(The implementation follows the pattern in Jenkins: Active Choices Parameter + Groovy to build a list based on REST responde)

So the question is:

  • How can I get the username and password into this script from Jenkins credentials (instead of hard-coding them into the script, as I did above)?

Thanks!

1

There are 1 best solutions below

1
Dave Cherkassky On BEST ANSWER

As M B wrote above, the way to do this is to:

At the end, my code is:

import com.cloudbees.plugins.credentials.*
import com.cloudbees.plugins.credentials.common.*
import jenkins.model.*

static String getDockerHubToken(String username, String password) {
  return ...;
}

static List getDockerHubTags(String jwtToken) {
  return ...;
}

def instance = jenkins.model.Jenkins.getInstance();
def dockerLogin = CredentialsProvider.lookupCredentials(StandardUsernameCredentials.class, instance, null, null).find {
    it.id == 'my-docker-login-credentials-id'
}
def token = getDockerHubToken(dockerLogin.username, dockerLogin.password.getPlainText())
def tagNames = getDockerHubTags(token)

return tagNames