user5917011
user5917011

Reputation: 1137

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

Upvotes: 5

Views: 43914

Answers (2)

Anthony DiPirro
Anthony DiPirro

Reputation: 11

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.

Upvotes: 1

Ken
Ken

Reputation: 1985

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)

Upvotes: 5

Related Questions