title: Declarative Jenkins Pipeline; How to declare a variable and use it in script or mail notification?
body: <p><strong>(update below)</strong></p>
  
  <p>I have a declarative pipeline job which can take an argument VERSION.</p>
  
  <pre><code>pipeline {
    parameters {
      string(name: VERSION, defaultValue: '')
    }
  
    // ...
  }
  </code></pre>
  
  <p>If no VERSION is given, like when Gitlab send a hook to this job, I want to compute it from git, so I do something like this</p>
  
  <pre><code>stages {
    stage('Prepare') {
      steps {
        // ...
        if (! env.VERSION) {
          VERSION = sh(script: "git describe", returnStdout: true).trim()
        }
      }
    }
  }       
  </code></pre>
  
  <p>Now I want to "inject" this variable to</p>
  
  <ul>
  <li>my build script. It needs to find "VERSION" in the environment variables</li>
  <li>to the jenkins mail notificator. And get it to retreive ${VERSION} in subject or body text</li>
  </ul>
  
  <p>I tried changing above code with</p>
  
  <pre><code>stages {
    stage('Prepare') {
      steps {
        // ...
        if (! env.VERSION) {
          env.VERSION = sh(script: "git describe", returnStdout: true).trim()
        }
      }
    }
  }       
  </code></pre>
  
  <p>Got this error <code>groovy.lang.MissingPropertyException: No such property: VERSION for class: groovy.lang.Binding</code></p>
  
  <p>I then tried to add a "environment" step below</p>
  
  <pre><code>environment {
      VERSION = ${VERSION}
  }
  </code></pre>
  
  <p>but it didn't solve my problem.</p>
  
  <p>I'm looking for any help to solve it.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I now have a working pipeline which looks like</p>
  
  <pre><code>pipeline {
      agent any
      parameters {
          string(name: 'VERSION', defaultValue: '')
      }   
      environment {          
          def VERSION = "${params.VERSION}"
      }
      stages {
          stage('Prepare &amp; Checkout') {
              steps {
                  script {
                      if (! env.VERSION) {
                         VERSION = sh(script: "date", returnStdout: true).trim()
                      }
                      echo "** version: ${VERSION} **"
                  }
              }
          }  
          stage('Build') {
              steps {
                  // sh "./build.sh"
                  echo "** version2: ${VERSION} **"
              }
          }
      } // stages
      post {
          always {
              mail to: 'foo@example.com',
                   subject: "SUCCESS: ${VERSION}",
                   body: """&lt;html&gt;&lt;body&gt;&lt;p&gt;SUCCESS&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;""",
                   mimeType: 'text/html',
                   charset: 'UTF-8'
              deleteDir()
          }
      }
  } // pipeline
  </code></pre>
  
  <p>I needed to add the "environment" step to be able to get $VERSION in all Stages (not only in the one it is manipulated).</p>
  
  <p>I still need to find a way to inject this $VERSION variable in the environment variables, so that my build script can find it.</p>