Skip to content

Merge pull request #6 from shakacode/jg/5-docs-card-copy #23

Merge pull request #6 from shakacode/jg/5-docs-card-copy

Merge pull request #6 from shakacode/jg/5-docs-card-copy #23

name: Site Build and Deploy
on:
pull_request:
push:
branches: [main]
repository_dispatch:
types: [docs-updated]
workflow_dispatch:
permissions:
contents: read
pull-requests: write
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v5
- name: Setup Node
uses: actions/setup-node@v5
with:
node-version: 20
- name: Sync docs
run: npm run sync:docs
env:
SHAKAPACKER_REPO_URL: https://github.com/shakacode/shakapacker.git
SHAKAPACKER_REF: main
- name: Prepare docs
run: npm run prepare:docs
- name: Install site dependencies
run: npm --prefix prototypes/docusaurus install
- name: Build site
run: npm run build:site
deploy:
if: (github.event_name == 'push' && github.ref == 'refs/heads/main') || github.event_name == 'repository_dispatch' || github.event_name == 'workflow_dispatch' || (github.event_name == 'pull_request' && github.event.pull_request.head.repo.fork == false)
needs: build
runs-on: ubuntu-latest
environment:
name: pages-deploy
url: ${{ steps.deploy.outputs.deployment_url }}
steps:
- name: Checkout
uses: actions/checkout@v5
- name: Setup Node
uses: actions/setup-node@v5
with:
node-version: 20
- name: Sync docs
run: npm run sync:docs
env:
SHAKAPACKER_REPO_URL: https://github.com/shakacode/shakapacker.git
SHAKAPACKER_REF: main
- name: Prepare docs
run: npm run prepare:docs
- name: Install site dependencies
run: npm --prefix prototypes/docusaurus install
- name: Build site
run: npm run build:site
- name: Determine deploy branch
id: deploy_branch
run: |
if [ "${{ github.event_name }}" = "pull_request" ]; then
echo "branch=pr-${{ github.event.pull_request.number }}" >> "$GITHUB_OUTPUT"
elif [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
echo "branch=${{ github.ref_name }}" >> "$GITHUB_OUTPUT"
else
echo "branch=main" >> "$GITHUB_OUTPUT"
fi
- name: Deploy to Cloudflare Pages
id: deploy
run: |
set -euo pipefail
npx wrangler pages deploy prototypes/docusaurus/build \
--project-name "${CLOUDFLARE_PAGES_PROJECT:-shakapacker-com}" \
--branch "${{ steps.deploy_branch.outputs.branch }}" | tee /tmp/wrangler-pages-deploy.log
DEPLOYMENT_URL="$(grep -Eo 'https://[^ ]+\.pages\.dev' /tmp/wrangler-pages-deploy.log | head -n 1 || true)"
if [ -z "$DEPLOYMENT_URL" ]; then
if [ "${{ steps.deploy_branch.outputs.branch }}" = "main" ]; then
DEPLOYMENT_URL="https://shakapacker.com"
else
SAFE_BRANCH="$(echo "${{ steps.deploy_branch.outputs.branch }}" | sed 's/[^A-Za-z0-9-]/-/g')"
DEPLOYMENT_URL="https://${SAFE_BRANCH}.${CLOUDFLARE_PAGES_PROJECT:-shakapacker-com}.pages.dev"
fi
fi
echo "deployment_url=$DEPLOYMENT_URL" >> "$GITHUB_OUTPUT"
{
echo "### Cloudflare Pages Deployment"
echo ""
echo "- Branch: \`${{ steps.deploy_branch.outputs.branch }}\`"
echo "- URL: $DEPLOYMENT_URL"
} >> "$GITHUB_STEP_SUMMARY"
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
CLOUDFLARE_PAGES_PROJECT: ${{ vars.CLOUDFLARE_PAGES_PROJECT }}
- name: Comment preview URL
if: github.event_name == 'pull_request'
uses: actions/github-script@v7
env:
DEPLOYMENT_URL: ${{ steps.deploy.outputs.deployment_url }}
DEPLOY_BRANCH: ${{ steps.deploy_branch.outputs.branch }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const marker = "<!-- cf-pages-preview -->";
const body = `${marker}
Cloudflare preview deployed.
- Branch: \`${process.env.DEPLOY_BRANCH}\`
- URL: ${process.env.DEPLOYMENT_URL}`;
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
per_page: 100,
});
const existing = comments.find((comment) =>
comment.user?.type === "Bot" && comment.body?.includes(marker)
);
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body,
});
}