且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 Jenkins 管道中使用的 groovy 脚本中调用 shell 命令

更新时间:2023-12-05 14:00:04

您不应将 Jenkins Pipeline Groovy 视为常规 Groovy 代码.所有 Pipeline Groovy(截至目前)始终在 Jenkins 主服务器上执行..execute() 永远不会在您期望的地方执行,Jenkins 脚本安全限制会阻止您,以及 Jenkins 上的 CPS 转换 Groovy 引擎都是不期望能够使用完整的 Groovy 语言的原因在管道代码中.

You should not treat Jenkins Pipeline Groovy as regular Groovy code. All Pipeline Groovy (as of right now) is always executed on the Jenkins master. .execute() will never execute where you expect it to, Jenkins script security restrictions will block you, and CPS transformation Groovy engine on Jenkins are all reasons to not expect to be able to use the full Groovy language in pipeline code.

这是来自 Google 网上论坛的相关帖子斯蒂芬·康诺利:

Here is a relevant post from Google Groups by Stephen Connolly:

管道不是常规的.

它是一个建立在 Groovy 之上的 CPS 引擎...它可能看起来像 Groovy,它有时甚至可能像 Groovy 一样走路和嘎嘎叫,但如果你接受它不是,你的生活将会变得无限美好 时髦的.

It is a CPS engine built on top of Groovy... it may look like Groovy, it may even sometimes walk and quack like Groovy, but your life will be infinitely better if you just accept that it is NOT Groovy.

如果您想编写惯用的 Groovy,您可以去全球共享库,即使在那里您也可能遇到问题,除非您真正了解 CPS 的魔力及其全部含义.

Global Shared Libraries is where you go if you want to write idiomatic Groovy, and even there you can hit issues unless you truly understand the CPS magic and its full implications.

使用管道作为最终的编排粘合层,您的生活会轻松很多

Use pipeline as a final orchestration glue layer and your life will be much easier

我建议您阅读整个线程,因为它应该表明您不应将管道代码视为普通的 Groovy 代码.

I recommend you read that entire thread as it should demonstrate that you should not treat the Pipeline code as normal Groovy code.