From a9c8aa17e5a5b594e06a34411a22edd2f8e60210 Mon Sep 17 00:00:00 2001 From: LeeJiWon Date: Wed, 25 Dec 2024 18:58:47 +0900 Subject: [PATCH 1/2] =?UTF-8?q?be/feat:=20github=20actions=20=EB=B0=B0?= =?UTF-8?q?=ED=8F=AC=20=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/backend-cd.yml | 40 +++++++++++++++++++ backend/Dockerfile.prod | 4 ++ backend/build.gradle | 4 ++ .../backend/global/config/SecurityConfig.java | 2 +- 4 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/backend-cd.yml create mode 100644 backend/Dockerfile.prod diff --git a/.github/workflows/backend-cd.yml b/.github/workflows/backend-cd.yml new file mode 100644 index 00000000..a79b6ea1 --- /dev/null +++ b/.github/workflows/backend-cd.yml @@ -0,0 +1,40 @@ +name: 배포 + +on: + push: + branches: [ "develop" ] + +jobs: + deploy: + runs-on: ubuntu-latest # 작업이 실행될 환경 + steps: + - name: 체크아웃 + uses: actions/checkout@v3 + - name: JDK 17 사용 + uses: actions/setup-java@v3 + with: + java-version: '17' + distribution: 'corretto' + - name: Gradle Wrapper 실행 권한 추가 + run: chmod +x gradlew + - name: Gradle로 빌드(CI) + run: ./gradlew build + - name: 도커허브에 로그인 + uses: docker/login-action@v1 + with: + username: ${{ secrets.DOCKER_USER_NAME }} + password: ${{ secrets.DOCKER_USER_PW }} + - name: 이미지 빌드 + run: docker buildx build --platform linux/amd64 -f Dockerfile.prod -t ${{ secrets.DOCKER_USER_NAME }}/${{ secrets.DOCKER_IMAGE_NAME }}-be:latest . + - name: 도커허브에 이미지 푸시 + run: docker push ${{ secrets.DOCKER_USER_NAME }}/${{ secrets.DOCKER_IMAGE_NAME }}-be:latest + - name: AWS EC2에 ssh 접속 후 배포 + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.AWS_IP }} + port: 22 + username: ubuntu + key: ${{ secrets.AWS_KEY }} + script: | + docker pull ${{ secrets.DOCKER_USER_NAME }}/${{ secrets.DOCKER_IMAGE_NAME }}-be:latest + docker-compose --env-file .env.production up -d \ No newline at end of file diff --git a/backend/Dockerfile.prod b/backend/Dockerfile.prod new file mode 100644 index 00000000..1a9101c6 --- /dev/null +++ b/backend/Dockerfile.prod @@ -0,0 +1,4 @@ +FROM openjdk:17-jdk +ARG JAR_FILE=build/libs/*.jar +COPY ${JAR_FILE} techeer-resume-0.0.1-SNAPSHOT.jar +ENTRYPOINT ["java","-jar","/techeer-resume-0.0.1-SNAPSHOT.jar"] \ No newline at end of file diff --git a/backend/build.gradle b/backend/build.gradle index cb2260a1..db3fd16c 100644 --- a/backend/build.gradle +++ b/backend/build.gradle @@ -13,6 +13,10 @@ java { } } +jar { + enabled = false +} + configurations { compileOnly { extendsFrom annotationProcessor diff --git a/backend/src/main/java/com/techeer/backend/global/config/SecurityConfig.java b/backend/src/main/java/com/techeer/backend/global/config/SecurityConfig.java index c5e26010..abc40843 100644 --- a/backend/src/main/java/com/techeer/backend/global/config/SecurityConfig.java +++ b/backend/src/main/java/com/techeer/backend/global/config/SecurityConfig.java @@ -40,7 +40,7 @@ public CorsConfigurationSource corsConfigurationSource() { CorsConfiguration config = new CorsConfiguration(); config.setAllowCredentials(true); - config.setAllowedOrigins(List.of("http://localhost:8080", "http://localhost:5173", "http://backend:8080")); + config.setAllowedOrigins(List.of("http://localhost:8080", "http://localhost:5173", "http://backend:8080", "http://backend:5173", "http://rexume.site:8080", "http://rexume.site:5173", "http://rexume.site", "http://52.78.85.237:8080", "http://52.78.85.237:5173")); config.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS")); config.setAllowedHeaders(List.of("*")); config.setExposedHeaders(List.of("*")); From 9ac68a36e5f2bfb3718f7be4af2bfcfd0c80566d Mon Sep 17 00:00:00 2001 From: Lee Jiwon <107186291+dlwldnjs1009@users.noreply.github.com> Date: Sat, 1 Mar 2025 10:42:51 +0900 Subject: [PATCH 2/2] fix: backend-cd.yml MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 이벤트 트리거: on: 아래에 push 이벤트는 develop 브랜치에, 그리고 pull_request 이벤트는 main과 develop 브랜치에 대해 트리거되도록 수정 기본 작업 디렉토리: jobs.deploy.defaults.run.working-directory를 backend로 지정해 모든 run 단계가 backend 디렉토리에서 실행되도록 수정 --- .github/workflows/backend-cd.yml | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/.github/workflows/backend-cd.yml b/.github/workflows/backend-cd.yml index a79b6ea1..41e4a77b 100644 --- a/.github/workflows/backend-cd.yml +++ b/.github/workflows/backend-cd.yml @@ -2,11 +2,19 @@ name: 배포 on: push: - branches: [ "develop" ] + branches: + - 'be/**' + pull_request: + branches: + - main + - develop jobs: deploy: runs-on: ubuntu-latest # 작업이 실행될 환경 + defaults: + run: + working-directory: backend steps: - name: 체크아웃 uses: actions/checkout@v3 @@ -36,5 +44,7 @@ jobs: username: ubuntu key: ${{ secrets.AWS_KEY }} script: | + sudo docker rm -f $(docker ps -qa) docker pull ${{ secrets.DOCKER_USER_NAME }}/${{ secrets.DOCKER_IMAGE_NAME }}-be:latest - docker-compose --env-file .env.production up -d \ No newline at end of file + docker-compose --env-file .env.production up -d + docker image prune -f