Skip to content

fix workflow

fix workflow #25

Workflow file for this run

name: PR Preview
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:
deploy-cloudflare-preview:
runs-on: ubuntu-latest
if: ${{ !github.event.pull_request.head.repo.fork && vars.CLOUDFLARE_PAGES_PROJECT != '' && vars.CLOUDFLARE_ACCOUNT_ID != '' }}
permissions:
contents: read
deployments: write
outputs:
preview_url: ${{ steps.cf.outputs.alias }}
has_token: ${{ steps.token_check.outputs.has_token }}
steps:
- name: Check Cloudflare token
id: token_check
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
run: |
if [ -n "$CLOUDFLARE_API_TOKEN" ]; then
echo "has_token=true" >> "$GITHUB_OUTPUT"
else
echo "has_token=false" >> "$GITHUB_OUTPUT"
fi
- name: Checkout
if: ${{ steps.token_check.outputs.has_token == 'true' }}
uses: actions/checkout@v4
- name: Set up Ruby
if: ${{ steps.token_check.outputs.has_token == 'true' }}
uses: ruby/setup-ruby@v1
with:
ruby-version: "3.1"
bundler-cache: true
- name: Build site
if: ${{ steps.token_check.outputs.has_token == 'true' }}
run: bundle exec jekyll build
- name: Publish to Cloudflare Pages
id: cf
if: ${{ steps.token_check.outputs.has_token == 'true' }}
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: deploy-cloudflare-preview
if: ${{ always() && !github.event.pull_request.head.repo.fork && (needs.deploy-cloudflare-preview.result == 'success' || needs.deploy-cloudflare-preview.result == 'skipped' || needs.deploy-cloudflare-preview.result == 'failure') }}
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 }}
CLOUDFLARE_URL: ${{ needs.deploy-cloudflare-preview.outputs.preview_url }}
CLOUDFLARE_ENABLED: ${{ vars.CLOUDFLARE_PAGES_PROJECT != '' && vars.CLOUDFLARE_ACCOUNT_ID != '' && needs.deploy-cloudflare-preview.outputs.has_token == 'true' }}
CLOUDFLARE_RESULT: ${{ needs.deploy-cloudflare-preview.result }}
with:
script: |
const marker = "<!-- pr-preview-comment -->";
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](${process.env.RUN_URL}).`)
: "- Hosted preview (Cloudflare Pages): not configured (set repository vars/secrets; see README).";
const body = `${marker}
## PR Preview Ready
${cloudflareLine}
`;
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,
});
}