diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index 3814fe5..948f92e 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -54,3 +54,25 @@ jobs: # 6. Push Docker image to DockerHub - name: Docker Hub push run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/chargecontrol_backend + +# 위 과정에서 푸시한 이미지를 ec2에서 풀받아서 실행시키는 과정 + run-docker-image-on-ec2: + # build-docker-image (위)과정이 완료되어야 실행됩니다. + needs: build-docker-image + runs-on: self-hosted + + steps: + # 1. 최신 이미지를 풀받습니다 + - name: docker pull + run: sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/chargecontrol_backend + + # 2. 기존의 컨테이너를 중지시킵니다 + - name: docker stop container + run: sudo docker stop $(sudo docker ps -q) 2>/dev/null || true + + # 3. 최신 이미지를 컨테이너화하여 실행시킵니다 + - name: docker run new container + run: sudo docker run --name chargecontrol_backend --rm -d -p 8080:8080 ${{ secrets.DOCKERHUB_USERNAME }}/chargecontrol_backend + # 4. 미사용 이미지를 정리합니다 + - name: delete old docker image + run: sudo docker system prune -f