My colleague pointed me in. The trick is to iterate over all CredentialsProvider and filter by id:
@Field def privateToken = CredentialsProvider .lookupCredentials(StandardCredentials, (Item) null, ACL.SYSTEM) .find { it.id == "gitlab-api" } .apiToken Am Freitag, 15. März 2019 12:37:32 UTC+1 schrieb Sandra Parsick: > > Hello, > > I'm currently programming a Seed Job that requests via REST API a list of > repositories from a Gitlab instance. I want to iterate this list and create > a pipeline job for each repository. My idea is to use Gitlab plugin and > Credential Plugin for storing the API token and the Job DSL plugin for > creating this bunch of jobs. At the moment, my seed job has only one build > step "Process Job DSLs". In this step, I call following DSL script > > import groovy.transform.Field > import com.cloudbees.plugins.credentials.CredentialsProvider > import com.dabsquared.gitlabjenkins.connection.GitLabApiTokenImpl > > @Field > def gitUrl = 'https://mygitlab.instance/api/v4' > @Field > def privateToken = CredentialsProvider.lookupCredentials( > GitLabApiTokenImpl.class).apiToken > > // Reading projects from GitLab REST API > def projectList = new URL( > "${gitUrl}/projects/?private_token=${privateToken}") > def projects = new groovy.json.JsonSlurper().parse(projectList.newReader > ()) > > Unfortunately, the job build fails with > > Started by user admin <http://192.168.33.10:8083/user/admin> > Running as SYSTEM > Building on master in workspace /opt/jenkins/workspace/Seed Job > Processing DSL script seed.groovy > ERROR: startup failed: > seed.groovy: 3: unable to resolve class > com.dabsquared.gitlabjenkins.connection.GitLabApiTokenImpl > @ line 3, column 1. > import com.dabsquared.gitlabjenkins.connection.GitLabApiTokenImpl > ^ > > 1 error > > Finished: FAILURE > > > When I run following code snippet in Jenkins Script Console, I get the > expected result, the API Token. > > import com.cloudbees.plugins.credentials.CredentialsProvider > import com.dabsquared.gitlabjenkins.connection.GitLabApiTokenImpl > > CredentialsProvider.lookupCredentials(GitLabApiTokenImpl.class).apiToken > > Have anyone an idea what I'm doing wrong? > > The setup of my Jenkins is > - Jenkins version: 2.168 > - Installed plugins with version: > > jdk-tool 1.2 > script-security 1.54 > command-launcher 1.3 > structs 1.17 > workflow-step-api 2.19 > bouncycastle-api 2.17 > scm-api 2.3.0 > workflow-api 2.33 > apache-httpcomponents-client-4-api 4.5.5-3.0 > display-url-api 2.3.0 > mailer 1.23 > workflow-basic-steps 2.14 > momentjs 1.1.1 > simple-theme-plugin 0.5.1 > credentials 2.1.18 > pipeline-milestone-step 1.3.1 > workflow-support 3.2 > workflow-job 2.32 > cloudbees-folder 6.7 > branch-api 2.1.2 > ssh-credentials 1.15 > token-macro 2.7 > config-file-provider 3.5 > jacoco 3.0.4 > junit 1.27 > matrix-project 1.14 > pipeline-build-step 2.7 > pipeline-maven 3.6.7 > authentication-tokens 1.3 > workflow-scm-step 2.7 > ace-editor 1.1 > jquery-detached 1.2.1 > workflow-cps 2.64 > plain-credentials 1.5 > credentials-binding 1.18 > jackson2-api 2.9.8 > pipeline-model-api 1.3.6 > pipeline-model-extensions 1.3.6 > pipeline-model-declarative-agent 1.1.1 > jsch 0.1.55 > git-client 2.7.6 > git-server 1.7 > workflow-cps-global-lib 2.13 > durable-task 1.29 > workflow-durable-task-step 2.29 > workflow-multibranch 2.21 > docker-commons 1.13 > docker-workflow 1.17 > pipeline-input-step 2.9 > pipeline-stage-step 2.3 > pipeline-stage-tags-metadata 1.3.6 > pipeline-model-definition 1.3.6 > ssh-slaves 1.29.4 > configuration-as-code 1.7 > job-dsl 1.72 > configuration-as-code-support 1.7 > pipeline-graph-analysis 1.9 > pipeline-rest-api 2.10 > handlebars 1.1.1 > pipeline-stage-view 2.10 > lockable-resources 2.4 > workflow-aggregator 2.6 > email-ext 2.65 > git 3.9.3 > gitlab-plugin 1.5.11 > > > > Thank you and best regards, > > Sandra > -- You received this message because you are subscribed to the Google Groups "Jenkins Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/abea79d5-5a5c-4f4b-b28d-22da84edfeff%40googlegroups.com.