【问题标题】:AWS not stopping when unitest fails in the codebuild当代码构建中的 unitest 失败时,AWS 不会停止
【发布时间】:2022-10-06 05:31:55
【问题描述】:

我正在使用带有(codeCommit、codebuild、codeDeploy)的代码管道做一个非常简单的 CI/CD。

我有一个简单的 node.js 应用程序,它有一个如下所示的单元测试

const Allsayings = require(\'./testAllsayings\');
function logPass(tName){
    console.log(\"PASS - \" + tName);
}
function logFail(tName){
    console.log(\"FAIL - \" + tName )
}
// T01 - Search for a saying and succeed
let say01 = new Allsayings();
say01.addQuote(\"Everyone is looking for something.\");
say01.addQuote(\"Let\'s try to figure this out together, so help me please\");
let output01 = aq01.findSaying(\"Not here\");
if (output01.search(\"Before you embark\") > -1){
    logPass(\"T01\");
} else {
    logFail(\"T01\");
}

我希望当单元测试失败时它会停止/停止部署或管道的进展。

我的 byuildspec

version: 0.2
phases:
  install:
    runtime-versions:
      nodejs: 16
    commands:
      - echo Installing
  pre_build:
    commands:
      - echo Installing source NPM dependencies.
      - cd serverSide
      - npm install
  build:
    commands:
      - echo Build started on `date`
      - npm install pm2@latest -g
        # buildspec is able to get into your servSide file?
      - ls
      - echo \"for debugging ... starting test\"
      - node testAllsayings.js
      - echo \"test is successful ... \"
  post_build:
    commands:
      - echo Build completed on `date`
artifacts:
  files:
    - \'**/*\'

但是,我的问题是,当我运行 codepipeline 时,尽管我的 unittest 失败了,但 codebuild 仍成功完成,这是 codebuild 日志的一部分


[Container] 2022/10/03 00:45:05 Running command echo \"for debugging ... starting test\"
for debugging ... starting test

[Container] 2022/10/03 00:45:05 Running command node testAllsayings.js
Fail - T01


[Container] 2022/10/03 00:45:05 Running command echo \"test is successful ... \"
test is successful ... 

我阅读了this,并将命令node testAllsayings.js 移动到了pre_build 阶段,但在没有停止构建阶段或部署阶段的情况下一切正常。

    标签: node.js amazon-web-services aws-codepipeline aws-codebuild aws-code-deploy


    【解决方案1】:

    所以我找到了解决方案。对于从单元测试中捕获错误的代码构建,该函数必须与一个存在。所以我添加了这行代码,现在当单元测试失败时代码构建停止。

    function logFail(tName){
        console.log("FAIL - " + tName )
          process.exitCode(1);
    
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-04
      • 2013-07-30
      • 2022-11-14
      • 2021-04-17
      • 2019-09-20
      • 2012-12-17
      • 1970-01-01
      相关资源
      最近更新 更多