diff --git a/.github/workflows/dev-aws-CI-CD.yml b/.github/workflows/dev-aws-CI-CD.yml new file mode 100644 index 0000000..88ac481 --- /dev/null +++ b/.github/workflows/dev-aws-CI-CD.yml @@ -0,0 +1,176 @@ +name: dev - CI/CD to Amazon ECS + +on: + push: + branches: [ "develop" ] + pull_request: + branches: [ "develop" ] + +permissions: + contents: read + checks: write + issues: write + +jobs: + build: + runs-on: ubuntu-latest + environment: dev + if: github.event_name == 'push' || github.event_name == 'pull_request' + env: # Vault 값들을 전역 환경 변수로 설정 + VAULT_URI: ${{ secrets.VAULT_URI }} + VAULT_TOKEN: ${{ secrets.VAULT_TOKEN }} + VAULT_BACKEND: ${{ secrets.VAULT_BACKEND }} + VAULT_DEFAULT_CONTEXT: ${{ secrets.VAULT_DEFAULT_CONTEXT }} + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Set up JDK 17 + uses: actions/setup-java@v3 + with: + java-version: '17' + distribution: 'temurin' + + - name: Gradle Caching + uses: actions/cache@v3 + with: + path: | + ~/.gradle/caches + ~/.gradle/wrapper + key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} + restore-keys: | + ${{ runner.os }}-gradle- + + - name: Grant Execute Permission For Gradlew + run: chmod +x gradlew + + - name: Build With Gradle + run: ./gradlew build -x test --info + + - name: List All Files for Debugging + run: ls -laR # 모든 디렉토리의 파일 나열 + + - name: Upload JAR Artifact + uses: actions/upload-artifact@v3 + with: + name: jar-file + path: build/libs/clothstar-0.0.1-SNAPSHOT.jar + + deploy: + name: Deploy + runs-on: ubuntu-latest + environment: dev + if: github.event_name == 'push' + needs: build + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Download JAR Artifact + uses: actions/download-artifact@v3 + with: + name: jar-file + path: build/libs/ + + - name: List Files for Debugging + run: ls -la ./ # 현재 디렉토리의 모든 파일 나열 + + - name: Show docker-compose.yml Content + run: cat docker-compose.yml # docker-compose.yml 파일 내용 확인 + + - name: Send docker-compose.yml and nginx.conf to Home Directory + uses: appleboy/scp-action@master + with: + username: ubuntu + host: ${{ secrets.AWS_DEV_HOSTNAME }} + key: ${{ secrets.AWS_DEV_PRIVATE_KEY }} + source: "./docker-compose.yml,./nginx/conf.d/nginx.conf" + target: "/home/ubuntu/" + strip_components: 3 # nginx.conf 경로 구성 요소를 제거하여 파일만 전송 + + # Move nginx.conf from Home Directory to /etc/nginx/conf.d + - name: Move nginx.conf to /etc/nginx/conf.d + uses: appleboy/ssh-action@master + with: + username: ubuntu + host: ${{ secrets.AWS_DEV_HOSTNAME }} + key: ${{ secrets.AWS_DEV_PRIVATE_KEY }} + script: | + sudo mkdir -p /etc/nginx/conf.d/ + if [ -f /home/ubuntu/nginx.conf ]; then + sudo mv /home/ubuntu/nginx.conf /etc/nginx/conf.d/nginx.conf + else + echo "nginx.conf not found in /home/ubuntu/" + exit 1 + fi + + ## Docker login + - name: Docker Login + run: | + docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} + + ## Nginx 도커 이미지 빌드 후 도커허브에 push하기 + - name: Docker build & Push for Nginx + run: | + docker build -f Dockerfile-nginx -t ${{ secrets.DOCKER_REPOSITORY_NGINX }} . + docker push ${{ secrets.DOCKER_REPOSITORY_NGINX }} + + - name: Check JAR file existence + run: ls -la build/libs/ + + ## Spring Boot 도커 이미지 빌드 후 도커허브에 push하기 + - name: Docker build & Push for Spring Boot + run: | + docker build -t ${{ secrets.DOCKER_REPOSITORY }} . + docker push ${{ secrets.DOCKER_REPOSITORY }} + + - name: Check Files on AWS Ubuntu + uses: appleboy/ssh-action@master + with: + username: ubuntu + host: ${{ secrets.AWS_DEV_HOSTNAME }} + key: ${{ secrets.AWS_DEV_PRIVATE_KEY }} + script: | + ls -la /home/ubuntu/ # AWS 서버에서 파일이 있는지 확인 + cat /home/ubuntu/docker-compose.yml # AWS 서버에서 파일 내용 확인 + + # SSH로 서버에 연결 및 Docker compose 실행 + - name: Deploy and Run Docker Compose on Server + uses: appleboy/ssh-action@master + with: + username: ubuntu + host: ${{ secrets.AWS_DEV_HOSTNAME }} + key: ${{ secrets.AWS_DEV_PRIVATE_KEY }} + script: | # SSH 연결 후 실행할 명령어들 + docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} + export DOCKER_REPOSITORY=${{ secrets.DOCKER_REPOSITORY }} + export DOCKER_REPOSITORY_NGINX=${{ secrets.DOCKER_REPOSITORY_NGINX }} + # Pull 최신 이미지 + docker-compose -f /home/ubuntu/docker-compose.yml pull + # 새 컨테이너 실행 + docker-compose -f /home/ubuntu/docker-compose.yml up -d --build + + discord-notify: + name: Discord Notify + runs-on: ubuntu-latest + environment: dev + needs: [ build, deploy ] + if: always() + steps: + - name: Send Discord Notification + uses: sarisia/actions-status-discord@v1 + with: + webhook: ${{ secrets.DISCORD_WEBHOOK_URL }} + status: ${{ job.status }} + title: "CI/CD Pipeline Status" + description: | + ${{ format( + 'The CI/CD pipeline has completed.\n- **Build Job Status**: {0}\n- **Deploy Job Status**: {1}\n- **Branch**: {2}\n- **Commit**: {3}\n- **Author**: {4}', + needs.build.result, + needs.deploy.result, + github.ref, + github.sha, + github.actor + ) }} + url: "https://github.com/sarisia/actions-status-discord" + username: GitHub Actions Bot \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a12deca --- /dev/null +++ b/Dockerfile @@ -0,0 +1,20 @@ +FROM openjdk:17-alpine + +# Vault 관련 ARG 정의 +ARG VAULT_URI +ARG VAULT_TOKEN +ARG VAULT_BACKEND +ARG VAULT_DEFAULT_CONTEXT + +# Vault 관련 ENV 설정 +ENV VAULT_URI=${VAULT_URI} +ENV VAULT_TOKEN=${VAULT_TOKEN} +ENV VAULT_BACKEND=${VAULT_BACKEND} +ENV VAULT_DEFAULT_CONTEXT=${VAULT_DEFAULT_CONTEXT} + +# JAR 파일 복사 +ARG JAR_FILE=build/libs/Chekirout-0.0.1-SNAPSHOT.jar +COPY ${JAR_FILE} app.jar + +# 애플리케이션 실행 +ENTRYPOINT ["java", "-Dspring.profiles.active=dev", "-jar", "/app.jar"] \ No newline at end of file diff --git a/Dockerfile-nginx b/Dockerfile-nginx new file mode 100644 index 0000000..2de6e9c --- /dev/null +++ b/Dockerfile-nginx @@ -0,0 +1,2 @@ +FROM nginx +COPY ./nginx/conf.d/nginx.conf /etc/nginx/conf.d \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..b19cbb0 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,20 @@ +services: + application: + container_name: chekirout-springboot-dev + image: ${DOCKER_REPOSITORY}:latest + environment: + - VAULT_URI=${VAULT_URI} + - VAULT_TOKEN=${VAULT_TOKEN} + - VAULT_BACKEND=${VAULT_BACKEND} + - VAULT_DEFAULT_CONTEXT=${VAULT_DEFAULT_CONTEXT} + ports: + - "8080:8080" + restart: on-failure + + nginx: + container_name: nginx + image: ${DOCKER_REPOSITORY_NGINX}:latest + ports: + - 80:80 + depends_on: + - application diff --git a/nginx/conf.d/nginx.conf b/nginx/conf.d/nginx.conf new file mode 100644 index 0000000..49bb038 --- /dev/null +++ b/nginx/conf.d/nginx.conf @@ -0,0 +1,13 @@ +server { + listen 80; + server_name *.compute.amazonaws.com + access_log off; + + location / { + proxy_pass http://clothstar-springboot-dev:8080; + proxy_set_header Host $host:$server_port; + proxy_set_header X-Forwarded-Host $server_name; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + } +} \ No newline at end of file diff --git a/src/test/java/com/sch/chekirout/ChekiroutApplicationTests.java b/src/test/java/com/sch/chekirout/ChekiroutApplicationTests.java index bd1232f..57fbd5a 100644 --- a/src/test/java/com/sch/chekirout/ChekiroutApplicationTests.java +++ b/src/test/java/com/sch/chekirout/ChekiroutApplicationTests.java @@ -1,10 +1,15 @@ package com.sch.chekirout; +import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; -@SpringBootTest -@ActiveProfiles("test") +//@SpringBootTest +@ActiveProfiles("dev") class ChekiroutApplicationTests { + @Test + void contextLoads() { + } + }