|
1 | | -name: Backend CI |
| 1 | +# week08/.github/workflows/backend_ci.yml |
| 2 | +name: Backend CI - Test, Build and Push Images to ACR |
2 | 3 |
|
3 | 4 | on: |
| 5 | + workflow_dispatch: |
4 | 6 | push: |
5 | 7 | branches: [ development, main ] |
6 | 8 | paths: |
7 | 9 | - 'backend/**' |
8 | 10 | - '.github/workflows/backend_ci.yml' |
9 | | - pull_request: |
10 | | - branches: [ development, main ] |
11 | | - paths: |
12 | | - - 'backend/**' |
13 | | - - '.github/workflows/backend_ci.yml' |
14 | | - workflow_dispatch: |
15 | 11 |
|
16 | 12 | concurrency: |
17 | 13 | group: backend-ci-${{ github.ref }} |
18 | 14 | cancel-in-progress: true |
19 | 15 |
|
20 | 16 | env: |
21 | | - ACR_NAME: ${{ secrets.ACR_NAME }} |
| 17 | + # Registry FQDN (e.g., mystudentregistry.azurecr.io) |
22 | 18 | ACR_LOGIN_SERVER: ${{ secrets.ACR_LOGIN_SERVER }} |
| 19 | + # Registry short name (e.g., mystudentregistry) |
| 20 | + ACR_NAME: ${{ secrets.ACR_NAME }} |
23 | 21 | BRANCH: ${{ github.ref_name }} |
24 | | - IMAGE_BASE: ${{ secrets.ACR_LOGIN_SERVER }}/my-backend |
25 | | - IMAGE_SHA: ${{ env.IMAGE_BASE }}:${{ github.sha }} |
26 | | - IMAGE_BR: ${{ env.IMAGE_BASE }}:${{ env.BRANCH }} |
| 22 | + IMAGE_TAG: ${{ github.sha }}-${{ github.run_id }} |
27 | 23 |
|
28 | 24 | jobs: |
29 | | - test: |
30 | | - name: Smoke/Test (PR only) |
| 25 | + test_and_lint_backends: |
31 | 26 | runs-on: ubuntu-latest |
32 | | - if: ${{ github.event_name == 'pull_request' }} |
| 27 | + |
| 28 | + services: |
| 29 | + product_db: |
| 30 | + image: postgres:15 |
| 31 | + env: |
| 32 | + POSTGRES_USER: postgres |
| 33 | + POSTGRES_PASSWORD: postgres |
| 34 | + POSTGRES_DB: products |
| 35 | + options: >- |
| 36 | + --health-cmd "pg_isready -U postgres" |
| 37 | + --health-interval 10s |
| 38 | + --health-timeout 5s |
| 39 | + --health-retries 5 |
| 40 | + ports: [ "5432:5432" ] |
| 41 | + |
| 42 | + order_db: |
| 43 | + image: postgres:15 |
| 44 | + env: |
| 45 | + POSTGRES_USER: postgres |
| 46 | + POSTGRES_PASSWORD: postgres |
| 47 | + POSTGRES_DB: orders |
| 48 | + options: >- |
| 49 | + --health-cmd "pg_isready -U postgres" |
| 50 | + --health-interval 10s |
| 51 | + --health-timeout 5s |
| 52 | + --health-retries 5 |
| 53 | + ports: [ "5433:5432" ] |
| 54 | + |
33 | 55 | steps: |
34 | | - - uses: actions/checkout@v4 |
35 | | - - run: docker build -f backend/Dockerfile backend |
| 56 | + - name: Checkout repository |
| 57 | + uses: actions/checkout@v4 |
36 | 58 |
|
37 | | - build-push: |
38 | | - name: Build & Push (branch pushes) |
| 59 | + - name: Set up Python 3.10 |
| 60 | + uses: actions/setup-python@v5 |
| 61 | + with: |
| 62 | + python-version: '3.10' |
| 63 | + |
| 64 | + - name: Install dependencies |
| 65 | + run: | |
| 66 | + pip install --upgrade pip |
| 67 | + for req in backend/*/requirements.txt; do |
| 68 | + echo "Installing $req" |
| 69 | + pip install -r "$req" |
| 70 | + done |
| 71 | + pip install pytest httpx |
| 72 | +
|
| 73 | + - name: Run product_service tests |
| 74 | + working-directory: backend/product_service |
| 75 | + env: |
| 76 | + POSTGRES_HOST: localhost |
| 77 | + POSTGRES_PORT: 5432 |
| 78 | + POSTGRES_DB: products |
| 79 | + POSTGRES_USER: postgres |
| 80 | + POSTGRES_PASSWORD: postgres |
| 81 | + run: pytest tests --maxfail=1 --disable-warnings -q |
| 82 | + |
| 83 | + - name: Run order_service tests |
| 84 | + working-directory: backend/order_service |
| 85 | + env: |
| 86 | + POSTGRES_HOST: localhost |
| 87 | + POSTGRES_PORT: 5433 |
| 88 | + POSTGRES_DB: orders |
| 89 | + POSTGRES_USER: postgres |
| 90 | + POSTGRES_PASSWORD: postgres |
| 91 | + run: pytest tests --maxfail=1 --disable-warnings -q |
| 92 | + |
| 93 | + build_and_push_images: |
39 | 94 | runs-on: ubuntu-latest |
40 | | - if: ${{ github.event_name == 'push' }} |
41 | | - permissions: { id-token: write, contents: read } |
| 95 | + needs: test_and_lint_backends |
| 96 | + |
42 | 97 | steps: |
43 | | - - uses: actions/checkout@v4 |
| 98 | + - name: Checkout repository |
| 99 | + uses: actions/checkout@v4 |
44 | 100 |
|
45 | | - - uses: azure/login@v2 |
| 101 | + - name: Azure Login (client secret) |
| 102 | + uses: azure/login@v2 |
46 | 103 | with: |
47 | | - client-id: ${{ secrets.AZURE_CLIENT_ID }} |
48 | | - tenant-id: ${{ secrets.AZURE_TENANT_ID }} |
49 | | - subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }} |
| 104 | + creds: ${{ secrets.AZURE_CREDENTIALS }} |
50 | 105 |
|
51 | | - - name: ACR login |
| 106 | + - name: Login to Azure Container Registry |
52 | 107 | run: az acr login --name "${{ env.ACR_NAME }}" |
53 | 108 |
|
54 | | - - uses: docker/setup-buildx-action@v3 |
| 109 | + - name: Set up Docker Buildx |
| 110 | + uses: docker/setup-buildx-action@v3 |
55 | 111 |
|
56 | | - - name: Build & push |
57 | | - uses: docker/build-push-action@v6 |
58 | | - with: |
59 | | - context: ./backend |
60 | | - file: ./backend/Dockerfile |
61 | | - push: true |
62 | | - tags: | |
63 | | - ${{ env.IMAGE_SHA }} |
64 | | - ${{ env.IMAGE_BR }} |
65 | | -
|
66 | | - - name: Logout Azure |
| 112 | + # Product service image |
| 113 | + - name: Build & Push product_service |
| 114 | + run: | |
| 115 | + docker build -t ${{ env.ACR_LOGIN_SERVER }}/product_service:latest ./backend/product_service |
| 116 | + docker tag ${{ env.ACR_LOGIN_SERVER }}/product_service:latest ${{ env.ACR_LOGIN_SERVER }}/product_service:${{ env.BRANCH }} |
| 117 | + docker tag ${{ env.ACR_LOGIN_SERVER }}/product_service:latest ${{ env.ACR_LOGIN_SERVER }}/product_service:${{ env.IMAGE_TAG }} |
| 118 | + docker push ${{ env.ACR_LOGIN_SERVER }}/product_service:latest |
| 119 | + docker push ${{ env.ACR_LOGIN_SERVER }}/product_service:${{ env.BRANCH }} |
| 120 | + docker push ${{ env.ACR_LOGIN_SERVER }}/product_service:${{ env.IMAGE_TAG }} |
| 121 | +
|
| 122 | + # Order service image |
| 123 | + - name: Build & Push order_service |
| 124 | + run: | |
| 125 | + docker build -t ${{ env.ACR_LOGIN_SERVER }}/order_service:latest ./backend/order_service |
| 126 | + docker tag ${{ env.ACR_LOGIN_SERVER }}/order_service:latest ${{ env.ACR_LOGIN_SERVER }}/order_service:${{ env.BRANCH }} |
| 127 | + docker tag ${{ env.ACR_LOGIN_SERVER }}/order_service:latest ${{ env.ACR_LOGIN_SERVER }}/order_service:${{ env.IMAGE_TAG }} |
| 128 | + docker push ${{ env.ACR_LOGIN_SERVER }}/order_service:latest |
| 129 | + docker push ${{ env.ACR_LOGIN_SERVER }}/order_service:${{ env.BRANCH }} |
| 130 | + docker push ${{ env.ACR_LOGIN_SERVER }}/order_service:${{ env.IMAGE_TAG }} |
| 131 | +
|
| 132 | + - name: Azure logout |
67 | 133 | if: always() |
68 | 134 | run: az logout |
69 | | - |
|
0 commit comments