|
| 1 | +on: |
| 2 | + pull_request: |
| 3 | + branches: [main] |
| 4 | + types: [opened, synchronize, closed] |
| 5 | + |
| 6 | +jobs: |
| 7 | + deploy-preview: |
| 8 | + runs-on: ubuntu-latest |
| 9 | + permissions: |
| 10 | + contents: read |
| 11 | + pages: write |
| 12 | + id-token: write |
| 13 | + pull-requests: write |
| 14 | + |
| 15 | + steps: |
| 16 | + - uses: actions/checkout@v4 |
| 17 | + with: |
| 18 | + fetch-depth: 0 |
| 19 | + |
| 20 | + - name: Setup micromamba |
| 21 | + if: github.event.action != 'closed' |
| 22 | + uses: mamba-org/setup-micromamba@v2 |
| 23 | + with: |
| 24 | + environment-file: environment.yml |
| 25 | + init-shell: bash |
| 26 | + cache-environment: true |
| 27 | + |
| 28 | + - name: Setup Quarto |
| 29 | + if: github.event.action != 'closed' |
| 30 | + uses: quarto-dev/quarto-actions/setup@v2 |
| 31 | + |
| 32 | + - name: Get commit info |
| 33 | + id: commit |
| 34 | + run: | |
| 35 | + echo "short_sha=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT |
| 36 | + echo "full_sha=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT |
| 37 | + echo "commit_message=$(git log -1 --pretty=format:'%s')" >> $GITHUB_OUTPUT |
| 38 | + |
| 39 | + - name: Render Quarto Project |
| 40 | + if: github.event.action != 'closed' |
| 41 | + shell: micromamba-shell {0} |
| 42 | + run: quarto render |
| 43 | + |
| 44 | + - name: Deploy to GitHub Pages |
| 45 | + if: github.event.action != 'closed' |
| 46 | + uses: peaceiris/actions-gh-pages@v3 |
| 47 | + with: |
| 48 | + github_token: ${{ secrets.GITHUB_TOKEN }} |
| 49 | + publish_dir: ./_site |
| 50 | + destination_dir: commit-${{ steps.commit.outputs.short_sha }} |
| 51 | + keep_files: true |
| 52 | + |
| 53 | + - name: Find existing preview comment |
| 54 | + if: github.event.action != 'closed' |
| 55 | + uses: actions/github-script@v6 |
| 56 | + id: find-comment |
| 57 | + with: |
| 58 | + script: | |
| 59 | + const comments = await github.rest.issues.listComments({ |
| 60 | + issue_number: context.issue.number, |
| 61 | + owner: context.repo.owner, |
| 62 | + repo: context.repo.repo, |
| 63 | + }); |
| 64 | + |
| 65 | + const previewComment = comments.data.find(comment => |
| 66 | + comment.user.type === 'Bot' && |
| 67 | + comment.body.includes('<!-- quarto-preview-comment -->') |
| 68 | + ); |
| 69 | + |
| 70 | + return previewComment ? previewComment.id : null; |
| 71 | + |
| 72 | + - name: Create or update preview comment |
| 73 | + if: github.event.action != 'closed' |
| 74 | + uses: actions/github-script@v6 |
| 75 | + with: |
| 76 | + script: | |
| 77 | + const shortSha = '${{ steps.commit.outputs.short_sha }}'; |
| 78 | + const fullSha = '${{ steps.commit.outputs.full_sha }}'; |
| 79 | + const commitMessage = '${{ steps.commit.outputs.commit_message }}'; |
| 80 | + const previewUrl = `https://${context.repo.owner}.github.io/${context.repo.repo}/commit-${shortSha}/`; |
| 81 | + const commitUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/commit/${fullSha}`; |
| 82 | + const runUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`; |
| 83 | + |
| 84 | + const body = `<!-- quarto-preview-comment --> |
| 85 | + ## 📖 Quarto Preview |
| 86 | + |
| 87 | + | | | |
| 88 | + |---|---| |
| 89 | + | **Latest commit** | [\`${shortSha}\`](${commitUrl}) | |
| 90 | + | **Commit message** | ${commitMessage} | |
| 91 | + | **Preview URL** | ${previewUrl} | |
| 92 | + | **Build log** | [View workflow run](${runUrl}) | |
| 93 | + |
| 94 | + > The preview will be updated automatically when you push new commits to this PR.`; |
| 95 | + |
| 96 | + const commentId = ${{ steps.find-comment.outputs.result }}; |
| 97 | + |
| 98 | + if (commentId) { |
| 99 | + await github.rest.issues.updateComment({ |
| 100 | + comment_id: commentId, |
| 101 | + owner: context.repo.owner, |
| 102 | + repo: context.repo.repo, |
| 103 | + body: body |
| 104 | + }); |
| 105 | + } else { |
| 106 | + await github.rest.issues.createComment({ |
| 107 | + issue_number: context.issue.number, |
| 108 | + owner: context.repo.owner, |
| 109 | + repo: context.repo.repo, |
| 110 | + body: body |
| 111 | + }); |
| 112 | + } |
| 113 | + |
| 114 | + - name: Clean up preview comment on PR close |
| 115 | + if: github.event.action == 'closed' |
| 116 | + uses: actions/github-script@v6 |
| 117 | + with: |
| 118 | + script: | |
| 119 | + const comments = await github.rest.issues.listComments({ |
| 120 | + issue_number: context.issue.number, |
| 121 | + owner: context.repo.owner, |
| 122 | + repo: context.repo.repo, |
| 123 | + }); |
| 124 | + |
| 125 | + const previewComment = comments.data.find(comment => |
| 126 | + comment.user.type === 'Bot' && |
| 127 | + comment.body.includes('<!-- quarto-preview-comment -->') |
| 128 | + ); |
| 129 | + |
| 130 | + if (previewComment) { |
| 131 | + await github.rest.issues.updateComment({ |
| 132 | + comment_id: previewComment.id, |
| 133 | + owner: context.repo.owner, |
| 134 | + repo: context.repo.repo, |
| 135 | + body: `<!-- quarto-preview-comment --> |
| 136 | + ## 📖 Quarto Preview |
| 137 | + |
| 138 | + ~~Preview was removed when PR was closed.~~` |
| 139 | + }); |
| 140 | + } |
0 commit comments