Gitlab CI:即使NPM错误也能建立成功

时间:2017-02-13 15:17:27

标签: node.js npm gitlab-ci gitlab-ci-runner

我目前的gitlab-ci.yaml文件配置有三个阶段: - 测试 - 部署 - 发布

如果我或开发人员忘记--save npm包,-test阶段未正确完成但仍返回“构建已通过”,则有时会发生NPM错误。然后它将盲目地部署和发布,因为管道尚未被取消。

如何制作NPM ERR!停止当前阶段并停止其余的运行?

我通过故意省略bluebird包来模拟这个问题。它影响了整个测试/部署/发布过程,并且在我的服务器上部署并运行了错误的代码:

  myClass.js
    .selectSpecificXYZ()
      1) Should return an object containing SUCCESS
      2) should return an object regardless of the input
      3) should return an object with count 0
      4) should return an ABC error 
      5) should return a DEF error

  Array
    #indexOf()
      ✓ should return -1 when the value is not present


  1 passing (87ms)
  5 failing

  1) myClass.js .selectSpecificXYZ() Should return an object containing SUCCESS when a correct myClass reference is passed:
     Error: Cannot find module 'bluebird'
      at require (internal/module.js:20:19)
      at Object.<anonymous> (api_system/functions/helpers/database_wrappers.js:5:15)
      at require (internal/module.js:20:19)
      at Object.selectXYZByReference (api_system/functions/myClass/myClass.js:55:37)
      at Context.it (api_system/test/myClass/myClassTests.js:19:32)

  2) myClass.js .selectSpecificXYZ() should return an object regardless of the input (to handle success or error):
     Error: Cannot find module 'bluebird'
      at require (internal/module.js:20:19)
      at Object.<anonymous> (api_system/functions/helpers/database_wrappers.js:5:15)
      at require (internal/module.js:20:19)
      at Object.selectXYZByReference (api_system/functions/myClass/myClass.js:55:37)
      at Context.it (api_system/test/myClass/myClassTests.js:23:32)

  3) myClass.js .selectSpecificXYZ() should return an object with count 0 as there's no value:
     Error: Cannot find module 'bluebird'
      at require (internal/module.js:20:19)
      at Object.<anonymous> (api_system/functions/helpers/database_wrappers.js:5:15)
      at require (internal/module.js:20:19)
      at Object.selectXYZByReference (api_system/functions/myClass/myClass.js:55:37)
      at Context.it (api_system/test/myClass/myClassTests.js:27:30)

  4) myClass.js .selectSpecificXYZ() should return an Invalid Parameter error :
     AssertionError: expected [Function] to throw error including 'Invalid Parameter' but got 'Cannot find module \'bluebird\''
      at Context.it (api_system/test/myClass/myClassTests.js:33:73)

  5) myClass.js .selectSpecificXYZ() should return an Parameter Too Short error:
     AssertionError: expected [Function] to throw error including 'Parameter Too Short' but got 'Cannot find module \'bluebird\''
      at Context.it (api_system/test/myClass/myClassTests.js:38:79)




npm ERR! Linux 4.4.34-v7+
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "test" "-recursive"
npm ERR! node v7.2.1
npm ERR! npm  v3.10.10
npm ERR! code ELIFECYCLE
npm ERR! myServer@1.0.0 test: `mocha api_system/test --recursive`
npm ERR! Exit status 5
npm ERR! 
npm ERR! Failed at the myServer@1.0.0 test script 'mocha api_system/test --recursive'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the myServer package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     mocha api_system/test --recursive
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs myServer
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls myServer
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /var/www/ci-tests/myServer/npm-debug.log
[32;1m$ cd ..[0;m
[32;1m$ rm -rf myServer[0;m
[32;1m$ cd ..[0;m
[32;1m$ rm -rf ci-tests[0;m
[32;1m$ EOF[0;m
[32;1mBuild succeeded
[0;m

1 个答案:

答案 0 :(得分:1)

您没有包含Travis-CI配置或运行所有内容的脚本,例如,当您有脚本时:

#!/bin/sh
npm install
npm test

然后即使注释返回错误,脚本仍可能成功退出。你能做的是:

#!/bin/sh
npm install \
&& npm test \
|| exit 1

确保脚本本身将错误返回给操作系统。这是一个不是GitLab特有的一般性建议,但我不能发布任何更具体的信息而不会看到你的配置和你的脚本。