MIDE11
MIDE11

Reputation: 3530

Access jenkins parameters from parameterized build in shell script called from jenkins job

I choose to This project is parameterized, and I have two params: query and index. Then I choose to execute shell option:

#!/bin/sh

curl -XPOST 'localhost:9200/_reindex?pretty' -H 'Content-Type: application/json' -d'
{
  "source": {
    "index": "{$index}", "query": "{$query}"}}
  },
  "dest": {
    "index": "myindex_output"
  }
}
'

I see that it doesn't read the params at all, and I get: "type" : "index_not_found_exception", "resource.id" : "{$index}"

How should I do it correctly?

Upvotes: 2

Views: 5940

Answers (2)

burnettk
burnettk

Reputation: 14047

i think your issue might be on this line:

"index": "{$index}", "query": "{$query}"}}

i think you might want:

"index": "$index", "query": "$query"}}

you probably should add something like this to the top of the shell script to see what's going on:

echo "$query"
echo "$index"

the full answer is, assuming this declarative pipeline:

pipeline {
  agent { label 'docker' }
  parameters {
    string(name: 'query', defaultValue: 'hot_query_value', description: 'query value')
    string(name: 'index', defaultValue: 'hot_index_value', description: 'index value')
  }
  stages {
    stage('build') {
      steps {
        withEnv(["query=${params.query}" ]) {
          sh('./shell_script')
        }
      }
    }
  }
}

and this shell script:

#!/bin/sh

echo "in shell script"
echo "query is: $query"

echo "anything query or index-related in env:"
env | egrep -i "query|index"

output in console is:

in shell script
query is: hot_query_value
anything query or index-related in env:
index=hot_index_value
ANOTHER_QUERY_ENV_VAR=hot_query_value
query=hot_query_value

even if you're not using a Jenkinsfile (why do you hate yourself so much? :D), the parameters will already be available as environment variables.

Upvotes: 1

Krishan Kant
Krishan Kant

Reputation: 195

Since it's a environment variable. Use below syntax:

$ENV:index 
$ENV:query

Upvotes: 0

Related Questions