Can't access environment in windows bat script in pipline

pipeline {
  agent {label 'windows'}
 parameters {
    choice(choices: ['22.03', '20.03', '22.09'], name: 'release')     }

  environment {
    release_version = '0'    
  }

  stages {
    stage('prepare') {
      steps {
        script {
          nstdout = bat(returnStdout: true, script: '@python get_sp_version.py %release%').trim()                                
          release_version = nstdout                    
          println("stdout ####" + release_version + "###########")
        }
     bat "echo %release_version%.%BUILD_NUMBER%"
     echo "${release_version}.${BUILD_NUMBER}"
   }
   }
  }
}

The code above will output like:

stdout ####2203.1###########
0.47
2203.1.47

2203.1 is the value of the nstdout and release_version.

My expected output is

stdout ####2203.1########### 
2203.1.47
2203.1.47

Am i missing something?
IMO the env var can be accessed by the bat script directly, right?

I’m not sure you can override the environment variable value like you do. In the second example with the echo you are not reading the environment variable but the variable defined in the pipeline, that might explain the difference.
Looking at this documentation you can dynamically define the variable by calling the bat step directly in the environment block.

script { env.VARIABLENAME = "foo" } does work, i’ve used it for a while.

I’m not sure you can update an env variable without using env., i think it may create a new local variable, which as pierre pointed out, is why the echo statement works ${} is groovy not env

1 Like