# Select agent based on environment variable

**URL:** <https://community.jenkins.io/t/select-agent-based-on-environment-variable/1947>\
**Category:** Using Jenkins\
**Created:** [March 29, 2022, 4:46am UTC](https://community.jenkins.io/t/select-agent-based-on-environment-variable/1947 "2022-03-29T04:46:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chaku](https://avatars.discourse-cdn.com/v4/letter/c/d07c76/32.png) [@chaku](https://community.jenkins.io/u/chaku)\
**Post date:** [March 29, 2022, 4:46am UTC](https://community.jenkins.io/t/select-agent-based-on-environment-variable/1947/1 "2022-03-29T04:46:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![shadycuz](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/shadycuz/32/196_2.png) [@shadycuz](https://community.jenkins.io/u/shadycuz)\
**Post date:** [March 30, 2022, 1:48pm UTC](https://community.jenkins.io/t/select-agent-based-on-environment-variable/1947/2 "2022-03-30T13:48:59Z")

</div>

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.

```auto

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.

---

<div class="post-metadata">

**Author:** ![arturrekawek](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/arturrekawek/32/2224_2.png) [@arturrekawek](https://community.jenkins.io/u/arturrekawek)\
**Post date:** [March 15, 2023, 6:21pm UTC](https://community.jenkins.io/t/select-agent-based-on-environment-variable/1947/3 "2023-03-15T18:21:59Z")

</div>

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 }**
