当我的 linting 脚本返回错误时,如何让我的 Azure DevOps Pipeline 构建失败?
Posted
技术标签:
【中文标题】当我的 linting 脚本返回错误时,如何让我的 Azure DevOps Pipeline 构建失败?【英文标题】:How do I get my Azure DevOps Pipeline build to fail when my linting script returns an error? 【发布时间】:2019-08-07 05:34:42 【问题描述】:我正在使用 Azure Pipelines GitHub 插件来确保拉取请求通过我的 linting。但是,我刚刚提出了一个测试拉取请求,但我的 linting 失败了,但 Azure 管道成功了。
这是我的azure-pipelines.yml
# Node.js with React
# Build a Node.js project that uses React.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/javascript
trigger:
- master
pool:
vmImage: 'Ubuntu-16.04'
steps:
- task: NodeTool@0
inputs:
versionSpec: '8.x'
displayName: 'Install Node.js'
- script: |
npm install
npm run lint # Mapped to `eslint src` in package.json
npm run slint # `stylelint src` in package.json
npm run build
displayName: 'npm install and build'
这是我知道在npm run lint
上失败的分支上的(部分)输出
> geograph-me@0.1.0 lint /home/vsts/work/1/s
> eslint src
/home/vsts/work/1/s/src/js/components/CountryInput.js
26:45 error 'onSubmit' is missing in props validation react/prop-types
27:71 error 'onSubmit' is missing in props validation react/prop-types
✖ 2 problems (2 errors, 0 warnings)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! geograph-me@0.1.0 lint: `eslint src`
npm ERR! Exit status 1 # Exit status 1, yet the build succeeds?
npm ERR!
npm ERR! Failed at the geograph-me@0.1.0 lint script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/vsts/.npm/_logs/2019-03-16T05_30_52_226Z-debug.log
> geograph-me@0.1.0 slint /home/vsts/work/1/s
> stylelint src
> geograph-me@0.1.0 build /home/vsts/work/1/s
> react-scripts build
Creating an optimized production build...
Compiled successfully.
# Truncated...
如您所见,linter 运行良好并捕获了我的故意错误(我删除了一个 prop 类型验证),并以代码 1 退出。
然而构建只是继续其愉快的方式。
我需要怎么做才能使这样的 linting 错误停止我的构建并且不返回成功?
提前谢谢你。
【问题讨论】:
您是否想出一种方法来在管道的摘要选项卡中报告这些 lint 错误/警告? 【参考方案1】:这意味着您的脚本“吞下”了退出代码并正常退出。您需要在脚本中添加一个检查,以捕获 npm run lint
的退出代码并使用相同的退出代码退出,例如:
- script: |
npm install
npm run lint # Mapped to `eslint src` in package.json
if [ $? -ne 0 ]; then
exit 1
fi
npm run slint # `stylelint src` in package.json
npm run build
【讨论】:
您能否提供一个链接来解释您的以下代码周围的语法?if [ $? -ne 0 ]; then
.
答案中有解释。美元? - 返回最后一个退出代码
那是 azure-pipelines 语法 ($?) 吗?它来自哪里,npm?我想对此进行进一步阅读。
@Zac askubuntu.com/questions/324423/…
有人知道如何将这些错误/警告报告到管道的摘要选项卡吗? (或其他地方,除了向下钻取)【参考方案2】:
您也可以使用 npm 任务。默认设置是在出现错误时使构建失败。我遇到了同样的问题,以下对我有用:
- task: Npm@1
displayName: 'Lint'
inputs:
command: 'custom'
customCommand: 'run lint'
来自tasks 的文档:
- task: string # reference to a task and version, e.g. "VSBuild@1"
condition: expression # see below
continueOnError: boolean # 'true' if future steps should run even if this step fails; defaults to 'false'
enabled: boolean # whether or not to run this step; defaults to 'true'
timeoutInMinutes: number # how long to wait before timing out the task
【讨论】:
【参考方案3】:上述两种方法都不适用于我的特定场景(Windows 构建代理,想要运行自定义 linting 脚本,不想在 package.json 中使用脚本)
如果您只是从节点脚本中抛出错误,管道会将其视为失败的步骤。
管道 yml:
- script: 'node lint-my-stuff'
displayName: 'Lint my stuff'
lint-my-stuff.js
// run eslint, custom checks, whatever
if (/* conditions aren't met */)
throw new Error('Lint step failed')
console.log('Lint passed')
【讨论】:
以上是关于当我的 linting 脚本返回错误时,如何让我的 Azure DevOps Pipeline 构建失败?的主要内容,如果未能解决你的问题,请参考以下文章
@typescript-eslint 编译错误不同于 linting 错误