jenkins java.lang.NoSuchMethodError:在步骤中未找到此类DSL方法"阶段

woobm2wo  于 2023-02-03  发布在  Jenkins
关注(0)|答案(1)|浏览(184)

我收到一个错误“*java.lang.NoSuchMethodError:当我运行下面的脚本时,在步骤 *”中没有找到这样的DSL方法阶段。
有人能告诉我为什么以及如何解决这个问题吗?
基本上我需要做我的每个阶段,它必须并行运行。我们可以在声明管道上做吗?

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

cbwuti441#

我不知道你是否可以像你试图做的那样有嵌套的阶段。
这似乎行得通:

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

输出:

[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

相关问题