Jenkins声明性管道 - 参数中的用户变量

时间:2018-04-02 10:33:30

标签: jenkins-plugins jenkins-pipeline

我有一个Jenkinsfle。我需要从 Build with Parameters 插件传递参数,并在脚本中定义变量。我无法上班。这可能是语法问题?

#!/usr/bin/env groovy

pipeline {
  agent any

  stages {
   stage('Config API (dev)') {
      steps {
        script {
            apiName = "config_API"
            taskDefinitionFamily = "mis-core-dev-config"
            taskDefinition = "mis-core-dev-config"
            if (params.apiName.contains('Stop Task')) {
            build(job: 'Stop ECS Task (utility)',
            parameters: [
            string(name: 'region', value: params.region),
            string(name: 'cluster', value: params.cluster),
            string(name: 'family', value: params.taskDefinitionFamily)
            ])
          }
            else if (params."${apiName}".contains('Start Task')) {
            build(job: 'Start ECS Task (utility)',
            parameters: [
            string(name: 'region', value: params."${region}"),
            string(name: 'cluster', value: params."${cluster}"),
            string(name: 'taskDefinition', value: params."${taskDefinition}"),
            string(name: 'containerInstanceIds', value: params."${containerInstanceIdsToStartOn}")
            ])
          }
            else if (params."${apiName}" == null || params."${apiName}" == "") {
            echo "Did you forget to check a box?"
          }
        }
      }
    }

我的Build with parameters变量在GUI中设置为`字符串变量

containerInstanceIdsToStartOn = "463b8b6f-9388-4fbd-8257-b056e28c0a43"
region = "eu-west-1"
cluster = "mis-core-dev"

我哪里错了?

1 个答案:

答案 0 :(得分:1)

在参数块中定义参数:

pipeline {
    agent any

    parameters {
        string(defaultValue: 'us-west-2', description: 'Provide your region', name: 'REGION')
    }


    stages {
        stage('declarative'){
            steps {
                print params.REGION
                 sh "echo ${params.REGION}"
            }
        }

        stage('scripted'){
            steps {
                script {
                    print params.REGION
                }
            }
        }
    }
}

输出:

[Pipeline] {
[Pipeline] stage
[Pipeline] { (declarative)
[Pipeline] echo
us-west-2
[Pipeline] sh

[test] Running shell script
+ echo us-west-2
us-west-2
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (scripted)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
us-west-2
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS