feat: 도커 설정 및 pr preview action 추가 #3
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: Deploy PR Preview | |
| on: | |
| pull_request: | |
| types: [opened, synchronize, reopened] | |
| env: | |
| REGISTRY: ghcr.io | |
| IMAGE_NAME: ${{ github.repository }} | |
| jobs: | |
| deploy-preview: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| packages: write | |
| pull-requests: write | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Log in to GitHub Container Registry | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ${{ env.REGISTRY }} | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Extract metadata | |
| id: meta | |
| uses: docker/metadata-action@v5 | |
| with: | |
| images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} | |
| tags: | | |
| type=ref,event=pr | |
| - name: Build and push Docker image | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: . | |
| push: true | |
| tags: ${{ steps.meta.outputs.tags }} | |
| labels: ${{ steps.meta.outputs.labels }} | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| - name: Deploy preview via webhook | |
| run: | | |
| PR_NUMBER=${{ github.event.pull_request.number }} | |
| echo "Calling webhook to deploy PR #${PR_NUMBER}..." | |
| RESPONSE=$(curl -X POST http://dreampaste.com:9000/webhook/pr-preview \ | |
| -H "Content-Type: application/json" \ | |
| -d "{ | |
| \"secret\": \"${{ secrets.WEBHOOK_SECRET }}\", | |
| \"action\": \"deploy\", | |
| \"pr_number\": \"${PR_NUMBER}\", | |
| \"image_tag\": \"pr-${PR_NUMBER}\" | |
| }" \ | |
| -w "\n%{http_code}" \ | |
| -s) | |
| HTTP_CODE=$(echo "$RESPONSE" | tail -n1) | |
| BODY=$(echo "$RESPONSE" | head -n-1) | |
| echo "Response: $BODY" | |
| if [ "$HTTP_CODE" -eq 200 ]; then | |
| echo "✅ PR Preview deployed successfully" | |
| else | |
| echo "❌ Webhook call failed with status $HTTP_CODE" | |
| exit 1 | |
| fi | |
| - name: Comment PR with preview URL | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const prNumber = context.payload.pull_request.number; | |
| const previewUrl = "https://pr-" + prNumber + ".poodle-kit.my"; | |
| const commitHash = context.sha.substring(0, 7); | |
| const body = "## 🐩 Poodle Docs Preview Deployment\n\n" + | |
| "Your preview deployment is ready!\n\n" + | |
| "🔗 **Preview URL**: " + previewUrl + "\n\n" + | |
| "This preview will be automatically updated when you push new commits.\n\n" + | |
| "---\n" + | |
| "_Deployed from commit " + commitHash + "_"; | |
| // 기존 코멘트 찾기 | |
| const { data: comments } = await github.rest.issues.listComments({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: prNumber, | |
| }); | |
| const botComment = comments.find(comment => | |
| comment.user.type === 'Bot' && | |
| comment.body.includes('Poodle Docs Preview Deployment') | |
| ); | |
| if (botComment) { | |
| // 기존 코멘트 업데이트 | |
| await github.rest.issues.updateComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| comment_id: botComment.id, | |
| body: body | |
| }); | |
| } else { | |
| // 새 코멘트 생성 | |
| await github.rest.issues.createComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: prNumber, | |
| body: body | |
| }); | |
| } |