groovy Jenkins -如何创建基于Jenkins管道的条件

nvbavucw  于 2022-12-17  发布在  Jenkins
关注(0)|答案(1)|浏览(221)

我试图创建两个独立的管道,它们应该基于一个布尔值运行。我可以创建布尔条件来帮助运行管道,但我没有得到一个正确的方法来创建管道。

stage('creating env') {
    steps {
       dir("${WORKSPACE}/test") {
           echo 'creating work env'
           catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
                sh "python3 -u test.py"
           }
       }
    }
}

   static def test_1() {
              echo 'Running test_1'
              stage('TEST_1') { 
                   parallel {
                       steps {
                          script {
                             def startTime = new Date()
                             long l1 = startTime.getTime()
                             Date endTime = new Date()
                            long l2 = endTime.getTime()
                            long diff = l2 - l1
                            long secondInMillis = 1000
                            long minuteInMillis = secondInMillis * 60
                            long hourInMillis = minuteInMillis * 60
                            long elapsedHours = diff / hourInMillis
                            diff = diff % hourInMillis
                            long elapsedMinutes = diff / minuteInMillis
                            diff = diff % minuteInMillis
                            long elapsedSeconds = diff / secondInMillis
                            if (elapsedMinutes>10){
                                buildbadgevdi.setColor('orange')
                            }
                            buildbadgevdi.setStatus("${elapsedMinutes}min ${elapsedSeconds}sec ")

stage('Tests') {
        if (params.Test_1) {
            Test_1()
        }
        if (params.Test_2) {
            Test_2()
        }

    }
}
    post {

我想在Test_2()中使用不同的测试集重复相同的逻辑。

**上述逻辑失败,出现错误:**此处不应有方法定义。请在适当的位置定义方法,或者尝试使用块/闭包

8cdiaqws

8cdiaqws1#

在声明性语法DSL管道中执行Groovy代码需要一个script块:

stage('Tests') {
  script {
    if (params.Test_1) {
      Test_1()
    }
    if (params.Test_2) {
      Test_2()
    }
  }
}

请注意,在此问题解决后可能会出现更多问题,但这将解决您的错误并修复当前问题中出现的问题。

相关问题