eekfonky
eekfonky

Reputation: 855

Jenkins Declarative Pipeline - user variables in params

I have a Jenkinsfle. I need to pass parameters from Build with Parameters plugin and also have variables defined within the script. I cannot get either to work. It may be a syntax issue?

#!/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?"
          }
        }
      }
    }

My Build with parameters variables are set in the GUI as `string variables,

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

Where am I going wrong?

Upvotes: 0

Views: 4136

Answers (1)

lvthillo
lvthillo

Reputation: 30723

Define your parameters in a parameter block:

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

Output:

[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

Upvotes: 5

Related Questions