<p>How can i use multiple type of agents in a single declarative jenkinsfile.
like i have two labels.
1st of type simple label</p>
<pre class="lang-sh prettyprint-override"><code>       agent {
              label &quot;node_name&quot;
            }
</code></pre>
<p>2nd of of kubernetes type.</p>
<pre class="lang-sh prettyprint-override"><code>           agent {
                     kubernetes {
                            cloud 'cloudName'
                            namespace 'NameSpaceName'
                            label 'AgentLabel'
                            inheritFrom 'agent'
                        }
           }
</code></pre>
<p>And i want to select between these two based on a condition
like if some parameter is given, then run node agent , else run kubernetes agent.</p>

Abhishek tomar
<p>I believe it is not possible exactly in the way you want, but you could work around by defining agents at the stage level:</p>
<pre class="lang-groovy prettyprint-override"><code>pipeline {
    agent none

    stages {
        stage('A') {
            when { /* some condition */ }
            agent {
                label &quot;node_name&quot;
            }
            steps {
                sameCodeForBothStages()
            }
        }
        stage('B') {
            when { /* some condition */ }
            agent {
                kubernetes {
                    cloud 'cloudName'
                    namespace 'NameSpaceName'
                    label 'AgentLabel'
                    inheritFrom 'agent'
                }
            }       
            steps {
                sameCodeForBothStages()
            }
        }
    }
}

void sameCodeForBothStages() {
    sh &quot;echo 'Hello'&quot;
}

</code></pre>
<p>The obvious disadvantage is that two separate stages will show up in the pipeline view.</p>
<p>To avoid duplicate code on both stages you could call a function like I did in the example.</p>
