Declarative pipeline when condition in post

前端 未结 3 829
無奈伤痛
無奈伤痛 2020-12-30 21:35

As far as declarative pipelines go in Jenkins, I\'m having trouble with the when keyword.

I keep getting the error No such DSL method \'when\' found a

3条回答
  •  不思量自难忘°
    2020-12-30 22:08

    Ran into the same issue with post. Worked around it by annotating the variable with @groovy.transform.Field. This was based on info I found in the Jenkins docs for defining global variables.

    e.g.

    #!groovy
    
    pipeline {
        agent none
        stages {
            stage("Validate") {
                parallel {
                    stage("Ubuntu") {
                        agent {
                            label "TEST_MACHINE"
                        }
                        steps {{
                            sh "run tests command"
                            recordFailures('Ubuntu', 'test-results.xml')
                            junit 'test-results.xml'
                        }
                    }
                }
            }
        }
        post { 
            unsuccessful { 
                notify()
            }
        }
    }
    
    // Make testFailures global so it can be accessed from a 'post' step
    @groovy.transform.Field
    def testFailures = [:]
    
    def recordFailures(key, resultsFile) {
        def failures = ... parse test-results.xml script for failures ...
        if (failures) {
            testFailures[key] = failures
        }
    }
    
    def notify() {
        if (testFailures) {
            ... do something here ...
        }
    }
    

提交回复
热议问题