jenkins agent definition loaded from a jenkins library

14 views
Skip to first unread message

Marco Sacchetto

unread,
Jun 15, 2020, 8:20:05 AM6/15/20
to Jenkins Users
Hi,

I'm currently using a Jenkins declarative pipeline (note: if my issue would be solved with a scripted pipeline, I can switch).
The builds run inside an ephemeral docker agent spinned up by the pipeline using a syntax similar to

agent{ docker{ image "my-image"}}}

The issue with that is that I'm going to have a big number of pipelines defined as Jenkinsfiles which are all kind of similar
to each other, and all of them will be running using the same docker agent.
As such, I'd love to be able to parameterise the image name by defining it as a variable or as a function inside a jenkins
library, so that if the image needs to be change I don't have to commit back to all of the existing Jenkinsfile.

I saw it's possible to define the whole declarative pipeline inside a function, but there seems to be no option to set instead 
just some String variable to be used when setting up the agent, and no possibility to define anything to run outside of the
normal pipeline steps - is that correct?

Gianluca

unread,
Jun 15, 2020, 8:28:33 AM6/15/20
to Jenkins Users
Hi,
I'm not entirely sure what are you looking for but I want to tell you that you can use variables as image name, or at least you can do for agent labels:

agent{ label "${builder.label" }

We use the above in our pipelines to have different agents depending on the branch and PR number and other environment factors.

Cheers,
Gianluca.

Marco Sacchetto

unread,
Jun 15, 2020, 10:10:16 AM6/15/20
to Jenkins Users
Thanks Gianluca, that actually works. I missed the bit of information regarding the annotation that needs to go on variables inside the library to make them global!
Reply all
Reply to author
Forward
0 new messages