如何将 Groovy 变量传递给 Jenkinsfile 中的 python 程序
How to pass the Groovy variable to python program in Jenkinsfile
我需要将 groovy 变量传递给 python 模块。
我试过下面的代码,但它不起作用。在 echo 中,它正在打印结果,但在将其传递给 python 时,它被视为空值。
stage('Run python code') {
steps {
script {
ACTIVE_CONNECTORS = sh(script: """
test="a,b,c"
echo "**********test value*********"
echo $test
TEMP=$(python -c \
'import json; print(${test}); SPLIT_PUB=${data}; JSON_SPLIT_PUB=json.dumps(SPLIT_PUB); \
JSON_SPLIT_PUB=JSON_SPLIT_PUB.replace("rename_value",$"{test}"); \
JSON_SPLIT_PUB=JSON_SPLIT_PUB.replace("name","${params.app}");
""".stripIndent(), returnStdout: true).trim()
}
}
}
您可以使用环境变量,然后使用 os.environ 在 python 程序中调用它。一个例子:
stage('Test') {
steps {
script{
env.TEST = 'a,b,c'
ACTIVE_CONNECTORS = sh(script: """
python -c "import os;\
print(os.environ['TEST'])"
""".stripIndent(), returnStdout: true).trim()
println ACTIVE_CONNECTORS
}
}
}
我需要将 groovy 变量传递给 python 模块。
我试过下面的代码,但它不起作用。在 echo 中,它正在打印结果,但在将其传递给 python 时,它被视为空值。
stage('Run python code') {
steps {
script {
ACTIVE_CONNECTORS = sh(script: """
test="a,b,c"
echo "**********test value*********"
echo $test
TEMP=$(python -c \
'import json; print(${test}); SPLIT_PUB=${data}; JSON_SPLIT_PUB=json.dumps(SPLIT_PUB); \
JSON_SPLIT_PUB=JSON_SPLIT_PUB.replace("rename_value",$"{test}"); \
JSON_SPLIT_PUB=JSON_SPLIT_PUB.replace("name","${params.app}");
""".stripIndent(), returnStdout: true).trim()
}
}
}
您可以使用环境变量,然后使用 os.environ 在 python 程序中调用它。一个例子:
stage('Test') {
steps {
script{
env.TEST = 'a,b,c'
ACTIVE_CONNECTORS = sh(script: """
python -c "import os;\
print(os.environ['TEST'])"
""".stripIndent(), returnStdout: true).trim()
println ACTIVE_CONNECTORS
}
}
}