Select agent based on environment variable

My Jenkinsfile uses the matrix job plugin with PLATFORM being one of the axes and it has three values ‘linux’, ‘windows’ and ‘macOS’. Earlier we had fixed nodes allotted for each platform so it was easy to have platform specific nodes and allocate them using agent { label ${PLATFORM} }. We have now dynamically allocated nodes only for PLATFORM ‘linux’, and these need to specified as follows -
agent { node { label "${scriptToGetLabel()}" customWorkspace "/my/custom/workspace/" } } }
Nodes for ‘windows’ and ‘macOS’ continue to use agent { label ${PLATFORM} }.
Is there a way to do this based on PLATFORM value? I am using Declarative syntax.

I can’t help you with declarative but this is super easy to do in pipeline. Which is the more advanced and IMHO superior pipeline syntax.


node('some-starter-node') {
    // not familiar with matrix builds, so this might not be the correct way to access the value
    String label = env.PLATFORM

    if (label == 'linux') {
        label = scriptToGetLabel() // This ony works for groovy function, if its bash script need to use "sh()"
    }

    node(label, '/my/custom/workspace/) {
         // your pipeline code here
    }
}

Treat this as rough pseudo code, but it should be really close.

1 Like

Hi hi
This method lets us select node labels
I need something more
I need to select dynamically agent types based on parameter
to implement something like that

**agent { **
**if ( $TESTS == “BitbucketIntraTest”) { kubernetes { yamlFile ‘rc/config/default/node-template.yaml’ // separate yaml file defining the pod template defaultContainer ‘python’ // the default container where all task will be exceuted, unless other specified } } **
else { agent none }