Skip to content

Commit 3ca71fa

Browse files
committed
feat: add GitHub Actions workflow for build, test, and Docker image management with security scanning
1 parent 9ac5eeb commit 3ca71fa

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

.github/workflows/build-test.yml

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
security-events: write # Required if you want to upload scan results to GitHub Security tab
13+
14+
jobs:
15+
build-test:
16+
name: Build, Test & Push Image
17+
runs-on: ubuntu-latest
18+
19+
steps:
20+
- name: Checkout code
21+
uses: actions/checkout@v4
22+
23+
- name: Use Node.js 20 and cache npm
24+
uses: actions/setup-node@v4
25+
with:
26+
node-version: '20'
27+
cache: 'npm'
28+
cache-dependency-path: package-lock.json
29+
30+
- name: Cache Next.js build artifacts
31+
uses: actions/cache@v4
32+
with:
33+
path: ./.next/cache
34+
key: ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}-${{ hashFiles('**/*.{js,jsx,ts,tsx,css,html}') }}
35+
restore-keys: |
36+
${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}-
37+
38+
- name: Install dependencies
39+
run: npm ci
40+
41+
- name: Run linter
42+
run: npm run lint
43+
44+
- name: SonarQube Scan
45+
uses: SonarSource/sonarqube-scan-action@v6
46+
env:
47+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
48+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
49+
50+
- name: Build production bundle
51+
run: npm run build
52+
53+
- name: Extract branch name
54+
id: branch
55+
run: |
56+
BRANCH_NAME=${GITHUB_REF#refs/heads/}
57+
echo "name=${BRANCH_NAME}" >> $GITHUB_OUTPUT
58+
59+
# --- NEW STEP: Required for advanced Docker caching and building ---
60+
- name: Set up Docker Buildx
61+
uses: docker/setup-buildx-action@v3
62+
63+
- name: Docker meta
64+
id: meta
65+
uses: docker/metadata-action@v5
66+
with:
67+
images: ghcr.io/${{ github.repository_owner }}/frontend_web
68+
tags: |
69+
type=raw,value=${{ steps.branch.outputs.name }}-{{sha}},enable=true
70+
type=raw,value=latest,enable={{is_default_branch}}
71+
72+
# --- NEW STEP: Build locally for scanning (Don't push yet) ---
73+
- name: Build and export to Docker
74+
uses: docker/build-push-action@v5
75+
with:
76+
context: .
77+
load: true # This loads the image into the local Docker daemon
78+
tags: local-image-scan:latest # A temporary tag just for scanning
79+
80+
# --- NEW STEP: Run the Security Scan ---
81+
- name: Run Trivy vulnerability scanner
82+
uses: aquasecurity/trivy-action@0.20.0
83+
with:
84+
image-ref: 'local-image-scan:latest'
85+
format: 'table'
86+
exit-code: '1' # Fail the build if vulnerabilities are found
87+
ignore-unfixed: true # Don't fail on bugs that have no patch yet
88+
vuln-type: 'os,library'
89+
severity: 'CRITICAL,HIGH' # Only fail on Critical and High issues
90+
91+
- name: Log in to GHCR
92+
uses: docker/login-action@v3
93+
with:
94+
registry: ghcr.io
95+
username: ${{ github.actor }}
96+
password: ${{ secrets.GITHUB_TOKEN }}
97+
98+
# Actual Push Step (Uses cache from previous build step)
99+
- name: Build and push Docker image
100+
uses: docker/build-push-action@v5
101+
with:
102+
context: .
103+
push: true
104+
tags: ${{ steps.meta.outputs.tags }}
105+
labels: ${{ steps.meta.outputs.labels }}
106+
107+
- name: Image Summary
108+
run: |
109+
echo "### 🐳 Docker Image Built" >> $GITHUB_STEP_SUMMARY
110+
echo "**Tags pushed:**" >> $GITHUB_STEP_SUMMARY
111+
echo '```' >> $GITHUB_STEP_SUMMARY
112+
echo "${{ steps.meta.outputs.tags }}" >> $GITHUB_STEP_SUMMARY
113+
echo '```' >> $GITHUB_STEP_SUMMARY

0 commit comments

Comments
 (0)