diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 0a04b8b..ff2d348 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 8080: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/gradlew b/gradlew old mode 100644 new mode 100755 diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml deleted file mode 100644 index e69de29..0000000