From a36f6ff4b426ff36cc40e30aef129cfdc2702174 Mon Sep 17 00:00:00 2001 From: Junha Date: Sat, 29 Mar 2025 12:58:58 +0900 Subject: [PATCH 1/3] ci/cd --- .github/workflows/deploy.yml | 51 +++++++++++++++++++----------- .gitignore | 1 + Dockerfile | 3 ++ src/main/resources/application.yml | 0 4 files changed, 36 insertions(+), 19 deletions(-) create mode 100644 Dockerfile delete mode 100644 src/main/resources/application.yml diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 0a04b8b..66be50c 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -2,11 +2,14 @@ name: Deploy To EC2 on: push: - branches: [ "main", "ci/cd" ] + branches: + - ci/cd jobs: deploy: runs-on: ubuntu-latest + env: + APP_NAME: futsal-finder steps: - name: Github Repository 파일 불러오기 uses: actions/checkout@v4 @@ -17,32 +20,42 @@ jobs: distribution: temurin java-version: 17 - - name: gradlew 파일 실행 권한 추가 - run: chmod +x ./gradlew + - name: application.yml 파일 만들기 + run: echo "${{ secrets.APPLICATION_PROPERTIES }}" > ./src/main/resources/application.yml - name: 테스트 및 빌드하기 - run: ./gradlew clean build -x test + run: ./gradlew clean build - - name: 빌드된 파일 이름 변경하기 - run: mv ./build/libs/*SNAPSHOT.jar ./futsal-finder.jar - - - name: SCP로 EC2에 빌드된 파일 전송하기 - uses: appleboy/scp-action@v0.1.7 + - name: AWS Resource에 접근할 수 있게 AWS credentials 설정 + uses: aws-actions/configure-aws-credentials@v4 with: - host: ${{ secrets.EC2_HOST }} - username: ${{ secrets.EC2_USERNAME }} - key: ${{ secrets.EC2_PRIVATE_KEY }} - source: futsal-finder.jar - target: /home/ubuntu/futsal-finder + aws-region: ap-northeast-2 + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + + - name: ECR에 로그인하기 + id: login-ecr + uses: aws-actions/amazon-ecr-login@v2 + + - name: Docker 이미지 생성 + run: docker build -t $APP_NAME . + + - name: Docker 이미지에 Tag 붙이기 + run: docker tag $APP_NAME ${{ steps.login-ecr.outputs.registry }}/$APP_NAME:latest + + - name: ECR에 Docker 이미지 Push하기 + run: docker push ${{ steps.login-ecr.outputs.registry }}/$APP_NAME:latest - - name: SSH로 EC2에 접속하여 배포 스크립트 실행 + - name: SSH로 EC2에 접속하기 uses: appleboy/ssh-action@v1.0.3 with: host: ${{ secrets.EC2_HOST }} username: ${{ secrets.EC2_USERNAME }} key: ${{ secrets.EC2_PRIVATE_KEY }} - script_stop: false + script_stop: true script: | - cd /home/ubuntu/futsal-finder - bash deploy.sh - continue-on-error: true \ No newline at end of file + APP_NAME=${{ env.APP_NAME }} + docker stop $APP_NAME || true + docker rm $APP_NAME || true + docker pull ${{ steps.login-ecr.outputs.registry }}/$APP_NAME:latest + docker run -d --name $APP_NAME -p 80:8080 ${{ steps.login-ecr.outputs.registry }}/$APP_NAME:latest \ No newline at end of file diff --git a/.gitignore b/.gitignore index c2065bc..d9f47e6 100644 --- a/.gitignore +++ b/.gitignore @@ -16,6 +16,7 @@ build/ bin/ !**/src/main/**/bin/ !**/src/test/**/bin/ +/src/main/resources/application*.yml ### IntelliJ IDEA ### .idea diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..c57f141 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,3 @@ +FROM eclipse-temurin:17-jdk-alpine +COPY ./build/libs/*SNAPSHOT.jar project.jar +ENTRYPOINT ["java", "-jar", "project.jar"] \ No newline at end of file diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml deleted file mode 100644 index e69de29..0000000 From 8b2ac996f44cbccc4a0735ad23900a326292e446 Mon Sep 17 00:00:00 2001 From: Junha Date: Sat, 29 Mar 2025 13:01:51 +0900 Subject: [PATCH 2/3] Fix: gradlew permission --- gradlew | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 gradlew diff --git a/gradlew b/gradlew old mode 100644 new mode 100755 From 7f3324fa3f4a02be1d628e2d3bbd34ca7b0c12da Mon Sep 17 00:00:00 2001 From: Junha Date: Sat, 29 Mar 2025 13:12:08 +0900 Subject: [PATCH 3/3] =?UTF-8?q?8080->8080=20=ED=8F=AC=ED=8A=B8=EB=B0=94?= =?UTF-8?q?=EC=9D=B8=EB=94=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 66be50c..ff2d348 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -58,4 +58,4 @@ jobs: docker stop $APP_NAME || true docker rm $APP_NAME || true docker pull ${{ steps.login-ecr.outputs.registry }}/$APP_NAME:latest - docker run -d --name $APP_NAME -p 80:8080 ${{ steps.login-ecr.outputs.registry }}/$APP_NAME:latest \ No newline at end of file + docker run -d --name $APP_NAME -p 8080:8080 ${{ steps.login-ecr.outputs.registry }}/$APP_NAME:latest \ No newline at end of file