# Using withCredentials in FreeStyle Groovy Script

**URL:** <https://community.jenkins.io/t/using-withcredentials-in-freestyle-groovy-script/3051>\
**Category:** Using Jenkins\
**Created:** [July 15, 2022, 8:17pm UTC](https://community.jenkins.io/t/using-withcredentials-in-freestyle-groovy-script/3051 "2022-07-15T20:17:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![philu123](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/philu123/32/1875_2.png) [@philu123](https://community.jenkins.io/u/philu123)\
**Post date:** [July 15, 2022, 8:17pm UTC](https://community.jenkins.io/t/using-withcredentials-in-freestyle-groovy-script/3051/1 "2022-07-15T20:17:37Z")

</div>

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:

```auto
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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![halkeye](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/halkeye/32/8_2.png) [@halkeye](https://community.jenkins.io/u/halkeye)\
**Post date:** [July 15, 2022, 8:56pm UTC](https://community.jenkins.io/t/using-withcredentials-in-freestyle-groovy-script/3051/2 "2022-07-15T20:56:37Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![philu123](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/philu123/32/1875_2.png) [@philu123](https://community.jenkins.io/u/philu123)\
**Post date:** [July 16, 2022, 7:03pm UTC](https://community.jenkins.io/t/using-withcredentials-in-freestyle-groovy-script/3051/3 "2022-07-16T19:03:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mawinter69](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/mawinter69/32/1625_2.png) [@mawinter69](https://community.jenkins.io/u/mawinter69)\
**Post date:** [July 16, 2022, 10:49pm UTC](https://community.jenkins.io/t/using-withcredentials-in-freestyle-groovy-script/3051/4 "2022-07-16T22:49:40Z")

</div>

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](https://javadoc.jenkins.io/plugin/credentials/)

```auto
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()

```

---

<div class="post-metadata">

**Author:** ![themgi](https://avatars.discourse-cdn.com/v4/letter/t/eb8c5e/32.png) [@themgi](https://community.jenkins.io/u/themgi)\
**Post date:** [March 30, 2024, 5:24pm UTC](https://community.jenkins.io/t/using-withcredentials-in-freestyle-groovy-script/3051/5 "2024-03-30T17:24:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![keoren3](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/keoren3/32/8697_2.png) [@keoren3](https://community.jenkins.io/u/keoren3)\
**Post date:** [June 19, 2024, 9:03am UTC](https://community.jenkins.io/t/using-withcredentials-in-freestyle-groovy-script/3051/6 "2024-06-19T09:03:20Z")

</div>

It is model instead of mode - Just tested it on Jenkins version 2.452.2.  
Good thing you mentioned this.
