AWS Builder Center
Building a CI Pipeline with GitHub, AWS CodeBuild and pytest (and Breaking It on Purpose)

Building a CI Pipeline with GitHub, AWS CodeBuild and pytest (and Breaking It on Purpose)

I connected a Flask app on GitHub to AWS CodeBuild so every push runs pytest automatically. Then I broke a test on purpose to prove the pipeline catches failures.

Why I built this :
"It works on my machine" is not a test strategy. I wanted every push to my repo to be built and tested automatically, with no clicking "Start build" and no trusting myself to remember to run the tests.
So I set up a small continuous integration (CI) pipeline: GitHub → AWS CodeBuild → Amazon S3. A Flask app is tested with pytest on every push, and only a passing build produces a deployable ZIP artifact.

The architecture

Piece
Role in the pipeline
GitHub
Holds the Flask app, the pytest tests and  buildspec.yml . Sends a webhook on every push to  main .
AWS CodeBuild
Pulls the code, installs Python 3.12 and dependencies, runs pytest.
Amazon S3
Stores the ZIP artifact, but only when the build succeeds.
Amazon CloudWatch Logs
Keeps the full build log for debugging.
IAM service role
Gives CodeBuild permission to write to S3 and CloudWatch.

How pytest finds the tests

pytest uses naming conventions to discover tests automatically. This is called test discovery. By default:
  • test files are named  test_*.py  or  *_test.py 
  • test functions must start with  test_ 
If a function is named  check_add() , pytest silently ignores it. Name it  test_add()  and it runs.
The command CodeBuild runs in the build phase is:
python -m pytest -v
This runs every discovered test in the project and prints a detailed pass/fail line for each one ( -v  is verbose mode).

The buildspec.yml

CodeBuild is driven by a  buildspec.yml  in the repo root. This is the general shape (adapt the names to your own project):
version: 0.2

phases:
install:
runtime-versions:
python: 3.12
pre_build:
commands:
- pip install -r requirements.txt
build:
commands:
- python -m pytest -v

artifacts:
files:
- '**/*'
The key idea: if any command in a phase returns a non-zero exit code, CodeBuild marks that phase, and the whole build, as FAILED. pytest returns exit code  0  when all tests pass and  1  when any test fails. That single fact is what turns a test result into a pipeline decision.

Proving it works: failing on purpose

A pipeline that has only ever passed hasn't proven anything. So I deliberately broke it.
I changed the expected output in the test for my addition function so the assertion no longer matched the real result, then started a build.
Build #2 result:
  • pytest ran the suite: four tests passed, one failed
  • pytest raised an  AssertionError  and exited with status  1 
  • CodeBuild marked the BUILD phase, and the overall build, as FAILED
  • The build log showed exactly which test failed and why
That is the point of CI: the error was caught automatically, with the evidence sitting right in the log, before anything could reach an artifact bucket or a deployment.
I reverted the change, rebuilt, and Build #3 succeeded.

Removing the manual step: the GitHub webhook

Builds #1 to #3 were all started by me clicking Start build. That is not CI. The final step was connecting GitHub so a push triggers the build on its own.
After I pushed a new commit, Build #4 appeared in the build history with the submitter shown as  GitHub-Hookshot/...  instead of  root . That submitter name is the proof: the build was started by a GitHub webhook, not by me. It finished in about 26 seconds and succeeded.
Build
Trigger
Result
What it proved
#1
Manual
Succeeded
Baseline pipeline works
#2
Manual
Failed
pytest failure stops the build
#3
Manual
Succeeded
Recovery after fixing the test
#4
GitHub webhook
Succeeded
Push-to-build automation works

Which DevOps tools does this connect to?

This small project already touches several tools in a typical DevOps toolchain:
  • Source control: GitHub (webhook trigger)
  • CI / build: AWS CodeBuild
  • Testing: pytest
  • Artifact storage: Amazon S3
  • Logging and monitoring: Amazon CloudWatch Logs
  • Access control: AWS IAM service role
And it is a natural base to extend:
  • AWS CodePipeline to chain Source → Build → Deploy stages
  • AWS CodeDeploy to ship the artifact to EC2 or other targets
  • Jenkins or GitHub Actions as alternative CI engines (CodeBuild lists both under related integrations)
  • Docker and Amazon ECR to build and push container images from the same pipeline

Lessons learned

  1. Name your tests  test_* . Otherwise pytest won't find them and your "green" build means nothing.
  1. Exit codes drive the pipeline. Any non-zero exit in a phase fails the build.
  1. Break it on purpose. Testing the failure path is as important as testing the success path.
  1. Look for  GitHub-Hookshot  in the build history to confirm your webhook is really working.
  1. Read the logs first. CodeBuild's build log tells you which phase failed and why.

Wrap-up

With a Flask app, a handful of pytest tests, one  buildspec.yml  and a GitHub webhook, I now have a pipeline that tests every push automatically, blocks broken code, and stores clean artifacts in S3. The next step for me is wiring this into CodePipeline and CodeDeploy for a full CI/CD flow.
If you're new to AWS developer tools, CodeBuild is a gentle place to start: no servers to manage, and you pay only for the build minutes you use.
__
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article