Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute WIndows bat file in Jenkins

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

like image 751
user5917011 Avatar asked Apr 21 '17 20:04

user5917011


2 Answers

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)

like image 177
Kendall Trego Avatar answered Oct 02 '22 04:10

Kendall Trego


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.

like image 22
Anthony DiPirro Avatar answered Oct 02 '22 06:10

Anthony DiPirro