|
| 1 | +name: CD with Docker |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_run: |
| 5 | + workflows: [ "Java CI with Gradle" ] |
| 6 | + types: [ completed ] |
| 7 | + branches: [ "dev" ] |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: read |
| 11 | + packages: write |
| 12 | + |
| 13 | +jobs: |
| 14 | + build: |
| 15 | + if: ${{ github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.head_branch == 'dev' }} |
| 16 | + runs-on: ubuntu-latest |
| 17 | + outputs: |
| 18 | + image-tag: ${{ steps.meta.outputs.tags }} |
| 19 | + |
| 20 | + steps: |
| 21 | + # 도커 이미지 빌드를 위한 빌드 파일 내려 받기 |
| 22 | + - name: Download artifact |
| 23 | + uses: actions/download-artifact@v4 |
| 24 | + with: |
| 25 | + run-id: ${{ github.event.workflow_run.id }} |
| 26 | + github-token: ${{ secrets.ACTION_TOKEN }} |
| 27 | + name: build-libs |
| 28 | + |
| 29 | + # 도커 이미지 빌드를 위한 Docker Buildx 내려 받기 |
| 30 | + - name: Setup Docker Buildx |
| 31 | + uses: docker/setup-buildx-action@v3 |
| 32 | + with: |
| 33 | + driver: docker-container |
| 34 | + driver-opts: | |
| 35 | + network=host |
| 36 | +
|
| 37 | + # 도커 이미지 업로드를 위한 GitHub Container Registry 로그인 |
| 38 | + # https://docs.github.com/ko/packages/working-with-a-github-packages-registry/working-with-the-container-registry |
| 39 | + - name: Login to Github Container Registry |
| 40 | + uses: docker/login-action@v3 |
| 41 | + with: |
| 42 | + registry: ghcr.io |
| 43 | + # 개인 사용자 토큰의 소유자가 변경되면 이 설정 파일을 다시 커밋하여 |
| 44 | + # GitHub Actions 최초 실행 사용자를 변경해야 합니다. |
| 45 | + username: ${{ github.actor }} |
| 46 | + password: ${{ secrets.ACTION_TOKEN }} |
| 47 | + |
| 48 | + # 메타 데이터를 추출해 이미지 태그 및 레이블 생성 |
| 49 | + - name: Extract metadata |
| 50 | + id: meta |
| 51 | + uses: docker/metadata-action@v5 |
| 52 | + with: |
| 53 | + images: ghcr.io/${{ github.repository }} |
| 54 | + tags: | |
| 55 | + type=sha |
| 56 | +
|
| 57 | + - name: Build and Push Docker image |
| 58 | + uses: docker/build-push-action@v6 |
| 59 | + with: |
| 60 | + context: . |
| 61 | + file: Dockerfile |
| 62 | + push: true |
| 63 | + tags: ${{ steps.meta.outputs.tags }} |
| 64 | + labels: ${{ steps.meta.outputs.labels }} |
| 65 | + platforms: linux/amd64,linux/arm64 |
| 66 | + cache-from: type=gha |
| 67 | + cache-to: type=gha,mode=max |
| 68 | + |
| 69 | + deploy: |
| 70 | + needs: build |
| 71 | + runs-on: [self-hosted] |
| 72 | + steps: |
| 73 | + # Docker 이미지 내려 받기를 위한 GHCR 로그인 |
| 74 | + - name: Login to GHCR |
| 75 | + uses: docker/login-action@v3 |
| 76 | + with: |
| 77 | + registry: ghcr.io |
| 78 | + username: ${{ github.actor }} |
| 79 | + password: ${{ secrets.ACTION_TOKEN }} |
| 80 | + |
| 81 | + # 이미지 내려 받기 |
| 82 | + - name: Pull latest image |
| 83 | + run: docker pull ${{ needs.build.outputs.image-tag }} |
| 84 | + |
| 85 | + # 실행 중인 컨테이너 정리 및 기존 이미지 삭제 |
| 86 | + - name: Clean up old container and image |
| 87 | + run: | |
| 88 | + docker rm -f ${{ vars.CONTAINER_NAME }} || true |
| 89 | + docker images "ghcr.io/${{ github.repository }}" \ |
| 90 | + --format "{{.Repository}}:{{.Tag}}" \ |
| 91 | + | grep -v "${{ needs.build.outputs.image-tag }}" \ |
| 92 | + | xargs -r docker rmi -f || true |
| 93 | +
|
| 94 | + # 새로운 이미지로 새로운 컨테이너 생성하여 실행 |
| 95 | + - name: Run New Container |
| 96 | + run: | |
| 97 | + docker run -d \ |
| 98 | + --name ${{ vars.CONTAINER_NAME }} \ |
| 99 | + --network ${{ vars.NETWORK_NAME }} \ |
| 100 | + -p ${{ secrets.DEV_WEB_PORT }}:8080 \ |
| 101 | + -e USE_PROFILE=dev \ |
| 102 | + -e TZ=Asia/Seoul \ |
| 103 | + ${{ needs.build.outputs.image-tag }} |
0 commit comments