使用jenkins管道在字符串比较之间切换不同的参数值

时间:2018-05-22 16:58:18

标签: jenkins groovy jenkins-pipeline

如何使用字符串比较在具有不同选项的参数之间切换?

我有这样的事情:

#!/usr/bin/env groovy
pipeline {  
agent any
   stages {
        stage ('Class') {
        steps {
            script{
                switch(env.class) {
                   case { if ("${class}" == "a") }:
                        echo "match"
                    break
                    }
                }
            }
        }
    }
}

在我的詹金斯工作中,我有一个名为string parameter的{​​{1}},这对于不同的工作是不同的。 为此,如果class,我想做一些步骤。 有人,请让我知道如何实现这一目标 感谢

1 个答案:

答案 0 :(得分:0)

声明性管道与Scripted管道的工作方式不同,但您可能会对此脚本管道示例有所启发:

#!groovy
node {
    properties([
            parameters([
                    choice(choices: 'a\nb',
                            description: 'Pick a letter.',
                            name: 'class')
            ])
    ])

    try {
        stage('Class') {
            switch (params.class) {
                case 'a':
                    result = "matching a"
                    // do some things if choice a is given
                    echo "Step 1"
                    break
                case 'b':
                    result = "matching b"
                    // do some things if choice b is given
                    echo "Step 2"
                    break
            }
            echo "${result}"

        }
    }
    catch (Throwable t) {
        // do not swallow exception
        echo "Exception: ${t}"
        throw t;
    }
}
相关问题