0

What I'm trying to achieve here to run multiple stages in same node which will get captured in initial stage, then will be used by some of the following stages as required.

Below is the sample Jenkinsfile:

pipeline {
    agent {
    label 'redhat_linux'
}
     stages {
            stage('build') {
                    options {
                            timeout(time: 100, unit: 'SECONDS')
                    }
                    input {
                            message "Should we continue?"
                            ok "Yes, we should."
                            submitter "user1,user2"
                            parameters {
                                     string(name: 'PERSON', defaultValue: 'Mr Jenkins', description: 'Who should I say hello to?')
                            }
                    }
                   steps {
                        script{
                                agentName = "${NODE_NAME}"
                                echo "The node name is :: ${agentName}"
                            }
                            sh 'pwd;ls -ltr'
                     }
            }
            stage("promote"){
                 agent{
                    node {
                        label "${agentName}"
                    } 
                }
                 steps{
                          script{
                                echo "The node name is :: ${agentName}"
                          }
                            sh 'echo "This is a promote step " '
                }
            }
         }
     }

I'm facing following error:

groovy.lang.MissingPropertyException: No such property: agentName for class: groovy.lang.Binding

I did tried solution from below link, but it didn't work.

In a declarative jenkins pipeline - can I set the agent label dynamically?

Jenkins version: CloudBees Jenkins Enterprise 2.121.3.1

Rakesh.N
  • 151
  • 7
  • 12
  • Possible duplicate of [In a declarative jenkins pipeline - can I set the agent label dynamically?](https://stackoverflow.com/questions/46630168/in-a-declarative-jenkins-pipeline-can-i-set-the-agent-label-dynamically) – Szymon Stepniak Mar 28 '19 at 10:14
  • did tried the solution from mentioned link, but it doesn't work. getting error: groovy.lang.MissingPropertyException: No such property: agentName for class: groovy.lang.Binding – Rakesh.N Mar 28 '19 at 10:15
  • I don't think it's possible to access the environment variables within the `Agent` block. Perhaps you can take a look at the Jenkins issues page, one of the links I found that might be helpful is the following: https://issues.jenkins-ci.org/browse/JENKINS-43911 The pipeline stages are initalized before the code is executed, meaning that `agentName` is not set when you use it for a label. – Remy Mar 28 '19 at 11:59
  • @Remy So in that case, shouldn't I be getting a Null exception error? – Rakesh.N Mar 28 '19 at 12:04

0 Answers0