diff --git a/.github/workflows/mumuk-ci-cd.yml b/.github/workflows/mumuk-ci-cd.yml index 66c312f0..18a6e059 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,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 @@ -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 \ 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..343335c8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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"] \ 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: 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 @@