Jenkins - 如何创建动态并行阶段?

问题描述 投票:0回答:1

我基本上有 4 个curl 命令来触发 webhook,除了一个参数之外,它们大致相同。因此,我想提取这个curl命令并创建一个并行阶段,它可以迭代每个唯一的参数,然后调用curl命令。

我有这样的东西:

 stage('Push Artifactory Images to ECR') {
                    steps {
                        script {
                            def images = [
                                ['stageName': 'Push Metadata Service Image', 'image': env.METADATA_SERVICE_IMAGE],
                                ['stageName': 'Push UI Image', 'image': env.UI_IMAGE],
                                ['stageName': 'Push AWS Batch Default Image', 'image': env.BATCH_IMAGE],
                                ['stageName': 'Push Studio Sample Image', 'image': env.NOTEBOOK_IMAGE]
                            ]

                            def parallelStages = [:]

                            images.each { image ->
                                parallelStages[image.stageName] = {
                                    stage(image.stageName) {
                                        steps {
                                            script {
                                                sh """
                                                curl ${env.ECR_PUSH_URL} -X POST -H 'Content-Type: application/json' \
                                                -d '{"parameters":{"aws_account_id": "${env.AWS_ACCOUNT_ID}", "business_service": "${env.BUSINESS_SERVICE}", "moniker": "${env.MONIKER}", "region": "${env.REGION}", "role_arn": "arn:aws:iam::${env.AWS_ACCOUNT_ID}:role/Deployer", "sourceRepository": "${image.image}"}}'
                                                """
                                            }
                                        }
                                    }
                                }
                            }

但这会产生以下错误:

Also:   java.lang.NoSuchMethodError: No such DSL method 'steps' found among steps 
loops jenkins groovy parallel-processing
1个回答
1
投票

大概是这样的:

def imageStage(image) {
    steps {
        script {
            sh """
            curl ${env.ECR_PUSH_URL} -X POST -H 'Content-Type: application/json' \
            -d '{"parameters":{"aws_account_id": "${env.AWS_ACCOUNT_ID}", "business_service": "${env.BUSINESS_SERVICE}", "moniker": "${env.MONIKER}", "region": "${env.REGION}", "role_arn": "arn:aws:iam::${env.AWS_ACCOUNT_ID}:role/Deployer", "sourceRepository": "${image.image}"}}'
            """
        }
    }
}

def images = [
    ['stageName': 'Push Metadata Service Image', 'image': env.METADATA_SERVICE_IMAGE],
    ['stageName': 'Push UI Image', 'image': env.UI_IMAGE],
    ['stageName': 'Push AWS Batch Default Image', 'image': env.BATCH_IMAGE],
    ['stageName': 'Push Studio Sample Image', 'image': env.NOTEBOOK_IMAGE]
]

def parallelStages = images.collectEntries{ image->
    [ image.stageName, { imageStage(image) } ]
}

parallel parallelStages
© www.soinside.com 2019 - 2024. All rights reserved.