From ded5bdff891c86067c409203052d63ddccbca780 Mon Sep 17 00:00:00 2001 From: Jangan Lee Date: Thu, 7 Nov 2024 22:31:16 +0900 Subject: [PATCH] =?UTF-8?q?[Weekly/10/Chore/Deploy]=20CI/CD=20=EC=8A=A4?= =?UTF-8?q?=ED=81=AC=EB=A6=BD=ED=8A=B8=20=EA=B7=B8=EB=A3=B9=ED=99=94=20(#7?= =?UTF-8?q?8)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * chore: github action jobs 그룹화 * chore: Dockerfile 못찾는 문제 수정 * chore: docker build 오류 수정 * chore: docker build 오류 수정 2 * chore: docker build 오류 수정 3 --- .github/workflows/gradle.yml | 63 +++++++++++++++++++++++++----------- 1 file changed, 45 insertions(+), 18 deletions(-) diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index ccff7b05..5093154d 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -10,9 +10,9 @@ permissions: contents: read jobs: - CI-CD: + ## 1단계: 프로젝트 빌드 + Build: runs-on: ubuntu-22.04 - steps: - uses: actions/checkout@v4 @@ -32,7 +32,6 @@ jobs: restore-keys: | ${{ runner.os }}-gradle- - # 테스트까지 포함해 빌드하려면 -x test를 제거하기 - name: Build With Gradle (exclude test) run: | chmod +x gradlew @@ -44,25 +43,53 @@ jobs: 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 --build-arg DEPENDENCY=build/dependency -t ${{ secrets.DOCKER_REPO_FULLNAME }} . + + - name: Docker Push + run: docker push ${{ secrets.DOCKER_REPO_FULLNAME }} + + ## 2단계: 서버에 배포 + Deploy: + runs-on: ubuntu-22.04 + needs: Build + 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