I'm using pipeline declarative syntax and I need to get a value returned from a method in a shared library
pipeline{
String label= new Define.getLabel()
agent (label ${label})
// stages, options ...
}
I'm using pipeline declarative syntax and I need to get a value returned from a method in a shared library
pipeline{
String label= new Define.getLabel()
agent (label ${label})
// stages, options ...
}
This should work. I didn't test with a shared library but it works the same (of course you have to import the shared library with @Library('library-name')_
and your script should be stored there. (Mine is in the pipeline).
My script is very easy. GetLabel
wil just return "docker"
but you can make this more complex with conditional states.
I call the method and save the output in labelID
. So labelID
contains "docker"
. I read the content of labelID
when I specify the label I want to use.
def labelID = getLabel()
pipeline {
agent { node { label labelID } }
stages {
stage('Stage 1') {
steps {
sh 'echo "hallo"'
}
}
}
}
def getLabel(){
return 'docker'
}
This pipeline has run on the slave with label 'docker'
.