Execute WIndows bat file in Jenkins

38,715

Solution 1

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)

Solution 2

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.

Share:
38,715
user5917011
Author by

user5917011

Updated on July 09, 2022

Comments

  • user5917011
    user5917011 almost 2 years

    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