The correction is integrated into the TP (See the green boxes)
What you will learn in this TP
- Write a test → build workflow
- Add a security scan
- See a test fail
The pipeline #
What you will learn in this section
- Write a test → build workflow
- Add a security scan
- See a test fail
Context. A team ships by hand, with frequent mistakes and painful rollbacks. A CI/CD pipeline automates tests, build and publishing on every change: you build a complete one and see how it blocks a faulty change.
Prerequisites: A GitHub (or GitLab) account, Docker · Estimated time: 45 min
From application to registry.1. A testable application
Why? A unit test is the pipeline's safety net: without it, automating deployment only automates mistakes.
Create a small API and a unit test.See the correction
# app.py def addition(a, b): return a + b # test_app.py from app import addition def test_addition(): assert addition(2, 3) == 5pip install pytest && pytest -q2. Dockerfile and workflow
Why? The "build needs test" dependency guarantees an image is never published if tests fail.
Write the workflow: tests then image build.See the correction
# .github/workflows/ci.yml name: ci on: { push: { branches: [main] }, pull_request: {} } jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-python@v5 with: { python-version: "3.12" } - run: pip install pytest && pytest -q build: needs: test runs-on: ubuntu-latest permissions: { contents: read, packages: write } steps: - uses: actions/checkout@v4 - uses: docker/login-action@v3 with: { registry: ghcr.io, username: ${{ github.actor }}, password: ${{ secrets.GITHUB_TOKEN }} } - uses: docker/build-push-action@v6 with: { push: true, tags: "ghcr.io/${{ github.repository }}:${{ github.sha }}" }3. Add a security scan
Why? Integrating security in the pipeline (shift-left) finds a vulnerability at build time rather than in production.
Fail the pipeline on critical vulnerabilities.See the correction
- uses: aquasecurity/trivy-action@0.24.0 with: { image-ref: "ghcr.io/${{ github.repository }}:${{ github.sha }}", severity: CRITICAL, exit-code: "1" }4. Observe and fix
Why? Deliberately breaking a test proves the pipeline does its job: it blocks the change and protects the main branch.
Deliberately break the test, see it fail then fix it.See the correction
sed -i 's/== 5/== 6/' test_app.py && git commit -am 'test cassé' && git push # le job test échoue, build ne s'exécute pas (needs) git revert HEAD && git push
Difficulty level : ●●○○○ (2/5)
My progress
Sign in to track your progress and take notes. Sign in
💬 Questions about this lesson
No questions yet: be the first to ask for help or share your configuration.
Recommended Articles
DevOps best practices: culture, CI/CD, infrastructure as code and measurement
DevOps best practices: culture and collaboration, continuous integration and delivery, trunk-base...
End-to-end CI/CD (5/5): GitHub Actions, GitLab CI, Jenkins and Argo CD in practice
Compare and use CI/CD tools: GitHub Actions, GitLab CI, Jenkins, Argo CD and Flux; a complete pip...
End-to-end CI/CD (1/5): principles and anatomy of a pipeline
Understand end-to-end CI/CD: continuous integration, continuous delivery and continuous deploymen...
End-to-end CI/CD (2/5): continuous integration, testing and quality
Succeed with continuous integration: the test pyramid, unit, integration, contract and end-to-end...
End-to-end CI/CD (4/5): deployment strategies and rollback
Deploy without downtime and without risk: recreate, rolling update, blue/green, canary, feature f...
End-to-end CI/CD (3/5): artifacts, registries and environments
Deliver continuously: immutable, versioned artifacts, image and package registries, promoting the...
DevSecOps: securing the software supply chain end to end
Integrate security into the DevOps pipeline: shift-left, threat modeling, secret detection, SAST,...
k6 in continuous integration and on Kubernetes (k6-operator)
Automate performance tests: k6 in GitHub Actions and GitLab CI with blocking thresholds, distribu...
Terraform in CI/CD: pipelines, plan review, OIDC and GitOps
Automate Terraform: plan pipeline on merge requests and apply after approval, posting the plan as...