Prod - CD with Docker #23
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Prod - CD with Docker | |
| on: | |
| workflow_run: | |
| workflows: [ "Prod - Java CI with Gradle" ] # Prod CI 파일 이름과 일치해야 함 | |
| types: [ completed ] | |
| permissions: | |
| contents: read | |
| packages: write | |
| jobs: | |
| build: | |
| # Prod 브랜치인 경우에만 실행 | |
| if: ${{ github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.head_branch == 'main' }} | |
| runs-on: ubuntu-latest | |
| outputs: | |
| image-tag: ${{ steps.meta.outputs.tags }} | |
| steps: | |
| - name: Download artifact | |
| uses: actions/download-artifact@v4 | |
| with: | |
| run-id: ${{ github.event.workflow_run.id }} | |
| github-token: ${{ secrets.ACTION_TOKEN }} | |
| name: build-libs | |
| - name: Setup Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| with: | |
| driver: docker-container | |
| driver-opts: | | |
| network=host | |
| - name: Login to Github Container Registry | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.ACTION_TOKEN }} | |
| - name: Extract metadata | |
| id: meta | |
| uses: docker/metadata-action@v5 | |
| with: | |
| images: ghcr.io/${{ github.repository }} | |
| tags: | | |
| type=sha | |
| - name: Build and Push Docker image | |
| uses: docker/build-push-action@v6 | |
| with: | |
| context: . | |
| file: Dockerfile | |
| push: true | |
| tags: ${{ steps.meta.outputs.tags }} | |
| labels: ${{ steps.meta.outputs.labels }} | |
| platforms: linux/amd64,linux/arm64 | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| deploy: | |
| needs: build | |
| runs-on: [self-hosted] | |
| # [Prod] GitHub Environment 사용 | |
| environment: | |
| name: production | |
| steps: | |
| - name: Login to GHCR | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.ACTION_TOKEN }} | |
| - name: Pull latest image | |
| run: docker pull ${{ needs.build.outputs.image-tag }} | |
| - name: Clean up old container and image | |
| run: | | |
| docker rm -f ${{ secrets.CONTAINER_NAME }} || true | |
| docker image prune -f | |
| - name: Run New Container (Prod) | |
| run: | | |
| docker run -d \ | |
| --name ${{ secrets.CONTAINER_NAME }} \ | |
| --network ${{ secrets.NETWORK_NAME }} \ | |
| -p ${{ secrets.WEB_PORT }}:8080 \ | |
| -e SPRING_PROFILES_ACTIVE=prod \ | |
| -e TZ=Asia/Seoul \ | |
| ${{ needs.build.outputs.image-tag }} |