我想用命令的输出填充groovy变量“提交器”:
def committer = utils.sh("curl -s -u \${J_USER}:\${J_PASS} \${env.BUILD_URL}/api/json | python -mjson.tool | grep authorEmail | awk '{print \$2}' | tr -d '"|,' ")由于Jenkins (詹金斯-26133)中已知的问题,所以不可能这样做,而只能使用命令的退出状态填充变量。
我做了这两个函数:
def gen_uuid(){
randomUUID() as String
}
def sh_out(cmd){ // As required by bug JENKINS-26133
String uuid = gen_uuid()
sh """( ${cmd} )> ${uuid}"""
String out = readFile(uuid).trim()
sh "set +x ; rm ${uuid}"
return out
}这些函数允许我将shell命令包装在sh_out(COMMAND)中,在后台,我使用了上面提到的已知问题链接中建议的解决方法,这意味着运行命令,同时将它的输出重定向到一个文件(在我的函数中是一个随机文件名),然后将它读入变量中。
因此,在我的管道开始时,我加载了以return this;结尾的函数文件,如下所示:
fileLoader.withGit('git@bitbucket.org:company/pipeline_utils.git', 'master', git_creds, ''){
utils = fileLoader.load('functions.groovy');
}这就是为什么您在命令中看到的"utils.sh_out“,但是当我在Jenkins管道中使用上面所示的命令时,我会得到以下错误:
/home/ubuntu/workspace/-6870-bitbucket-integration-ECOPKSSBUJ6HCDNM4TOY77X7UTZ@tmp/durable-006d5c7e/script.sh: 2: /home/ubuntu/workspace/-6870-bitbucket-integration-ECOPKSSBUJ6HCDNM4TOY77X7UTZ@tmp/durable-006d5c7e/script.sh: Bad substitution在shell中运行命令可以正常工作:
$ curl -s -u user:password http://IPADDR:8080/job/COMPANY_BitBucket_Integration/job/research/job/COMPANY-6870-bitbucket-integration/3/api/json/api/json | python -mjson.tool | grep authorEmail | awk '{print $2}' | tr -d '"|,'
user@email.com我怀疑这与最终的tr命令有关,与我在那里所做的角色转义有关,但无论我尝试什么失败,都有人有主意吗?
发布于 2017-06-14 13:05:29
根据现在的文档,sh支持std输出。
我知道我没有直接回答你的问题,但我建议使用groovy来解析json。
您正在尝试从json获取authorEmail的值。
如果来自/api/json的响应如下(仅举一个例子):
{
"a":{
"b":{
"c":"ccc",
"authorEmail":"user@email.com"
}
}
}然后用groovy来获取athorEmail
def cmd = "curl -s -u \${J_USER}:\${J_PASS} \${env.BUILD_URL}/api/json"
def json = sh(returnStdout: true, script: cmd).trim()
//parse json and access it as an object (Map/Array)
json = new groovy.json.JsonSlurper().parseText(json)
def mail = json.a.b.athorEmail你可以接收到java.io.NotSerializableException在这里解释了
所以我就这样修改了代码:
node {
def json = sh(
returnStdout: true,
script: "curl -s -u \${J_USER}:\${J_PASS} \${env.BUILD_URL}/api/json"
).trim()
def mail = evaluateJson(json, '${json.a.b.authorEmail}')
echo mail
}
@NonCPS
def evaluateJson(String json, String gpath){
//parse json
def ojson = new groovy.json.JsonSlurper().parseText(json)
//evaluate gpath as a gstring template where $json is a parsed json parameter
return new groovy.text.GStringTemplateEngine().createTemplate(gpath).make(json:ojson).toString()
}https://stackoverflow.com/questions/44542800
复制相似问题