Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins Declarative Pipeline - dynamically select node (agent) based on git branch

I have a Jenkins master and pool of slave nodes which dynamically grows and shrinks (based on load). The master node is called "master" and the slaves have guids for names. Currently none of the nodes have labels.

For my project, I want the "develop" branch from github to build on the master node and pull request branches to build on any one of the slaves. This has been working successfully in a scripted pipeline using node('master') and node('!master').

I would like to start using the new Declarative style of pipeline. Is it currently possible to achieve the same "master" and "not master" behaviour in a Declarative Pipeline, based on the branch name?

In the scripted pipeline, it looks like this:

def selectedNode = BRANCH_NAME == 'develop' ? 'master' : '!master'

node(selectedNode) {
}

Thanks

like image 296
Alex Avatar asked Aug 20 '26 15:08

Alex


1 Answers

if it works in scripted, you can generally include the exact same contents within a script step in a declarative pipeline. this runs for me:

pipeline {
  agent { label 'docker' }
  stages {
    stage('build') {
      steps {
        script {
          def selectedNode = BRANCH_NAME == 'develop' ? 'master' : '!master'

          node(selectedNode) {
          }
        }
      }
    }
  }
}

i'd probably go this route rather than futzing with the top-level agent declarations.

like image 177
burnettk Avatar answered Aug 22 '26 13:08

burnettk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!