2525  build-and-deploy :
2626    runs-on : ubuntu-latest 
2727    permissions :
28-       contents : write 
28+       contents : read 
2929      pull-requests : write 
3030      pages : write 
3131      id-token : write 
@@ -34,13 +34,13 @@ jobs:
3434    - name : Checkout current branch 
3535      uses : actions/checkout@v4 
3636      with :
37-         ref : ${{ github.head_ref || github.ref  }} 
37+         ref : ${{ github.head_ref }} 
3838        token : ${{ env.GH_TOKEN }} 
3939        submodules : ' true' 
4040
4141    - name : Display branch information 
4242      run : | 
43-         echo "Running on branch: ${{ github.head_ref || github.ref_name  }}" 
43+         echo "Running on branch: ${{ github.head_ref }}" 
4444        echo "Target branch: ${{ github.base_ref }}" 
4545        echo "PR number: ${{ github.event.number }}" 
4646        echo "Commit SHA: ${{ github.sha }}" 
@@ -63,17 +63,12 @@ jobs:
6363      with :
6464        path : ' ./lectures/_build/html' 
6565
66-     #  Deploy to GitHub Pages on main branch push 
66+     #  Deploy to GitHub Pages when pushing to main 
6767    - name : Deploy to GitHub Pages 
6868      if : github.event_name == 'push' && github.ref == 'refs/heads/main' 
69-       uses : peaceiris/actions-gh-pages@v3 
70-       with :
71-         github_token : ${{ secrets.GITHUB_TOKEN }} 
72-         publish_dir : ./lectures/_build/html 
73-         publish_branch : gh-pages 
74-         force_orphan : true 
69+       id : deployment 
70+       uses : actions/deploy-pages@v4 
7571
76-     #  Netlify preview for PRs (optional - keep if you want PR previews)
7772    - name : Install Netlify CLI 
7873      if : github.event_name == 'pull_request' 
7974      run : | 
8479      if : github.event_name == 'pull_request' 
8580      id : netlify-deploy 
8681      run : | 
82+         # Deploy to Netlify and capture the response 
8783        deploy_message="Preview Deploy from GitHub Actions PR #${{ github.event.pull_request.number }} (commit: ${{ github.event.pull_request.head.sha }})" 
8884         
8985        netlify_output=$(netlify deploy \ 
@@ -98,12 +94,14 @@ jobs:
9894        echo "Netlify deployment output:" 
9995        echo "$netlify_output" 
10096         
97+         # Extract the actual deploy URL from the JSON response 
10198        deploy_url=$(echo "$netlify_output" | jq -r '.deploy_url') 
10299         
103100        echo "deploy_url=$deploy_url" >> $GITHUB_OUTPUT 
104101        echo "✅ Deployment completed!" 
105102        echo "🌐 Deploy URL: $deploy_url" 
106103         
104+         # Display manual preview page if specified 
107105        if [ ! -z "${{ github.event.inputs.preview_page }}" ]; then 
108106          echo "" 
109107          echo "🎯 Manual preview page: ${deploy_url}/${{ github.event.inputs.preview_page }}" 
@@ -123,39 +121,55 @@ jobs:
123121          const commitSha = `${{ github.event.pull_request.head.sha }}`; 
124122          const shortSha = commitSha.substring(0, 7); 
125123           
124+           console.log(`Deploy URL: ${deployUrl}`); 
125+           console.log(`Manual preview page: ${manualPage}`); 
126+           console.log(`PR Number: ${prNumber}`); 
127+           console.log(`Commit SHA: ${shortSha}`); 
128+            
129+           // Get all comments on this PR to check for duplicates 
126130          const comments = await github.rest.issues.listComments({ 
127131            issue_number: prNumber, 
128132            owner: context.repo.owner, 
129133            repo: context.repo.repo, 
130134          }); 
131135           
136+           console.log(`Found ${comments.data.length} comments on PR`); 
137+            
138+           // Look for existing comment with this exact commit SHA and deploy URL 
132139          const duplicateComment = comments.data.find(comment => { 
133140            const hasMarker = comment.body.includes('**📖 Netlify Preview Ready!**'); 
134141            const hasCommitSha = comment.body.includes(`([${shortSha}]`); 
135142            const hasDeployUrl = comment.body.includes(deployUrl); 
143+              
136144            return hasMarker && hasCommitSha && hasDeployUrl; 
137145          }); 
138146           
139147          if (duplicateComment) { 
140-             console.log(`Duplicate comment found, skipping...`); 
148+             console.log(`Duplicate comment found (${duplicateComment.id}) , skipping...`); 
141149            return; 
142150          } 
143151           
152+           console.log(`No duplicate found, creating new comment for commit ${shortSha}`); 
153+            
144154          const commitUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/commit/${commitSha}`; 
145155           
146156          let comment = `**📖 Netlify Preview Ready!**\n\n`; 
147157          comment += `**Preview URL:** ${deployUrl} ([${shortSha}](${commitUrl}))\n\n`; 
148158           
159+           // Add manual preview page if specified 
149160          if (manualPage) { 
150161            comment += `🎯 **Manual Preview:** [${manualPage}](${deployUrl}/${manualPage})\n\n`; 
151162          } 
152163           
153164          comment += `✨ Browse the preview at the URL above.\n`; 
154165           
166+           // Post the comment 
155167          await github.rest.issues.createComment({ 
156168            issue_number: prNumber, 
157169            owner: context.repo.owner, 
158170            repo: context.repo.repo, 
159171            body: comment 
160172          }); 
161- timeout-minutes : 10 
173+            
174+           console.log('Comment posted successfully'); 
175+ timeout-minutes : 10 
0 commit comments