在管道中使用参数时出现詹金斯语法错误

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

我有一个jenkins设置,每个Prod和UAT都有一个从属节点。我需要运行一个作业,以便如果用户选择uat1或uat2之一,则该作业将在UAT节点上运行。如果用户选择生产,则应在“生产”节点中运行作业。这项工作涉及1.从git下载要执行的代码2.根据选择的是UAT还是Prod,ec2实例应承担适当的角色]

在下面的代码中,我尝试运行为作业设置参数以及在主节点上选择节点的第一阶段。此后,我在脚本内部使用了一个脚本来为agentLabel变量提供一个值,以便在后续步骤中使用它,以便作业在适当的nde上运行。

    pipeline {
    agent {
        label 'master'
    }

    stages {
        stage('Get Inputs') {
            steps {
                parameters {
                    string(
                        name: 'branch',
                        defaultValue: 'master',
                        description: 'Branch of code to be deployed'
                    )
                    choice(
                        name: 'service',
                        choices: 'calcs-service\naudits-service\nrate-alerts',
                        description: 'Service to be cleaned'
                    )
                    choice(
                        name: 'environment',
                        choices: 'uat1\nuat2\nprod',
                        description: 'Environement whose services need cleanup'
                    )
                }
                script {
                    switch("${environment}") {
                        case uat1:
                            agentLabel = 'uat'
                        case uat2:
                            agentLabel = 'uat'
                        case prod:
                            agentLabel = 'prod'
                    }
                }
            }
        }
        stage('Clone cleanup_lambda_functions') {
            agent { 
                label "${agentLabel}"
            }
            steps {
                checkout([$class: 'GitSCM',
                    branches: [[name: "*/${branch}"]],
                    doGenerateSubmoduleConfigurations: false,
                    extensions: [],
                    submoduleCfg: [],
                    userRemoteConfigs: [[url: '[email protected]:canstar-dev/devops.git']]])
            }
        }
        stage('Clean up Lambda un UAT') {
            agent { 
                label "${agentLabel}"
            }
            when {
                expression {"${agentLabel}" == 'uat'}
            }
            steps {
                 withAWS(role: 'arn:aws:iam::000000000006:role/cns-iam-role-delete-lambda-version') {
                    ansiColor('xterm') {
                        sh '''
                            set -x
                            set -eo pipefail
                            echo "Assumed Support role for:"
                            echo "${environment}"
                        '''
                    }
                }
            }
        }
        stage('Clean up Lambda un Prod') {
            agent { 
                label "${agentLabel}"
            }
            when {
                expression {"${agentLabel}" == 'prod'}
            }
            steps {
                 withAWS(role: 'arn:aws:iam::0000000000005:role/cns-iam-role-delete-lambda-version') {
                    ansiColor('xterm') {
                        sh '''
                            set -x
                            set -eo pipefail
                            echo "Assumed Support role for Prod"
                        '''
                    }
                }
            }
        }

    }

    post {
        cleanup {
            cleanWs()
        }
    }
}

但是这失败并显示错误:

org.codehaus.groovy.control.MultipleCompilationErrorsException:启动失败:WorkflowScript:9:缺少必需的参数:第9行,第17列,“ parameterDefinitions”。参数{^

WorkflowScript:10:缺少必填参数:“修剪” @第10行,第21栏串(^

2个错误

我对编写jenkinsfiles还是很陌生,所以如果有人可以指出正确的方向,那将非常有帮助。

jenkins jenkins-pipeline jenkins-groovy
1个回答
0
投票

parameters必须在管道范围内声明,而不是在单个阶段的范围内声明。 (Source)。

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