From 9ab3f80a8118348ec7770cfcbd32a0f5ca25a7cd Mon Sep 17 00:00:00 2001 From: parkmineum Date: Sun, 17 Aug 2025 17:38:12 +0900 Subject: [PATCH 1/3] =?UTF-8?q?[REFACTOR]=20#135=20:=20=EC=9B=8C=ED=81=AC?= =?UTF-8?q?=ED=94=8C=EB=A1=9C=EC=9A=B0=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/mumuk-ci-cd.yml | 79 ++++++++++++++++++------------- Dockerfile | 10 +++- docker-compose.yml | 29 ++++++------ 3 files changed, 69 insertions(+), 49 deletions(-) diff --git a/.github/workflows/mumuk-ci-cd.yml b/.github/workflows/mumuk-ci-cd.yml index 66c312f0..8427ebb1 100644 --- a/.github/workflows/mumuk-ci-cd.yml +++ b/.github/workflows/mumuk-ci-cd.yml @@ -1,4 +1,4 @@ -name: MuMuk CI/CD Workflow with Gradle +name: CI/CD Workflow on: pull_request: @@ -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 @@ -43,21 +42,44 @@ 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 + cache-from: type=gha + cache-to: type=gha,mode=max + CD: needs: CI runs-on: ubuntu-latest @@ -67,31 +89,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 \ No newline at end of file + docker image prune -af --filter "until=24h" \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 17c42312..86af92b6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,12 +1,18 @@ -FROM openjdk:17 +FROM openjdk:17-jre-slim 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 +# 환경 변수 설정 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"] \ No newline at end of file +ENTRYPOINT ["sh", "-c", "java $JAVA_OPTS -Dspring.profiles.active=${SPRING_PROFILES_ACTIVE} -jar app.jar"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 6dfe133e..ae732f86 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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} @@ -20,12 +18,7 @@ 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 @@ -33,11 +26,12 @@ services: 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 @@ -52,6 +46,7 @@ services: timeout: 3s retries: 5 start_period: 30s + restart: unless-stopped redis-exporter: image: oliver006/redis_exporter:v1.61.0 @@ -62,6 +57,7 @@ services: - mumuk_network environment: - REDIS_ADDR=redis:6379 + restart: unless-stopped nginx: container_name: nginx @@ -94,7 +90,7 @@ services: - nginx networks: - mumuk_network - + restart: unless-stopped prometheus: image: prom/prometheus:v3.5.0 @@ -107,6 +103,7 @@ services: - "9090:9090" networks: - mumuk_network + restart: unless-stopped loki: image: grafana/loki:2.9.2 @@ -118,6 +115,7 @@ services: - ./loki:/loki networks: - mumuk_network + restart: unless-stopped promtail: image: grafana/promtail:2.9.2 @@ -133,6 +131,7 @@ services: - mumuk_network depends_on: - loki + restart: unless-stopped grafana: image: grafana/grafana:12.1.0 @@ -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: From e6894a0c827f27f596bbe2c61c5b1b6fa3824f6b Mon Sep 17 00:00:00 2001 From: parkmineum Date: Sun, 17 Aug 2025 17:47:41 +0900 Subject: [PATCH 2/3] =?UTF-8?q?[REFACTOR]=20#135=20:=20=EB=B0=B0=ED=8F=AC?= =?UTF-8?q?=20=EC=B2=B4=ED=81=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 7 +++++-- src/main/resources/static/index.html | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 86af92b6..343335c8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,11 +1,14 @@ -FROM openjdk:17-jre-slim +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 diff --git a/src/main/resources/static/index.html b/src/main/resources/static/index.html index d9b4a417..9185d41d 100644 --- a/src/main/resources/static/index.html +++ b/src/main/resources/static/index.html @@ -5,6 +5,6 @@ Title -

Server is Running

+

Server is Running !

\ No newline at end of file From e613899abb65cbdecefca487b5df24e96fc939fc Mon Sep 17 00:00:00 2001 From: parkmineum Date: Sun, 17 Aug 2025 18:06:02 +0900 Subject: [PATCH 3/3] =?UTF-8?q?[REFACTOR]=20#135=20:=20SHA=20=ED=83=9C?= =?UTF-8?q?=EA=B7=B8=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/mumuk-ci-cd.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/mumuk-ci-cd.yml b/.github/workflows/mumuk-ci-cd.yml index 8427ebb1..18a6e059 100644 --- a/.github/workflows/mumuk-ci-cd.yml +++ b/.github/workflows/mumuk-ci-cd.yml @@ -76,7 +76,9 @@ jobs: context: . file: ./Dockerfile push: true - tags: ${{ secrets.DOCKERHUB_USERNAME }}/mumuk:latest + tags: | + ${{ secrets.DOCKERHUB_USERNAME }}/mumuk:latest + ${{ secrets.DOCKERHUB_USERNAME }}/mumuk:${{ github.sha }} cache-from: type=gha cache-to: type=gha,mode=max