0

I have a requirement where I have to SET a groovy variable inside a batch script.

The reason I want to do is because in the jenkinsile batch script I am trying to figure out whether there have been any git changes on a checked out folder by using git porcelain command. I tried the below way but that doesn't seem to be working at all . It always goes to the else part.

    def boolFlagVar = false

    stages {
    stage('Test') {
      steps {
        script {                             
            bat '''
              SET ${boolFlagVar} = true #### or SET boolFlagVar = true
            '''
            
            if (isSomeFlag.toBoolean())
            {
                echo "came inside the if condition"
            }
            else
            {
                echo "came to else part"
            }
        }
      }
    }

Could you help here please ?

Thanks a lot for help !!

  • Does this answer your question? [How do I get the output of a shell command executed using into a variable from Jenkinsfile (groovy)?](https://stackoverflow.com/questions/36547680/how-do-i-get-the-output-of-a-shell-command-executed-using-into-a-variable-from-j) – Azeem Mar 31 '23 at 15:51
  • No actually. My logic of whether the flag should be set to true or false happens inside the batch script. Thats the reason i cant do it in that fashion which i have already tried. Thanks! – hi_shobhit76 Mar 31 '23 at 16:03
  • You do know that when you use the `Set` command in your `.bat` file, that the variable is only defined for the duration of the specific `cmd.exe` instance in which that batch file is running, don't you? So as soon as your batch file has run the variable you just defined will no longer be defined. Perhaps you need to use the `setx.exe` utility instead. Please open a Command Prompt window, type `%SystemRoot%\System32\setx.exe /?`, then press the `[ENTER]` key, to learn how that utility works. – Compo Mar 31 '23 at 16:11
  • @Compo yes, i do understand that. But i wanted to explain what i wanted to achieve with the code i have written. The intent is that there is a global variable defined in groovy(jenkinsfile) value of which i want to change within the batch script. – hi_shobhit76 Mar 31 '23 at 16:28
  • Also your use of the `Set` command is incorrect too. So please open a Command Prompt window, type ```set /?```, then press the [`ENTER]` key, to learn how that command works. The recommended syntax is ```Set "VarName=StrValue"```. Pay special attention to the lack of spaces eother side of the `=` character. You certainly wouldn't want a trailing space as part of the variable name, and may not want a leading space at the beginning of the string value! – Compo Mar 31 '23 at 16:55
  • @Compo I tried the setx command as well but that didn't help either. – hi_shobhit76 Mar 31 '23 at 17:58
  • @hi_shobhit76 If you want to pass a value from a child process to a parent process, you can write the value into a file with the child process which is read from the file by the parent process. Then the parent process deletes the file no longer needed. Or the child process writes the value somewhere into Windows registry hive of current user and the parent process reads the value and next deletes the registry value. The child process can output also the value to standard output stream which is captured by the parent process and processed further. – Mofi Apr 01 '23 at 12:14
  • @Mofi thanks.. I get your point. My understanding was wrong when I thought the commands within jenkinsfile are executed within the same process. The idea of writing the results to a flat file in batch process and accessing the same in jenkinsfile groovy process did the trick for me. – hi_shobhit76 Apr 01 '23 at 17:58

0 Answers0