Skip to content

Commit c6ce5b1

Browse files
committed
Add CI
1 parent 561efe5 commit c6ce5b1

File tree

4 files changed

+195
-24
lines changed

4 files changed

+195
-24
lines changed

.github/workflows/ci-sonar.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name:
2+
CI & SonarQube
3+
on:
4+
push:
5+
branches: [ "**" ]
6+
pull_request:
7+
8+
permissions:
9+
contents: read
10+
pull-requests: write
11+
12+
jobs:
13+
build-test-and-analyze:
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- name: Checkout
18+
uses: actions/checkout@v4
19+
with:
20+
fetch-depth: 0 # Sonar cần có full history để tính "blame"
21+
22+
- name: Set up JDK 21
23+
uses: actions/setup-java@v4
24+
with:
25+
distribution: 'temurin'
26+
java-version: '21'
27+
cache: 'maven'
28+
29+
- name: Build & Test
30+
run: mvn -B -DskipTests=false verify
31+
32+
- name: SonarQube Scan
33+
env:
34+
# SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
35+
# SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
36+
SONAR_TOKEN: f3d4a7c57b1c39eaebcc3d05496d56ca407d7872
37+
SONAR_HOST_URL: https://sonarcloud.io
38+
run: |
39+
if [ -z "$SONAR_TOKEN" ] || [ -z "$SONAR_HOST_URL" ]; then
40+
echo "SONAR_* secrets chưa được cấu hình => bỏ qua phân tích Sonar."
41+
exit 0
42+
fi
43+
mvn -B -DskipTests=true \
44+
-Dsonar.coverage.jacoco.xmlReportPaths='**/target/site/**/jacoco*.xml' \
45+
sonar:sonar
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
name: Build & Push Docker Images
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
tags: [ "v*.*.*" ]
7+
workflow_dispatch:
8+
9+
permissions:
10+
contents: read
11+
packages: write
12+
13+
concurrency:
14+
group: docker-${{ github.ref }}
15+
cancel-in-progress: true
16+
17+
jobs:
18+
build-push:
19+
runs-on: ubuntu-latest
20+
strategy:
21+
fail-fast: false
22+
matrix:
23+
module:
24+
- identity-service
25+
- submission-service
26+
- quiz-service
27+
- coding-service
28+
- ai-service
29+
- search-service
30+
- notification-service
31+
- chat-service
32+
- post-service
33+
- profile-service
34+
- payment-service
35+
- gateway-service
36+
- org-service
37+
38+
steps:
39+
- name: Checkout
40+
uses: actions/checkout@v4
41+
42+
- name: Set up QEMU (multi-arch)
43+
uses: docker/setup-qemu-action@v3
44+
45+
- name: Set up Docker Buildx
46+
uses: docker/setup-buildx-action@v3
47+
48+
- name: Derive IMAGE_TAG
49+
run: |
50+
if [ "${GITHUB_REF_TYPE}" = "tag" ]; then
51+
echo "IMAGE_TAG=${GITHUB_REF_NAME#v}" >> $GITHUB_ENV
52+
else
53+
echo "IMAGE_TAG=${GITHUB_SHA::12}" >> $GITHUB_ENV
54+
fi
55+
56+
- name: Build & Push ${{ matrix.module }}
57+
env:
58+
DOCKERHUB_USER: yunomix2834
59+
DOCKERHUB_TOKEN: dckr_pat_jwDE4gMMqQZ7y3-e3dZFmsMDYKM
60+
IMAGE_TAG: 0.1.0
61+
DOCKER_PLATFORMS: linux/amd64 # đổi thành "linux/amd64,linux/arm64" nếu muốn multi-arch
62+
run: |
63+
chmod +x ./build-image-github.sh
64+
./build-image-github.sh ${{ matrix.module }}

.github/workflows/qodana_code_quality.yml

Lines changed: 0 additions & 24 deletions
This file was deleted.

build-image-github.sh

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
export DOCKER_BUILDKIT=1
4+
5+
DOCKERHUB_USER="${DOCKERHUB_USER:-yunomix2834}"
6+
DOCKERHUB_TOKEN="${DOCKERHUB_TOKEN:-}"
7+
IMAGE_TAG="${IMAGE_TAG:-$(date +%Y%m%d.%H%M%S)}"
8+
DOCKER_PLATFORMS="${DOCKER_PLATFORMS:-linux/amd64}"
9+
10+
DEFAULT_SERVICES=(
11+
ai-service chat-service coding-service gateway-service identity-service
12+
notification-service payment-service post-service profile-service
13+
quiz-service search-service submission-service org-service
14+
)
15+
16+
login() {
17+
if [ -n "$DOCKERHUB_TOKEN" ]; then
18+
log "Logging in Docker Hub as $DOCKERHUB_USER"
19+
echo "$DOCKERHUB_TOKEN" | docker login -u "$DOCKERHUB_USER" --password-stdin
20+
else
21+
log "DOCKERHUB_TOKEN empty -> skip docker login (build will fail on --push if registry requires auth)"
22+
fi
23+
}
24+
25+
# Tự động bổ sung tag latest khi build trên nhánh main, và bổ sung tag theo tag Git
26+
extra_tags_args() {
27+
local repo="$1"
28+
local args=()
29+
30+
# Tag theo ref
31+
if [ "${GITHUB_REF_TYPE:-}" = "tag" ] && [ -n "${GITHUB_REF_NAME:-}" ]; then
32+
local version="${GITHUB_REF_NAME#v}"
33+
args+=(-t "${repo}:${version}")
34+
fi
35+
36+
# latest cho nhánh main
37+
if [ "${GITHUB_REF_NAME:-}" = "main" ]; then
38+
args+=(-t "${repo}:latest")
39+
fi
40+
41+
printf '%s ' "${args[@]}"
42+
}
43+
44+
45+
build_push_java() {
46+
local module="$1"
47+
local repo="${DOCKERHUB_USER}/codecampus-${module}"
48+
49+
log "Building Java service: ${module}"
50+
docker buildx build \
51+
--platform "${DOCKER_PLATFORMS}" \
52+
-f docker/java-service.Dockerfile \
53+
--build-arg "MODULE=${module}" \
54+
-t "${repo}:${IMAGE_TAG}" \
55+
$(extra_tags_args "${repo}") \
56+
--label "org.opencontainers.image.source=${GITHUB_SERVER_URL:-}/$([ -n "${GITHUB_REPOSITORY:-}" ] && echo "${GITHUB_REPOSITORY}")" \
57+
--label "org.opencontainers.image.revision=${GITHUB_SHA:-}" \
58+
--label "org.opencontainers.image.created=$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
59+
--push .
60+
}
61+
62+
63+
main() {
64+
login
65+
log "Using DOCKER_PLATFORMS=${DOCKER_PLATFORMS}"
66+
log "Using IMAGE_TAG=${IMAGE_TAG}"
67+
68+
# Lấy list services từ args hoặc env hoặc default
69+
local services=()
70+
if [ "$#" -gt 0 ]; then
71+
services=("$@")
72+
elif [ -n "${SERVICES:-}" ]; then
73+
# shellcheck disable=SC2206
74+
services=(${SERVICES})
75+
else
76+
services=("${DEFAULT_SERVICES[@]}")
77+
fi
78+
79+
for svc in "${services[@]}"; do
80+
build_push_java "${svc}"
81+
done
82+
83+
log "All done."
84+
}
85+
86+
main "$@"

0 commit comments

Comments
 (0)