0

I might have been misunderstood the concept, but does not a Codebuild step fail when there is an error in your code and it can not build that code? I assumed that if one command in my build commands fails, it will notify me and/or stop the further after build actions.

I have a very simple Codebuild that suppose to run a python code (my unit tests in this case). Below is my buildspec.yml:

version: 0.2

phases:
  build:
    commands:
      - echo Build started on `date`
      - echo Compiling the Python code...
      - pythonk run_unittests.py
  post_build:
    commands:
      - echo Build completed on `date`

In that run_unittests.py file there is a syntax error.

When the run ends, it shows it as a success, but in the log files, I see it sees python syntax error is logged!

The only way I get to fail this is when there is an error in my buildspec.yml file, not inside my python file! It even goes to the next step in the CodePipleline!!

Am I misunderstood? And if not how can I get this to fail on the python code or unit test asserts?

Below is the lines that I see in the details:

Traceback (most recent call last):
File "/codebuild/output/...py", line 33, in test_func
poo
NameError: global name 'poo' is not defined

----------------------------------------------------------------------
Ran 5 tests in 0.000s

FAILED (errors=2)
Mahshid Zeinaly
  • 3,590
  • 6
  • 25
  • 32

1 Answers1

3

CodeBuild determines success or failure based on the common convention of zero/nonzero exit codes to indicate success/failure, respectively.

If the build is succeeding, it means the command you're using to run your unit tests is returning a zero value, which CodeBuild interprets as success.

In Python, you can use sys.exit() to return a non-zero (error) exit code. For example: sys.exit(1)

Unsigned
  • 9,640
  • 4
  • 43
  • 72