jenkins 如何在同一阶段运行多个when条件

bnlyeluc  于 2023-03-17  发布在  Jenkins
关注(0)|答案(1)|浏览(285)

我有Jenkins pipeline,要求用户选择要部署的实验室,如果选中“Set is developed deployment”(设置已开发部署),则要求pipeline执行其他操作。当尝试在同一阶段下必须多个“when”条件时,它无法运行

pipeline {
    parameters {
        extendedChoice(name: 'Lab to update', description: 'Select the labs on which you wish to deploy', multiSelectDelimiter: ',',
                type: 'PT_CHECKBOX', value: 'lab1,Lab2', visibleItemCount: 5)
        extendedChoice(name: 'Set is develop deployment', description: '', multiSelectDelimiter: ',',
                type: 'PT_CHECKBOX', value: 'Develop deployment', visibleItemCount: 5)
    }
    agent { label 'ansible.cyber.local' }

    stages {
        stage ('Deploy pyspark_dependencies on Lab 1 ') {
            when  { environment name: 'Lab to update', value: 'Lab1' }
            steps {
                echo "Stage-1"              
            }
            when  { environment name: 'Set is develop deployment', value: 'Develop deployment' }
            steps {
                echo "Stage-2"              
            }
        }
    }
m2xkgtsf

m2xkgtsf1#

不能在步骤级别添加when条件。必须将附加步骤移至新阶段或使用if条件。请看以下示例。

pipeline {
    agent any
    parameters {
        extendedChoice(name: 'LabToUpdate', description: 'Select the labs on which you wish to deploy', multiSelectDelimiter: ',',
                type: 'PT_CHECKBOX', value: 'Lab1,Lab2', visibleItemCount: 5)
        extendedChoice(name: 'IsDev', description: 'Set is develop deployment', multiSelectDelimiter: ',',
                type: 'PT_CHECKBOX', value: 'Develop deployment', visibleItemCount: 5)
    }
    stages {
        stage('Test') {
            when  { expression { params.LabToUpdate == 'Lab1' } }
            steps {
                script {
                    echo "Do something"
                    if(params.IsDev == 'Develop deployment') {
                        echo "Do something additinal if second param checked."
                    }
                }
            }
        }
    }
}

相关问题