|
1 | | -# GitHub Actions CI for Docker Compose (Week 7) |
2 | | - |
3 | | -name: CI - Docker Compose Week 7 |
| 1 | +name: End-to-End Tests |
4 | 2 |
|
5 | 3 | on: |
6 | | - push: |
7 | | - branches: |
8 | | - - week7 |
9 | | - workflow_dispatch: |
| 4 | + push: |
| 5 | + branches: [week7] |
10 | 6 |
|
11 | 7 |
|
12 | 8 | jobs: |
13 | | - e2e-tests: |
| 9 | + e2e: |
| 10 | + name: Run Docker Compose End-to-End Tests |
14 | 11 | runs-on: ubuntu-latest |
15 | | - services: |
16 | | - docker: |
17 | | - image: docker:24.0.5-dind |
18 | | - options: --dns 8.8.8.8 |
| 12 | + |
19 | 13 | steps: |
20 | | - - name: Checkout code |
21 | | - uses: actions/checkout@v3 |
| 14 | + - name: Checkout repository |
| 15 | + uses: actions/checkout@v4 |
22 | 16 |
|
23 | | - - name: Set up Docker Compose |
| 17 | + - name: Set up Docker Buildx |
24 | 18 | uses: docker/setup-buildx-action@v3 |
25 | 19 |
|
26 | | - - name: Install Docker Compose |
27 | | - run: | |
28 | | - sudo apt-get update |
29 | | - sudo apt-get install -y docker-compose |
| 20 | + - name: Set up Docker cache |
| 21 | + uses: actions/cache@v4 |
| 22 | + with: |
| 23 | + path: /tmp/.buildx-cache |
| 24 | + key: ${{ runner.os }}-buildx-${{ github.sha }} |
| 25 | + restore-keys: | |
| 26 | + ${{ runner.os }}-buildx- |
30 | 27 |
|
31 | | - - name: Build and start services |
32 | | - run: | |
33 | | - docker-compose -f week7/docker-compose.yml up --build -d |
| 28 | + - name: Build Docker containers |
| 29 | + run: docker-compose build |
34 | 30 |
|
35 | | - - name: Get running containers |
36 | | - run: | |
37 | | - docker ps -a |
| 31 | + - name: Run Docker Compose (detached) |
| 32 | + run: docker-compose up -d |
38 | 33 |
|
39 | | - - name: Wait for backend healthcheck |
| 34 | + - name: Wait for services to be healthy |
40 | 35 | run: | |
41 | | - CONTAINER_ID=$(docker ps -qf "name=backend") |
42 | | - echo "Backend container ID: $CONTAINER_ID" |
43 | | - for i in {1..10}; do |
44 | | - STATUS=$(docker inspect --format='{{json .State.Health.Status}}' $CONTAINER_ID || echo "none") |
45 | | - echo "Backend health: $STATUS" |
46 | | - if [ "$STATUS" = '"healthy"' ]; then exit 0; fi |
47 | | - sleep 5 |
| 36 | + set -e |
| 37 | + timeout=30 |
| 38 | + echo "Waiting up to $timeout seconds for services to become healthy..." |
| 39 | + for i in $(seq 1 $timeout); do |
| 40 | + unhealthy=$(docker inspect --format='{{.State.Health.Status}}' $(docker-compose ps -q db || true) || echo "unhealthy") |
| 41 | + if [ "$unhealthy" == "healthy" ]; then |
| 42 | + echo "DB is healthy." |
| 43 | + break |
| 44 | + fi |
| 45 | + sleep 1 |
48 | 46 | done |
49 | | - echo "Backend did not become healthy in time" && docker-compose -f week7/docker-compose.yml logs backend && exit 1 |
50 | 47 |
|
51 | | - - name: Run end-to-end tests |
| 48 | + - name: Run health check and test endpoint |
52 | 49 | run: | |
53 | | - curl -f http://localhost:3000/health |
| 50 | + echo "Testing health endpoint..." |
| 51 | + curl --fail http://localhost:3000/health |
54 | 52 |
|
55 | | - - name: Show logs on failure |
| 53 | + echo "Testing user GET (expecting empty array or success)..." |
| 54 | + curl --fail http://localhost:3000/api/users |
| 55 | +
|
| 56 | + - name: Handle failed services |
56 | 57 | if: failure() |
57 | 58 | run: | |
58 | | - docker-compose -f week7/docker-compose.yml logs > week7/compose-logs.txt || true |
| 59 | + echo "One or more steps failed. Showing logs..." |
| 60 | + docker-compose logs > full_logs.txt |
| 61 | + cat full_logs.txt |
59 | 62 |
|
60 | 63 | - name: Upload logs as artifact |
61 | 64 | if: always() |
62 | 65 | uses: actions/upload-artifact@v4 |
63 | 66 | with: |
64 | | - name: compose-logs |
65 | | - path: week7/compose-logs.txt |
| 67 | + name: docker-compose-logs |
| 68 | + path: full_logs.txt |
66 | 69 |
|
67 | | - - name: Tear down |
| 70 | + - name: Shut down containers |
68 | 71 | if: always() |
69 | | - run: | |
70 | | - docker-compose -f week7/docker-compose.yml down -v |
| 72 | + run: docker-compose down --volumes |
0 commit comments