简体   繁体   中英

java.lang.NoSuchMethodError: No such DSL method 'stages' found among steps

I got an error " java.lang.NoSuchMethodError: No such DSL method stages found among steps " when I run the script below.

Can someone tell me why and how I can solve the issue?

Basically I need to do foreach of my stage and it has to be run parallel. Can we do it on declarative pipeline?

def services = ["A", "B", "C"]
 
def parallelStagesMap = services.collectEntries {
    ["${it}" : generateStage(it)]
}

def generateStage(service) {
    return {
        stage("${service}") {
            agent { label 'dotnet-agent' }
            environment {
                PROJECT = "myProject.${service}"
                ARTIFACT_NAME = "${GIT_COMMIT}-${service}.zip"
            }
            stages {
                stage('Build-${service}') {
                    steps {
                        include 'automation/jenkins/${service}/buildAndArtifact'
                    }
                }

                stage('Test-${service}') {
                    steps {
                        dir("src/${PROJECT}.Tests") {
                            sh("dotnet test ${PROJECT}.Tests.csproj -c ${ASPNETCORE_ENVIRONMENT}")
                        }
                    }
                }
                stage('Deploy-${service}') {
                    steps {
                        include 'automation/jenkins/${service}/deploy'
                    }
                }
            }
        }
    }
}

pipeline {
    agent { label 'dotnet-agent' }
    stages {
        stage('Set credentials') {
            stages {
                stage('Build') {
                    steps {
                        script {
                            parallel parallelStagesMap
                        }
                     }
                }           
            }
        }
    }
}

I don´t know if you can have nested stages like you are trying to do.

This seems to work:

def services = ["A", "B", "C"]
 
def parallelStagesMap = services.collectEntries {
    ["${it}" : generateStage(it)]
}

def generateStage(service) {
    return {
        echo "build $service"
        echo "test $service"
        echo "deploy $service"
    }    
}

pipeline {
    agent any 
    environment {
        PROJECT = "myProject.${service}"
        ARTIFACT_NAME = "${GIT_COMMIT}-${service}.zip"
    }
    stages {
        stage('Build') {
            steps {
                script {
                    parallel parallelStagesMap
                }
             }
        }   
    }
}

Output:

[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] script
[Pipeline] {
[Pipeline] parallel
[Pipeline] { (Branch: A)
[Pipeline] { (Branch: B)
[Pipeline] { (Branch: C)
[Pipeline] echo
build A
[Pipeline] echo
test A
[Pipeline] echo
deploy A
[Pipeline] }
[Pipeline] echo
build B
[Pipeline] echo
test B
[Pipeline] echo
deploy B
[Pipeline] }
[Pipeline] echo
build C
[Pipeline] echo
test C
[Pipeline] echo
deploy C
[Pipeline] }
[Pipeline] // parallel
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM