groovy - 如何从 Jenkins 文件中读取 package.json 中的命令
问题描述
我有一个 package.json
"scripts": {
"pact:tests": <script>
}
我需要检查 JenkinsFile(Groovy) 的 package.json 中是否存在 pact:tests。如果它存在运行协议测试,则跳过它。
尝试了几种方法
def pactExists = sh "grep \"pact:tests\" package.json | wc -l"
if(pactExists > 0)
run pact
else
skip
但是上面的代码返回 pactExists = null
尝试 2:
def pactExists2 = sh "${grep pact:tests package.json}"
但这直接运行我认为的命令
尝试 3:
def packageJSON = readJSON file: 'package.json'
def pactExists = packageJSON.scripts.pact:tests
但是我收到一个错误,因为在协议之后使用 ':':
有没有办法将输出保存在某个变量中?
解决方案
如果要捕获sh
步骤的结果,则需要将returnStdout
选项设置为true
,例如
def pactExists = sh(script: "grep \"pact:tests\" package.json | wc -l", returnStdout: true)
或者,如果您想使用readJSON
step,那么您需要使用引号定义一个键,例如
def pactExists = packageJSON.scripts.'pact:tests'
推荐阅读
- wordpress - 如何在wordpress中根据类别(分类)检索帖子(书籍)?
- javascript - 使用 React-Router 尝试根据 state 重定向用户时遇到问题
- reactjs - Algolia React 默认值及操作问题
- c# - 查找两个向量之间的斜率3
- python - Django 管理员总是重置 docker 容器中的字段值更改
- javascript - Typescript/Jest 无法识别 Web Workers
- ionic-framework - 如何使用带有 ionic3 条形码扫描器插件的自定义布局
- javascript - ES6 Promise then() 实现处理程序返回值有什么用?
- php - 如何从编码的字符串内容创建 pdf 文件 - Codeigniter
- docker - 如何在一个容器中运行多个 JMX 文件?