首页 > 解决方案 > 在管道中使用参数时 Jenkins 语法错误

问题描述

我有一个詹金斯设置,每个节点都有一个用于 Prod 和 UAT 的从节点。我需要运行一个作业,如果用户选择 uat1 或 uat2 中的任何一个,该作业就会在 UAT 节点上运行。如果用户选择 prod,它应该在 Prod 节点中运行作业。该工作涉及 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: 'git@bitbucket.org: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: Missing required parameter: "parameterDefinitions" @ line 9, column 17. parameters { ^

WorkflowScript:10:缺少必需参数:“trim”@ 第 10 行,第 21 列。字符串(^

2 个错误

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

标签: jenkinsjenkins-pipelinejenkins-groovy

解决方案


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

以下应该修复您的语法错误:

pipeline {
    agent {
        label 'master'
    }
    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'
                )
     } // close parameters
    stages {
        stage('Get Inputs') {
    ...

推荐阅读