现在我是 Shell、Jenkins、Groovy pipeline 的新手。我的要求是我正在将文件文本读取到 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
}
}
}
}
如何获得readVal
的值脱离 shell 来访问?
See Jenkins,管道最佳实践 https://www.jenkins.io/doc/book/pipeline/pipeline-best-practices/#avoiding-complex-groovy-code-in-pipelines:
A。解决方案:不要使用[复杂的函数],而是使用 shell 步骤并返回标准输出。这个 shell 看起来像这样:
def JsonReturn = sh label: '', returnStdout: true, script: 'echo "$LOCAL_FILE"| jq "$PARSING_QUERY"'
Example
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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)