|
23 | 23 | permissions: |
24 | 24 | contents: read |
25 | 25 | pull-requests: write |
26 | | - deployments: write |
27 | | - statuses: write |
28 | 26 |
|
29 | 27 | steps: |
30 | 28 | - name: Checkout current branch |
@@ -60,23 +58,114 @@ jobs: |
60 | 58 | with: |
61 | 59 | path: './lectures/_build/html' |
62 | 60 |
|
| 61 | + - name: Install Netlify CLI |
| 62 | + run: | |
| 63 | + sudo apt-get update |
| 64 | + sudo apt-get install -y nodejs npm |
| 65 | + sudo npm install -g netlify-cli |
| 66 | +
|
63 | 67 | - name: Deploy Preview to Netlify |
64 | | - uses: nwtgck/actions-netlify@v3 |
65 | | - with: |
66 | | - publish-dir: './lectures/_build/html' |
67 | | - production-branch: main |
68 | | - production-deploy: false |
69 | | - github-token: ${{ secrets.GITHUB_TOKEN }} |
70 | | - deploy-message: | |
71 | | - Deploy from PR #${{ github.event.number }} |
72 | | - Branch: ${{ github.head_ref }} |
73 | | - Commit: ${{ github.sha }} |
74 | | - alias: pr-${{ github.event.number }} |
75 | | - enable-pull-request-comment: true |
76 | | - enable-commit-comment: false |
77 | | - enable-commit-status: true |
78 | | - overwrites-pull-request-comment: true |
| 68 | + id: netlify-deploy |
| 69 | + run: | |
| 70 | + if [ "${{ github.event_name }}" = "pull_request" ]; then |
| 71 | + # Deploy to Netlify and capture the response |
| 72 | + deploy_message="Preview Deploy from GitHub Actions PR #${{ github.event.pull_request.number }} (commit: ${{ github.event.pull_request.head.sha }})" |
| 73 | + |
| 74 | + netlify_output=$(netlify deploy \ |
| 75 | + --dir lectures/_build/html/ \ |
| 76 | + --site ${{ secrets.NETLIFY_SITE_ID }} \ |
| 77 | + --auth ${{ secrets.NETLIFY_AUTH_TOKEN }} \ |
| 78 | + --alias pr-${{ github.event.pull_request.number }} \ |
| 79 | + --message "${deploy_message}" \ |
| 80 | + --json) |
| 81 | + |
| 82 | + echo "Netlify deployment output:" |
| 83 | + echo "$netlify_output" |
| 84 | + |
| 85 | + # Extract the actual deploy URL from the JSON response |
| 86 | + deploy_url=$(echo "$netlify_output" | jq -r '.deploy_url') |
| 87 | + |
| 88 | + echo "deploy_url=$deploy_url" >> $GITHUB_OUTPUT |
| 89 | + echo "✅ Deployment completed!" |
| 90 | + echo "🌐 Deploy URL: $deploy_url" |
| 91 | + else |
| 92 | + # Handle push to branch |
| 93 | + deploy_message="Deploy from GitHub Actions (commit: ${{ github.sha }})" |
| 94 | + |
| 95 | + netlify_output=$(netlify deploy \ |
| 96 | + --dir lectures/_build/html/ \ |
| 97 | + --site ${{ secrets.NETLIFY_SITE_ID }} \ |
| 98 | + --auth ${{ secrets.NETLIFY_AUTH_TOKEN }} \ |
| 99 | + --alias ${{ github.ref_name }} \ |
| 100 | + --message "${deploy_message}" \ |
| 101 | + --json) |
| 102 | + |
| 103 | + echo "Netlify deployment output:" |
| 104 | + echo "$netlify_output" |
| 105 | + |
| 106 | + # Extract the actual deploy URL from the JSON response |
| 107 | + deploy_url=$(echo "$netlify_output" | jq -r '.deploy_url') |
| 108 | + |
| 109 | + echo "deploy_url=$deploy_url" >> $GITHUB_OUTPUT |
| 110 | + echo "✅ Deployment completed!" |
| 111 | + echo "🌐 Deploy URL: $deploy_url" |
| 112 | + fi |
79 | 113 | env: |
80 | 114 | NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }} |
81 | 115 | NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }} |
| 116 | + |
| 117 | + - name: Post PR Comment with Preview Link |
| 118 | + if: github.event_name == 'pull_request' |
| 119 | + uses: actions/github-script@v7 |
| 120 | + with: |
| 121 | + script: | |
| 122 | + const deployUrl = `${{ steps.netlify-deploy.outputs.deploy_url }}`; |
| 123 | + const prNumber = ${{ github.event.pull_request.number }}; |
| 124 | + const commitSha = `${{ github.event.pull_request.head.sha }}`; |
| 125 | + const shortSha = commitSha.substring(0, 7); |
| 126 | + |
| 127 | + console.log(`Deploy URL: ${deployUrl}`); |
| 128 | + console.log(`PR Number: ${prNumber}`); |
| 129 | + console.log(`Commit SHA: ${shortSha}`); |
| 130 | + |
| 131 | + // Get all comments on this PR to check for duplicates |
| 132 | + const comments = await github.rest.issues.listComments({ |
| 133 | + issue_number: prNumber, |
| 134 | + owner: context.repo.owner, |
| 135 | + repo: context.repo.repo, |
| 136 | + }); |
| 137 | + |
| 138 | + console.log(`Found ${comments.data.length} comments on PR`); |
| 139 | + |
| 140 | + // Look for existing comment with this exact commit SHA and deploy URL |
| 141 | + const duplicateComment = comments.data.find(comment => { |
| 142 | + const hasMarker = comment.body.includes('**📖 Netlify Preview Ready!**'); |
| 143 | + const hasCommitSha = comment.body.includes(`([${shortSha}]`); |
| 144 | + const hasDeployUrl = comment.body.includes(deployUrl); |
| 145 | + |
| 146 | + return hasMarker && hasCommitSha && hasDeployUrl; |
| 147 | + }); |
| 148 | + |
| 149 | + if (duplicateComment) { |
| 150 | + console.log(`Duplicate comment found (${duplicateComment.id}), skipping...`); |
| 151 | + return; |
| 152 | + } |
| 153 | + |
| 154 | + console.log(`No duplicate found, creating new comment for commit ${shortSha}`); |
| 155 | + |
| 156 | + const commitUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/commit/${commitSha}`; |
| 157 | + |
| 158 | + let comment = `**📖 Netlify Preview Ready!**\n\n`; |
| 159 | + comment += `**Preview URL:** ${deployUrl} ([${shortSha}](${commitUrl}))\n\n`; |
| 160 | + comment += `✨ Browse the preview at the URL above.\n`; |
| 161 | + |
| 162 | + // Post the comment |
| 163 | + await github.rest.issues.createComment({ |
| 164 | + issue_number: prNumber, |
| 165 | + owner: context.repo.owner, |
| 166 | + repo: context.repo.repo, |
| 167 | + body: comment |
| 168 | + }); |
| 169 | + |
| 170 | + console.log('Comment posted successfully'); |
82 | 171 | timeout-minutes: 10 |
0 commit comments