Skip to content

fix: build and deploy github action #9

fix: build and deploy github action

fix: build and deploy github action #9

name: Build & Deploy API
on:
push:
branches: ['main']
jobs:
# =================================================================================
# JOB 1: Detect changed services and prepare variables
# =================================================================================
detect-changes:
runs-on: ubuntu-latest
outputs:
services_to_build: ${{ steps.filter.outputs.changes }}
version: ${{ steps.prep.outputs.VERSION }}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 2
- name: Prepare version tag
id: prep
run: |
VERSION=$(date +%Y%m%d%H%M%S)-${GITHUB_SHA::7}
echo "VERSION=${VERSION}" >> $GITHUB_OUTPUT
- name: Detect changed services
id: filter
uses: dorny/paths-filter@v3
with:
list-files: none
filters: |
gateway:
- 'apps/gateway/**'
- 'libs/shared/**'
- '.github/workflows/build-and-deploy.yml'
- 'package.json'
- 'pnpm-lock.yaml'
auth:
- 'apps/auth/**'
- 'libs/shared/**'
- '.github/workflows/build-and-deploy.yml'
- 'package.json'
- 'pnpm-lock.yaml'
# =================================================================================
# JOB 2: Build and push Docker images (in parallel via matrix)
# =================================================================================
build-and-push:
needs: detect-changes
if: needs.detect-changes.outputs.services_to_build != '[]'
runs-on: ubuntu-latest
permissions:
packages: write
strategy:
fail-fast: false
matrix:
service: ${{ fromJson(needs.detect-changes.outputs.services_to_build) }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and Push Docker image for ${{ matrix.service }}
run: |
IMAGE_NAME="api-${{ matrix.service }}"
docker build \
-f apps/${{ matrix.service }}/Dockerfile \
-t ghcr.io/${{ github.repository_owner }}/${IMAGE_NAME}:${{ needs.detect-changes.outputs.version }} \
-t ghcr.io/${{ github.repository_owner }}/${IMAGE_NAME}:latest .
docker push ghcr.io/${{ github.repository_owner }}/${IMAGE_NAME} --all-tags
# =================================================================================
# JOB 3: Update infrastructure repository (runs once)
# =================================================================================
update-infra:
needs: [detect-changes, build-and-push]
if: needs.detect-changes.outputs.services_to_build != '[]'
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Clone infra repo
run: |
git clone https://ci-bot:${{ secrets.PAT_TOKEN }}@github.com/${{ github.repository_owner }}/infra.git
- name: Update deployment image tags
run: |
cd infra
SERVICES_JSON='${{ needs.detect-changes.outputs.services_to_build }}'
VERSION='${{ needs.detect-changes.outputs.version }}'
for service in $(echo $SERVICES_JSON | jq -r '.[]'); do
IMAGE_NAME="api-$service"
DEPLOYMENT_PATH="apps/services/api/$service/deployment.yaml"
echo "Updating deployment for $service in $DEPLOYMENT_PATH"
sed -i "s|image: ghcr.io.*/${IMAGE_NAME}:.*|image: ghcr.io/${{ github.repository_owner }}/${IMAGE_NAME}:${VERSION}|" "$DEPLOYMENT_PATH"
done
- name: Commit and Push Manifest Changes
run: |
cd infra
git config user.name "ci-bot"
git config user.email "ci-bot@users.noreply.github.com"
git add .
if git diff --staged --quiet; then
echo "No changes to commit."
else
git commit -m "deploy(api): update image versions to ${{ needs.detect-changes.outputs.version }}"
git push
fi