Search code examples
windowsjenkinsgroovyjenkins-pipeline

How to access jenkins groovy variable in a multiline batch script


I have a simple jenkins pipeline as below :-

node ('windows') {

def myvar = bat(returnStdout: true, script: '''FOR /F "tokens=2 delims==" %%a IN ('C:\\Windows\\System32\\find.exe "test2" ^<%WORKSPACE%\\test.txt') DO SET test2value=%%a
echo %test2value%''').trim()
echo myvar

 bat '''
        SET var='+myvar+'
        echo %var%
     '''
     
 }

What i am trying to achieve here is to access the groovy variable myvar inside a multiline batch script however it doesn't work. Same thing works in multiline triple single quoted shell script as below. This is just an example and the groovy variable myvar is being assigned some other value using some shell script

sh '''
     var=''' + myvar + '''
     echo $var
  '''

How do i access this groovy variable myvar inside this triple single quoted multiline batch script?


Solution

  • Solution

    There are a lot of small nuanced changes in my answer as compared to yours. Please first copy and paste it into your pipeline to verify it works for you. You can then make modifications as needed.

    You have two issues. Firstly, batch scripts print the command you're executing to standard out when it executes. Bash does not do this. In your example the value being stored in stdout is not only the value you echoed but also the entire command. To fix this I added @ symbol in two spots in your first batch script. This is your primary issue.

    Your secondary issue is that you are incorrectly referencing a Groovy variable. It wouldn't have worked regardless of whether it was a single line or multiline.

    I made a few other modifications to remove code I deemed unnecessary. I removed the %workspace% variable because it was unnecessary. I also removed the SET command from your first batch script because it was also unnecessary. I also removed the absolute path to find.exe because it should be in your PATH variable

    node('windows') {
        def myvar = bat(returnStdout:true, script:'''@ FOR /F "tokens=2 delims==" %%a IN ('find "test2" test.txt') DO @ echo %%a''').trim()
        echo myvar + ' is set'
        
        bat "echo ${myvar} is the value"
        
        bat """
            SET var=$myvar
            echo %var%
        """
    }
    

    The above example was tested in 2.x and 1.x. It works in Jenkins 2.x but will not work in some versions of Jenkins 1.x.

    Build Log

    [Pipeline] {
    [Pipeline] bat
    [miles-objects] Running batch script
    [Pipeline] echo
    xyz is set
    [Pipeline] bat
    [miles-objects] Running batch script
    
    c:\jenkins\workspace\miles-objects>echo xyz is the value 
    xyz is the value
    [Pipeline] bat
    [miles-objects] Running batch script
    
    c:\jenkins\workspace\miles-objects>SET var=xyz 
    
    c:\jenkins\workspace\miles-objects>echo xyz 
    xyz
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] End of Pipeline
    Finished: SUCCESS