簡體   English   中英

當我的 linting 腳本返回錯誤時,如何使我的 Azure DevOps Pipeline 構建失敗?

[英]How do I get my Azure DevOps Pipeline build to fail when my linting script returns an error?

我正在使用 Azure Pipelines GitHub 插件來確保拉取請求通過我的 linting。 但是,我剛剛提出了一個測試拉取請求,但我的 linting 失敗了,但 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 運行良好並捕獲了我的故意錯誤(我刪除了 prop 類型驗證),並以代碼 1 退出。

然而,構建只是繼續它的快樂方式。

我需要做什么才能使這樣的 linting 錯誤停止我的構建並且不返回成功?

先感謝您。

這意味着您的腳本會“吞下”退出代碼並正常退出。 您需要在腳本中添加一個檢查,以捕獲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

您還可以使用 npm 任務。 默認設置是在出現錯誤時使構建失敗。 我遇到了同樣的問題,以下對我有用:

- task: Npm@1
  displayName: 'Lint'
  inputs:
    command: 'custom'
    customCommand: 'run lint'

任務的文檔中:

- 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

上述兩種方法都不適用於我的特定場景(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')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM