Continuous Integration and Continuous Delivery (CI/CD) is the backbone of modern software delivery. In this guide we walk through configuring a GitLab pipeline that builds a Docker image, runs tests, pushes to a registry, and deploys to staging and production automatically.
Start by creating a .gitlab-ci.yml file at the root of your repository. Define the stages: build, test, and deploy. Each stage runs in a Docker container, ensuring a clean, reproducible environment every time.
For the build stage, use the docker:latest image with Docker-in-Docker (dind) service. Tag your image with the commit SHA so every build is traceable. Push it to the GitLab Container Registry using the built-in CI_REGISTRY variables.
The test stage pulls the built image and runs your test suite. Keep tests fast — under five minutes — so the feedback loop stays tight. Use pytest with coverage reporting and fail the pipeline if coverage drops below a threshold.
Finally, the deploy stage uses SSH or the Kubernetes API to roll out the new image. Protect your production deploy behind a manual trigger so accidental pushes to main don't go live immediately.