gpt4 book ai didi

Jenkins Pipeline sleep(10) 阻止功能完成

转载 作者:行者123 更新时间:2023-12-04 20:28:58 26 4
gpt4 key购买 nike

我在 Jenkinsfile 中发现了一个奇怪的 Groovy 代码问题:

@NonCPS
def test() {
println "Start"
sleep(10)
println "Stop"
}

问题是,在休眠 10 秒后,代码永远不会到达 println "Stop" .
看起来, sleep 在 10 秒后返回并运行下一个管道步骤。

输出只是:
[Pipieline] echo
Start
[Pipeline] sleep
Sleeping for 10 sec
[Pipeline] }
... next pipeline steps

有没有人有同样的问题?

最佳答案

当您调用 sleep(10)在您的 Groovy 脚本中,Workflow CPS 插件执行 SleepStep 而不是 DefaultGroovyStaticMethods.sleep(Object self, long time) .您的问题是由 @NonCPS 引起的功能(感谢 mkobit 的建议!)。考虑以下示例:

node {
stage("Test") {
test()
}
}

@NonCPS
def test() {
echo "Start"
sleep(5)
echo "Stop"
}

输出:
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/test-pipeline
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] echo
Start
[Pipeline] sleep
Sleeping for 5 sec
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

没有 Stop从管道运行中回显。现在,如果我们只删除 @NonCPS我们在管道中调用的函数的注释:
node {
stage("Test") {
test()
}
}

def test() {
echo "Start"
sleep(5)
echo "Stop"
}

事情发生了变化, Stop按预期得到回显:
Started by user admin
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/test-pipeline
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] echo
Start
[Pipeline] sleep
Sleeping for 5 sec
[Pipeline] echo
Stop
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

有关使用 @NonCPS 的更多信息请阅读以下 Best Practices For Pipeline Code文章。

关于Jenkins Pipeline sleep(10) 阻止功能完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51215348/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com