title: jenkins on windows. Declarative pipeline jenkinsfile. how to set and get correct variable values
body: <p>I use Jenkinsefile file to run the Stages.
  It is in Jenkins pipeline installed on windows, Declarative pipeline.</p>
  
  <p>On the begining I do:</p>
  
  <pre><code>pipeline {
      agent { label 'master'}
      environment {
          My_build_result       = 7
      }
  ....
  Than
          stage('Test') {
              steps {
                  echo 'Testing..'
  
                  bat """
                  cd Utils
                  "C:\\Program Files\\MATLAB\\R2019b\\bin\\matlab.exe" -wait -nodisplay -nosplash -nodesktop -r "run('automatic_tests\\run_test.m');"
                  echo %errorlevel%
                  set /a My_build_result_temp = %errorlevel%
                  set My_build_result = %My_build_result_temp%
                  """     
                  script {
                      My_build_result = bat(returnStatus:true , script: "exit (2)").trim()                                
                      echo "My_build_result ${env.My_build_result}"               
                      if (My_build_result != 0) {
                          echo "inside if"
                      }
                  }
              }
          }
  </code></pre>
  
  <p>The variable My_build_result get value 7 at the begining
  Inside the bat section, it suppose to get value 0 from %errorlevel%
  Inside the script section it suppose to get value 2 
  BUT
  in the echo "My_build_result ${env.My_build_result}"  I get print of 7
  (and it goes inside the if sentense)</p>
  
  <p>How do I define variable that can be set value in bat"""
                                                    """
  and in script """
  """
  section of the stage
  and also be familiar in another stages and in the post { always { .. }} at the end ???</p>
  
  <p>BTW: add env.before My_build_result  (env.My_build_result  ) does not work
  Thanks a lot</p>