Skip to content

refactor author keys #10

refactor author keys

refactor author keys #10

Workflow file for this run

name: PR Preview

Check failure on line 1 in .github/workflows/pr-preview.yml

View workflow run for this annotation

GitHub Actions / .github/workflows/pr-preview.yml

Invalid workflow file

(Line: 100, Col: 9): Unrecognized named-value: 'secrets'. Located at position 121 within expression: !github.event.pull_request.head.repo.fork && vars.CLOUDFLARE_PAGES_PROJECT != '' && vars.CLOUDFLARE_ACCOUNT_ID != '' && secrets.CLOUDFLARE_API_TOKEN != ''
on:
pull_request:
types: [opened, synchronize, reopened, ready_for_review]
permissions:
contents: read
issues: write
pull-requests: write
concurrency:
group: pr-preview-${{ github.event.pull_request.number }}
cancel-in-progress: true
jobs:
build-preview:
runs-on: ubuntu-latest
outputs:
artifact_name: ${{ steps.meta.outputs.artifact_name }}
screenshot_name: ${{ steps.meta.outputs.screenshot_name }}
post_path: ${{ steps.changed_post.outputs.post_path }}
post_url: ${{ steps.changed_post.outputs.post_url }}
steps:
- name: Set artifact name
id: meta
run: |
echo "artifact_name=site-preview-pr-${{ github.event.number }}" >> "$GITHUB_OUTPUT"
echo "screenshot_name=site-screenshots-pr-${{ github.event.number }}" >> "$GITHUB_OUTPUT"
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
bundler-cache: true
- name: Build site
run: bundle exec jekyll build
- name: Detect changed post
id: changed_post
env:
BASE_SHA: ${{ github.event.pull_request.base.sha }}
HEAD_SHA: ${{ github.sha }}
run: |
mapfile -t posts < <(git diff --name-only "$BASE_SHA" "$HEAD_SHA" -- '_posts/*.md')
if [[ ${#posts[@]} -eq 0 ]]; then
echo "post_path=" >> "$GITHUB_OUTPUT"
echo "post_url=/" >> "$GITHUB_OUTPUT"
exit 0
fi
post="${posts[0]}"
slug="$(basename "$post" .md)"
slug="${slug#????-??-??-}"
echo "post_path=$post" >> "$GITHUB_OUTPUT"
echo "post_url=/$slug/" >> "$GITHUB_OUTPUT"
- name: Upload site artifact
uses: actions/upload-artifact@v4
with:
name: ${{ steps.meta.outputs.artifact_name }}
path: _site
retention-days: 7
- name: Install Playwright Chromium
run: npx -y playwright@1.53.0 install --with-deps chromium
- name: Capture preview screenshot
env:
POST_URL: ${{ steps.changed_post.outputs.post_url }}
run: |
python3 -m http.server --directory _site 4173 > /tmp/pr-preview-server.log 2>&1 &
server_pid=$!
trap 'kill "$server_pid"' EXIT
for i in {1..20}; do
if curl -fsS "http://127.0.0.1:4173${POST_URL}" >/dev/null; then
break
fi
sleep 0.5
done
npx -y playwright@1.53.0 screenshot --browser=chromium --full-page "http://127.0.0.1:4173${POST_URL}" preview-long.png
- name: Upload screenshot artifact
uses: actions/upload-artifact@v4
with:
name: ${{ steps.meta.outputs.screenshot_name }}
path: preview-long.png
retention-days: 7
deploy-cloudflare-preview:
runs-on: ubuntu-latest
needs: build-preview
if: ${{ !github.event.pull_request.head.repo.fork && vars.CLOUDFLARE_PAGES_PROJECT != '' && vars.CLOUDFLARE_ACCOUNT_ID != '' && secrets.CLOUDFLARE_API_TOKEN != '' }}
permissions:
contents: read
deployments: write
outputs:
preview_url: ${{ steps.cf.outputs.alias }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
bundler-cache: true
- name: Build site
run: bundle exec jekyll build
- name: Publish to Cloudflare Pages
id: cf
uses: cloudflare/pages-action@v1
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ vars.CLOUDFLARE_ACCOUNT_ID }}
projectName: ${{ vars.CLOUDFLARE_PAGES_PROJECT }}
directory: _site
gitHubToken: ${{ secrets.GITHUB_TOKEN }}
branch: pr-${{ github.event.number }}
wranglerVersion: '3'
comment-preview:
runs-on: ubuntu-latest
needs: [build-preview, deploy-cloudflare-preview]
if: ${{ always() && !github.event.pull_request.head.repo.fork && needs.build-preview.result == 'success' && (needs.deploy-cloudflare-preview.result == 'success' || needs.deploy-cloudflare-preview.result == 'skipped') }}
steps:
- name: Add or update PR preview comment
uses: actions/github-script@v7
env:
RUN_URL: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
ARTIFACT_NAME: ${{ needs.build-preview.outputs.artifact_name }}
SCREENSHOT_NAME: ${{ needs.build-preview.outputs.screenshot_name }}
POST_PATH: ${{ needs.build-preview.outputs.post_path }}
POST_URL: ${{ needs.build-preview.outputs.post_url }}
CLOUDFLARE_URL: ${{ needs.deploy-cloudflare-preview.outputs.preview_url }}
CLOUDFLARE_ENABLED: ${{ vars.CLOUDFLARE_PAGES_PROJECT != '' && vars.CLOUDFLARE_ACCOUNT_ID != '' && secrets.CLOUDFLARE_API_TOKEN != '' }}
CLOUDFLARE_RESULT: ${{ needs.deploy-cloudflare-preview.result }}
with:
script: |
const marker = "<!-- pr-preview-comment -->";
const postLine = process.env.POST_PATH
? `- Changed post detected: \`${process.env.POST_PATH}\`\n- Suggested file to open after extracting: \`_site${process.env.POST_URL}index.html\``
: "- No post markdown file changed in this PR; preview starts at `_site/index.html`.";
const cloudflareLine = process.env.CLOUDFLARE_ENABLED === "true"
? (process.env.CLOUDFLARE_RESULT === "success"
? (process.env.CLOUDFLARE_URL
? `- Hosted preview (Cloudflare Pages): [Open preview](${process.env.CLOUDFLARE_URL})`
: "- Hosted preview (Cloudflare Pages): deployed, but URL output was empty. Check deployment details.")
: "- Hosted preview (Cloudflare Pages): deployment did not succeed; check workflow logs.")
: "- Hosted preview (Cloudflare Pages): not configured (set repository vars/secrets; see README).";
const body = `${marker}
## PR Preview Ready
A static preview was built for this PR.
- Download artifact: **${process.env.ARTIFACT_NAME}** from [this workflow run](${process.env.RUN_URL})
- Download long screenshot artifact: **${process.env.SCREENSHOT_NAME}** from [this workflow run](${process.env.RUN_URL})
${postLine}
${cloudflareLine}
- Quick local preview:
- Extract artifact zip
- Run: \`python3 -m http.server --directory _site 4173\`
- Open: \`http://localhost:4173${process.env.POST_URL}\`
`;
const { owner, repo } = context.repo;
const issue_number = context.issue.number;
const comments = await github.rest.issues.listComments({
owner,
repo,
issue_number,
per_page: 100,
});
const existing = comments.data.find((comment) => comment.body && comment.body.includes(marker));
if (existing) {
await github.rest.issues.updateComment({
owner,
repo,
comment_id: existing.id,
body,
});
} else {
await github.rest.issues.createComment({
owner,
repo,
issue_number,
body,
});
}