DevOps Troubleshooting Challenge: The Broken CI/CD Pipeline
Describe this diagram
Parts
- Developer: Pushes a commit to the repository.
- status-api repository: The repository with the app, its tests, the Dockerfile and the workflow.
- Unit tests: unittest checks the /health endpoint. The pipeline stops here if a test fails.
- Docker build: Builds the image from the Dockerfile and tags it with your Docker Hub user.
- Repository secrets: REGISTRY_USER and REGISTRY_TOKEN, used to log in to Docker Hub.
- Docker Hub: Receives the image as REGISTRY_USER/status-api.
Connections
- Developer to status-api repository: git push
- status-api repository to Unit tests: starts the workflow
- Unit tests to Docker build: when tests pass
- Docker build to Docker Hub: push USER/status-api
- Repository secrets to Docker build: registry login
Show the answer
Four faults stop the pipeline:
- Unit test:
response.datais bytes, so comparing it with the string"Service is up"always fails. Compare withb"Service is up"or useresponse.get_data(as_text=True). - Dockerfile:
-no-cache-diris not a pip option. It is--no-cache-dir. - Build step:
dokceris a typo fordocker. - Push step: an image tagged
status-apipushes to the officiallibrarynamespace, which is refused. Tag it with your Docker Hub user, and document the two repository secrets the step needs.
The fixed files:
.github/workflows/ci.yml
name: ci
on:
push:
branches: [main]
pull_request:
jobs:
build:
runs-on: ubuntu-latest
env:
IMAGE: ${{ secrets.REGISTRY_USER }}/status-api
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
cache: pip
- name: Test
run: |
pip install -r requirements.txt
python -m unittest discover -s tests -t .
- name: Build image
run: docker build -t "$IMAGE:${{ github.sha }}" -t "$IMAGE:latest" .
- name: Push image
if: github.ref == 'refs/heads/main'
run: |
echo "${{ secrets.REGISTRY_TOKEN }}" | docker login -u "${{ secrets.REGISTRY_USER }}" --password-stdin
docker push --all-tags "$IMAGE"Dockerfile
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
USER 10001
EXPOSE 8000
CMD ["python", "app.py"]tests/test_app.py
import unittest
from app import app
class HealthTest(unittest.TestCase):
def setUp(self):
self.client = app.test_client()
def test_health(self):
response = self.client.get("/health")
self.assertEqual(response.status_code, 200)
self.assertEqual(response.get_data(as_text=True), "Service is up")README.md
# status-api
A small Flask service. CI tests every push and pull request, and on `main` builds the image and pushes it to Docker Hub.
## Repository secrets
| Secret | Value |
| --- | --- |
| `REGISTRY_USER` | Your Docker Hub user name. The image is pushed as `REGISTRY_USER/status-api`. |
| `REGISTRY_TOKEN` | A Docker Hub access token with write access. Never your password. |
## Run locally
pip install -r requirements.txt
python -m unittest discover -s tests -t .
python app.py