当linting脚本返回错误时,如何使Azure DevOps Pipeline构建失败?

时间:2019-03-16 07:28:31

标签: azure azure-devops pipeline devops azure-pipelines

我正在使用Azure Pipelines GitHub加载项来确保拉取请求能够通过我的操作。但是,我刚刚发出了一个测试请求请求,但失败了,但Azure Pipeline成功了。

这是我的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运行良好,并捕获了我的故意错误(我删除了道具类型验证),并退出代码1。

但是构建只是继续进行着快乐的方式。

我应该怎么做才能使这种掉毛错误使我的构建停滞不前,而不返回成功?

谢谢。

3 个答案:

答案 0 :(得分:3)

这意味着您的脚本“吞噬”了退出代码并正常退出。您需要在脚本中添加一条检查指令,以捕获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

答案 1 :(得分:1)

您还可以使用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

答案 2 :(得分:1)

以上两种方法均不适用于我的特定情况(Windows构建代理,想运行自定义的脚本脚本,不想在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')