简体   繁体   English

如何使用 groovy 在 jenkins windows slave 上执行 CMD 命令?

[英]How to execute CMD command on jenkins windows slave using groovy?

I am trying to execute cmd /c echo hello using groovy on a jenkins slave running on windows.我正在尝试在 Windows 上运行的 jenkins slave 上使用 groovy 执行cmd /c echo hello

Here is my groovy:这是我的常规:

node('WINDOWS-SLAVE-1') {
    def cmd_command = "cmd /c echo hello"
    cmd_command.execute()
}

And I can see in the job logs that it is indeed running on that windows slave: 'Running on WINDOWS-SLAVE-1'我可以在作业日志中看到它确实在该 Windows 从站上运行“在 WINDOWS-SLAVE-1 上运行”

But I get an error: java.io.IOException: error=2, No such file or directory但我收到一个错误: java.io.IOException: error=2, No such file or directory

And if I try to run linux like ls -l it works fine.如果我尝试像ls -l一样运行 linux,它可以正常工作。 Shows me the files of my master.给我看我主人的文件。

How can I execute this CMD command on my windows jenkins slave from my groovy script?如何从我的 groovy 脚本在我的 windows jenkins slave 上执行这个 CMD 命令?

If your intention is to execute a command on a given node, you need to use one of the Jenkins Pipeline's steps designed to execute shell scripts (eg sh or bat ).如果您打算在给定节点上执行命令,则需要使用 Jenkins Pipeline 的步骤之一来执行 shell 脚本(例如shbat )。 You need to be aware that any Groovy code in your Jenkinsfile is always executed on the master node:您需要注意 Jenkinsfile 中的任何 Groovy 代码始终在master节点上执行:

"1. Except for the steps themselves, all of the Pipeline logic, the Groovy conditionals, loops, etc execute on the master. Whether simple or complex! Even inside a node block!" “1. 除了步骤本身,所有流水线逻辑、Groovy 条件、循环等都在主node上执行。无论简单还是复杂!甚至在node块内!”


Source: https://jenkins.io/blog/2017/02/01/pipeline-scalability-best-practice/#fundamentals资料来源: https : //jenkins.io/blog/2017/02/01/pipeline-scalability-best-practice/#fundamentals

node('WINDOWS-SLAVE-1') {
    bat "cmd /c echo hello"
}

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

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