tarantoga
tarantoga

Reputation: 1106

How do I add authToken for "Trigger builds remotely" to job's config.xml

I want to enable the "Trigger builds remotely" option for a Jenkins job, with an authentication token defined. I tried this:

freeStyleJob('Sandbox/test-trigger') {
    configure { project ->
      (project / 'authToken').setValue('mytoken')
    }
}

According to http://job-dsl.herokuapp.com/, I end up with an authToken line on the top level of the project's config XML (as desired):

<project>
    [...]
    <authToken>mytoken</authToken>
</project>

However, after running the Job-DSL, I do not get the authToken defined in the resulting XML, nor is the option enabled in the config. Any ideas what I'm doing wrong?

Using Jenkins 1.609.2 with job-dsl 1.37.

UPDATE: job-dsl >= 1.39 now supports the token setting; see https://jenkinsci.github.io/job-dsl-plugin/#method/javaposse.jobdsl.dsl.jobs.FreeStyleJob.authenticationToken

Upvotes: 4

Views: 4905

Answers (3)

Alysander
Alysander

Reputation: 56

If you want to avoid hard coding your token you and are using the dynamic dsl plugin:

In your Jenkinsfile.build

  string(credentialsId: 'deploy-trigger-token', variable: 'TRIGGER_TOKEN'),
]) {
    jobDsl targets: ".jenkins/deploy_${env.INSTANCE}_svc.dsl",
    ignoreMissingFiles: true,
    additionalParameters: [
      trigger_token: env.TRIGGER_TOKEN
    ]
}

Then in your dsl file:

pipelineJob("Deploy Service") {
...
 authenticationToken (trigger_token)
...
}

And you will need to configure the credential deploy-trigger-token in your jenkins credential store.

Upvotes: 0

Yuhao Zhang
Yuhao Zhang

Reputation: 490

You can simply use:

FreeStyleJob {
    authenticationToken('mytoken')
    ...
}

It does not have DSL API docs, but the DSL API viewer generates one for you. You can view it at

<YourJenkinsURL>/plugin/job-dsl/api-viewer/index.html#method/javaposse.jobdsl.dsl.jobs.FreeStyleJob.authenticationToken

Upvotes: 4

tarantoga
tarantoga

Reputation: 1106

It was fixed when I moved the "configure" block as first part of the job definition.

So instead of:

freeStyleJob('Sandbox/test-trigger') {
    <lots of other job config>
    configure { project ->
        (project / 'authToken').setValue('mytoken')
    }
}

I changed it to:

freeStyleJob('Sandbox/test-trigger') {
    configure { project ->
        (project / 'authToken').setValue('mytoken')
    }
    <lots of other job config>
}

Now the token configuration was properly kept in job config.

Upvotes: 3

Related Questions