Skip to content

Commit c9f50bd

Browse files
committed
feat: Add GitHub Actions workflow for building, testing, and pushing Docker images
1 parent 2c4e7ec commit c9f50bd

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

.github/workflows/build-test.yml

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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: Set up JDK 17
24+
uses: actions/setup-java@v4
25+
with:
26+
java-version: '17'
27+
distribution: 'temurin'
28+
cache: maven
29+
30+
- name: Cache Maven packages
31+
uses: actions/cache@v4
32+
with:
33+
path: ~/.m2/repository
34+
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
35+
restore-keys: |
36+
${{ runner.os }}-maven-
37+
38+
- name: Build with Maven
39+
env:
40+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
41+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
42+
run: mvn -B clean verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=CSO2_product-catalogue-service -Dsonar.organization=cso2 -Dsonar.host.url=https://sonarcloud.io -DskipTests
43+
44+
- name: Extract branch name
45+
id: branch
46+
run: |
47+
BRANCH_NAME=${GITHUB_REF#refs/heads/}
48+
echo "name=${BRANCH_NAME}" >> $GITHUB_OUTPUT
49+
50+
# --- NEW STEP: Required for advanced Docker caching and building ---
51+
- name: Set up Docker Buildx
52+
uses: docker/setup-buildx-action@v3
53+
54+
- name: Docker meta
55+
id: meta
56+
uses: docker/metadata-action@v5
57+
with:
58+
images: ghcr.io/${{ github.repository_owner }}/product_catalogue_service
59+
tags: |
60+
type=raw,value=${{ steps.branch.outputs.name }}-{{sha}},enable=true
61+
type=raw,value=latest,enable={{is_default_branch}}
62+
63+
# --- NEW STEP: Build locally for scanning (Don't push yet) ---
64+
- name: Build and export to Docker
65+
uses: docker/build-push-action@v5
66+
with:
67+
context: .
68+
load: true # This loads the image into the local Docker daemon
69+
tags: local-image-scan:latest # A temporary tag just for scanning
70+
71+
# --- NEW STEP: Run the Security Scan ---
72+
- name: Run Trivy vulnerability scanner
73+
uses: aquasecurity/trivy-action@0.20.0
74+
with:
75+
image-ref: 'local-image-scan:latest'
76+
format: 'table'
77+
exit-code: '1' # Fail the build if vulnerabilities are found
78+
ignore-unfixed: true # Don't fail on bugs that have no patch yet
79+
vuln-type: 'os,library'
80+
severity: 'CRITICAL,HIGH' # Only fail on Critical and High issues
81+
82+
- name: Log in to GHCR
83+
uses: docker/login-action@v3
84+
with:
85+
registry: ghcr.io
86+
username: ${{ github.actor }}
87+
password: ${{ secrets.GITHUB_TOKEN }}
88+
89+
# Actual Push Step (Uses cache from previous build step)
90+
- name: Build and push Docker image
91+
uses: docker/build-push-action@v5
92+
with:
93+
context: .
94+
push: true
95+
tags: ${{ steps.meta.outputs.tags }}
96+
labels: ${{ steps.meta.outputs.labels }}
97+
98+
- name: Image Summary
99+
run: |
100+
echo "### 🐳 Docker Image Built" >> $GITHUB_STEP_SUMMARY
101+
echo "**Tags pushed:**" >> $GITHUB_STEP_SUMMARY
102+
echo '```' >> $GITHUB_STEP_SUMMARY
103+
echo "${{ steps.meta.outputs.tags }}" >> $GITHUB_STEP_SUMMARY
104+
echo '```' >> $GITHUB_STEP_SUMMARY

0 commit comments

Comments
 (0)