5

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
  • 81,915
  • 7
  • 76
  • 137
Chaves
  • 141
  • 1
  • 1
  • 6
  • Is `mytest.bat` executable? – Matt 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

1 Answers1

9

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
  • 141
  • 1
  • 1
  • 6