为什么我无法在 windows 10 中 运行 jenkins 管道 运行ning 中批处理文件?

why am I not able to run batch file in jenkins pipeline running in windows 10?

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

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

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

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

单行

如果您需要使用 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()
}

这是一个高级示例:

多行

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 ..."
}

示例:

调用批处理文件

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

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

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.

参考:https://thenucleargeeks.com/2020/11/24/how-to-run-batch-scripts-commands-in-jenkinsfile/

node {
        stage('Preparation') {
           //Preparations and checkout the code 
        }
        stage('Build') {
           //Build command     
        }
        stage('Post build action'){
     
       bat '''  ECHO Hello World  '''
    
        }
     }