+4 votes
1 view
in Devops and Agile by (25.5k points)

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 successful but no command window opens up. Please suggest.

4 Answers

+5 votes
by (42.7k points)

To launch a new command window (cmd.exe) and run the batch file given.

You can use 

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

This command will do what you are asking that should be able to run.

Note: It depends on the working of your jenkins slave.

by (25.5k points)
Can I run the Bat file from its original location?
by (42.7k points)
No you cannot run the bat file from it's original location. If you try to run from a temp location  created by durable task system.Your script might not return the path.
by (24.5k points)
It helped, thanks!
by (13.5k points)
It worked for me.
Thanks!
by (33.1k points)
Thanks, Your answer worked well.
by (41.2k points)
The reason why you must open in a new command window is that the batch file will run a server. The command window remains open as long as the server is running. So the build will always show as 'getting executed' and never as 'completed
by (27.7k points)
nice solution worked for me!!!
by (40.3k points)
Good explanation. Thank you.
+1 vote
by (57.5k points)

Just replace cmd \k with call

The Execute Windows Batch command is running inside a cmd already so no need to specifically start a new one. Just "call" your BAT file.

by (42.5k points)
why in some jobs we can use CMD to run, but in some jobs, it cannot?
0 votes
by (11.2k points)

Create file name jenkins.bat and place this file where your jenkins.war file is available.

You have to click jenkins.bat file.

Note :- After click on jenkins.bat file, chrome browser will automatically open with jenkins url.

Try this If your system configured with Java 8 or 7 Installation.

timeout 3

start chrome http://localhost:8080

timeout 3

java -jar jenkins.war

Try this If your system configured with Java 10 or above Installation.

timeout 3

start chrome http://localhost:8080

timeout 3

java -jar jenkins.war --enable-future-java

0 votes
by (107k points)

You can try like this,  first point it to the path where your bat file exists, and then 'call' command to trigger that bat file. As below:

cd yourFilePath

and then

call yourFile.bat

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...