How to enable "Trigger builds remotely (e.g., from scripts)" with authentication token via Jenkinsfile?

Hey,

I am currently converting an old pipeline to a Jenkinsfile (scripted) based one. But one thing I was not able to get going is the “Trigger builds remotely (e.g., from scripts)”. This has been asked over and over on Stackoverflow and so far I have not found an answer.

It is possible to create a user token and use that and passing it via a POST call. But that way I am not able to make the new pipeline backward compatible which may break other scripts and pipelines.

What I want to do is to specify a token which can be used when my build is called like this: http://jenkins_url/job/HUGE_PROJECT/build?token=MY_TOKEN
Where MY_TOKEN is specified by me in the script. It is possible with the normal pipeline so it should be possible with a Jenkinsfile as well, shouldn’t it?

Edit my only way I can see so far is to keep the old job and make it trigger the Jenkinsfile-based one.

Best regards,
green

I’ve never used that plugin specifically but it should just work. If you use the syntax generator link on the side of your job you should be able to generate code to set properties and options

1 Like

Yes, that is what I would have guessed too, but I am not able to find it:

Oh the left is the job itself, on the right is the generator.

I tried to find it in code, and I found jenkins/BuildAuthorizationToken.java at b5e5d0e8eb18cff50d82daa12562648391f705d2 · jenkinsci/jenkins · GitHub which says its depreciated. (though that might just be removing it from core or something).

I did find https://plugins.jenkins.io/build-token-trigger/ which seems like its a wrapper to support pipelines. I’m guessing so the regular plugin doesn’t need to install pipeline support or something. Maybe it’ll be helpful?

1 Like

Thank you, that looks promising!

Okay, it seems that this is only a trigger. It does not add a Token.
I’ll discuss this with my colleagues if we can change the scrips to do a POST with an Auth Token. If not, I’ll simply keep the old job with the trigger, which then triggers the Jenkinsfile based one. Not an ideal solution, but this is a huge legacy software and thus using a lot of bat files for the build process. It will probably take years to move everything to the new pipelines.

Hi,

You’ll need https://plugins.jenkins.io/build-token-root/ for token based triggering. And you should not configure this from the Jenkinsfile, but in the job (the token would be in plain text in the Jenkinsfile…)
The nice thing is that non-overlapping configurations in the job and the Jenkinsfile are normally additive.

Björn

1 Like

Thank you, I’ll have a look at it.