简体   繁体   English

在Jenkins中执行WINdows bat文件

[英]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.我正在使用 Jenkins 2.46.1 我安装了一个构建管道插件,我想在其中执行一个 Windows 批处理文件。 The batch file should execute in a new command window and not on jenkins console output.批处理文件应该在新的命令窗口中执行,而不是在 jenkins 控制台输出中执行。 I give the below Jenkins pipeline groovy script:我给出了以下 Jenkins 管道 groovy 脚本:

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.init阶段,如果进程已经打开,我想终止它,并且在阶段部署中它应该打开一个新的命令窗口并运行我的批处理文件。 The problem is that the above does not work.问题是上面的方法不起作用。 The build is succesful but no command window opens up.构建成功,但没有打开命令窗口。 Pls suggest请建议

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.这将启动一个新的命令窗口 (cmd.exe) 并运行给定的批处理文件。 Depending how your Jenkins slave is running you may not see anything.根据 Jenkins slave 的运行方式,您可能看不到任何东西。 (eg if it's running as a windows service or different user, you won't see anything) (例如,如果它作为 Windows 服务或其他用户运行,您将看不到任何内容)

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.注意:bat 文件需要在您的工作区中。

Additional Note: You are no longer running the bat file from its original location.附加说明:您不再从原始位置运行 bat 文件。 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.这意味着脚本中的%~dp0类的内容不会返回您可能期望的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM