55 branches : ['main']
66
77jobs :
8- build-deploy :
8+ # =================================================================================
9+ # JOB 1: Detect changed services and prepare variables
10+ # =================================================================================
11+ detect-changes :
912 runs-on : ubuntu-latest
13+ outputs :
14+ services_to_build : ${{ steps.filter.outputs.changes }}
15+ version : ${{ steps.prep.outputs.VERSION }}
16+ steps :
17+ - name : Checkout code
18+ uses : actions/checkout@v4
19+ with :
20+ fetch-depth : 2
1021
22+ - name : Prepare version tag
23+ id : prep
24+ run : |
25+ VERSION=$(date +%Y%m%d%H%M%S)-${GITHUB_SHA::7}
26+ echo "VERSION=${VERSION}" >> $GITHUB_OUTPUT
27+
28+ - name : Detect changed services
29+ id : filter
30+ uses : dorny/paths-filter@v3
31+ with :
32+ list-files : none
33+ filters : |
34+ all: &all
35+ - 'libs/shared/**'
36+ - '.github/workflows/**'
37+ - 'package.json'
38+ - 'pnpm-lock.yaml'
39+ gateway:
40+ - 'apps/gateway/**'
41+ - *all
42+ auth:
43+ - 'apps/auth/**'
44+ - *all
45+
46+ # =================================================================================
47+ # JOB 2: Build and push Docker images (in parallel via matrix)
48+ # =================================================================================
49+ build-and-push :
50+ needs : detect-changes
51+ if : needs.detect-changes.outputs.services_to_build != '[]'
52+ runs-on : ubuntu-latest
1153 permissions :
12- contents : write
1354 packages : write
14-
55+ strategy :
56+ fail-fast : false
57+ matrix :
58+ service : ${{ fromJson(needs.detect-changes.outputs.services_to_build) }}
1559 steps :
16- - uses : actions/checkout@v4
60+ - name : Checkout code
61+ uses : actions/checkout@v4
1762
18- - uses : docker/login-action@v3
63+ - name : Login to GitHub Container Registry
64+ uses : docker/login-action@v3
1965 with :
2066 registry : ghcr.io
2167 username : ${{ github.actor }}
2268 password : ${{ secrets.GITHUB_TOKEN }}
2369
24- - name : Build image
70+ - name : Build and Push Docker image for ${{ matrix.service }}
2571 run : |
26- VERSION=$(date +%Y%m%d%H%M%S)
27- echo "VERSION=$VERSION" >> $GITHUB_ENV
72+ IMAGE_NAME="api-${{ matrix.service }}"
2873 docker build \
29- -t ghcr.io/${{ github.repository_owner }}/api:$VERSION .
30- docker tag ghcr.io/${{ github.repository_owner }}/api:$VERSION ghcr.io/${{ github.repository_owner }}/api:latest
31-
32- - name : Push image
33- run : |
34- docker push ghcr.io/${{ github.repository_owner }}/api:$VERSION
35- docker push ghcr.io/${{ github.repository_owner }}/api:latest
74+ -f apps/${{ matrix.service }}/Dockerfile \
75+ -t ghcr.io/${{ github.repository_owner }}/${IMAGE_NAME}:${{ needs.detect-changes.outputs.version }} \
76+ -t ghcr.io/${{ github.repository_owner }}/${IMAGE_NAME}:latest .
77+ docker push ghcr.io/${{ github.repository_owner }}/${IMAGE_NAME} --all-tags
3678
79+ # =================================================================================
80+ # JOB 3: Update infrastructure repository (runs once)
81+ # =================================================================================
82+ update-infra :
83+ needs : [detect-changes, build-and-push]
84+ if : needs.detect-changes.outputs.services_to_build != '[]'
85+ runs-on : ubuntu-latest
86+ permissions :
87+ contents : write
88+ steps :
3789 - name : Clone infra repo
3890 run : |
3991 git clone https://ci-bot:${{ secrets.PAT_TOKEN }}@github.com/${{ github.repository_owner }}/infra.git
4092
41- - name : Update api deployment image tag
93+ - name : Update deployment image tags
4294 run : |
43- cd infra/apps/services/api
44- sed -i "s|image: ghcr.io.*/api.*|image: ghcr.io/${{ github.repository_owner }}/api:$VERSION|" deployment.yaml
95+ cd infra
96+ SERVICES_JSON='${{ needs.detect-changes.outputs.services_to_build }}'
97+ VERSION='${{ needs.detect-changes.outputs.version }}'
98+
99+ for service in $(echo $SERVICES_JSON | jq -r '.[]'); do
100+ IMAGE_NAME="api-$service"
101+ DEPLOYMENT_PATH="apps/services/api/$service/deployment.yaml"
102+ echo "Updating deployment for $service in $DEPLOYMENT_PATH"
103+
104+ sed -i "s|image: ghcr.io.*/${IMAGE_NAME}:.*|image: ghcr.io/${{ github.repository_owner }}/${IMAGE_NAME}:${VERSION}|" "$DEPLOYMENT_PATH"
105+ done
45106
46- - name : Commit manifest change
107+ - name : Commit and Push Manifest Changes
47108 run : |
48109 cd infra
49110 git config user.name "ci-bot"
50- git config user.email "ci-bot@github.com"
51-
52- git remote set-url origin https://ci-bot:${{ secrets.PAT_TOKEN }}@github.com/${{ github.repository_owner }}/infra.git
53-
111+ git config user.email "ci-bot@users.noreply.github.com"
54112 git add .
55- git commit -m "deploy: api $VERSION" || echo "No changes to commit"
56- git push origin main
57- env :
58- GIT_AUTHOR_NAME : ci-bot
59- GIT_AUTHOR_EMAIL : ci-bot@github.com
60- GIT_COMMITTER_NAME : ci-bot
61- GIT_COMMITTER_EMAIL : ci-bot@github.com
113+ if git diff --staged --quiet; then
114+ echo "No changes to commit."
115+ else
116+ git commit -m "deploy(api): update image versions to ${{ needs.detect-changes.outputs.version }}"
117+ git push
118+ fi
0 commit comments