5

我想运行 npm 命令npm audit作为 ci 构建的一部分,并以某种方式在 jenkins ci 构建中显示输出。

如果发现严重漏洞,我想通过返回非零退出代码来使当前构建失败。

4

2 回答 2

2

这篇文章的启发,我通过创建自定义的“编译器警告解析器”实现了这一点;

  1. 安装警告插件
  2. 配置自定义解析器:
    管理 Jenkins -> 配置系统 -> '编译器警告'部分
    名称:NPM 审计分析器
    链接名称:NPM 审计分析器
    趋势报告名称:检测到的漏洞
    正则表达式[\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); 
  1. 配置作业;
  2. 添加执行 shell构建步骤:
npm install  
# parseable report for 'compile warning post build step'  
npm audit --parseable >> npm_audit_report_parseable.txt || true # suppress npm audit error code
  1. 添加[已弃用]在构建步骤后扫描编译器警告:
    '扫描工作区文件'->文件模式:npm_audit_report_parseable.txt
    '扫描工作区文件'->解析器:NPM 审计分析器

您可以选择配置“健康/状态阈值”;这决定了构建何时失败,以及何时将其标记为不稳定


另一个解决方案:使用依赖检查插件

  1. 安装依赖检查插件
  2. 配置一个“依赖检查”全局工具:
    管理 Jenkins -> 全局工具配置 -> '依赖检查' -> '添加依赖检查'
  3. 配置作业;
  4. 添加调用依赖检查构建步骤:
    依赖检查安装:选择配置的全局工具
    我还配置了其他参数 --scan /non/existing/dir以避免在已安装的包相关文件(如“演示”、“文档”等)中发现漏洞,因为“依赖检查插件”执行我们不需要的完整递归文件扫描,因为 npm 扫描程序已经找到“npm 审计”结果。
  5. 添加Publish Dependency-Check results post build step:
    XML Report : **/dependency-check-report.xml
于 2019-11-19T10:33:11.870 回答
1

我不知道有任何插件可以实现这一点,但一些手动解析可能会起作用(如果您使用的是管道)。

def output = sh script: "npm audit", returnStdout: true
def summary = output.split("\n")[-1] //get the summary from the last line
...

然后,您可以使用一些正则表达式或其他字符串操作来查找关键后的数字。您还可以使用 archiveArtifacts 将整个输出保存在构建历史记录中。

于 2019-10-11T21:05:38.963 回答