How to access env var in 'bat' step?

Hi, for declarative pipeline, I know that one can access an environment variable using, for example:

steps {
    sh """ 
        echo "Build number in sh script: ${env.BUILD_NUMBER}"
    """
}

How does one do that in a ‘bat’ step (on Windows)?

The way to access env variables in batch script is using the %% “operator”. So, something like

echo "Build number in bat script: %BUILD_NUMBER%"

1 Like

@slide_o_mix Thank you.

You are very welcome! Hope its all working for you now.

@slide_o_mix The env variable is being referenced correctly now, but what is the syntax for a groovy variable please? Is the following correct?

    steps {
        script {
            def variant = "Quasar"

            bat '''
                cmake -G "Visual Studio 16 2019" -A x64 -DPROJECT_VARIANT="%variant%" ..\\..
            '''

The syntax:

"%variant%"

isn’t working.

Actually, I’ve found the answer, thanks.

https://stackoverflow.com/questions/52863062/access-a-groovy-variable-from-within-bat-step-in-jenkins-pipeline-on-windows-sla

Right, local variables are not put into the environment for a batch script to use. You would need to do env.variant = "Quasar" for it to be added to the environment variables for spawned batch file execution. Glad it is working for you now.

1 Like