Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 31 additions & 9 deletions .github/workflows/preview-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ on:
workflow_run:
workflows: ["Preview Build"]
types: [completed]
pull_request_target:
branches: [ main ]
types: [closed]

permissions:
actions: read
Expand All @@ -27,23 +24,48 @@ jobs:
steps:
- uses: actions/checkout@v4

- name: Extract PR number from workflow_run payload
# workflow_run.pull_requests may be empty for fork PRs.
- name: Resolve PR number from preview build
id: pr
env:
GH_TOKEN: ${{ github.token }}
SOURCE_REPOSITORY: ${{ github.event.workflow_run.head_repository.full_name }}
SOURCE_BRANCH: ${{ github.event.workflow_run.head_branch }}
SOURCE_SHA: ${{ github.event.workflow_run.head_sha }}
run: |
pr_number=$(jq -r '.workflow_run.pull_requests[0].number // empty' "$GITHUB_EVENT_PATH")
echo "number=$pr_number" >> "$GITHUB_OUTPUT"
set -euo pipefail
ref="$SOURCE_BRANCH"
if [[ "$SOURCE_REPOSITORY" != "$GITHUB_REPOSITORY" ]]; then
ref="${SOURCE_REPOSITORY%%/*}:$SOURCE_BRANCH"
fi
gh pr view "$ref" -R "$GITHUB_REPOSITORY" \
--json number,state,headRefOid,headRepository,baseRefName |
jq -er --arg sha "$SOURCE_SHA" --arg repo "$SOURCE_REPOSITORY" \
'select(.state == "OPEN" and .baseRefName == "main" and
.headRefOid == $sha and .headRepository.nameWithOwner == $repo) |
"number=\(.number)"' >> "$GITHUB_OUTPUT"

- name: Download preview build artifact
if: steps.pr.outputs.number != ''
uses: actions/download-artifact@v4
with:
name: preview-build
path: build_production/
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ secrets.GITHUB_TOKEN }}

# Installation token can only write contents in the preview repository.
# A dedicated GitHub App must be installed only on site-preview.
- name: Get scoped publication token
id: publication-token
uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0
with:
client-id: ${{ vars.SITE_PREVIEW_APP_CLIENT_ID }}
private-key: ${{ secrets.SITE_PREVIEW_APP_PRIVATE_KEY }}
owner: LibreCodeCoop
repositories: site-preview
permission-contents: write

- name: Deploy preview
if: steps.pr.outputs.number != ''
uses: LibreSign/pr-preview-action@2a6da1177b47dc23173ba9a75e01da0a0d800978
with:
action: deploy
Expand All @@ -53,4 +75,4 @@ jobs:
deploy-repository: LibreCodeCoop/site-preview
preview-branch: main
wait-for-pages-deployment: false
token: ${{ secrets.PREVIEW_TOKEN }}
token: ${{ steps.publication-token.outputs.token }}
Loading