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

zag*_*agd 4 pipeline azure devops azure-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'
Run Code Online (Sandbox Code Playgroud)

这是我知道失败的分支上的输出(的一部分) 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...
Run Code Online (Sandbox Code Playgroud)

如您所见,linter运行良好,并捕获了我的故意错误(我删除了道具类型验证),并退出代码1。

然而,构建只是继续其快乐的方式。

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

先感谢您。

4c7*_*b41 8

这意味着您的脚本会“吞噬”退出代码并正常退出。您需要在脚本中添加一个检查,以捕获您的退出代码,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
Run Code Online (Sandbox Code Playgroud)

  • 您能否提供一个链接来解释以下代码的语法?`如果[$? -ne 0 ]; 然后`。 (2认同)

小智 5

您还可以使用 npm 任务。默认设置是在出现错误时使构建失败。我遇到了同样的问题,以下对我有用:

- task: Npm@1
  displayName: 'Lint'
  inputs:
    command: 'custom'
    customCommand: 'run lint'
Run Code Online (Sandbox Code Playgroud)

任务的文档中:

- 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
Run Code Online (Sandbox Code Playgroud)