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

时间:2018-11-05 06:50:50

标签: windows jenkins jenkins-pipeline devops

我正在尝试运行jenkins工作空间内存在的批处理脚本。我已经编写了如下的groovy脚本

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

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

2 个答案:

答案 0 :(得分:4)

Jenkins旨在在后台模式下执行shell命令,而不是在 interactive 模式下执行。

单行

如果您需要使用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路径有时是奇怪的:s。无论如何,Linux是托管jenkins的最佳选择。

答案 1 :(得分:0)

参考: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  '''
    
        }
     }