windows 在Jenkins中执行WINdows bat文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43551394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:51:44  来源:igfitidea点击:

Execute WIndows bat file in Jenkins

windowsjenkinsjenkins-pipelinedevops

提问by user5917011

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:

我正在使用 Jenkins 2.46.1 我安装了一个构建管道插件,我想在其中执行一个 Windows 批处理文件。批处理文件应该在新的命令窗口中执行,而不是在 jenkins 控制台输出中执行。我给出了以下 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 initstage, I want to kill the process if it is already open and in stage deployit 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

init阶段,如果它已经打开,我想终止该进程,并且在阶段部署中它应该打开一个新的命令窗口并运行我的批处理文件。问题是上面的方法不起作用。构建成功,但没有打开命令窗口。请建议

回答by Kendall Trego

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)

这将启动一个新的命令窗口 (cmd.exe) 并运行给定的批处理文件。根据 Jenkins slave 的运行方式,您可能看不到任何东西。(例如,如果它作为 Windows 服务或其他用户运行,您将看不到任何内容)

回答by Anthony DiPirro

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. Instead it is being run from a temp location created by the underlying durable task system. This means things like %~dp0in your script are not going to return the paths you might expect.

附加说明:您不再从原始位置运行 bat 文件。相反,它是从底层持久任务系统创建的临时位置运行的。这意味着%~dp0脚本中的内容不会返回您可能期望的路径。