根据用户选择设置变量

Set variable depending on users choice

在詹金斯我有一个选择:

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 中。

完成

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"
      }
    }
  }
}