为什么我无法在 Windows 10 中运行的詹金斯管道中运行批处理文件? [英] why am I not able to run batch file in jenkins pipeline running in windows 10?

查看:13
本文介绍了为什么我无法在 Windows 10 中运行的詹金斯管道中运行批处理文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行 jenkins 工作区中的批处理脚本.我写了一个如下的 groovy 脚本来做到这一点

stage('batchscript') {脚步{bat '启动 cmd.exe/c C:\Program Files (x86)\Jenkins\workspace\jenkins Project\batchfile.bat'}}

当我构建作业时,它应该打开一个新的命令窗口并在新的命令提示符下运行我的批处理文件,执行所有 bat 命令.构建成功,但没有打开命令窗口.任何建议都会有所帮助

解决方案

Jenkins 的目标是在 background 模式下执行 shell 命令,而不是 interactive(UI) 模式.当您运行 start cmd.exe/c c://some/app.exe 时,会打开一个新的 cmd UI,这在 jenkins 中永远不会发生.

单行

如果你需要用 jenkins 执行一个简单的批处理命令:

stage('build') {cmd_exec('echo "开始编译..."')cmd_exec('echo "dir/a/b"')}def cmd_exec(命令){return bat(returnStdout: true, script: "${command}").trim()}

这是一个高级示例:

多行

步骤{echo '部署到临时环境'//启动tomcat蝙蝠""cd c:\qa\bin目录/a/b启动""蝙蝠""cd c:\qa\bin启动""//将 WAR 移动到 Tomcat 的代码bat "xcopy/y c:\webapp\target\webapp.war ..."bat "xcopy/y c:\webapp\target\webapp.war ..."}

例子:

调用批处理文件

如果你需要使用 jenkins 执行批处理文件:

stage('build') {目录(build_folder"){蝙蝠run_build_windows.bat"}}

stage('build') {蝙蝠c://some/folder/run_build_windows.bat"}

<块引用>

Windows 路径有时很奇怪 :s .无论如何,linux 是托管 jenkins 的最佳选择.

I'm trying to run a batchscript present inside the workspace of jenkins. I have written a groovy script as below to do this

stage('batchscript') {
   steps{
      bat 'start cmd.exe /c C:\Program Files (x86)\Jenkins\workspace\jenkins Project\batchfile.bat'
   }
}

when I build the job it should open a new command window and run my batch file in a new command prompt executing all the bat commands. The build is succesful but no command window opens up. Any suggestion will be helpfull

解决方案

Jenkins is aimed to execute shell commands in background mode, not for interactive(UI) mode. When you run start cmd.exe /c c://some/app.exe a new cmd UI is opened and this will never happen in jenkins.

Single line

If you need to execute a simple batch commands with jenkins :

stage('build') {
      cmd_exec('echo "Buils starting..."')
      cmd_exec('echo "dir /a /b"')
}

def cmd_exec(command) {
    return bat(returnStdout: true, script: "${command}").trim()
}

Here a advanced example :

Multiline

steps {
  echo 'Deploy to staging environment'

  // Launch tomcat
  bat """
    cd c:\qa\bin
    dir /a /b
    startup
  """
  
  bat """
    cd c:\qa\bin
    startup
  """

  // Code to move WAR to Tomcat
  bat "xcopy /y c:\webapp\target\webapp.war ..."
  bat "xcopy /y c:\webapp\target\webapp.war ..."
}

Example:

Invoke batch file

If you need to execute a batch file with jenkins :

stage('build') {
  dir("build_folder"){
      bat "run_build_windows.bat"
  }
}

or

stage('build') {
  bat "c://some/folder/run_build_windows.bat"
}

Windows paths some time are bizarre :s . Anyway, linux is the best choice to host jenkins.

这篇关于为什么我无法在 Windows 10 中运行的詹金斯管道中运行批处理文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆