Azure DevOps如何配置ESLint代码检查
解读
国内大厂与金融、运营商、车企的 Azure DevOps Server(原名 TFS)或 Azure DevOps Services 占比极高,面试官问“怎么配”并不是让你背文档,而是考察三点:
- 能否把 ESLint 无缝嵌入现有 CI/CD 流水线,而不破坏原有构建节奏;
- 能否让结果在“代码扫描”页、PR 状态检查、邮件/企微/飞书通知里“看得见、卡得住”;
- 能否兼顾 monorepo、私有 npm 源、代理、缓存、权限等国内真实痛点。
因此,回答必须给出“端到端落地骨架”,并预留可剪裁的扩展点。
知识点
- Azure DevOps Pipeline 语法(vmImage、pool、task、script、condition、template)
- npm 与私有 registry 认证(.npmrc、npmAuthenticate@0)
- ESLint 运行时优化——缓存、增量扫描、format 与 output 文件
- 国内镜像与代理设置(registry、sass_binary_site、electron_mirror)
- SARIF 上传与 Azure DevOps Security Tab 集成(PublishCodeAnalysisResults@1)
- Branch Policy 质量门(Build Validation、自动评论、状态检查)
- 多项目 monorepo 的并发扫描与合并报告
- 失败策略:warning 变 error、阈值熔断(max-warnings=0)、增量 diff 扫描
- 产物保留与合规审计(artifact retention、RBAC、Build Service 权限)
答案
下面给出一条可直接落地的 yaml 流水线,兼顾 Azure DevOps Services 与本地 Azure DevOps Server 2022,并假设代码库根目录已包含 .eslintrc.js 与 package.json。
# file: eslint-ci.yml
trigger:
branches:
include: [main, develop, 'feature/*']
pr:
branches:
include: [main, develop]
pool:
vmImage: 'ubuntu-22.04' # 国内可用 East Asia 代理源,Server 可换成自托管 'linux-docker'
variables:
NODE_VERSION: '18.17.1'
SARIF_FILE: '$(Build.ArtifactStagingDirectory)/eslint.sarif'
# 私有 npm 源,若用官方源可删除
NPM_REGISTRY: 'https://pkgs.dev.azure.com/$(System.TeamFoundationCollectionUriName)/_packaging/myfeed/npm/registry/'
# 缓存 key,package-lock 变化即失效
CACHE_KEY: 'npm | "$(Agent.OS)" | package-lock.json'
steps:
# 1. 缓存 node_modules,国内网络提速
- task: Cache@2
inputs:
key: $(CACHE_KEY)
path: '$(Pipeline.Workspace)/.npm'
displayName: 'npm cache'
# 2. 安装 Node
- task: NodeTool@0
inputs:
versionSpec: $(NODE_VERSION)
displayName: 'Use Node $(NODE_VERSION)'
# 3. 注入私有源认证
- task: npmAuthenticate@0
inputs:
workingFile: '.npmrc'
displayName: 'npm authenticate'
# 4. 安装依赖
- script: |
npm ci --cache $(Pipeline.Workspace)/.npm --prefer-offline --no-audit
displayName: 'npm ci'
# 5. 运行 ESLint,生成 SARIF
- script: |
npx eslint . --ext .js,.jsx,.ts,.tsx \
--format @microsoft/eslint-formatter-sarif \
--output-file $(SARIF_FILE) \
--max-warnings 0
displayName: 'ESLint scan'
continueOnError: false # 任何 error 或 warning 超限即失败
# 6. 上传 SARIF 到 Security Tab(Services 原生支持;Server 需装“SARIF SAST”扩展)
- task: PublishCodeAnalysisResults@1
inputs:
sarifFile: $(SARIF_FILE)
displayName: 'Publish SARIF'
# 7. 可选:把原始报告当 artifact 留存,供审计
- publish: $(SARIF_FILE)
artifact: 'ESLintReport'
displayName: 'Archive report'
Branch Policy 配置(在浏览器完成,回答时口述即可):
- 进入 Project Settings → Repositories → main → Branch Policies;
- 添加 Build Validation,选择上述流水线,勾选“Required”与“Reset code reviewer approval when new pushes are made”;
- 打开“Check for comment resolution”,确保 ESLint 失败即阻塞合并;
- 若团队使用飞书/企微,可在 Pipeline 里加一条“Webhook@1”任务,把失败摘要推送到群机器人。
至此,ESLint 结果既能在 Security Tab 可视化,也能在 PR 页面直接阻断,满足国内合规“质量门”要求。
拓展思考
- 增量扫描:在 PR 场景,只扫
git diff origin/main...HEAD涉及的文件,可缩短 60% 时间;可用eslint --cache --cache-location .eslintcache --cache-strategy content配合自定义脚本过滤 diff。 - 分级质量门:允许老代码逐步达标,通过
eslint-nibble或lint-filter只检查变更行;在流水线里解析git diff -U0提取行号,再调 ESLint JSON formatter 做交集。 - 多语言 monorepo:若仓库含 Vue、React、Node 子包,可在根流水线用
each循环调用子目录 template,把各子包 SARIF 合并成单个文件再上传,避免 Security Tab 被覆盖。 - 私有规则包:把公司自定义的
@mycompany/eslint-config发布到 Azure Artifacts,流水线里npm install -D @mycompany/eslint-config即可统一规则,避免每个项目拷贝配置文件。 - 灰度与自动修复:对 warning 级别规则开启
--fix,在 PR 里由机器人自动 commitstyle: auto fix,再触发重跑;通过condition: eq(variables['Build.Reason'], 'PullRequest')区分 CI 与 PR 行为。 - 国产化替代:若客户要求脱离 GitHub,可把
@microsoft/eslint-formatter-sarif源码同步到私有 npm 源,并写 PowerShell 脚本把 SARIF 转成 Azure DevOps Server 的 Generic Test Results 格式,实现离线可视化。
掌握以上扩展,面试时即可从“能跑起来”谈到“能规模化、能审计、能灰度”,直接对标国内一线 DevOps 平台的真实痛点。