6

Environment:

  • Master Linux Jenkins server
  • Two Windows slave nodes
  • The windows slaves are running as a service

First Test

  • I create a Pipeline and use a use a "Execute Windows batch command" in the build section
  • In the Command box I put "C:\Jenkins\mytest.bat"
  • I checked the box "Restrict where this project can be run" and write down the name of the Windows slave
  • I built the pipeline and was successful

Second Test

  • I create a Declarative Pipeline as follows:

    pipeline {  
        agent { label 'SiebelWindows' }
    
        stages {
            stage('Test Bat') {
               steps { 
                            bat 'C:\\Jenkins\\mytest.bat'
                            //bat 'start cmd.exe /c C:\\Jenkins\\mytest.bat'
                            //call C:\\Jenkins\\mytest.bat
                            }
                    }
        }
       }
    
  • In this case the build FAIL with error "cmd is not recognized as a internal o external command"

So, why can I run the .bat with a non-declarative pipeline, but fails with a declarative pipeline?

melpomene
  • 84,125
  • 8
  • 85
  • 148
Chaves
  • 171
  • 1
  • 1
  • 7
  • Is `mytest.bat` executable? – Matthew Schuchard Feb 02 '19 at 12:20
  • What happend if you run the second command: `bat 'start cmd.exe /c C:\\Jenkins\\mytest.bat'` ? – gmc Feb 02 '19 at 22:58
  • Hi Matt... The file mytest.bat is a windows batch file – Chaves Feb 05 '19 at 04:34
  • With the second command is the same result, "cmd is not a recognized internal o external command" – Chaves Feb 05 '19 at 04:35
  • Do you have C:\Windows\system32 in your %PATH%variable of your Windows? The error 'X is not recognized as interntal or external command" usually tells you that a path is missing in your %PATH% variable. – Michael Kemmerzell Feb 05 '19 at 13:16
  • 1
    Yes, in the %PATH% variable we have C:\Windows\system32. I don't know why works fine if I run as a "Execute Windows batch command", but when I create a pipeline as a code (declarative) is not working. Its the same server, the same command – Chaves Feb 05 '19 at 14:49
  • @MattSchuchard The mytest.bat is a very simple batch file. Its only have a "dir" command – Chaves Feb 05 '19 at 14:59
  • I am in a similar situation and I believe that it is related to the background mode explained in https://stackoverflow.com/questions/53149603/why-am-i-not-able-to-run-batch-file-in-jenkins-pipeline-running-in-windows-10 – Ceddaerrix Nov 27 '20 at 09:42
  • Question: Does `C:\\Jenkins\\mytest.bat` use _CMD_ or _START_ within it? – Ceddaerrix Nov 27 '20 at 13:17

2 Answers2

11

When I display the "Path" and the "PATH" variables, this was the result

Original_Path

The solution was redefine the PATH enviorement variable, like this

environment {

    PATH = "C:\\WINDOWS\\SYSTEM32"

}

@JustAProgrammer aske me if C:\WINDOWS\SYSTEM32 was in the PATH of my Windows machine, and that is correct, but seems like Jenkins master do not know the slave's Windows Path.

I resolve my issue, but I still looking for a complete solution, I need to set the PATH enviorement variable with ALL the path, no only C:\WINDOWS\SYSTEM32

Chaves
  • 171
  • 1
  • 1
  • 7
  • i would suggest to use "C:\\WINDOWS\\SYSTEM32;%PATH%" here else there are chances that we might lost some old PATH values set earlier and that might cause even bigger problem – Karan Aug 23 '23 at 09:08
0

If someone has the problem too although the path variable looks correct, check the length of the PATH variable (echo %PATH%). Windows has some restriction of the length of an environment variable (see Microsoft DevBlog).
Remove some entries or set the PATH explicit like already mentioned.

Check also the configuration of the Environment Injector Plugin, which can override the default user and system PATH variable.

mpoqq
  • 96
  • 5