我想创建一个配置映射,它使用JSON对象作为其值
JSON对象看起来像这样,(variable name = manifestJSON
)
{
"appRepoVersionsMap": {
"repoA": "1.0.0.131",
"repoB": "1.0.0.7393"
},
"deployerVersion": "49",
"openshiftConfigCommitId": "o76to87y"
}
然后我想创建一个获取此JSON对象的configmap,并将其添加为configmap的值。
我试图让它发挥作用的命令是
def osCmd = "create configmap manifest-config" +
" --from-literal=manifest.os_config_branch=${envVars.OS_CONFIG_BRANCH}" +
" --from-literal=manifest.os_server=${envVars.OPENSHIFT_SERVER_URL}"
" --from-literal=manifest.os_manifest=${manifestJSON}"
os.call(osCmd)
OpenShift客户端出现以下错误:
10:23:37 error: cannot add key manifest.os_manifest, another key by that name already exists: map[manifest.os_config_branch:deployment-orchestrator manifest.os_server:<snipped>, manifest.os_manifest:appRepoVersionsMap:repoA:1.0.0.131 ].
因此groovy或OpenShift都会在JSON对象中看到JSON对象而无法处理它。
我试图避免使用--from-file
,因为我必须写入磁盘然后运行命令,我担心这会导致Jenkins环境中出现多个项目的多个部署问题。
答案 0 :(得分:0)
解决方案最终变得相当简单,我在尝试各种解决方案时过度思考角色逃脱。
在创建配置图(或秘密)时使用JSON作为值是在JSON对象本身周围添加''
def osCmd = "create configmap manifest-config" +
" --from-literal=manifest.os_config_branch=${envVars.OS_CONFIG_BRANCH}" +
" --from-literal=manifest.os_server=${envVars.OPENSHIFT_SERVER_URL}"
" --from-literal=manifest.os_manifest='${manifestJSON}'" // <----- single quotes
os.call(osCmd)
这允许创建配置图,我从OpenShift方面确认配置图与我期望的清单值一起出现。