在 Jenkins 管道中使用按参数切换

Using switch by parameter in Jenkins pipeline

我正在尝试确定在哪里上传使用开关购买的构建工件,这取决于参数。

pipeline {
    agent any
    parameters{

    // Some other params..

    choice(name: 'DeployTo', choices: ['Diawi', 'GoogleDrive', 'None'], description: 'Whenever to upload build to diawi or google drive. Set to None if you do not want to upload build')

    }
    stages {

        // Some other stages..

        stage('Deploy') {
            steps {
                sh "echo 'Deploy build (${BUILD_NUMBER}) to ${params.DeployTo}'"
                switch(params.DeployTo) {
                    case "Diawi":
                    sh """
                        node ${params.DiawiUploadScriptPath} ${getBuildPath()} ${params.DiawiUploadToken}
                    """
                    break
                    case "GoogleDrive":
                    sh """
                        node ${params.GDriveUploadScriptPath} ${params.ProjectName} ${BUILD_NUMBER} ${getBuildPath()}
                    """
                    break
                    default:
                    break
                }
            }
        }
    }
}

但是我得到了错误

WorkflowScript: 43: Expected a step @ line 43, column 5.
                switch(params.DeployTo) {

那么,我如何 select 使用 switch 语句和 string/choice 参数的步骤?

谢谢!

您不能在 declarative pipeline. For that you have to use scripted pipelines 中使用 switch 或任何其他 groovy 控制流。

在声明式 pieline 中,您最多可以使用 when step for simple control flow logic, or wrap a scripted pipeline in a script 步骤。