Modify agent using a declarative pipeline shared library

28 views
Skip to first unread message

Mario Jauvin

unread,
Dec 27, 2019, 4:59:13 PM12/27/19
to Jenkins Users
I currently use a shared library with a call method.  I would like to specify an argument to either use the agent any or agent {label 'some-label'}.

I have the call method as such:

def call(String buildType, String agentLabel=null) {
if ("versionBranch".equals(buildType)) {

}

Mario Jauvin

unread,
Dec 27, 2019, 5:09:01 PM12/27/19
to Jenkins Users
Sorry the post reply got pressed by accident.  Here is what I have which does not work:

def call(String buildType, String agentLabel=null) {
if ("versionBranch".equals(buildType)) {
 pipeline {
 
   if (agentLabel==null) {
    agent any
   } else {
    agent {label "${agentLabel}"}
   }
   .... rest of the pipeline
 }

Mario Jauvin

unread,
Dec 30, 2019, 8:53:12 AM12/30/19
to Jenkins Users
This seems to be the only way that I can get it working but it requires repeating the pipeline twice which in my case is a complicated one:

def call(String buildType, String agentLabel=null) {
 if ("versionBranch".equals(buildType)) { 
  if (agentLabel==null) {
   pipeline {
    agent any
    ... rest of the pipeline
   }
   } else {
   pipeline {
    agent {label "${agentLabel}"}
    ... rest of the pipeline
   }
  }
 }
}

Is there another way? 

Mario Jauvin

unread,
Dec 30, 2019, 10:34:18 AM12/30/19
to Jenkins Users
Would there be a way to create a groovy variable named agentObject that either contains the object {label "$agentLabel"} or the object any provided by the pipeline namespace and use a syntax like: agent $agentObject?
Reply all
Reply to author
Forward
0 new messages