Skip to content

Commit 6478bea

Browse files
authored
Update backend_ci.yml
1 parent 1caa64b commit 6478bea

1 file changed

Lines changed: 104 additions & 39 deletions

File tree

.github/workflows/backend_ci.yml

Lines changed: 104 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,134 @@
1-
name: Backend CI
1+
# week08/.github/workflows/backend_ci.yml
2+
name: Backend CI - Test, Build and Push Images to ACR
23

34
on:
5+
workflow_dispatch:
46
push:
57
branches: [ development, main ]
68
paths:
79
- 'backend/**'
810
- '.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:
1511

1612
concurrency:
1713
group: backend-ci-${{ github.ref }}
1814
cancel-in-progress: true
1915

2016
env:
21-
ACR_NAME: ${{ secrets.ACR_NAME }}
17+
# Registry FQDN (e.g., mystudentregistry.azurecr.io)
2218
ACR_LOGIN_SERVER: ${{ secrets.ACR_LOGIN_SERVER }}
19+
# Registry short name (e.g., mystudentregistry)
20+
ACR_NAME: ${{ secrets.ACR_NAME }}
2321
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 }}
2723

2824
jobs:
29-
test:
30-
name: Smoke/Test (PR only)
25+
test_and_lint_backends:
3126
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+
3355
steps:
34-
- uses: actions/checkout@v4
35-
- run: docker build -f backend/Dockerfile backend
56+
- name: Checkout repository
57+
uses: actions/checkout@v4
3658

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:
3994
runs-on: ubuntu-latest
40-
if: ${{ github.event_name == 'push' }}
41-
permissions: { id-token: write, contents: read }
95+
needs: test_and_lint_backends
96+
4297
steps:
43-
- uses: actions/checkout@v4
98+
- name: Checkout repository
99+
uses: actions/checkout@v4
44100

45-
- uses: azure/login@v2
101+
- name: Azure Login (client secret)
102+
uses: azure/login@v2
46103
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 }}
50105

51-
- name: ACR login
106+
- name: Login to Azure Container Registry
52107
run: az acr login --name "${{ env.ACR_NAME }}"
53108

54-
- uses: docker/setup-buildx-action@v3
109+
- name: Set up Docker Buildx
110+
uses: docker/setup-buildx-action@v3
55111

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
67133
if: always()
68134
run: az logout
69-

0 commit comments

Comments
 (0)