can i use variable to specify the agent label in my declarative pipeline ?

7,131 views
Skip to first unread message

ishan jain

unread,
May 3, 2017, 10:58:09 AM5/3/17
to Jenkins Users
I am using declarative syntax to build my pipeline. I am loading several variables from a properties file in the beginning and i now i would like to control the 'on which slave node something executes' via variables. But i cannot figure out how to use variable as a agent label. The following is one of the snippets and like all its other variations, it fails:

properties = null

def loadProperties() {
 node {
         checkout scm
           properties = readProperties file: 'pipeline.properties'
        echo "Immediate one ${properties.repo}"
}
}

pipeline {
   agent none
 
     stages {
       
               stage ('prepare') {
                    agent any
                     
                        steps {
                        script {
                                       loadProperties()
                               }
                      }
              }
              stage('Build') {
           
                   agent { label '${properties.agent}' }
                 
                        steps {
                        sh 'hostname'
                          echo properties.repo
                   }
                     
        }
   }
}


Eric Pyle

unread,
May 3, 2017, 11:51:21 AM5/3/17
to jenkins...@googlegroups.com
It should work if you use double quotes around your agent variable. Variable resolution will not happen within single quotes.
--
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-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/4cfff4fb-aa1f-4ad2-9017-d6dcf89a7b36%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

-- 
Eric Pyle
Siemens PLM Software
Lebanon, NH
+1 603-277-3060
eric...@siemens.com
http://www.siemens.com/plm

ishan jain

unread,
May 4, 2017, 4:23:25 AM5/4/17
to Jenkins Users
Hi Eric,

I tried putting the var in double quotes but still it is not able to resolve it. I get 'There are no nodes with the label ‘null’' error. I am able to print the value of exact same variable but it seems it is not resolving the variable name in here. Is there something else i need to do here ?

Eric Pyle

unread,
May 4, 2017, 4:47:14 PM5/4/17
to jenkins...@googlegroups.com
OK, so the variable is now being evaluated, but it is not set. Possibly a variable scoping issue? Can you post the exact code?

For more options, visit https://groups.google.com/d/optout.

ishan jain

unread,
May 5, 2017, 7:10:51 AM5/5/17
to Jenkins Users
Hi Eric,

This the full code that copied earlier. You would only require to create a properties file. The other echo statements that i have put are printing out the values just fine. 

chris scott

unread,
Oct 10, 2017, 7:06:45 AM10/10/17
to Jenkins Users
Hi did you guys every get to a resolution on this? I have exactly the same issue. 
Message has been deleted

gi...@ziprecruiter.com

unread,
Oct 10, 2017, 10:58:44 AM10/10/17
to Jenkins Users

Björn Rohlén

unread,
Oct 11, 2017, 12:39:03 AM10/11/17
to Jenkins Users

There is an perfectly good answer in this issue?  

gi...@ziprecruiter.com

unread,
Oct 11, 2017, 8:37:09 AM10/11/17
to Jenkins Users
not as far as I know... still an open issue

Robert Hales

unread,
Oct 11, 2017, 5:01:19 PM10/11/17
to Jenkins Users
I posted a reply to this. It was a head twister, but I think I solved the problem and learned some interesting things. 

ishan jain

unread,
Oct 17, 2017, 8:29:36 AM10/17/17
to Jenkins Users
Hi Robert,

How did you resolve it ? I could never get it working with anything.

dandeliondodgeball

unread,
Oct 17, 2017, 9:28:29 AM10/17/17
to Jenkins Users

Cos Bug

unread,
Jan 10, 2018, 10:50:29 AM1/10/18
to Jenkins Users
Hello All,

Slightly related issue - I'm trying to use a variable interpolation in stage name but it doesn't work.



        stage
("Backup ${DATABASE}") {

            steps
{
                sh
'''
                    ...
                '''

           
}

       
}

but I get an exception:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: WorkflowScript: 25: Expected string literal @ line 25, column 15. stage("Backup ${DATABASE}") { 1 error at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310) at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1085) at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:603) at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581) at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558) at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298) at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268) at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688) at groovy.lang.GroovyShell.parse(GroovyShell.java:700) at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:129) at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:123) at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:517) at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:480) at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:268) at hudson.model.ResourceController.execute(ResourceController.java:97) at hudson.model.Executor.run(Executor.java:429) Finished: FAILURE

Does anyone know how to make it work ?

Victor Martinez

unread,
Jan 10, 2018, 11:43:22 AM1/10/18
to Jenkins Users
it seems it's already an expected behaviour https://issues.jenkins-ci.org/browse/JENKINS-43820  in the declarative pipelines but ... it seems to work in the scripted pipelines though.




ADG ADG

unread,
Oct 18, 2018, 7:46:15 AM10/18/18
to Jenkins Users
For making things work you can use this 


        stage
("Backup " + "${DATABASE}") {

Aryo Kusumo

unread,
Jan 16, 2019, 11:16:22 AM1/16/19
to Jenkins Users
I create agent as string parameters and put in the jennkinsfile without quote.

agent {
node {
label params.agent
}
}
Reply all
Reply to author
Forward
0 new messages