Declarative pipeline syntax - variable scope?

Hi, I have two stages, on different agents, that both need to access parameters from a yaml file. I am reading the file twice. How can I change the scope of yaml_params so that I only need to read the file once?

pipeline {

    agent { label "agent_1" }

    parameters { string defaultValue: '', description: '', name: 'yaml_path', trim: true }

    stages {
        stage('run') {
            steps {
                script {                    
                    def yaml_params = readYaml file: params.yaml_path    // Read the YAML file

                    <snip>                   
                }   
            }
        }  

        stage('zip') {
            agent { label "agent_2" }
            steps {
                script {                    
                    def yaml_params = readYaml file: params.yaml_path    // Read the YAML file

                    <snip>                   
                }   
            }
        }                              
    }
}
def let_it 

pipeline {
  agent none

  stages {

    stage('Checkout') {
      steps {
        script {
          let_it = 'snow'
        }
      }
    } // .stage.checkout

    stage('Build') {
      steps {
        println "Weather is: " + let_it
        script {
          println " ... and in script: " + let_it
        }
      }
    } // .stage.build

  } // .stages
} // .pipeline

1 Like