Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 44 additions & 73 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Build and Netlify Deploy
name: Build and Deploy to GitHub Pages

on:
pull_request:
Expand All @@ -25,25 +25,26 @@ jobs:
build-and-deploy:
runs-on: ubuntu-latest
permissions:
contents: read
contents: write
pull-requests: write
pages: write
id-token: write

steps:
- name: Checkout current branch
uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}
ref: ${{ github.head_ref || github.ref }}
token: ${{ env.GH_TOKEN }}
submodules: 'true'

- name: Display branch information
run: |
echo "Running on branch: ${{ github.head_ref }}"
echo "Running on branch: ${{ github.head_ref || github.ref_name }}"
echo "Target branch: ${{ github.base_ref }}"
echo "PR number: ${{ github.event.number }}"
echo "Commit SHA: ${{ github.sha }}"


- uses: actions/setup-node@v4
with:
node-version: 18.x
Expand All @@ -62,64 +63,50 @@ jobs:
with:
path: './lectures/_build/html'

# Deploy to GitHub Pages on main branch push
- name: Deploy to GitHub Pages
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./lectures/_build/html
publish_branch: gh-pages
force_orphan: true

# Netlify preview for PRs (optional - keep if you want PR previews)
- name: Install Netlify CLI
if: github.event_name == 'pull_request'
run: |
npm install -g netlify-cli
mkdir -p ~/.config/netlify

- name: Deploy Preview to Netlify
if: github.event_name == 'pull_request'
id: netlify-deploy
run: |
if [ "${{ github.event_name }}" = "pull_request" ]; then
# Deploy to Netlify and capture the response
deploy_message="Preview Deploy from GitHub Actions PR #${{ github.event.pull_request.number }} (commit: ${{ github.event.pull_request.head.sha }})"

netlify_output=$(netlify deploy \
--dir lectures/_build/html/ \
--site ${{ secrets.NETLIFY_SITE_ID }} \
--auth ${{ secrets.NETLIFY_AUTH_TOKEN }} \
--context pr-preview \
--alias pr-${{ github.event.pull_request.number }} \
--message "${deploy_message}" \
--json)

echo "Netlify deployment output:"
echo "$netlify_output"

# Extract the actual deploy URL from the JSON response
deploy_url=$(echo "$netlify_output" | jq -r '.deploy_url')

echo "deploy_url=$deploy_url" >> $GITHUB_OUTPUT
echo "✅ Deployment completed!"
echo "🌐 Deploy URL: $deploy_url"

# Display manual preview page if specified
if [ ! -z "${{ github.event.inputs.preview_page }}" ]; then
echo ""
echo "🎯 Manual preview page: ${deploy_url}/${{ github.event.inputs.preview_page }}"
fi
else
# Handle push to branch
deploy_message="Deploy from GitHub Actions (commit: ${{ github.sha }})"

netlify_output=$(netlify deploy \
--dir lectures/_build/html/ \
--site ${{ secrets.NETLIFY_SITE_ID }} \
--auth ${{ secrets.NETLIFY_AUTH_TOKEN }} \
--context dev \
--alias ${{ github.ref_name }} \
--message "${deploy_message}" \
--json)

echo "Netlify deployment output:"
echo "$netlify_output"

# Extract the actual deploy URL from the JSON response
deploy_url=$(echo "$netlify_output" | jq -r '.deploy_url')

echo "deploy_url=$deploy_url" >> $GITHUB_OUTPUT
echo "✅ Deployment completed!"
echo "🌐 Deploy URL: $deploy_url"
deploy_message="Preview Deploy from GitHub Actions PR #${{ github.event.pull_request.number }} (commit: ${{ github.event.pull_request.head.sha }})"

netlify_output=$(netlify deploy \
--dir lectures/_build/html/ \
--site ${{ secrets.NETLIFY_SITE_ID }} \
--auth ${{ secrets.NETLIFY_AUTH_TOKEN }} \
--context pr-preview \
--alias pr-${{ github.event.pull_request.number }} \
--message "${deploy_message}" \
--json)

echo "Netlify deployment output:"
echo "$netlify_output"

deploy_url=$(echo "$netlify_output" | jq -r '.deploy_url')

echo "deploy_url=$deploy_url" >> $GITHUB_OUTPUT
echo "✅ Deployment completed!"
echo "🌐 Deploy URL: $deploy_url"

if [ ! -z "${{ github.event.inputs.preview_page }}" ]; then
echo ""
echo "🎯 Manual preview page: ${deploy_url}/${{ github.event.inputs.preview_page }}"
fi
env:
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
Expand All @@ -136,55 +123,39 @@ jobs:
const commitSha = `${{ github.event.pull_request.head.sha }}`;
const shortSha = commitSha.substring(0, 7);

console.log(`Deploy URL: ${deployUrl}`);
console.log(`Manual preview page: ${manualPage}`);
console.log(`PR Number: ${prNumber}`);
console.log(`Commit SHA: ${shortSha}`);

// Get all comments on this PR to check for duplicates
const comments = await github.rest.issues.listComments({
issue_number: prNumber,
owner: context.repo.owner,
repo: context.repo.repo,
});

console.log(`Found ${comments.data.length} comments on PR`);

// Look for existing comment with this exact commit SHA and deploy URL
const duplicateComment = comments.data.find(comment => {
const hasMarker = comment.body.includes('**📖 Netlify Preview Ready!**');
const hasCommitSha = comment.body.includes(`([${shortSha}]`);
const hasDeployUrl = comment.body.includes(deployUrl);

return hasMarker && hasCommitSha && hasDeployUrl;
});

if (duplicateComment) {
console.log(`Duplicate comment found (${duplicateComment.id}), skipping...`);
console.log(`Duplicate comment found, skipping...`);
return;
}

console.log(`No duplicate found, creating new comment for commit ${shortSha}`);

const commitUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/commit/${commitSha}`;

let comment = `**📖 Netlify Preview Ready!**\n\n`;
comment += `**Preview URL:** ${deployUrl} ([${shortSha}](${commitUrl}))\n\n`;

// Add manual preview page if specified
if (manualPage) {
comment += `🎯 **Manual Preview:** [${manualPage}](${deployUrl}/${manualPage})\n\n`;
}

comment += `✨ Browse the preview at the URL above.\n`;

// Post the comment
await github.rest.issues.createComment({
issue_number: prNumber,
owner: context.repo.owner,
repo: context.repo.repo,
body: comment
});

console.log('Comment posted successfully');
timeout-minutes: 10
timeout-minutes: 10