**(update below)**

I have a declarative pipeline job which can take an argument VERSION.

```
pipeline {
  parameters {
    string(name: VERSION, defaultValue: '')
  }

  // ...
}

```

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

```
stages {
  stage('Prepare') {
    steps {
      // ...
      if (! env.VERSION) {
        VERSION = sh(script: "git describe", returnStdout: true).trim()
      }
    }
  }
}       

```

Now I want to "inject" this variable to

* my build script. It needs to find "VERSION" in the environment variables
* to the jenkins mail notificator. And get it to retreive ${VERSION} in subject or body text

I tried changing above code with

```
stages {
  stage('Prepare') {
    steps {
      // ...
      if (! env.VERSION) {
        env.VERSION = sh(script: "git describe", returnStdout: true).trim()
      }
    }
  }
}       

```

Got this error `groovy.lang.MissingPropertyException: No such property: VERSION for class: groovy.lang.Binding`

I then tried to add a "environment" step below

```
environment {
    VERSION = ${VERSION}
}

```

but it didn't solve my problem.

I'm looking for any help to solve it.

**UPDATE**

I now have a working pipeline which looks like

```
pipeline {
    agent any
    parameters {
        string(name: 'VERSION', defaultValue: '')
    }   
    environment {          
        def VERSION = "${params.VERSION}"
    }
    stages {
        stage('Prepare & 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: """<html><body><p>SUCCESS</p></body></html>""",
                 mimeType: 'text/html',
                 charset: 'UTF-8'
            deleteDir()
        }
    }
} // pipeline

```

I needed to add the "environment" step to be able to get $VERSION in all Stages (not only in the one it is manipulated).

I still need to find a way to inject this $VERSION variable in the environment variables, so that my build script can find it.