如何将 Shell 变量值访问到 Groovy 管道脚本
How to access Shell variable value into Groovy pipeline script
现在我是 Shell、Jenkins、Groovy 管道的新手。我的要求是我正在将文件文本读取到 shell 脚本下的一个变量中,我需要将这个变量值从 shell 脚本中传递出去并在 Groovy 脚本中使用。
这是我的代码:
stages
{
stage('example')
{
steps
{
script
{
sh'''
#!/bin/bash
set +x
readVal=$(<$WORKSPACE/test.json)
echo "$readVal" //this is priting my entire json successfully
echo 'closing shell script'
'''
println WORKSPACE /// this is printing my workspace value successfully
println readVal // not working
println env.readVal // not working
println $readVal // not working
}
}
}
}
如何从shell中取出readVal
的值来访问?
见Jenkins, Pipeline Best Practices:
a. Solution: Instead of using [a complex function], use a shell step and return the standard out. This shell would look something like this:
def JsonReturn = sh label: '', returnStdout: true, script: 'echo "$LOCAL_FILE"| jq "$PARSING_QUERY"'
示例
pipeline {
agent any
stages {
stage('example') {
steps {
script {
def readVal = sh script: 'echo "READ_VAL_VALUE"', returnStdout: true
echo readVal
println readVal
}
}
}
}
}
控制台输出
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in C:\Users\jenkins\AppData\Local\Jenkins\.jenkins\workspace\Pipeline project
[Pipeline] {
[Pipeline] stage
[Pipeline] { (example)
[Pipeline] script
[Pipeline] {
[Pipeline] sh
+ echo READ_VAL_VALUE
[Pipeline] echo
READ_VAL_VALUE
[Pipeline] echo
READ_VAL_VALUE
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
现在我是 Shell、Jenkins、Groovy 管道的新手。我的要求是我正在将文件文本读取到 shell 脚本下的一个变量中,我需要将这个变量值从 shell 脚本中传递出去并在 Groovy 脚本中使用。
这是我的代码:
stages
{
stage('example')
{
steps
{
script
{
sh'''
#!/bin/bash
set +x
readVal=$(<$WORKSPACE/test.json)
echo "$readVal" //this is priting my entire json successfully
echo 'closing shell script'
'''
println WORKSPACE /// this is printing my workspace value successfully
println readVal // not working
println env.readVal // not working
println $readVal // not working
}
}
}
}
如何从shell中取出readVal
的值来访问?
见Jenkins, Pipeline Best Practices:
a. Solution: Instead of using [a complex function], use a shell step and return the standard out. This shell would look something like this:
def JsonReturn = sh label: '', returnStdout: true, script: 'echo "$LOCAL_FILE"| jq "$PARSING_QUERY"'
示例
pipeline {
agent any
stages {
stage('example') {
steps {
script {
def readVal = sh script: 'echo "READ_VAL_VALUE"', returnStdout: true
echo readVal
println readVal
}
}
}
}
}
控制台输出
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in C:\Users\jenkins\AppData\Local\Jenkins\.jenkins\workspace\Pipeline project
[Pipeline] {
[Pipeline] stage
[Pipeline] { (example)
[Pipeline] script
[Pipeline] {
[Pipeline] sh
+ echo READ_VAL_VALUE
[Pipeline] echo
READ_VAL_VALUE
[Pipeline] echo
READ_VAL_VALUE
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS