Using withCredentials in FreeStyle Groovy Script

Hi,

I am trying to create a build job supporting different environment targets (I do this by parameterizing the job and using conditional build steps) and i need different credential values for each environment. All the credentials are stored in Jenkins, and I’d like to choose the correct values and associate to common environment variables depending on environment, similar to how I am already doing these:

if ("staging".equals(ContainerType)){
       def map = ["RunProfile": "qa", "RunPort":"","AppBuildType":"stagingprod"]
       return map
}
else if ("prod".equals(ContainerType)){
       def map = ["RunProfile": "prod", "RUN_PORT":"","AppBuildType":"stagingprod"]
       return map
} else if ("qa".equals(ContainerType)){

I was hoping to use withCredentials to a credential to a variable depending on environment, or some equivalent, but I cannot get withCredentials to work at all in a Groovy Script in a job or in the Script Console.

The following code always returns the same error:

withCredentials([string(credentialsId: 'MY_DEV', variable: 'DEV_PASS'), string(credentialsId: 'MY_QA', variable: 'QA_PASS')]) {
}

Error:
groovy.lang.MissingMethodException: No signature of method: Script1.script() is applicable for argument types: (Script1$_run_closure1) values: [Script1$_run_closure1@1907f7b]

I cannot find any reference to missing imports or other considerations that would allow this code to run. Any suggestions would be appreciated as well as thoughts on alternative approaches.

What is a freestyle groovy script? Is it a freestyle job with a groovy buildstep? or some other plugin?

withCredentials is a pipeline syntax, it really won’t work outside of pipeline. It’ll depend on a lot of other things.

You can use credentials directly in groovy console, but you’d have to use the java apis.

Why not just make a pipeline and have all the tools you want to use?

1 Like

Thanks Gavin. Yes, Freestyle job with a Groovy Script. To use the Java APIs in the groovy console, do I need to add any imports? Is there a reference where I can find the credentials APIs?

Thanks for your help.

Following snippet will allow you to get a user/password credential from inside a system groovy script.
We use this code in one of our Freestyle jobs.
The imports are required inside Jobs.

See Credentials Plugin 1139.veb_9579fca_33b_ API

import com.cloudbees.plugins.credentials.CredentialsMatchers
import com.cloudbees.plugins.credentials.CredentialsProvider
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials
import com.cloudbees.plugins.credentials.domains.DomainRequirement
import jenkins.mode.Jenkins
import hudson.security.ACL

jenkins = Jenkins.get()
def lookupSystemCredentials = { credentialsId ->
  return CredentialsMatchers.firstOrNull(
    CredentialsProvider
    .lookupCredentials(StandardUsernameCredentials.class, jenkins, ACL.SYSTEM,
    Collections.<DomainRequirement>emptyList()),
    CredentialsMatchers.withId(credentialsId)
  )
}

credential = lookupSystemCredentials("credentialId")
user = credential.getUsername()
password= credential.getPassword().getPlainText()
1 Like

is it import jenkins.mode.Jenkins or import jenkins.model.Jenkins?