Skip to content

Deploy preview

Deploy preview #38

name: Deploy preview
on:
workflow_dispatch:
inputs:
pr:
required: true
type: string
description: PR number
workflow_run:
workflows: [ci]
types: [completed]
pull_request_target:
types: [labeled]
concurrency:
group: preview
jobs:
deploy:
if: ${{ github.event_name == 'workflow_dispatch' || (github.event_name == 'workflow_run' && github.event.workflow_run.conclusion == 'success') || (github.event_name == 'pull_request_target' && github.event.label.name == 'deploy-preview') }}
runs-on: ubuntu-latest
steps:
- name: Get PR/workflow run info
id: get-info
uses: actions/github-script@v7
with:
script: |
let pr;
let workflowRun;
let isLabel = false;
switch (context.eventName) {
case "workflow_dispatch":
pr = (await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: +context.payload.inputs.pr,
})).data;
break;
case "pull_request_target":
pr = context.payload.pull_request;
break;
case "workflow_run":
workflowRun = context.payload.workflow_run;
pr = workflowRun.pull_requests[0];
if (pr) {
// Reload the PR to get the labels.
pr = (await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pr.number,
})).data;
} else {
// PRs sent from forks do not get the pull_requests field set.
// https://github.com/orgs/community/discussions/25220
prNumber = (await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
head: context.payload.workflow_run.head_sha,
})).data[0];
}
}
console.log("pr", pr);
if (!pr) {
throw new Error("Could not find PR");
}
if (!pr.labels.some((label) => label.name === "deploy-preview")) {
console.log(`PR ${pr.number} does not have the deploy-preview label`);
return null;
}
if (!workflowRun) {
try {
workflowRun = (await github.rest.actions.listWorkflowRuns({
owner: context.repo.owner,
repo: context.repo.repo,
workflow_id: "ci.yml",
head_sha: pr.head.sha,
})).data.workflow_runs[0];
} catch (e) {
console.log(e);
}
}
console.log("workflowRun", workflowRun);
if (!workflowRun) {
console.log(`Could not find workflow run for PR ${pr.number}`);
return null;
}
const result = { pr: pr.number, runId: workflowRun.id };
console.log(result);
return result;
- name: Download site build from PR
if: ${{ steps.get-info.outputs.result != 'null' }}
uses: actions/download-artifact@v4
with:
name: site
path: site
github-token: ${{ secrets.GITHUB_TOKEN }}
run-id: ${{ fromJson(steps.get-info.outputs.result).runId }}
- name: Deploy
id: deploy
if: ${{ steps.get-info.outputs.result != 'null' }}
run: |
echo deploying site
ls -R site
echo azure_static_web_apps_api_token=$azure_static_web_apps_api_token
echo repo_token=$repo_token
echo action=$action
echo app_location=$app_location
echo skip_app_build=$skip_app_build
echo production_branch=$production_branch
echo deployment_environment=$deployment_environment
env:
azure_static_web_apps_api_token: AZURE_STATIC_WEB_APPS_API_TOKEN_PROD
repo_token: GITHUB_TOKEN
action: 'upload'
app_location: 'site'
skip_app_build: true
production_branch: v2
deployment_environment: ${{ fromJson(steps.get-info.outputs.result).pr }}