Discussion:
can i use variable to specify the agent label in my declarative pipeline ?
ishan jain
2017-05-03 14:58:09 UTC
Permalink
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
}

}
}
}
--
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-users+***@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
2017-05-03 15:51:05 UTC
Permalink
It should work if you use double quotes around your agent variable.
Variable resolution will not happen within single quotes.
Post by ishan jain
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
|
properties =null
defloadProperties(){
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
}
}
}
}
|
--
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
To view this discussion on the web visit
https://groups.google.com/d/msgid/jenkinsci-users/4cfff4fb-aa1f-4ad2-9017-d6dcf89a7b36%40googlegroups.com
<https://groups.google.com/d/msgid/jenkinsci-users/4cfff4fb-aa1f-4ad2-9017-d6dcf89a7b36%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.
--
Eric Pyle
Siemens PLM Software
Lebanon, NH
+1 603-277-3060
***@siemens.com
http://www.siemens.com/plm
--
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-users+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/3dff065d-8b81-78fc-8ce5-ccd98b533ee9%40cd-adapco.com.
For more options, visit https://groups.google.com/d/optout.
ishan jain
2017-05-04 08:23:25 UTC
Permalink
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 ?
Post by Eric Pyle
It should work if you use double quotes around your agent variable.
Variable resolution will not happen within single quotes.
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
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
}
}
}
}
--
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
To view this discussion on the web visit
https://groups.google.com/d/msgid/jenkinsci-users/4cfff4fb-aa1f-4ad2-9017-d6dcf89a7b36%40googlegroups.com
<https://groups.google.com/d/msgid/jenkinsci-users/4cfff4fb-aa1f-4ad2-9017-d6dcf89a7b36%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
Eric Pyle
Siemens PLM Software
Lebanon, NH
--
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-users+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/d96a65f5-9926-4325-9f02-1c4649330b06%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Eric Pyle
2017-05-04 20:47:07 UTC
Permalink
OK, so the variable is now being evaluated, but it is not set. Possibly
a variable scoping issue? Can you post the exact code?
Post by ishan jain
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 ?
It should work if you use double quotes around your agent
variable. Variable resolution will not happen within single quotes.
Post by ishan jain
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
|
properties =null
defloadProperties(){
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
}
}
}
}
|
--
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,
To view this discussion on the web visit
https://groups.google.com/d/msgid/jenkinsci-users/4cfff4fb-aa1f-4ad2-9017-d6dcf89a7b36%40googlegroups.com
<https://groups.google.com/d/msgid/jenkinsci-users/4cfff4fb-aa1f-4ad2-9017-d6dcf89a7b36%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout
<https://groups.google.com/d/optout>.
--
Eric Pyle
Siemens PLM Software
Lebanon, NH
+1 603-277-3060
http://www.siemens.com/plm
--
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
To view this discussion on the web visit
https://groups.google.com/d/msgid/jenkinsci-users/d96a65f5-9926-4325-9f02-1c4649330b06%40googlegroups.com
<https://groups.google.com/d/msgid/jenkinsci-users/d96a65f5-9926-4325-9f02-1c4649330b06%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.
--
Eric Pyle
Siemens PLM Software
Lebanon, NH
+1 603-277-3060
***@siemens.com
http://www.siemens.com/plm
--
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-users+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/856f7178-0d3c-5163-0cce-84e055bd493e%40cd-adapco.com.
For more options, visit https://groups.google.com/d/optout.
ishan jain
2017-05-05 11:10:51 UTC
Permalink
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.
OK, so the variable is now being evaluated, but it is not set. Possibly a
variable scoping issue? Can you post the exact code?
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 ?
Post by Eric Pyle
It should work if you use double quotes around your agent variable.
Variable resolution will not happen within single quotes.
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
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
}
}
}
}
--
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
To view this discussion on the web visit
https://groups.google.com/d/msgid/jenkinsci-users/4cfff4fb-aa1f-4ad2-9017-d6dcf89a7b36%40googlegroups.com
<https://groups.google.com/d/msgid/jenkinsci-users/4cfff4fb-aa1f-4ad2-9017-d6dcf89a7b36%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
Eric Pyle
Siemens PLM Software
Lebanon, NH
--
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
To view this discussion on the web visit
https://groups.google.com/d/msgid/jenkinsci-users/d96a65f5-9926-4325-9f02-1c4649330b06%40googlegroups.com
<https://groups.google.com/d/msgid/jenkinsci-users/d96a65f5-9926-4325-9f02-1c4649330b06%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
Eric Pyle
Siemens PLM Software
Lebanon, NH
--
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-users+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/3a1840ac-a74a-4231-ab3d-ed573f6243bb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
chris scott
2017-10-10 10:02:19 UTC
Permalink
Hi did you guys every get to a resolution on this? I have exactly the same
issue.
Post by ishan jain
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.
OK, so the variable is now being evaluated, but it is not set. Possibly a
variable scoping issue? Can you post the exact code?
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 ?
Post by Eric Pyle
It should work if you use double quotes around your agent variable.
Variable resolution will not happen within single quotes.
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
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
}
}
}
}
--
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
To view this discussion on the web visit
https://groups.google.com/d/msgid/jenkinsci-users/4cfff4fb-aa1f-4ad2-9017-d6dcf89a7b36%40googlegroups.com
<https://groups.google.com/d/msgid/jenkinsci-users/4cfff4fb-aa1f-4ad2-9017-d6dcf89a7b36%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
Eric Pyle
Siemens PLM Software
Lebanon, NH
--
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
To view this discussion on the web visit
https://groups.google.com/d/msgid/jenkinsci-users/d96a65f5-9926-4325-9f02-1c4649330b06%40googlegroups.com
<https://groups.google.com/d/msgid/jenkinsci-users/d96a65f5-9926-4325-9f02-1c4649330b06%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
Eric Pyle
Siemens PLM Software
Lebanon, NH
--
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-users+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/f61f808b-183c-4016-a8b7-416feca3e839%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
gilad via Jenkins Users
2017-10-10 14:57:18 UTC
Permalink
same here :-\

even created a StackOverflow issue for this:
https://stackoverflow.com/questions/46630168/in-a-declarative-jenkins-pipeline-can-i-set-the-agent-label-dynamically
Post by ishan jain
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
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
}
}
}
}
--
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-users+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/4e4b367c-76c3-49b9-96bf-f0512dfaae73%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
gilad via Jenkins Users
2017-10-10 14:58:44 UTC
Permalink
same here :-\

even created a StackOverflow issue for this:
https://stackoverflow.com/questions/46630168/in-a-declarative-jenkins-pipeline-can-i-set-the-agent-label-dynamically
--
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-users+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/17136fe1-f194-413a-b3be-ce51354bfb09%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Björn Rohlén
2017-10-11 04:39:02 UTC
Permalink
Post by gilad via Jenkins Users
same here :-\
https://stackoverflow.com/questions/46630168/in-a-declarative-jenkins-pipeline-can-i-set-the-agent-label-dynamically
There is an perfectly good answer in this issue?
--
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-users+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/48d73dc1-66d9-4046-a6d8-5420511082e8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
gilad via Jenkins Users
2017-10-11 12:37:09 UTC
Permalink
not as far as I know... still an open issue
--
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-users+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/fd780a61-bd9c-49b5-8df6-584abe2da48b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Robert Hales
2017-10-11 21:01:19 UTC
Permalink
I posted a reply to this. It was a head twister, but I think I solved the
problem and learned some interesting things.
Post by gilad via Jenkins Users
same here :-\
https://stackoverflow.com/questions/46630168/in-a-declarative-jenkins-pipeline-can-i-set-the-agent-label-dynamically
--
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-users+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/f5caa2f6-aebe-4e79-9993-5c515d39889c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
ishan jain
2017-10-17 12:29:35 UTC
Permalink
Hi Robert,

How did you resolve it ? I could never get it working with anything.
Post by Robert Hales
I posted a reply to this. It was a head twister, but I think I solved the
problem and learned some interesting things.
Post by gilad via Jenkins Users
same here :-\
https://stackoverflow.com/questions/46630168/in-a-declarative-jenkins-pipeline-can-i-set-the-agent-label-dynamically
--
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-users+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/a4b297df-d301-4838-b89a-7469bfa612ca%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
dandeliondodgeball
2017-10-17 13:28:29 UTC
Permalink
I think it is here:
https://stackoverflow.com/questions/46630168/in-a-declarative-jenkins-pipeline-can-i-set-the-agent-label-dynamically
--
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-users+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/37a716b8-9574-4f21-b18f-0678016f301e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Cos Bug
2018-01-10 15:49:05 UTC
Permalink
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 ?
--
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-users+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/907d1cb0-89d1-43dd-b8c9-1816ff22a6cc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Victor Martinez
2018-01-10 16:43:21 UTC
Permalink
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.
--
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-users+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/5c92b166-9c38-41a6-8cbc-846152d399ac%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
ADG ADG
2018-10-18 09:58:09 UTC
Permalink
For making things work you can use this


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

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

}
Post by Cos Bug
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 '''
...
'''
}
}
Does anyone know how to make it work ?
--
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-users+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/4c40f0d3-b81c-497b-8923-b1ab6b51e039%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...