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

oyt4ldly  于 8个月前  发布在  Jenkins
关注(0)|答案(4)|浏览(127)

我是Jenkins Pipeline工作的新手,我面临着一个我无法解决的问题。
我有一个硬编码的sleep秒值的阶段:

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

但是我想通过作业(字符串)参数SLEEP_TIME_IN_SECONDS提供时间参数。但无论我怎么努力,我都无法让它发挥作用。
如何将字符串参数转换为int时间参数?

5tmbdcev

5tmbdcev1#

  • 此页面的小改进:*

如果您对指定睡眠的时间单位感兴趣,也可以使用sleep(time:3,unit:"SECONDS")
https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#sleep-sleep

t1rydlwq

t1rydlwq2#

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

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
}
kgsdhlau

kgsdhlau3#

试试这个,对我有用.

stage ("wait_for_testing")
{
   sh 'sleep 300'
}

使用“sh”可以用shell启动虚拟终端。如果你需要在一个终端写其他命令,写;在命令之后。举例来说:

sh 'pwd; sleep 300; echo "Hello World"'
yqlxgs2m

yqlxgs2m4#

在流水线执行期间,有许多方法可以注入睡眠。下面的管道在Jenkins版本2.319.2中运行良好。

pipeline {
agent any

stages {
    stage('Sleep') {
        steps {
            script {
                print('I am sleeping for a while')
                sleep(30)    
            }
            
        }
    }
     stage('Please let me Sleep 30 seconds more') {
        steps {
            echo 'Please let me Sleep 30 seconds more'
            sleep(time:30, unit: "SECONDS")
        }
    }
    stage('After Sleep') {
        steps {
            echo 'Already woke up!'
        }
    }
}
}

相关问题