gpt4 book ai didi

json - 使用 ${params} 在 JSON 文件中写入所有参数 jenkins

转载 作者:行者123 更新时间:2023-12-04 16:18:22 31 4
gpt4 key购买 nike

我是 shell 脚本的初学者,我在 jenkins 作业参数化时遇到了一些问题。我想使用 ${params} 将带有参数的 jenkins 作业管道构建的所有参数写入 JSON 文件中!
就我而言,我有 4 个参数(apis:multi-select,name:string,version:single-select and status:Boolean),有脚本 Jenkinsfile

pipeline {
agent any
stages {
stage('Build') {
steps {
script{
sh "./test.sh ${params}"
}
}
}
}
}

test.sh的内容
#!/bin/bash
echo $@ > file.json

Jenkins 的输出
+ ./test.sh [apis:dev,qa,prod, name:CC, version:g3, status:true]

结果在 file.json
SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data

我的问题是如何格式化输出以在 file.json 中获得干净的结果?请我需要帮助。

最佳答案

将此添加到脚本的顶部:

import groovy.json.JsonOutput

然后使用这一行而不是 sh "./test.sh ${params}" :
writeFile file: 'params.json', text: JsonOutput.toJson(params)

这使用了一个 Groovy 库和一个 native Jenkins method用于写入文件,这意味着您不需要使用 sh方法。

关于json - 使用 ${params} 在 JSON 文件中写入所有参数 jenkins,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60363363/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com