简体   繁体   English

执行批处理脚本在 Jenkins 管道作业中不起作用

[英]Execute Batch Script not working in Jenkins Pipeline Job

I have created a powershell script which will transfer(using winscp.dll) the files from Jenkins windows server to Linux server.我创建了一个 powershell 脚本,它将(使用 winscp.dll)将文件从 Jenkins Windows 服务器传输到 Linux 服务器。 In Jenkins batch command, I have executed that powershell script and it works fine.在 Jenkins 批处理命令中,我已经执行了那个 powershell 脚本并且它工作正常。

But when i tried the same in Jenkins pipeline job, it calls the powershell script and comes to the next step.但是当我在 Jenkins 管道作业中尝试相同的操作时,它会调用 powershell 脚本并进入下一步。 Its not waiting for powershell script response.它不等待 powershell 脚本响应。

bat 'powershell.exe -ExecutionPolicy Bypass  "D:\\Test\\Deploymentscripts\\PowerShellScript\\FileTransfer.ps1 $env:EndMarket $env:Environment"'

I have tried with another powershell script which will connect to Linux server and execute some commands.我曾尝试使用另一个 powershell 脚本,该脚本将连接到 Linux 服务器并执行一些命令。 It works fine in pipeline job它在管道工作中运行良好

Kindly guide me to fix this issue.请指导我解决这个问题。

Interesting.有趣。 Your problem doesn't seem to be in your script because you already explained that it works in a batch job.您的问题似乎不在您的脚本中,因为您已经解释过它可以在批处理作业中使用。

I don't know how your pipeline is written, but I would suggest taking a look to Stage, Lock and Milestone which is probably what you need.我不知道您的管道是如何编写的,但我建议您查看Stage、Lock 和 Milestone ,这可能是您所需要的。

The stage step is a primary building block in Pipeline, dividing the steps of a Pipeline into explicit units and helping to visualize the progress using the "Stage View" plugin阶段步骤是流水线中的主要构建块,将流水线的步骤划分为明确的单元,并使用“阶段视图”插件帮助可视化进度

I guess you could add a stage block like this one in your pipeline:我想你可以在你的管道中添加一个像这样的阶段块:

 stage("Previous Step") {

      // Some previous step
    }

stage("Wait for Script Execution") {

  // Call your script
  bat 'powershell.exe -ExecutionPolicy Bypass  "D:\\Test\\Deploymentscripts\\PowerShellScript\\FileTransfer.ps1 $env:EndMarket $env:Environment"'
}

 stage("Next Step") {

      // Script already finished its execution
    }

But without your pipeline info is just a guessing.但是没有您的管道信息只是猜测。 Also to improve your script compatibility avoiding "bat and ExcutionPolicy" and using the PowerShell plugin , with that plugin you could simplify your code like this:此外,为了提高脚本兼容性,避免“bat 和 ExcutionPolicy”并使用PowerShell 插件,使用该插件,您可以像这样简化代码:

powershell -File your_script.ps1

EDIT: I forgot to mention that you can try a different alternative to powershell and the winscp lib using "scp" direct compatibility between Windows and Linux, I'm talking about Cygwin.编辑:我忘了提到你可以尝试使用 Windows 和 Linux 之间的“scp”直接兼容性来替代 powershell 和 winscp lib,我说的是 Cygwin。

With Cygwin installed (with scp) you can use scp as it was a Linux box and a bash script instead powershell:安装 Cygwin(使用 scp)后,您可以使用 scp,因为它是一个 Linux 机器和一个 bash 脚本,而不是 powershell:

D:/cygwin-64/bin/run.exe /usr/bin/bash -lic \"/home/user/file.sh\" 

In this case I'm running a script silently through Cygwin within a Jenkins Project with a "Run Windows Batch" option.在这种情况下,我通过带有“运行 Windows 批处理”选项的 Jenkins 项目中的 Cygwin 静默运行脚本。 In the script you can add shell commands and the scp instructions you want.在脚本中,您可以添加 shell 命令和所需的 scp 指令。

It may seems a little bit more complex but adds more flexibility to perform Windows - Linux tasks.它可能看起来有点复杂,但增加了执行 Windows - Linux 任务的灵活性。

I've detailed more examples in my blog , you may find it useful.我在我的博客中详细介绍了更多示例,您可能会发现它很有用。

As avvi mentioned you should check if that variables are being loaded.正如 avvi 提到的,您应该检查是否正在加载该变量。

You are not calling the script correctly.您没有正确调用脚本。 You are passing in $Env in the powershell invocation which is a powershell variable.您在 powershell 调用中传入 $Env,这是一个 powershell 变量。

You are in batch mode so should be passing in %.您处于批处理模式,因此应该传入 %。

bat 'powershell.exe -ExecutionPolicy Bypass  "D:\\Test\\Deploymentscripts\\PowerShellScript\\FileTransfer.ps1" "%EndMarket%" "%Environment%"'

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

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