5

I am using Jenkins 2.46.1 I have a build pipeline plugin installed and I want to execute a Windows batch file in it. The batch file should execute in a new command window and not on jenkins console output. I give the below Jenkins pipeline groovy script:

node {  
    stage 'Init'
    bat '''
        call C:\\myprj\\mybat.bat stop
        EXIT /B 0
    '''
    stage 'Deploy'
    bat '''call C:\\myprj\\mybat.bat'''
}

In the init stage, I want to kill the process if it is already open and in stage deploy it should open a new command window and run my batch file. The problem is that the above does not work. The build is succesful but no command window opens up. Pls suggest

user5917011
  • 1,137
  • 5
  • 14
  • 22

2 Answers2

5

Technically, to do what you're asking you should be able to run

bat 'start cmd.exe /c C:\\myprj\\mybat.bat'

This will launch a new command windows (cmd.exe) and run the batch file given. Depending how your Jenkins slave is running you may not see anything. (eg if it's running as a windows service or different user, you won't see anything)

Kendall Trego
  • 1,975
  • 13
  • 21
  • Jenkins is running as a windows service. Is there no way in this case that I can open a new command window? – user5917011 Apr 22 '17 at 06:14
  • The reason why I want it to open in a new command window is because the batch file will run a server. THe command window remains opened as long as the server is running. So the build will always show as 'getting executed' and never as 'completed' – user5917011 Apr 22 '17 at 07:08
  • There is no easy way that I know of, it's part of the design for windows services. You may be able to hang the bat process indefinitely with a loop. Of course, this then ties up that build node. If you really want to hack something together you can have a scheduled task that is triggered by an event in the event log, then have your bat file/service create that event. – Kendall Trego Apr 24 '17 at 20:14
1

Alternative solution if the agent is running in a service and you would like to get output:

bat(readFile("mybat.bat"))

Note: The bat file will need to be in your workspace.

Additional Note: You are no longer running the bat file from its original location. Instead it is being run from a temp location created by the underlying durable task system. This means things like %~dp0 in your script are not going to return the paths you might expect.