Skip to content

Commit d5c968e

Browse files
committed
feat: Add GitHub Actions workflows for Build & Test and K8s Manifest update
1 parent 97ce408 commit d5c968e

File tree

2 files changed

+165
-0
lines changed

2 files changed

+165
-0
lines changed

.github/workflows/build-test.yml

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
name: Build and Test
2+
3+
on:
4+
push:
5+
branches:
6+
- 'main'
7+
- 'dev'
8+
9+
permissions:
10+
contents: read
11+
packages: write
12+
13+
jobs:
14+
build-test:
15+
name: Build, Test & Push Image
16+
runs-on: ubuntu-latest
17+
18+
steps:
19+
- name: Checkout code
20+
uses: actions/checkout@v4
21+
22+
- name: Use Node.js 20 and cache npm
23+
uses: actions/setup-node@v4
24+
with:
25+
node-version: '20'
26+
cache: 'npm'
27+
cache-dependency-path: package-lock.json
28+
29+
- name: Cache Next.js build artifacts
30+
uses: actions/cache@v4
31+
with:
32+
path: ./.next/cache
33+
key: ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}-${{ hashFiles('**/*.{js,jsx,ts,tsx,css,html}') }}
34+
restore-keys: |
35+
${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}-
36+
37+
- name: Install dependencies
38+
run: npm ci
39+
40+
- name: Run linter
41+
run: npm run lint
42+
43+
- name: Build production bundle
44+
run: npm run build
45+
46+
- name: Extract branch name
47+
id: branch
48+
run: |
49+
BRANCH_NAME=${GITHUB_REF#refs/heads/}
50+
echo "name=${BRANCH_NAME}" >> $GITHUB_OUTPUT
51+
52+
- name: Docker meta
53+
id: meta
54+
uses: docker/metadata-action@v5
55+
with:
56+
images: ghcr.io/${{ github.repository_owner }}/frontend_web
57+
tags: |
58+
type=raw,value=${{ steps.branch.outputs.name }}-{{sha}},enable=true
59+
type=raw,value=latest,enable={{is_default_branch}}
60+
61+
- name: Log in to GHCR
62+
uses: docker/login-action@v3
63+
with:
64+
registry: ghcr.io
65+
username: ${{ github.actor }}
66+
password: ${{ secrets.GITHUB_TOKEN }}
67+
68+
- name: Build and push Docker image
69+
uses: docker/build-push-action@v5
70+
with:
71+
context: .
72+
push: true
73+
tags: ${{ steps.meta.outputs.tags }}
74+
labels: ${{ steps.meta.outputs.labels }}
75+
76+
- name: Image Summary
77+
run: |
78+
echo "### 🐳 Docker Image Built" >> $GITHUB_STEP_SUMMARY
79+
echo "**Tags pushed:**" >> $GITHUB_STEP_SUMMARY
80+
echo '```' >> $GITHUB_STEP_SUMMARY
81+
echo "${{ steps.meta.outputs.tags }}" >> $GITHUB_STEP_SUMMARY
82+
echo '```' >> $GITHUB_STEP_SUMMARY
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
name: Update K8s Manifest
2+
3+
on:
4+
workflow_run:
5+
workflows: ["Build and Test"]
6+
types: [completed]
7+
branches: ['main', 'dev']
8+
9+
jobs:
10+
update-manifest:
11+
name: Update Image Tag in k8s-config
12+
if: ${{ github.event.workflow_run.conclusion == 'success' }}
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- name: Get branch and SHA info
17+
id: info
18+
run: |
19+
BRANCH="${{ github.event.workflow_run.head_branch }}"
20+
SHORT_SHA="$(echo ${{ github.event.workflow_run.head_sha }} | cut -c1-7)"
21+
echo "branch=${BRANCH}" >> $GITHUB_OUTPUT
22+
echo "sha=${SHORT_SHA}" >> $GITHUB_OUTPUT
23+
echo "📍 Branch: ${BRANCH}, SHA: ${SHORT_SHA}"
24+
25+
- name: Checkout k8s-config repo
26+
uses: actions/checkout@v4
27+
with:
28+
repository: 'TechTorque-2025/Infrastructure'
29+
token: ${{ secrets.REPO_ACCESS_TOKEN }}
30+
ref: ${{ steps.info.outputs.branch }}
31+
path: 'Infrastructure'
32+
33+
- name: Install Kustomize
34+
run: |
35+
curl -s "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh" | bash
36+
sudo mv kustomize /usr/local/bin/
37+
38+
- name: Update image tag in Kustomize overlay
39+
env:
40+
SERVICE_NAME_KUSTOMIZE: "cso2/frontend"
41+
SERVICE_NAME_GHCR: "frontend_web"
42+
BRANCH: ${{ steps.info.outputs.branch }}
43+
run: |
44+
cd Infrastructure
45+
46+
# Determine overlay based on branch
47+
if [[ "${BRANCH}" == "main" ]]; then
48+
OVERLAY="prod"
49+
else
50+
OVERLAY="dev"
51+
fi
52+
53+
echo "🎯 Targeting overlay: ${OVERLAY}"
54+
cd cso2/k8s/overlays/${OVERLAY}
55+
56+
NEW_IMAGE="ghcr.io/${{ github.repository_owner }}/${SERVICE_NAME_GHCR}:${{ steps.info.outputs.branch }}-${{ steps.info.outputs.sha }}"
57+
58+
echo "🔄 Updating ${SERVICE_NAME_KUSTOMIZE} to use image: ${NEW_IMAGE}"
59+
60+
kustomize edit set image ${SERVICE_NAME_KUSTOMIZE}=${NEW_IMAGE}
61+
62+
echo "✅ Updated kustomization.yaml:"
63+
cat kustomization.yaml | grep ${SERVICE_NAME_KUSTOMIZE} -A 2
64+
65+
- name: Commit and push changes
66+
env:
67+
SERVICE_NAME: "frontend"
68+
run: |
69+
cd Infrastructure
70+
git config user.name "github-actions[bot]"
71+
git config user.email "github-actions[bot]@users.noreply.github.com"
72+
73+
git add cso2/k8s/overlays/
74+
75+
if git diff --cached --quiet; then
76+
echo "⚠️ No changes detected, skipping commit"
77+
exit 0
78+
fi
79+
80+
git commit -m "chore(${SERVICE_NAME}): update image to ${{ steps.info.outputs.branch }}-${{ steps.info.outputs.sha }}" \
81+
-m "Triggered by: ${{ github.event.workflow_run.html_url }}"
82+
83+
git push origin ${{ steps.info.outputs.branch }}

0 commit comments

Comments
 (0)