Skip to content

First cut at blog impl; content collections working #43

First cut at blog impl; content collections working

First cut at blog impl; content collections working #43

Workflow file for this run

name: Deploy Branch Previews
on:
push:
branches: ['**']
workflow_dispatch:
permissions:
contents: write
pages: write
id-token: write
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- name: Get branch info
id: branch-info
run: |
BRANCH_NAME=${GITHUB_REF#refs/heads/}
SAFE_BRANCH_NAME=$(echo "$BRANCH_NAME" | sed 's/\//-/g')
echo "branch=$BRANCH_NAME" >> $GITHUB_OUTPUT
echo "safe-branch=$SAFE_BRANCH_NAME" >> $GITHUB_OUTPUT
if [ "$BRANCH_NAME" = "main" ]; then
echo "is-main=true" >> $GITHUB_OUTPUT
else
echo "is-main=false" >> $GITHUB_OUTPUT
fi
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 23
- name: Setup pnpm
uses: pnpm/action-setup@v4
with:
version: latest
- name: Install dependencies
run: |
cd docs-infra-prototype
pnpm install
- name: Configure Astro for branch deployment
if: steps.branch-info.outputs.is-main == 'false'
run: |
cd docs-infra-prototype
# Create branch-specific config
cat > astro.config.branch.ts << 'EOF'
// @ts-check
import { defineConfig } from "astro/config";
import react from "@astrojs/react";
import tailwindcss from "@tailwindcss/vite";
import mdx from "@astrojs/mdx";
import remarkGfm from 'remark-gfm';
import rehypeSlug from 'rehype-slug';
import rehypeAutolinkHeadings from 'rehype-autolink-headings';
export default defineConfig({
site: 'https://block.github.io/docs-site-kickstarter/branch/${{ steps.branch-info.outputs.safe-branch }}',
base: '/',
integrations: [
mdx({
remarkPlugins: [remarkGfm],
rehypePlugins: [rehypeSlug, rehypeAutolinkHeadings],
}),
react()
],
markdown: {
remarkPlugins: [remarkGfm],
rehypePlugins: [rehypeSlug, rehypeAutolinkHeadings],
shikiConfig: {
themes: {
light: 'github-light',
dark: 'github-dark'
},
wrap: true,
defaultColor: 'light'
}
},
vite: {
plugins: [tailwindcss()],
resolve: {
alias: {
"@": "/src",
},
},
},
});
EOF
mv astro.config.branch.ts astro.config.ts
- name: Build site
run: |
cd docs-infra-prototype
# Show current directory and contents
echo "Current directory: $(pwd)"
echo "Directory contents:"
ls -la
# Check if package.json exists
if [ ! -f "package.json" ]; then
echo "❌ package.json not found!"
exit 1
fi
# Show package.json scripts
echo "Available scripts:"
cat package.json | grep -A 10 '"scripts"'
# Run the build
echo "Running pnpm build..."
echo "Node version: $(node --version)"
echo "PNPM version: $(pnpm --version)"
# Try building with verbose output
if ! pnpm build --verbose 2>&1; then
echo "❌ Build failed with pnpm build!"
echo "Trying with npx astro build..."
if ! npx astro build 2>&1; then
echo "❌ Build failed with npx astro build too!"
echo "Checking astro installation..."
pnpm list astro || echo "Astro not found in dependencies"
echo "Checking for any error logs..."
find . -name "*.log" -exec cat {} \; 2>/dev/null || echo "No log files found"
exit 1
fi
fi
# Check build result
echo "Build completed. Checking results..."
echo "Current directory contents after build:"
ls -la
# Verify build completed successfully
if [ ! -d "dist" ]; then
echo "❌ Build failed - dist directory not found"
echo "Looking for any build output directories:"
find . -name "*dist*" -type d 2>/dev/null || echo "No dist directories found"
exit 1
fi
echo "βœ… Build completed successfully"
echo "Build output contents:"
ls -la dist/
echo "Total files in dist: $(find dist -type f | wc -l)"
- name: Deploy main branch to GitHub Pages
if: steps.branch-info.outputs.is-main == 'true'
run: |
# Verify build completed and dist exists
if [ ! -d "docs-infra-prototype/dist" ]; then
echo "❌ Error: dist directory not found at docs-infra-prototype/dist"
echo "Current directory: $(pwd)"
echo "Contents of docs-infra-prototype:"
ls -la docs-infra-prototype/ || echo "docs-infra-prototype directory not found"
exit 1
fi
echo "βœ… Build verified - dist directory exists with $(find docs-infra-prototype/dist -type f | wc -l) files"
# Save built files to temporary location before switching branches
echo "Saving build files to temporary location..."
mkdir -p /tmp/build-output
cp -r docs-infra-prototype/dist/* /tmp/build-output/
echo "Build files saved. Contents:"
ls -la /tmp/build-output/
# Configure git
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
# Create deployment directory
mkdir -p /tmp/gh-pages-content
# Check if gh-pages branch exists and get existing branch content
if git ls-remote --heads origin gh-pages | grep -q gh-pages; then
echo "Fetching existing gh-pages content..."
git fetch origin gh-pages
git checkout gh-pages
# Copy only the branch directory to preserve branch previews
if [ -d "branch" ]; then
cp -r branch /tmp/gh-pages-content/
fi
# Go back to source branch
git checkout ${{ steps.branch-info.outputs.branch }}
fi
# Prepare the gh-pages content
rm -rf /tmp/gh-pages-deploy
mkdir -p /tmp/gh-pages-deploy
cd /tmp/gh-pages-deploy
# Copy main site content to root from temporary location
echo "Copying build files from temporary location..."
cp -r /tmp/build-output/* .
# Restore branch directory if it existed
if [ -d "/tmp/gh-pages-content/branch" ]; then
cp -r /tmp/gh-pages-content/branch .
else
# Create empty branch directory with index
mkdir -p branch
cat > branch/index.html << 'EOF'
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Branch Previews - docs-site-kickstarter</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
margin: 0;
padding: 40px;
background: #f6f8fa;
}
.container {
max-width: 800px;
margin: 0 auto;
background: white;
padding: 40px;
border-radius: 8px;
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}
.header {
border-bottom: 1px solid #e1e5e9;
padding-bottom: 20px;
margin-bottom: 30px;
}
.main-link {
display: inline-block;
margin-bottom: 30px;
padding: 12px 24px;
background: #0366d6;
color: white;
text-decoration: none;
border-radius: 6px;
font-weight: 500;
transition: background-color 0.2s;
}
.main-link:hover { background: #0256cc; }
.empty-state {
text-align: center;
padding: 40px;
color: #586069;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>Branch Previews</h1>
<p>Preview deployments for all branches in the docs-site-kickstarter repository.</p>
<a href="../" class="main-link">← Back to Main Site</a>
</div>
<div class="empty-state">
No branch previews available yet.<br>
Push to a non-main branch to create a preview.
</div>
</div>
</body>
</html>
EOF
fi
# Deploy to gh-pages
git init
git add .
# Add .nojekyll file to disable Jekyll processing
touch .nojekyll
git add .nojekyll
git commit -m "Deploy main branch to GitHub Pages"
git push --force https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git HEAD:gh-pages
echo "πŸš€ Main site deployed to: https://block.github.io/docs-site-kickstarter/"
echo "πŸ“‹ Branch previews available at: https://block.github.io/docs-site-kickstarter/branch/"
- name: Deploy branch preview
if: steps.branch-info.outputs.is-main == 'false'
run: |
# Verify build completed and dist exists
if [ ! -d "docs-infra-prototype/dist" ]; then
echo "❌ Error: dist directory not found at docs-infra-prototype/dist"
echo "Current directory: $(pwd)"
echo "Contents of docs-infra-prototype:"
ls -la docs-infra-prototype/ || echo "docs-infra-prototype directory not found"
exit 1
fi
echo "βœ… Build verified - dist directory exists with $(find docs-infra-prototype/dist -type f | wc -l) files"
# Save built files to temporary location before switching branches
echo "Saving build files to temporary location..."
mkdir -p /tmp/build-output
cp -r docs-infra-prototype/dist/* /tmp/build-output/
echo "Build files saved. Contents:"
ls -la /tmp/build-output/
# Configure git
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
# Create deployment directory
mkdir -p /tmp/gh-pages-content
# Check if gh-pages branch exists and get existing content
if git ls-remote --heads origin gh-pages | grep -q gh-pages; then
echo "Fetching existing gh-pages content..."
# Clean up any modified files that might conflict with git checkout
echo "Cleaning up modified config files before branch switch..."
git checkout -- docs-infra-prototype/astro.config.ts 2>/dev/null || echo "No config to restore"
rm -f docs-infra-prototype/astro.config.branch.ts
git fetch origin gh-pages
git checkout gh-pages
# Copy everything to temp directory
cp -r . /tmp/gh-pages-content/ 2>/dev/null || true
# Go back to source branch
git checkout ${{ steps.branch-info.outputs.branch }}
fi
# Prepare the gh-pages content
rm -rf /tmp/gh-pages-deploy
mkdir -p /tmp/gh-pages-deploy
cd /tmp/gh-pages-deploy
# Copy existing gh-pages content if it exists
if [ -d "/tmp/gh-pages-content" ]; then
cp -r /tmp/gh-pages-content/* . 2>/dev/null || true
# Remove .git directory from copied content
rm -rf .git
fi
# Create branch directory and copy new content from temporary location
mkdir -p branch/${{ steps.branch-info.outputs.safe-branch }}
echo "Copying build files from temporary location..."
cp -r /tmp/build-output/* branch/${{ steps.branch-info.outputs.safe-branch }}/
# Debug: Show what was actually deployed
echo "=== DEPLOYMENT DEBUG INFO ==="
echo "Branch directory created: branch/${{ steps.branch-info.outputs.safe-branch }}"
echo "Contents of branch directory:"
ls -la branch/
echo "Contents of specific branch directory:"
ls -la branch/${{ steps.branch-info.outputs.safe-branch }}/ || echo "Branch directory is empty!"
echo "Sample of files in branch preview:"
find branch/${{ steps.branch-info.outputs.safe-branch }} -type f | head -10
echo "Total files in branch preview: $(find branch/${{ steps.branch-info.outputs.safe-branch }} -type f | wc -l)"
echo "=== END DEBUG INFO ==="
# Create branch index page
mkdir -p branch
cat > branch/index.html << 'EOF'
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Branch Previews - docs-site-kickstarter</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
margin: 0;
padding: 40px;
background: #f6f8fa;
}
.container {
max-width: 800px;
margin: 0 auto;
background: white;
padding: 40px;
border-radius: 8px;
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}
.header {
border-bottom: 1px solid #e1e5e9;
padding-bottom: 20px;
margin-bottom: 30px;
}
.branch-list { list-style: none; padding: 0; }
.branch-item {
margin: 15px 0;
padding: 20px;
border: 1px solid #e1e5e9;
border-radius: 8px;
transition: border-color 0.2s;
}
.branch-item:hover { border-color: #0366d6; }
.branch-name {
font-weight: 600;
color: #24292e;
font-size: 16px;
margin-bottom: 8px;
}
.branch-link {
color: #0366d6;
text-decoration: none;
font-weight: 500;
}
.branch-link:hover { text-decoration: underline; }
.main-link {
display: inline-block;
margin-bottom: 30px;
padding: 12px 24px;
background: #0366d6;
color: white;
text-decoration: none;
border-radius: 6px;
font-weight: 500;
transition: background-color 0.2s;
}
.main-link:hover { background: #0256cc; }
.empty-state {
text-align: center;
padding: 40px;
color: #586069;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>Branch Previews</h1>
<p>Preview deployments for all branches in the docs-site-kickstarter repository.</p>
<a href="../" class="main-link">← Back to Main Site</a>
</div>
<ul class="branch-list" id="branchList">
<li class="empty-state">Loading branch previews...</li>
</ul>
</div>
<script>
// Get list of branch directories
const branches = [];
EOF
# Add branch names to the JavaScript
for dir in branch/*/; do
if [ -d "$dir" ]; then
branch_name=$(basename "$dir")
original_name=$(echo "$branch_name" | sed 's/-/\//g')
echo " branches.push({safe: '$branch_name', original: '$original_name'});" >> branch/index.html
fi
done
cat >> branch/index.html << 'EOF'
const branchList = document.getElementById('branchList');
if (branches.length > 0) {
branchList.innerHTML = branches.map(branch => `
<li class="branch-item">
<div class="branch-name">${branch.original}</div>
<a href="${branch.safe}/" class="branch-link">View Preview β†’</a>
</li>
`).join('');
} else {
branchList.innerHTML = '<li class="empty-state">No branch previews available yet.</li>';
}
</script>
</body>
</html>
EOF
# Initialize git and push to gh-pages
git init
git add .
# Add .nojekyll file to disable Jekyll processing
touch .nojekyll
git add .nojekyll
# Debug: Show final deployment structure
echo "=== FINAL DEPLOYMENT STRUCTURE ==="
echo "Current directory: $(pwd)"
echo "All files being deployed:"
find . -type f | head -20
echo "Branch directory structure:"
find branch -type f | head -10
echo "=== END DEPLOYMENT STRUCTURE ==="
git commit -m "Deploy branch preview: ${{ steps.branch-info.outputs.branch }}"
git push --force https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git HEAD:gh-pages
echo "πŸ” Branch preview deployed to: https://block.github.io/docs-site-kickstarter/branch/${{ steps.branch-info.outputs.safe-branch }}/"
echo "πŸ“‹ All previews available at: https://block.github.io/docs-site-kickstarter/branch/"