Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
ci: publish pull request previews from a privileged workflow_run
Elian Doraneedefa06f599
diff --git a/.github/actions/deploy-to-cloudflare-pages/action.yml b/.github/actions/deploy-to-cloudflare-pages/action.ymlindex 5f3b30a688..9f0529f5a4 100644--- a/.github/actions/deploy-to-cloudflare-pages/action.yml+++ b/.github/actions/deploy-to-cloudflare-pages/action.yml@@ -1,15 +1,8 @@name: "Deploy to Cloudflare Pages"-description: "Deploys to Cloudflare Pages on either a temporary branch with preview comment, or on the production version if on the main branch."+description: "Deploys the production version of a Cloudflare Pages project. Pull request previews go through .github/workflows/deploy-previews.yml, because a pull_request run started from a fork holds no Cloudflare credentials."inputs:project_name:description: "CloudFlare Pages project name"- comment_body:- description: "The message to display when deployment is ready"- default: "Deployment is ready."- required: false- production_url:- description: "The URL to mention as the production URL."- required: truedeploy_dir:description: "The directory from which to deploy."required: true@@ -19,9 +12,6 @@ inputs:cloudflare_account_id:description: "The Cloudflare account ID to use for deployment."required: true- github_token:- description: "The GitHub token to use for posting PR comments."- required: trueruns:using: compositesteps:@@ -32,8 +22,6 @@ runs:# Deploy using Wrangler (use pre-installed wrangler)- name: Deploy to Cloudflare Pages- id: deploy- if: github.event_name == 'push' || github.event_name == 'workflow_dispatch' || github.event_name == 'release'uses: cloudflare/wrangler-action@ebbaa1584979971c8614a24965b4405ff95890e0 # v4.0.0with:apiToken: ${{ inputs.cloudflare_api_token }}@@ -41,64 +29,3 @@ runs:# On release events github.ref_name is the tag (e.g. v0.103.0), so force the production branch.command: pages deploy ${{ inputs.deploy_dir }} --project-name=${{ inputs.project_name}} --branch=${{ github.event_name == 'release' && 'main' || github.ref_name }}wranglerVersion: '' # Use pre-installed version-- # Deploy preview for PRs- - name: Deploy Preview to Cloudflare Pages- id: preview-deployment- if: github.event_name == 'pull_request'- uses: cloudflare/wrangler-action@ebbaa1584979971c8614a24965b4405ff95890e0 # v4.0.0- with:- apiToken: ${{ inputs.cloudflare_api_token }}- accountId: ${{ inputs.cloudflare_account_id }}- command: pages deploy ${{ inputs.deploy_dir }} --project-name=${{ inputs.project_name}} --branch=pr-${{ github.event.pull_request.number }}- wranglerVersion: '' # Use pre-installed version-- # Post deployment URL as PR comment- - name: Comment PR with Preview URL- if: github.event_name == 'pull_request'- uses: actions/github-script@v9- env:- COMMENT_BODY: ${{ inputs.comment_body }}- PRODUCTION_URL: ${{ inputs.production_url }}- PROJECT_NAME: ${{ inputs.project_name }}- with:- github-token: ${{ inputs.github_token }}- script: |- const prNumber = context.issue.number;- // Construct preview URL based on Cloudflare Pages pattern- const projectName = process.env.PROJECT_NAME;- const previewUrl = `https://pr-${prNumber}.${projectName}.pages.dev`;-- // Check if we already commented- const comments = await github.rest.issues.listComments({- owner: context.repo.owner,- repo: context.repo.repo,- issue_number: prNumber- });-- const customMessage = process.env.COMMENT_BODY;- const botComment = comments.data.find(comment =>- comment.user.type === 'Bot' &&- comment.body.includes(customMessage)- );-- const mainUrl = process.env.PRODUCTION_URL;- const commentBody = `${customMessage}!\n\nπ Preview URL: ${previewUrl}\nπ Production URL: ${mainUrl}\n\nβ All checks passed\n\n_This preview will be updated automatically with new commits._`;-- if (botComment) {- // Update existing comment- await github.rest.issues.updateComment({- owner: context.repo.owner,- repo: context.repo.repo,- comment_id: botComment.id,- body: commentBody- });- } else {- // Create new comment- await github.rest.issues.createComment({- issue_number: prNumber,- owner: context.repo.owner,- repo: context.repo.repo,- body: commentBody- });- }diff --git a/.github/actions/upload-cloudflare-preview/action.yml b/.github/actions/upload-cloudflare-preview/action.ymlnew file mode 100644index 0000000000..4d96c8771b--- /dev/null+++ b/.github/actions/upload-cloudflare-preview/action.yml@@ -0,0 +1,36 @@+name: "Upload a Cloudflare Pages preview"+description: "Uploads a built directory and its pull request number for the 'Deploy previews' workflow to publish. A pull_request run started from a fork receives neither secrets nor repository variables, so it cannot deploy the preview itself."+inputs:+ artifact_name:+ description: "Artifact name the preview workflow downloads; must match the caller job in deploy-previews.yml."+ required: true+ deploy_dir:+ description: "The directory to publish as the preview."+ required: true+runs:+ using: composite+ steps:+ # github.event.workflow_run.pull_requests is empty for a fork's pull request,+ # so the number travels with the build instead.+ - name: Record the pull request number+ shell: bash+ run: |+ mkdir -p "${RUNNER_TEMP}/preview-meta"+ echo "${PR_NUMBER}" > "${RUNNER_TEMP}/preview-meta/pr-number"+ env:+ PR_NUMBER: ${{ github.event.pull_request.number }}++ - name: Upload the built site+ uses: actions/upload-artifact@v7+ with:+ name: ${{ inputs.artifact_name }}+ path: ${{ inputs.deploy_dir }}+ retention-days: 1+ include-hidden-files: true++ - name: Upload the preview metadata+ uses: actions/upload-artifact@v7+ with:+ name: ${{ inputs.artifact_name }}-meta+ path: ${{ runner.temp }}/preview-meta+ retention-days: 1diff --git a/.github/workflows/cloudflare-preview.yml b/.github/workflows/cloudflare-preview.ymlnew file mode 100644index 0000000000..87c65e4a7a--- /dev/null+++ b/.github/workflows/cloudflare-preview.yml@@ -0,0 +1,158 @@+# Publishes a preview that an unprivileged pull_request run has already built.+#+# A pull_request run started from a fork gets no secrets and no repository+# variables, so it cannot reach Cloudflare. It uploads its build instead, and a+# workflow_run event brings the result here, where the base repository's secrets+# are available. Nothing below checks out or executes the pull request's code:+# the only fork-controlled input is a directory of static files handed straight+# to wrangler.+name: Cloudflare Pages preview++on:+ workflow_call:+ inputs:+ artifact_name:+ description: "Artifact holding the built site, as uploaded by upload-cloudflare-preview."+ required: true+ type: string+ project_name:+ description: "Cloudflare Pages project name."+ required: true+ type: string+ comment_body:+ description: "The message to display when the preview is ready."+ required: true+ type: string+ production_url:+ description: "The URL to mention as the production URL."+ required: true+ type: string++jobs:+ deploy:+ name: Deploy preview+ runs-on: ubuntu-latest+ timeout-minutes: 10+ if: ${{ github.repository == vars.REPO_MAIN }}++ # Keep a superseded build from overwriting the preview of a newer commit.+ concurrency:+ group: cloudflare-preview-${{ inputs.project_name }}-${{ github.event.workflow_run.head_repository.full_name }}-${{ github.event.workflow_run.head_branch }}+ cancel-in-progress: true++ permissions:+ actions: read # Read the artifacts of the run that triggered this one+ contents: read+ deployments: write+ pull-requests: write # For PR preview comments++ steps:+ - name: Download the built site+ uses: actions/download-artifact@v8+ with:+ name: ${{ inputs.artifact_name }}+ path: preview+ run-id: ${{ github.event.workflow_run.id }}+ github-token: ${{ secrets.GITHUB_TOKEN }}++ - name: Download the preview metadata+ uses: actions/download-artifact@v8+ with:+ name: ${{ inputs.artifact_name }}-meta+ path: preview-meta+ run-id: ${{ github.event.workflow_run.id }}+ github-token: ${{ secrets.GITHUB_TOKEN }}++ # A pull request can edit the workflow that writes this file, so the number+ # it claims is only trusted once the pull request is confirmed to sit on the+ # commit the run built. Everything downstream uses the validated integer.+ - name: Resolve the pull request+ id: pr+ uses: actions/github-script@v9+ env:+ HEAD_SHA: ${{ github.event.workflow_run.head_sha }}+ with:+ github-token: ${{ secrets.GITHUB_TOKEN }}+ script: |+ const fs = require('fs');+ const claimed = Number(fs.readFileSync('preview-meta/pr-number', 'utf8').trim());+ if (!Number.isSafeInteger(claimed) || claimed <= 0) {+ core.setFailed('The preview metadata holds no pull request number.');+ return;+ }++ const { data: pullRequest } = await github.rest.pulls.get({+ owner: context.repo.owner,+ repo: context.repo.repo,+ pull_number: claimed+ });++ const headSha = process.env.HEAD_SHA;+ if (pullRequest.head.sha !== headSha) {+ core.setFailed(`Pull request #${claimed} is at ${pullRequest.head.sha}, but the build ran on ${headSha}.`);+ return;+ }++ core.setOutput('number', String(claimed));++ # Install wrangler globally to avoid workspace issues+ - name: Install Wrangler+ shell: bash+ run: npm install -g wrangler++ - name: Deploy preview to Cloudflare Pages+ uses: cloudflare/wrangler-action@ebbaa1584979971c8614a24965b4405ff95890e0 # v4.0.0+ with:+ apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}+ accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}+ command: pages deploy preview --project-name=${{ inputs.project_name }} --branch=pr-${{ steps.pr.outputs.number }}+ wranglerVersion: '' # Use pre-installed version++ - name: Comment PR with Preview URL+ uses: actions/github-script@v9+ env:+ COMMENT_BODY: ${{ inputs.comment_body }}+ PRODUCTION_URL: ${{ inputs.production_url }}+ PROJECT_NAME: ${{ inputs.project_name }}+ PR_NUMBER: ${{ steps.pr.outputs.number }}+ with:+ github-token: ${{ secrets.GITHUB_TOKEN }}+ script: |+ const prNumber = Number(process.env.PR_NUMBER);+ // Construct preview URL based on Cloudflare Pages pattern+ const projectName = process.env.PROJECT_NAME;+ const previewUrl = `https://pr-${prNumber}.${projectName}.pages.dev`;++ // Check if we already commented+ const comments = await github.paginate(github.rest.issues.listComments, {+ owner: context.repo.owner,+ repo: context.repo.repo,+ issue_number: prNumber+ });++ const customMessage = process.env.COMMENT_BODY;+ const botComment = comments.find(comment =>+ comment.user.type === 'Bot' &&+ comment.body.includes(customMessage)+ );++ const mainUrl = process.env.PRODUCTION_URL;+ const commentBody = `${customMessage}!\n\nπ Preview URL: ${previewUrl}\nπ Production URL: ${mainUrl}\n\nβ All checks passed\n\n_This preview will be updated automatically with new commits._`;++ if (botComment) {+ // Update existing comment+ await github.rest.issues.updateComment({+ owner: context.repo.owner,+ repo: context.repo.repo,+ comment_id: botComment.id,+ body: commentBody+ });+ } else {+ // Create new comment+ await github.rest.issues.createComment({+ issue_number: prNumber,+ owner: context.repo.owner,+ repo: context.repo.repo,+ body: commentBody+ });+ }diff --git a/.github/workflows/deploy-app.yml b/.github/workflows/deploy-app.ymlindex 409ea4001e..08be89457e 100644--- a/.github/workflows/deploy-app.yml+++ b/.github/workflows/deploy-app.yml@@ -26,14 +26,12 @@ jobs:build-and-deploy:name: Build and Deploy Appruns-on: ubuntu-latest- timeout-minutes: 10+ timeout-minutes: 15# Required permissions for deploymentpermissions:contents: readdeployments: write- pull-requests: write # For PR preview comments- id-token: write # For OIDC authentication (if needed)steps:- name: Checkout Repository@@ -63,12 +61,18 @@ jobs:- name: Deployuses: ./.github/actions/deploy-to-cloudflare-pages- if: github.repository == vars.REPO_MAIN+ if: github.event_name != 'pull_request' && github.repository == vars.REPO_MAINwith:project_name: "trilium-app"- comment_body: "π₯οΈ App preview is ready"- production_url: "https://app.triliumnotes.org"deploy_dir: "apps/standalone/dist"cloudflare_api_token: ${{ secrets.CLOUDFLARE_API_TOKEN }}cloudflare_account_id: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}- github_token: ${{ secrets.GITHUB_TOKEN }}++ # A pull request from a fork reaches neither secrets nor vars, so the build+ # travels to deploy-previews.yml instead of deploying from here.+ - name: Upload preview+ uses: ./.github/actions/upload-cloudflare-preview+ if: github.event_name == 'pull_request'+ with:+ artifact_name: "app-preview"+ deploy_dir: "apps/standalone/dist"diff --git a/.github/workflows/deploy-docs.yml b/.github/workflows/deploy-docs.ymlindex f00ed1a418..ac70158b3e 100644--- a/.github/workflows/deploy-docs.yml+++ b/.github/workflows/deploy-docs.yml@@ -31,14 +31,12 @@ jobs:build-and-deploy:name: Build and Deploy Documentationruns-on: ubuntu-latest- timeout-minutes: 10+ timeout-minutes: 15# Required permissions for deploymentpermissions:contents: readdeployments: write- pull-requests: write # For PR preview comments- id-token: write # For OIDC authentication (if needed)steps:- name: Checkout Repository@@ -69,12 +67,18 @@ jobs:- name: Deployuses: ./.github/actions/deploy-to-cloudflare-pages- if: ${{ github.repository == vars.REPO_MAIN }}+ if: ${{ github.event_name != 'pull_request' && github.repository == vars.REPO_MAIN }}with:project_name: "trilium-docs"- comment_body: "π Documentation preview is ready"- production_url: "https://docs.triliumnotes.org"deploy_dir: "site"cloudflare_api_token: ${{ secrets.CLOUDFLARE_API_TOKEN }}cloudflare_account_id: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}- github_token: ${{ secrets.GITHUB_TOKEN }}++ # A pull request from a fork reaches neither secrets nor vars, so the build+ # travels to deploy-previews.yml instead of deploying from here.+ - name: Upload preview+ uses: ./.github/actions/upload-cloudflare-preview+ if: ${{ github.event_name == 'pull_request' }}+ with:+ artifact_name: "docs-preview"+ deploy_dir: "site"diff --git a/.github/workflows/deploy-previews.yml b/.github/workflows/deploy-previews.ymlnew file mode 100644index 0000000000..5a2da63409--- /dev/null+++ b/.github/workflows/deploy-previews.yml@@ -0,0 +1,62 @@+# Publishes the previews built by the three deploy workflows.+#+# Those workflows build on pull_request, which is unprivileged: a pull request+# opened from a fork receives no secrets and no repository variables, so its run+# cannot deploy. The completed run arrives here as a workflow_run event, which+# always executes the default branch's copy of this file with the base+# repository's secrets and a write-capable token.+#+# Do not add a build step, a checkout of the pull request's head, or any other+# way of running its code. The privilege of this workflow rests on it doing+# nothing but forwarding a directory of static files.+name: Deploy previews++on:+ workflow_run:+ workflows:+ - Deploy Standalone App+ - Deploy Documentation+ - Deploy website+ types: [completed]++# A called workflow can only narrow these, so they are declared at the widest+# scope cloudflare-preview.yml needs.+permissions:+ actions: read+ contents: read+ deployments: write+ pull-requests: write++jobs:+ app:+ name: App+ if: ${{ github.event.workflow_run.name == 'Deploy Standalone App' && github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success' }}+ uses: ./.github/workflows/cloudflare-preview.yml+ secrets: inherit+ with:+ artifact_name: "app-preview"+ project_name: "trilium-app"+ comment_body: "π₯οΈ App preview is ready"+ production_url: "https://app.triliumnotes.org"++ docs:+ name: Documentation+ if: ${{ github.event.workflow_run.name == 'Deploy Documentation' && github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success' }}+ uses: ./.github/workflows/cloudflare-preview.yml+ secrets: inherit+ with:+ artifact_name: "docs-preview"+ project_name: "trilium-docs"+ comment_body: "π Documentation preview is ready"+ production_url: "https://docs.triliumnotes.org"++ website:+ name: Website+ if: ${{ github.event.workflow_run.name == 'Deploy website' && github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success' }}+ uses: ./.github/workflows/cloudflare-preview.yml+ secrets: inherit+ with:+ artifact_name: "website-preview"+ project_name: "trilium-homepage"+ comment_body: "π Website preview is ready"+ production_url: "https://triliumnotes.org"diff --git a/.github/workflows/website.yml b/.github/workflows/website.ymlindex a1ac61c240..5e2656e88c 100644--- a/.github/workflows/website.yml+++ b/.github/workflows/website.yml@@ -23,7 +23,6 @@ jobs:permissions:contents: readdeployments: write- pull-requests: write # For PR preview commentssteps:- uses: actions/checkout@v7@@ -42,13 +41,19 @@ jobs:run: pnpm website:build- name: Deploy- if: github.repository == vars.REPO_MAIN+ if: github.event_name != 'pull_request' && github.repository == vars.REPO_MAINuses: ./.github/actions/deploy-to-cloudflare-pageswith:project_name: "trilium-homepage"- comment_body: "π Website preview is ready"- production_url: "https://triliumnotes.org"deploy_dir: "apps/website/dist"cloudflare_api_token: ${{ secrets.CLOUDFLARE_API_TOKEN }}cloudflare_account_id: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}- github_token: ${{ secrets.GITHUB_TOKEN }}++ # A pull request from a fork reaches neither secrets nor vars, so the build+ # travels to deploy-previews.yml instead of deploying from here.+ - name: Upload preview+ if: github.event_name == 'pull_request'+ uses: ./.github/actions/upload-cloudflare-preview+ with:+ artifact_name: "website-preview"+ deploy_dir: "apps/website/dist"