title: Jenkins Pipeline read from yaml
body: <p>How do I read data from YAML file that is read in one stage and use it in another stage or outside the stage?</p>
  
  <pre><code>pipeline {
    agent any
    environment {
      MY_ENV_VAR1 = 'VALUE1'
    }
    parameters {
      string(name: 'DEPLOY_ENV', defaultValue: 'staging', description: 'Environment to deploy on')
      booleanParam(name: 'DEBUG_BUILD', defaultValue: true, description: 'Debug the build')
    }
    stages {
      stage('Stage1') {
        steps {
          script {
            def datas = readYaml file: 'release.yml'
            echo "Got version as ${datas.version} "
          }
          echo "Deploying to ${params.DEPLOY_ENV} with debug=${params.DEBUG_BUILD}"
        }
      }
      stage('Stage 2') {
        steps {
          sh 'run.sh datas.version'
        }
      }
    }
  }
  </code></pre>
  
  <p>I want to access the ${datas.version} inside steps of <code>Stage 2</code> that was fetched in <code>Stage 1</code>. </p>
  
  <p>I want to keep my pipeline definition as declarative as possible. 
  If i read the <a href="https://jenkins.io/doc/book/pipeline/syntax/#Steps" rel="nofollow noreferrer">docs</a> correctly, the script section can only be added within a stage. I tried this on the global pipeline level but got an error <code>Undefined section "script" at line 10</code>. </p>
  
  <p>I added just <code>datas = readYaml file: 'release.yml'</code> at the pipeline level but get an error saying <code>Not a valid section definition: "datas = readYaml file: 'release.yml'". Some extra configuration is required line 10, column 3.</code></p>
  
  <p>What would be the correct way to reading the file once and then using that read data in any of the stages?</p>