Skip to content

V5 draft

V5 draft #28

Workflow file for this run

name: Deploy to GitHub Pages
on:
push:
branches:
- main
- develop
- multi
pull_request:
types: [ opened, synchronize, reopened, closed ]
permissions:
contents: read
pages: write
id-token: write
pull-requests: write
statuses: write
# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
concurrency:
group: "pages"
cancel-in-progress: false
jobs:
# Build job
build:
runs-on: ubuntu-latest
if: ${{ !(github.event_name == 'pull_request' && github.event.action == 'closed') }}
outputs:
deployment-path: ${{ steps.determine-path.outputs.path }}
deployment-url: ${{ steps.determine-path.outputs.url }}
branch-name: ${{ steps.determine-path.outputs.branch }}
steps:
- name: Checkout source
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Build documentation
run: npm run render
- name: Determine deployment path and URL
id: determine-path
run: |
if [[ "${{ github.event_name }}" == "push" && "${{ github.ref }}" == "refs/heads/main" ]]; then
echo "path=." >> $GITHUB_OUTPUT
echo "url=https://${{ github.repository_owner }}.github.io/DIIP" >> $GITHUB_OUTPUT
echo "branch=main" >> $GITHUB_OUTPUT
elif [[ "${{ github.event_name }}" == "push" && "${{ github.ref }}" == "refs/heads/develop" ]]; then
echo "path=draft" >> $GITHUB_OUTPUT
echo "url=https://${{ github.repository_owner }}.github.io/DIIP/draft" >> $GITHUB_OUTPUT
echo "branch=develop" >> $GITHUB_OUTPUT
elif [[ "${{ github.event_name }}" == "push" && "${{ github.ref }}" == "refs/heads/multi" ]]; then
echo "path=draft" >> $GITHUB_OUTPUT
echo "url=https://${{ github.repository_owner }}.github.io/DIIP/draft" >> $GITHUB_OUTPUT
echo "branch=multi" >> $GITHUB_OUTPUT
elif [[ "${{ github.event_name }}" == "pull_request" ]]; then
PR_NUMBER="${{ github.event.pull_request.number }}"
echo "path=pr/pr-${PR_NUMBER}" >> $GITHUB_OUTPUT
echo "url=https://${{ github.repository_owner }}.github.io/DIIP/pr/pr-${PR_NUMBER}" >> $GITHUB_OUTPUT
echo "branch=pr-${PR_NUMBER}" >> $GITHUB_OUTPUT
fi
- name: Checkout existing gh-pages content
uses: actions/checkout@v4
with:
ref: gh-pages
path: existing-pages
continue-on-error: true
- name: Setup Pages
uses: actions/configure-pages@v4
- name: Prepare deployment directory with simultaneous deployments
run: |
DEPLOY_PATH="${{ steps.determine-path.outputs.path }}"
# Create _site directory for deployment
mkdir -p _site
# Copy existing gh-pages content if it exists
if [ -d "existing-pages" ]; then
# Copy everything except .git
rsync -av --exclude='.git' existing-pages/ _site/ 2>/dev/null || true
echo "Preserved existing deployments"
else
echo "No existing deployments found"
fi
# Update the specific deployment path
if [[ "$DEPLOY_PATH" == "." ]]; then
# For main branch, update root files but preserve subdirectories
find _site -maxdepth 1 -name "*.html" -delete 2>/dev/null || true
find _site -maxdepth 1 -name "*.png" -delete 2>/dev/null || true
find _site -maxdepth 1 -name "*.svg" -delete 2>/dev/null || true
find _site -maxdepth 1 -name "*.js" -delete 2>/dev/null || true
rm -rf _site/assets _site/custom-assets 2>/dev/null || true
# Copy new files to root
cp ./*.html _site/ 2>/dev/null || true
cp ./*.png _site/ 2>/dev/null || true
cp ./*.svg _site/ 2>/dev/null || true
cp ./*.js _site/ 2>/dev/null || true
cp -r ./assets _site/ 2>/dev/null || true
cp -r ./custom-assets _site/ 2>/dev/null || true
# Also create/update latest directory
rm -rf _site/latest
mkdir -p _site/latest
cp ./*.html _site/latest/ 2>/dev/null || true
cp ./*.png _site/latest/ 2>/dev/null || true
cp ./*.svg _site/latest/ 2>/dev/null || true
cp ./*.js _site/latest/ 2>/dev/null || true
cp -r ./assets _site/latest/ 2>/dev/null || true
cp -r ./custom-assets _site/latest/ 2>/dev/null || true
echo "Updated main deployment (root + /latest)"
else
# For develop/PR branches, update specific subdirectory
rm -rf "_site/$DEPLOY_PATH"
mkdir -p "_site/$DEPLOY_PATH"
cp ./*.html "_site/$DEPLOY_PATH/" 2>/dev/null || true
cp ./*.png "_site/$DEPLOY_PATH/" 2>/dev/null || true
cp ./*.svg "_site/$DEPLOY_PATH/" 2>/dev/null || true
cp ./*.js "_site/$DEPLOY_PATH/" 2>/dev/null || true
cp -r ./assets "_site/$DEPLOY_PATH/" 2>/dev/null || true
cp -r ./custom-assets "_site/$DEPLOY_PATH/" 2>/dev/null || true
echo "Updated deployment: $DEPLOY_PATH"
fi
# Create a deployment info file
echo "# GitHub Pages Deployment Info" > _site/deployment-info.md
echo "Last updated: $(date -u '+%Y-%m-%d %H:%M:%S UTC')" >> _site/deployment-info.md
echo "Deployed from: ${{ steps.determine-path.outputs.branch }}" >> _site/deployment-info.md
echo "Deployment path: $DEPLOY_PATH" >> _site/deployment-info.md
echo "" >> _site/deployment-info.md
echo "## Available deployments:" >> _site/deployment-info.md
find _site -type f -name "index.html" | sed 's|_site/||' | sed 's|/index.html||' | sed 's|^$|/ (main)|' | sort >> _site/deployment-info.md
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
# Single deployment job that handles all cases
deploy:
environment:
name: github-pages
url: ${{ needs.build.outputs.deployment-url }}
runs-on: ubuntu-latest
needs: build
if: ${{ github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.action != 'closed') }}
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
- name: Comment on PR with preview link
if: github.event_name == 'pull_request'
uses: actions/github-script@v7
with:
script: |
const deployUrl = '${{ needs.build.outputs.deployment-url }}';
const prNumber = context.payload.pull_request.number;
const message = `πŸš€ **Preview deployment successful!**
πŸ“„ **Preview URL:** ${deployUrl}
πŸ“ **PR:** #${prNumber}
πŸ”„ **Status:** Ready for review
This preview will be updated automatically when you push new commits to this PR.
> The preview will be removed when the PR is closed or merged.`;
// Check if we already commented
const comments = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
});
const existingComment = comments.data.find(comment =>
comment.user.login === 'github-actions[bot]' &&
comment.body.includes('Preview deployment successful!')
);
if (existingComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existingComment.id,
body: message
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: message
});
}
- name: Create commit status for develop branch
if: github.event_name == 'push' && (github.ref == 'refs/heads/develop' || github.ref == 'refs/heads/multi')
uses: actions/github-script@v7
continue-on-error: true
with:
script: |
try {
await github.rest.repos.createCommitStatus({
owner: context.repo.owner,
repo: context.repo.repo,
sha: context.sha,
state: 'success',
description: 'Draft deployed successfully',
target_url: '${{ needs.build.outputs.deployment-url }}',
context: 'github-pages/draft'
});
console.log('Commit status created successfully');
} catch (error) {
console.log('Could not create commit status:', error.message);
console.log('This is not critical - deployment was successful');
}
- name: Check GitHub Pages status and provide setup instructions
uses: actions/github-script@v7
with:
script: |
const deployUrl = '${{ needs.build.outputs.deployment-url }}';
const path = '${{ needs.build.outputs.deployment-path }}';
try {
// Try to get pages info to see if it's enabled
const pages = await github.rest.repos.getPages({
owner: context.repo.owner,
repo: context.repo.repo,
});
console.log(`GitHub Pages is enabled and configured`);
console.log(`Site URL: ${pages.data.html_url}`);
console.log(`Deployment URL: ${deployUrl}`);
} catch (error) {
if (error.status === 404) {
console.log('GitHub Pages is not enabled for this repository.');
console.log('');
console.log('To enable GitHub Pages:');
console.log(' 1. Go to repository Settings > Pages');
console.log(' 2. Under "Source", select "Deploy from a branch"');
console.log(' 3. Select branch "gh-pages" and folder "/ (root)"');
console.log(' 4. Click "Save"');
console.log('');
console.log(`After setup, your site will be available at: ${deployUrl}`);
// Create a summary for the workflow
await core.summary
.addHeading('GitHub Pages Setup Required')
.addRaw('GitHub Pages is not enabled for this repository. ')
.addRaw('The content has been deployed to the gh-pages branch, but you need to enable Pages manually.')
.addHeading('Setup Instructions')
.addList([
'Go to repository **Settings > Pages**',
'Under **Source**, select **Deploy from a branch**',
'Select branch **gh-pages** and folder **/ (root)**',
'Click **Save**'
])
.addRaw(`After setup, your site will be available at: **${deployUrl}**`)
.write();
} else {
console.log('Error checking pages status:', error.message);
}
}
# Cleanup job for closed PRs
cleanup-pr:
runs-on: ubuntu-latest
needs: build
if: ${{ github.event_name == 'pull_request' && github.event.action == 'closed' }}
steps:
- name: Checkout existing gh-pages content
uses: actions/checkout@v4
with:
ref: gh-pages
path: existing-pages
continue-on-error: true
- name: Setup Pages
uses: actions/configure-pages@v4
- name: Remove PR directory and redeploy
run: |
PR_NUMBER="${{ github.event.pull_request.number }}"
PR_DIR="pr/pr-${PR_NUMBER}"
# Create _site directory for deployment
mkdir -p _site
# Copy existing gh-pages content if it exists
if [ -d "existing-pages" ]; then
rsync -av --exclude='.git' existing-pages/ _site/
echo "Copied existing deployments"
# Remove the specific PR directory
if [ -d "_site/$PR_DIR" ]; then
rm -rf "_site/$PR_DIR"
echo "Removed PR directory: $PR_DIR"
# Update deployment info
echo "# GitHub Pages Deployment Info" > _site/deployment-info.md
echo "Last updated: $(date -u '+%Y-%m-%d %H:%M:%S UTC')" >> _site/deployment-info.md
echo "Action: Cleaned up PR #${PR_NUMBER}" >> _site/deployment-info.md
echo "" >> _site/deployment-info.md
echo "## Available deployments:" >> _site/deployment-info.md
find _site -type f -name "index.html" | sed 's|_site/||' | sed 's|/index.html||' | sed 's|^$|/ (main)|' | sort >> _site/deployment-info.md
else
echo "PR directory $PR_DIR does not exist, nothing to clean up"
exit 0
fi
else
echo "No existing deployments found, nothing to clean up"
exit 0
fi
- name: Upload artifact for cleanup
uses: actions/upload-pages-artifact@v3
- name: Deploy cleaned up pages
id: cleanup-deployment
uses: actions/deploy-pages@v4
- name: Comment on PR about cleanup
uses: actions/github-script@v7
with:
script: |
const prNumber = context.payload.pull_request.number;
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: `🧹 **Preview cleaned up**
The preview deployment for this PR has been removed since the PR was closed.
Thank you for your contribution! πŸŽ‰`
});