我想运行 npm 命令npm audit作为 ci 构建的一部分,并以某种方式在 jenkins ci 构建中显示输出。
如果发现严重漏洞,我想通过返回非零退出代码来使当前构建失败。
受这篇文章的启发,我通过创建自定义的“编译器警告解析器”实现了这一点;
[\w+-]+\s+([\w+-]+)\s+([\w\.-@]+)\s+(>=\s)*([\w\.-@]+)\s+(.+?(?=http))([\w\.-@]+)\s+(.*)
import hudson.plugins.warnings.parser.Warning
import hudson.plugins.analysis.util.model.Priority
priority = Priority.HIGH;
if ( "low" == matcher.group(2) ) {
priority = Priority.LOW;
}
else if ( "moderate" == matcher.group(2) ) {
priority = Priority.NORMAL;
}
String msg = "Vulnerability found in '" + matcher.group(1) + "' (" + matcher.group(6) + ") , prio: " + matcher.group(2) + " Fix: " + matcher.group(5) + " info: " + matcher.group(5);
return new Warning('package.json', 0, 'NSP Warning', matcher.group(1), msg, priority);
npm install
# parseable report for 'compile warning post build step'
npm audit --parseable >> npm_audit_report_parseable.txt || true # suppress npm audit error code
您可以选择配置“健康/状态阈值”;这决定了构建何时失败,以及何时将其标记为不稳定。
另一个解决方案:使用依赖检查插件
--scan /non/existing/dir
以避免在已安装的包相关文件(如“演示”、“文档”等)中发现漏洞,因为“依赖检查插件”执行我们不需要的完整递归文件扫描,因为 npm 扫描程序已经找到“npm 审计”结果。我不知道有任何插件可以实现这一点,但一些手动解析可能会起作用(如果您使用的是管道)。
def output = sh script: "npm audit", returnStdout: true
def summary = output.split("\n")[-1] //get the summary from the last line
...
然后,您可以使用一些正则表达式或其他字符串操作来查找关键后的数字。您还可以使用 archiveArtifacts 将整个输出保存在构建历史记录中。