1- name : Build and Netlify  Deploy 
1+ name : Build and Deploy to GitHub Pages  
22
33on :
44  pull_request :
@@ -25,25 +25,26 @@ jobs:
2525  build-and-deploy :
2626    runs-on : ubuntu-latest 
2727    permissions :
28-       contents : read 
28+       contents : write 
2929      pull-requests : write 
30+       pages : write 
31+       id-token : write 
3032
3133    steps :
3234    - name : Checkout current branch 
3335      uses : actions/checkout@v4 
3436      with :
35-         ref : ${{ github.head_ref }} 
37+         ref : ${{ github.head_ref || github.ref  }} 
3638        token : ${{ env.GH_TOKEN }} 
3739        submodules : ' true' 
3840
3941    - name : Display branch information 
4042      run : | 
41-         echo "Running on branch: ${{ github.head_ref }}" 
43+         echo "Running on branch: ${{ github.head_ref || github.ref_name  }}" 
4244        echo "Target branch: ${{ github.base_ref }}" 
4345        echo "PR number: ${{ github.event.number }}" 
4446        echo "Commit SHA: ${{ github.sha }}" 
4547
46- 
4748uses : actions/setup-node@v4 
4849      with :
4950        node-version : 18.x 
@@ -62,64 +63,50 @@ jobs:
6263      with :
6364        path : ' ./lectures/_build/html' 
6465
66+     #  Deploy to GitHub Pages on main branch push
67+     - name : Deploy to GitHub Pages 
68+       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 
75+ 
76+     #  Netlify preview for PRs (optional - keep if you want PR previews)
6577    - name : Install Netlify CLI 
78+       if : github.event_name == 'pull_request' 
6679      run : | 
6780        npm install -g netlify-cli 
6881        mkdir -p ~/.config/netlify 
6982
7083name : Deploy Preview to Netlify 
84+       if : github.event_name == 'pull_request' 
7185      id : netlify-deploy 
7286      run : | 
73-         if [ "${{ github.event_name }}" = "pull_request" ]; then 
74-           # Deploy to Netlify and capture the response 
75-           deploy_message="Preview Deploy from GitHub Actions PR #${{ github.event.pull_request.number }} (commit: ${{ github.event.pull_request.head.sha }})" 
76-            
77-           netlify_output=$(netlify deploy \ 
78-             --dir lectures/_build/html/ \ 
79-             --site ${{ secrets.NETLIFY_SITE_ID }} \ 
80-             --auth ${{ secrets.NETLIFY_AUTH_TOKEN }} \ 
81-             --context pr-preview \ 
82-             --alias pr-${{ github.event.pull_request.number }} \ 
83-             --message "${deploy_message}" \ 
84-             --json) 
85-            
86-           echo "Netlify deployment output:" 
87-           echo "$netlify_output" 
88-            
89-           # Extract the actual deploy URL from the JSON response 
90-           deploy_url=$(echo "$netlify_output" | jq -r '.deploy_url') 
91-            
92-           echo "deploy_url=$deploy_url" >> $GITHUB_OUTPUT 
93-           echo "✅ Deployment completed!" 
94-           echo "🌐 Deploy URL: $deploy_url" 
95-            
96-           # Display manual preview page if specified 
97-           if [ ! -z "${{ github.event.inputs.preview_page }}" ]; then 
98-             echo "" 
99-             echo "🎯 Manual preview page: ${deploy_url}/${{ github.event.inputs.preview_page }}" 
100-           fi 
101-         else 
102-           # Handle push to branch 
103-           deploy_message="Deploy from GitHub Actions (commit: ${{ github.sha }})" 
104-            
105-           netlify_output=$(netlify deploy \ 
106-             --dir lectures/_build/html/ \ 
107-             --site ${{ secrets.NETLIFY_SITE_ID }} \ 
108-             --auth ${{ secrets.NETLIFY_AUTH_TOKEN }} \ 
109-             --context dev \ 
110-             --alias ${{ github.ref_name }} \ 
111-             --message "${deploy_message}" \ 
112-             --json) 
113-            
114-           echo "Netlify deployment output:" 
115-           echo "$netlify_output" 
116-            
117-           # Extract the actual deploy URL from the JSON response 
118-           deploy_url=$(echo "$netlify_output" | jq -r '.deploy_url') 
119-            
120-           echo "deploy_url=$deploy_url" >> $GITHUB_OUTPUT 
121-           echo "✅ Deployment completed!" 
122-           echo "🌐 Deploy URL: $deploy_url" 
87+         deploy_message="Preview Deploy from GitHub Actions PR #${{ github.event.pull_request.number }} (commit: ${{ github.event.pull_request.head.sha }})" 
88+          
89+         netlify_output=$(netlify deploy \ 
90+           --dir lectures/_build/html/ \ 
91+           --site ${{ secrets.NETLIFY_SITE_ID }} \ 
92+           --auth ${{ secrets.NETLIFY_AUTH_TOKEN }} \ 
93+           --context pr-preview \ 
94+           --alias pr-${{ github.event.pull_request.number }} \ 
95+           --message "${deploy_message}" \ 
96+           --json) 
97+          
98+         echo "Netlify deployment output:" 
99+         echo "$netlify_output" 
100+          
101+         deploy_url=$(echo "$netlify_output" | jq -r '.deploy_url') 
102+          
103+         echo "deploy_url=$deploy_url" >> $GITHUB_OUTPUT 
104+         echo "✅ Deployment completed!" 
105+         echo "🌐 Deploy URL: $deploy_url" 
106+          
107+         if [ ! -z "${{ github.event.inputs.preview_page }}" ]; then 
108+           echo "" 
109+           echo "🎯 Manual preview page: ${deploy_url}/${{ github.event.inputs.preview_page }}" 
123110        fi 
124111env :
125112        NETLIFY_AUTH_TOKEN : ${{ secrets.NETLIFY_AUTH_TOKEN }} 
@@ -136,55 +123,39 @@ jobs:
136123          const commitSha = `${{ github.event.pull_request.head.sha }}`; 
137124          const shortSha = commitSha.substring(0, 7); 
138125           
139-           console.log(`Deploy URL: ${deployUrl}`); 
140-           console.log(`Manual preview page: ${manualPage}`); 
141-           console.log(`PR Number: ${prNumber}`); 
142-           console.log(`Commit SHA: ${shortSha}`); 
143-            
144-           // Get all comments on this PR to check for duplicates 
145126          const comments = await github.rest.issues.listComments({ 
146127            issue_number: prNumber, 
147128            owner: context.repo.owner, 
148129            repo: context.repo.repo, 
149130          }); 
150131           
151-           console.log(`Found ${comments.data.length} comments on PR`); 
152-            
153-           // Look for existing comment with this exact commit SHA and deploy URL 
154132          const duplicateComment = comments.data.find(comment => { 
155133            const hasMarker = comment.body.includes('**📖 Netlify Preview Ready!**'); 
156134            const hasCommitSha = comment.body.includes(`([${shortSha}]`); 
157135            const hasDeployUrl = comment.body.includes(deployUrl); 
158-              
159136            return hasMarker && hasCommitSha && hasDeployUrl; 
160137          }); 
161138           
162139          if (duplicateComment) { 
163-             console.log(`Duplicate comment found (${duplicateComment.id}) , skipping...`); 
140+             console.log(`Duplicate comment found, skipping...`); 
164141            return; 
165142          } 
166143           
167-           console.log(`No duplicate found, creating new comment for commit ${shortSha}`); 
168-            
169144          const commitUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/commit/${commitSha}`; 
170145           
171146          let comment = `**📖 Netlify Preview Ready!**\n\n`; 
172147          comment += `**Preview URL:** ${deployUrl} ([${shortSha}](${commitUrl}))\n\n`; 
173148           
174-           // Add manual preview page if specified 
175149          if (manualPage) { 
176150            comment += `🎯 **Manual Preview:** [${manualPage}](${deployUrl}/${manualPage})\n\n`; 
177151          } 
178152           
179153          comment += `✨ Browse the preview at the URL above.\n`; 
180154           
181-           // Post the comment 
182155          await github.rest.issues.createComment({ 
183156            issue_number: prNumber, 
184157            owner: context.repo.owner, 
185158            repo: context.repo.repo, 
186159            body: comment 
187160          }); 
188-            
189-           console.log('Comment posted successfully'); 
190- timeout-minutes : 10 
161+ timeout-minutes : 10 
0 commit comments