Jenkins管道作业:从字符串参数设置休眠时间?

Juh*_*nen 23 groovy jenkins jenkins-pipeline

我是詹金斯管道工作的新手,我面临一个我无法解决的问题.

我有一个硬编码sleep秒值的舞台:

stage ("wait_prior_starting_smoke_testing") {
  echo 'Waiting 5 minutes for deployment to complete prior starting smoke testing'
  sleep 300 // seconds
}
Run Code Online (Sandbox Code Playgroud)

但我想通过job(string)参数提供时间参数SLEEP_TIME_IN_SECONDS.但无论我尝试过什么,我都无法让它发挥作用.

如何将字符串参数转换为int时间参数?

Juh*_*nen 23

最后我确实找到了一种方法来完成这项工作:

stage ("wait_prior_starting_smoke_testing") {
    def time = params.SLEEP_TIME_IN_SECONDS
    echo "Waiting ${SLEEP_TIME_IN_SECONDS} seconds for deployment to complete prior starting smoke testing"
    sleep time.toInteger() // seconds
}
Run Code Online (Sandbox Code Playgroud)


Sys*_*nin 20

这个页面的小改进:

sleep(time:3,unit:"SECONDS")如果您有兴趣指定睡眠的时间单位,也可以使用

https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#sleep-sleep

  • 不要省略 `time:` 和 `unit:` 否则 Jenkins 会出错,并显示 `Arguments to "sleep" must be明确命名` (2认同)

小智 7

尝试这个。它对我有用。

stage ("wait_for_testing")
{
   sh 'sleep 300'
}
Run Code Online (Sandbox Code Playgroud)

使用“sh”,您可以使用 shell 启动虚拟终端。如果您需要在一个终端中编写其他命令,请编写 ; 命令后。例如:

sh 'pwd; sleep 300; echo "Hello World"'
Run Code Online (Sandbox Code Playgroud)