我不确定我为 Jenkins 管道步骤准备的一些语法,我想用我定义的变量替换 shell 脚本中的部分命令行参数(用于文件名)
for loop
我认为我上面的内容将查找变量 sh "npx cypress run --config-file cypress/cypress.$e2eEnvironment.json --env $envCredentials || true"
但我不想要那样,因为我需要将 .json 保留在 cli arg 中。例如。如果我将 $e2eEnvironment 值设置为 $e2eEnvironment.json
,则该值应构建为 integration
有人可以在这里指导我吗?
答案 0 :(得分:1)
您可以在 Groovy 中使用 ${<varName>}
语法在字符串插值中强制执行变量解析的确切名称。在您的示例中:
sh "npx cypress run --config-file cypress/cypress.${e2eEnvironment}.json --env $envCredentials || true"
将解析为:
sh "npx cypress run --config-file cypress/cypress.integration.json --env $envCredentials || true"
根据需要,而不是解析变量名称 e2eEnvironment.json
的分配值。