从jenkins作业调用的参数化内置shell脚本访问jenkins参数

从jenkins作业调用的参数化内置shell脚本访问jenkins参数,jenkins,Jenkins,我选择这个项目是参数化的,我有两个参数:查询和索引。然后我选择执行shell选项: #!/bin/sh curl -XPOST 'localhost:9200/_reindex?pretty' -H 'Content-Type: application/json' -d' { "source": { "index": "{$index}", "query": "{$query}"}} }, "dest": { "index": "myindex_output"

我选择
这个项目是参数化的
,我有两个参数:
查询
索引
。然后我选择执行shell选项:

#!/bin/sh

curl -XPOST 'localhost:9200/_reindex?pretty' -H 'Content-Type: application/json' -d'
{
  "source": {
    "index": "{$index}", "query": "{$query}"}}
  },
  "dest": {
    "index": "myindex_output"
  }
}
'
我看到它根本不读取参数,我得到:
“type”:“index\u not\u found\u exception”,“resource.id”:“{$index}”


我应该如何正确地做

因为它是一个环境变量。使用以下语法:

$ENV:index 
$ENV:query

因为它是一个环境变量。使用以下语法:

$ENV:index 
$ENV:query

我想你的问题可能在这一行:

"index": "{$index}", "query": "{$query}"}}
我想你可能想要:

"index": "$index", "query": "$query"}}
您可能应该在shell脚本的顶部添加类似的内容,以查看发生了什么:

echo "$query"
echo "$index"
完整答案是,假设此声明性管道:

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')
        }
      }
    }
  }
}
这个shell脚本:

#!/bin/sh

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

echo "anything query or index-related in env:"
env | egrep -i "query|index"
控制台中的输出为:

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

即使您没有使用Jenkins文件(为什么您这么讨厌自己?:D),参数也可以作为环境变量使用。

我认为您的问题可能在这一行:

"index": "{$index}", "query": "{$query}"}}
我想你可能想要:

"index": "$index", "query": "$query"}}
您可能应该在shell脚本的顶部添加类似的内容,以查看发生了什么:

echo "$query"
echo "$index"
完整答案是,假设此声明性管道:

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')
        }
      }
    }
  }
}
这个shell脚本:

#!/bin/sh

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

echo "anything query or index-related in env:"
env | egrep -i "query|index"
控制台中的输出为:

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
即使您没有使用Jenkins文件(为什么这么讨厌自己?:D),这些参数也可以作为环境变量使用