根据用户选择设置变量

时间:2019-07-15 14:40:39

标签: jenkins jenkins-plugins jenkins-groovy

在詹金斯大学,我有一个选择:

choice(name: 'SERVICE', choices: ['SERVICE1', 'SERVICE2', 'SERVICE3', 'SERVICE4', 'SERVICE5', 'SERVICE6'], description: 'service')

是否可以根据上述选择设置变量?

类似这样的东西:

IF SERVICE == SERVICE1 then SERVICE_ID == SERVICE1_ID
IF SERVICE == SERVICE2 then SERVICE_ID == SERVICE2_ID

我正在为此寻找一个插件,但是我不介意像上面那样硬编码到jenkinsfile中。

1 个答案:

答案 0 :(得分:0)

完成

pipeline {
    agent any


    parameters {
      choice(name: 'SERVICE', choices: ['Service1', 'Service2', 'Service3', 'Service4', 'Service5'], description: 'service')
    }
  stages {
      stage('Stage 1') {
          steps {
              echo "This is Stage 1"

                script {
                    if (env.SERVICE == 'Service1') {
                        echo 'You selected Service1'
                    } else {
                        echo 'You selected Some other service'
                    }
                }

          }
      }

    stage('Stage 2') {
        steps {
              echo "This is Stage 2"

                script {
                    if (env.SERVICE == 'Service1') {env.SERVICE_ID = 'Service1_ID'} 
                    if (env.SERVICE == 'Service2') {env.SERVICE_ID = 'Service2_ID'} 
                    if (env.SERVICE == 'Service3') {env.SERVICE_ID = 'Service3_ID'} 
                    if (env.SERVICE == 'Service4') {env.SERVICE_ID = 'Service4_ID'} 
                    if (env.SERVICE == 'Service5') {env.SERVICE_ID = 'Service5_ID'} 
                        echo "Service is ${env.SERVICE}"
                        echo "Service ID is ${env.SERVICE_ID}"

                        // Here goes some script you want to run 
                        // For example:
                        // container('docker') {
                        //  sh """            
                        //      some bash command with ${env.SERVICE_ID}
                        //  """
                        //  }

                }
        }
    }

    stage('Stage 3') {
      steps {
              echo "This is Stage 3"
      }
    }

    stage ('Stage 4') {
      steps {
              echo "This is Stage 4"
      }
    }
  }
}