Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 48 additions & 33 deletions .github/workflows/mumuk-ci-cd.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: MuMuk CI/CD Workflow with Gradle
name: CI/CD Workflow

on:
pull_request:
Expand All @@ -19,17 +19,16 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Set up JDK 17
uses: actions/setup-java@v3
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'

- name: Grant execute permission for gradlew
run: chmod +x ./gradlew
shell: bash

- name: Gradle Caching
uses: actions/cache@v3
Expand All @@ -43,21 +42,46 @@ jobs:

- name: Create application.yml
run: |
cd ./src/main/resources
echo "${{ secrets.APPLICATION }}" > ./application-prod.yml
shell: bash
mkdir -p ./src/main/resources
echo "${{ secrets.APPLICATION }}" > ./src/main/resources/application-prod.yml

- name: Build with Gradle without tests
- name: Build with Gradle
run: ./gradlew clean bootJar -x test

- name: Copy JAR to EC2
uses: appleboy/scp-action@master
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Test Docker Build (PR Only)
if: github.event_name == 'pull_request'
uses: docker/build-push-action@v5
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USERNAME }}
key: ${{ secrets.SERVER_SSH_KEY }}
source: "build/libs/*.jar"
target: "/home/ubuntu/BE/build/libs/"
context: .
file: ./Dockerfile
push: false
tags: test-build
cache-from: type=gha
cache-to: type=gha,mode=max

- name: Login to Docker Hub
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Build and push Docker image
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
push: true
tags: |
${{ secrets.DOCKERHUB_USERNAME }}/mumuk:latest
${{ secrets.DOCKERHUB_USERNAME }}/mumuk:${{ github.sha }}
cache-from: type=gha
cache-to: type=gha,mode=max

CD:
needs: CI
runs-on: ubuntu-latest
Expand All @@ -67,31 +91,22 @@ jobs:
url: https://api.mumuk.site
steps:
- name: Deploy to EC2
uses: appleboy/ssh-action@master
uses: appleboy/ssh-action@v1.0.3
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USERNAME }}
key: ${{ secrets.SERVER_SSH_KEY }}
script: |
cd /home/ubuntu/BE

sudo chmod -R u+w ./nginx

sudo docker-compose down


git fetch origin
git reset --hard origin/main

./gradlew clean bootjar

if [ "$(sudo docker ps -qa)" ]; then
sudo docker ps -qa | xargs -r sudo docker rm -f
fi

IMAGES=$(sudo docker images -q)
if [ -n "$IMAGES" ]; then
sudo docker rmi $IMAGES
fi
docker pull ${{ secrets.DOCKERHUB_USERNAME }}/mumuk:latest

sed -i "s|image: \${DOCKERHUB_USERNAME}/mumuk:latest|image: ${{ secrets.DOCKERHUB_USERNAME }}/mumuk:latest|g" docker-compose.yml

docker-compose down --remove-orphans
docker-compose up -d

sudo docker-compose up --build -d
sudo docker image prune -f
docker image prune -af --filter "until=24h"
15 changes: 12 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
FROM openjdk:17
FROM eclipse-temurin:17-jre-alpine

ARG JAR_FILE=build/libs/*.jar

COPY ${JAR_FILE} app.jar

RUN ln -sf /usr/share/zoneinfo/Asia/Seoul /etc/localtime && echo "Asia/Seoul" > /etc/timezone
# 시간대 설정
RUN apk add --no-cache tzdata && \
cp /usr/share/zoneinfo/Asia/Seoul /etc/localtime && \
echo "Asia/Seoul" > /etc/timezone && \
apk del tzdata

# 환경 변수 설정
ENV SPRING_PROFILES_ACTIVE=prod
ENV JAVA_OPTS="-Xms512m -Xmx1024m -Duser.timezone=Asia/Seoul"

# 애플리케이션 포트 노출
EXPOSE 8080

# 시스템 진입점 정의
ENTRYPOINT ["java", "-jar", "-Duser.timezone=Asia/Seoul", "-Dspring.profiles.active=${SPRING_PROFILES_ACTIVE}", "app.jar"]
ENTRYPOINT ["sh", "-c", "java $JAVA_OPTS -Dspring.profiles.active=${SPRING_PROFILES_ACTIVE} -jar app.jar"]
29 changes: 15 additions & 14 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ services:

backend:
container_name: backend
image: parkmineum/mumuk-backend:v1.0.0
build:
context: ./
dockerfile: Dockerfile
image: ${DOCKERHUB_USERNAME}/mumuk:latest
pull_policy: always
env_file:
- .env
expose:
- "8080"
ports:
- "8080:8080"
environment:
- SPRING_PROFILES_ACTIVE=prod
- POSTGRESQL_USER=${PROD_DB_USERNAME}
Expand All @@ -20,24 +18,20 @@ services:
depends_on:
redis:
condition: service_healthy
healthcheck:
test: [ "CMD", "curl", "-f", "http://localhost" ]
interval: 30s
timeout: 10s
retries: 5
start_period: 60s
restart: unless-stopped

postgres-exporter:
image: prometheuscommunity/postgres-exporter
container_name: postgres-exporter
env_file:
- .env
environment:
DATA_SOURCE_NAME: "postgresql://${PROD_DB_USERNAME}:${PROD_DB_PASSWORD}@${PROD_DB_ENDPOINT}:${PROD_DB_PORT}/${PROD_DB_NAME}"
DATA_SOURCE_NAME: "postgresql://${PROD_DB_USERNAME}:${PROD_DB_PASSWORD}@${PROD_DB_ENDPOINT}:5432/${PROD_DB_NAME}"
ports:
- "9187:9187"
networks:
- mumuk_network
restart: unless-stopped

redis:
image: redis:7.2
Expand All @@ -52,6 +46,7 @@ services:
timeout: 3s
retries: 5
start_period: 30s
restart: unless-stopped

redis-exporter:
image: oliver006/redis_exporter:v1.61.0
Expand All @@ -62,6 +57,7 @@ services:
- mumuk_network
environment:
- REDIS_ADDR=redis:6379
restart: unless-stopped

nginx:
container_name: nginx
Expand Down Expand Up @@ -94,7 +90,7 @@ services:
- nginx
networks:
- mumuk_network

restart: unless-stopped

prometheus:
image: prom/prometheus:v3.5.0
Expand All @@ -107,6 +103,7 @@ services:
- "9090:9090"
networks:
- mumuk_network
restart: unless-stopped

loki:
image: grafana/loki:2.9.2
Expand All @@ -118,6 +115,7 @@ services:
- ./loki:/loki
networks:
- mumuk_network
restart: unless-stopped

promtail:
image: grafana/promtail:2.9.2
Expand All @@ -133,6 +131,7 @@ services:
- mumuk_network
depends_on:
- loki
restart: unless-stopped

grafana:
image: grafana/grafana:12.1.0
Expand All @@ -148,6 +147,8 @@ services:
- GF_SECURITY_ADMIN_PASSWORD=${GRAFANA_ADMIN_PASSWORD}
- GF_SERVER_DOMAIN=grafana.mumuk.site
- GF_SERVER_ROOT_URL=https://grafana.mumuk.site/
restart: unless-stopped

volumes:
grafana-storage:

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
<title>Title</title>
</head>
<body>
<h1>Server is Running</h1>
<h1>Server is Running !</h1>
</body>
</html>