|
| 1 | + |
| 2 | +name: Site Deploy (Preview CD) |
| 3 | + |
| 4 | +on: |
| 5 | + workflow_run: |
| 6 | + workflows: ["Site Deploy (Preview CI)"] |
| 7 | + types: |
| 8 | + - completed |
| 9 | + |
| 10 | +jobs: |
| 11 | + preview-cd: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + concurrency: |
| 14 | + group: pull-request-preview-${{ github.event.workflow_run.head_repository.full_name }}-${{ github.event.workflow_run.head_branch }} |
| 15 | + cancel-in-progress: true |
| 16 | + |
| 17 | + if: ${{ github.event.workflow_run.conclusion == 'success' }} |
| 18 | + |
| 19 | + environment: pull request |
| 20 | + |
| 21 | + permissions: |
| 22 | + actions: read |
| 23 | + statuses: write |
| 24 | + pull-requests: write |
| 25 | + |
| 26 | + steps: |
| 27 | + - name: Set Commit Status |
| 28 | + uses: actions/github-script@v8 |
| 29 | + with: |
| 30 | + script: | |
| 31 | + github.rest.repos.createCommitStatus({ |
| 32 | + owner: context.repo.owner, |
| 33 | + repo: context.repo.repo, |
| 34 | + sha: context.payload.workflow_run.head_sha, |
| 35 | + context: 'Website Preview', |
| 36 | + description: 'Deploying...', |
| 37 | + state: 'pending', |
| 38 | + }) |
| 39 | +
|
| 40 | + - name: Download Artifact |
| 41 | + uses: actions/download-artifact@v7 |
| 42 | + with: |
| 43 | + name: website-preview |
| 44 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 45 | + run-id: ${{ github.event.workflow_run.id }} |
| 46 | + |
| 47 | + - name: Restore Context |
| 48 | + run: | |
| 49 | + PR_NUMBER=$(cat ./pr-number) |
| 50 | + if ! [[ "${PR_NUMBER}" =~ ^[0-9]+$ ]]; then |
| 51 | + echo "Invalid PR number: ${PR_NUMBER}" |
| 52 | + exit 1 |
| 53 | + fi |
| 54 | + echo "PR_NUMBER=${PR_NUMBER}" >> "${GITHUB_ENV}" |
| 55 | +
|
| 56 | + - name: Set Deploy Name |
| 57 | + run: | |
| 58 | + echo "DEPLOY_NAME=deploy-preview-${PR_NUMBER}" >> "${GITHUB_ENV}" |
| 59 | +
|
| 60 | + - name: Deploy to Netlify |
| 61 | + id: deploy |
| 62 | + uses: nwtgck/actions-netlify@v3 |
| 63 | + with: |
| 64 | + publish-dir: ./website/build |
| 65 | + production-deploy: false |
| 66 | + deploy-message: "Deploy ${{ env.DEPLOY_NAME }}@${{ github.event.workflow_run.head_sha }}" |
| 67 | + alias: ${{ env.DEPLOY_NAME }} |
| 68 | + env: |
| 69 | + NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }} |
| 70 | + NETLIFY_SITE_ID: ${{ secrets.SITE_ID }} |
| 71 | + |
| 72 | + # action netlify has no pull request context, so we need to comment by ourselves |
| 73 | + - name: Comment on Pull Request |
| 74 | + uses: marocchino/sticky-pull-request-comment@v2 |
| 75 | + with: |
| 76 | + header: website |
| 77 | + number: ${{ env.PR_NUMBER }} |
| 78 | + message: | |
| 79 | + :rocket: Deployed to ${{ steps.deploy.outputs.deploy-url }} |
| 80 | +
|
| 81 | + - name: Set Commit Status |
| 82 | + uses: actions/github-script@v8 |
| 83 | + if: always() |
| 84 | + with: |
| 85 | + script: | |
| 86 | + if (`${{ job.status }}` === 'success') { |
| 87 | + github.rest.repos.createCommitStatus({ |
| 88 | + owner: context.repo.owner, |
| 89 | + repo: context.repo.repo, |
| 90 | + sha: context.payload.workflow_run.head_sha, |
| 91 | + context: 'Website Preview', |
| 92 | + description: `Deployed to ${{ steps.deploy.outputs.deploy-url }}`, |
| 93 | + state: 'success', |
| 94 | + target_url: `${{ steps.deploy.outputs.deploy-url }}`, |
| 95 | + }) |
| 96 | + } else { |
| 97 | + github.rest.repos.createCommitStatus({ |
| 98 | + owner: context.repo.owner, |
| 99 | + repo: context.repo.repo, |
| 100 | + sha: context.payload.workflow_run.head_sha, |
| 101 | + context: 'Website Preview', |
| 102 | + description: `Deploy ${{ job.status }}`, |
| 103 | + state: 'failure', |
| 104 | + }) |
| 105 | + } |
0 commit comments