From f9ab742c6e3430d2ece270bb45ec34ae71f6c58b Mon Sep 17 00:00:00 2001 From: lja3723 Date: Thu, 7 Nov 2024 21:26:52 +0900 Subject: [PATCH 1/5] =?UTF-8?q?chore:=20github=20action=20jobs=20=EA=B7=B8?= =?UTF-8?q?=EB=A3=B9=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/gradle.yml | 79 ++++++++++++++++++++++++++++-------- 1 file changed, 61 insertions(+), 18 deletions(-) diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index ccff7b05..2eecaccb 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -10,9 +10,9 @@ permissions: contents: read jobs: - CI-CD: + ## 1단계: 프로젝트 빌드 + Build-Project: runs-on: ubuntu-22.04 - steps: - uses: actions/checkout@v4 @@ -32,37 +32,80 @@ jobs: restore-keys: | ${{ runner.os }}-gradle- - # 테스트까지 포함해 빌드하려면 -x test를 제거하기 - name: Build With Gradle (exclude test) run: | chmod +x gradlew ./gradlew clean build -x test + - name: Upload Build Artifacts + uses: actions/upload-artifact@v3 + with: + name: build-output + path: build/libs/*.jar + + ## 2단계: 도커 이미지 생성 및 푸시 + Build-Docker-Image-and-Push-to-DockerHub: + runs-on: ubuntu-22.04 + needs: Build-Project + steps: + - name: Download Build Artifacts + uses: actions/download-artifact@v3 + with: + name: build-output + - name: Docker Hub Login uses: docker/login-action@v3 with: username: ${{ secrets.DOCKER_USERNAME }} password: ${{ secrets.DOCKER_TOKEN }} - - name: Docker Build & Push - run: | - docker build -f Dockerfile --build-arg DEPENDENCY=build/dependency --platform linux/amd64 -t ${{ secrets.DOCKER_REPO_FULLNAME }} . - docker push ${{ secrets.DOCKER_REPO_FULLNAME }} + - name: Docker Build + run: docker build -f Dockerfile -t ${{ secrets.DOCKER_REPO_FULLNAME }} . + + - name: Docker Push + run: docker push ${{ secrets.DOCKER_REPO_FULLNAME }} + + ## 3단계: 서버에 배포 + Deploy-Docker-Image-to-Server-and-Run: + runs-on: ubuntu-22.04 + needs: Build-Docker-Image-and-Push-to-DockerHub + steps: + - name: Pull New Docker Image + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.SSH_HOST }} + username: ${{ secrets.SSH_USERNAME }} + key: ${{ secrets.SSH_PRIVATE_KEY }} + port: ${{ secrets.SSH_PORT }} + envs: GITHUB_SHA + script: sudo docker pull ${{ secrets.DOCKER_REPO_FULLNAME }} + + - name: Stop Old Docker Image + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.SSH_HOST }} + username: ${{ secrets.SSH_USERNAME }} + key: ${{ secrets.SSH_PRIVATE_KEY }} + port: ${{ secrets.SSH_PORT }} + envs: GITHUB_SHA + script: sudo docker stop would-you-in + + - name: Run New Docker Image + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.SSH_HOST }} + username: ${{ secrets.SSH_USERNAME }} + key: ${{ secrets.SSH_PRIVATE_KEY }} + port: ${{ secrets.SSH_PORT }} + envs: GITHUB_SHA + script: sudo docker run --rm -d -p 80:8080 --env-file ~/.env --name would-you-in ${{ secrets.DOCKER_REPO_FULLNAME }} - - name: Deploy to Server + - name: Clean-Up Docker Image uses: appleboy/ssh-action@master with: - host: ${{ secrets.SSH_HOST }} # EC2 퍼블릭 IPv4 DNS + host: ${{ secrets.SSH_HOST }} username: ${{ secrets.SSH_USERNAME }} key: ${{ secrets.SSH_PRIVATE_KEY }} port: ${{ secrets.SSH_PORT }} envs: GITHUB_SHA - script: | - echo "[script] docker pull" - sudo docker pull ${{ secrets.DOCKER_REPO_FULLNAME }} - echo "[script] docker stop" - sudo docker stop would-you-in - echo "[script] docker run" - sudo docker run --rm -d -p 80:8080 --env-file ~/.env --name would-you-in ${{ secrets.DOCKER_REPO_FULLNAME }} - echo "[script] docker image clean-up" - sudo docker image prune -f + script: sudo docker image prune -f \ No newline at end of file From 8f469352e0012e8262b7e8785d77acc03f2d2ca2 Mon Sep 17 00:00:00 2001 From: lja3723 Date: Thu, 7 Nov 2024 21:30:37 +0900 Subject: [PATCH 2/5] =?UTF-8?q?chore:=20Dockerfile=20=EB=AA=BB=EC=B0=BE?= =?UTF-8?q?=EB=8A=94=20=EB=AC=B8=EC=A0=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/gradle.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index 2eecaccb..0f816042 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -48,6 +48,8 @@ jobs: runs-on: ubuntu-22.04 needs: Build-Project steps: + - uses: actions/checkout@v4 + - name: Download Build Artifacts uses: actions/download-artifact@v3 with: From 6b87fb03070805b8ba392f82931be5e0a8d46dd8 Mon Sep 17 00:00:00 2001 From: lja3723 Date: Thu, 7 Nov 2024 21:35:04 +0900 Subject: [PATCH 3/5] =?UTF-8?q?chore:=20docker=20build=20=EC=98=A4?= =?UTF-8?q?=EB=A5=98=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/gradle.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index 0f816042..a0aec4a3 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -62,7 +62,7 @@ jobs: password: ${{ secrets.DOCKER_TOKEN }} - name: Docker Build - run: docker build -f Dockerfile -t ${{ secrets.DOCKER_REPO_FULLNAME }} . + run: docker build -f Dockerfile --build-arg DEPENDENCY=build/dependency -t ${{ secrets.DOCKER_REPO_FULLNAME }} . - name: Docker Push run: docker push ${{ secrets.DOCKER_REPO_FULLNAME }} From 7ce4f3c7721e0a620d8a8b313f71d768c9c0eeca Mon Sep 17 00:00:00 2001 From: lja3723 Date: Thu, 7 Nov 2024 21:58:34 +0900 Subject: [PATCH 4/5] =?UTF-8?q?chore:=20docker=20build=20=EC=98=A4?= =?UTF-8?q?=EB=A5=98=20=EC=88=98=EC=A0=95=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/gradle.yml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index a0aec4a3..0d11e4d7 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -55,6 +55,13 @@ jobs: with: name: build-output + + # 다운로드된 .jar 파일을 빌드 컨텍스트로 복사 + - name: Move Build Artifacts to Docker Build Context + run: | + mkdir -p build/libs + mv $GITHUB_WORKSPACE/build-output/*.jar build/libs/ + - name: Docker Hub Login uses: docker/login-action@v3 with: From 3fee935390d8fb3e7c8452ba20bda1abbff7e54b Mon Sep 17 00:00:00 2001 From: lja3723 Date: Thu, 7 Nov 2024 22:08:06 +0900 Subject: [PATCH 5/5] =?UTF-8?q?chore:=20docker=20build=20=EC=98=A4?= =?UTF-8?q?=EB=A5=98=20=EC=88=98=EC=A0=95=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/gradle.yml | 33 ++++----------------------------- 1 file changed, 4 insertions(+), 29 deletions(-) diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index 0d11e4d7..5093154d 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -11,7 +11,7 @@ permissions: jobs: ## 1단계: 프로젝트 빌드 - Build-Project: + Build: runs-on: ubuntu-22.04 steps: - uses: actions/checkout@v4 @@ -37,31 +37,6 @@ jobs: chmod +x gradlew ./gradlew clean build -x test - - name: Upload Build Artifacts - uses: actions/upload-artifact@v3 - with: - name: build-output - path: build/libs/*.jar - - ## 2단계: 도커 이미지 생성 및 푸시 - Build-Docker-Image-and-Push-to-DockerHub: - runs-on: ubuntu-22.04 - needs: Build-Project - steps: - - uses: actions/checkout@v4 - - - name: Download Build Artifacts - uses: actions/download-artifact@v3 - with: - name: build-output - - - # 다운로드된 .jar 파일을 빌드 컨텍스트로 복사 - - name: Move Build Artifacts to Docker Build Context - run: | - mkdir -p build/libs - mv $GITHUB_WORKSPACE/build-output/*.jar build/libs/ - - name: Docker Hub Login uses: docker/login-action@v3 with: @@ -74,10 +49,10 @@ jobs: - name: Docker Push run: docker push ${{ secrets.DOCKER_REPO_FULLNAME }} - ## 3단계: 서버에 배포 - Deploy-Docker-Image-to-Server-and-Run: + ## 2단계: 서버에 배포 + Deploy: runs-on: ubuntu-22.04 - needs: Build-Docker-Image-and-Push-to-DockerHub + needs: Build steps: - name: Pull New Docker Image uses: appleboy/ssh-action@master