Skip to content

Create pr-test-2

Create pr-test-2 #43

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]
env:
TARGET_PR_NUMBER: ${{ inputs.pr || github.event.pull_request.number || github.event.workflow_run.pull_requests[0].number }}
TARGET_RUN_ID: ${{ github.event.workflow_run.id }}
concurrency:
group: preview-${{ github.env.TARGET_PR_NUMBER }}
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: Fail if PR number is unknown
if: ${{ ! env.TARGET_PR_NUMBER }}
run: |
echo "PR number was not set"
cat $GITHUB_EVENT_PATH
exit 1
- name: Get latest run ID for CI workflow
if: ${{ ! env.TARGET_RUN_ID }}
uses: actions/github-script@v7
with:
result-encoding: string
script: |
const TARGET_PR_NUMBER = +process.env.TARGET_PR_NUMBER;
let workflowRun;
try {
if (github.eventName === 'workflow_run') {
workflowRun = github.event.workflow_run;
} else {
let pr;
if (context.eventName === 'pull_request_target') {
pr = context.payload.pull_request;
} else {
pr = (await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: TARGET_PR_NUMBER,
})).data;
}
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);
}
if (!workflowRun) {
console.log(`Could not find workflow run for PR ${TARGET_PR_NUMBER}`);
return;
}
if (workflowRun.conclusion !== 'success') {
console.log(`Found ${workflowRun.html_url}, but it did not succeed`);
return;
}
console.log(`Found ${workflowRun.html_url}`);
core.exportVariable('TARGET_RUN_ID', workflowRun.id);
- name: Download site build from PR
if: ${{ env.TARGET_RUN_ID }}
uses: actions/download-artifact@v4
with:
name: site
path: site
github-token: ${{ secrets.GITHUB_TOKEN }}
run-id: ${{ env.TARGET_RUN_ID }}
- name: Deploy
id: deploy
if: ${{ env.TARGET_RUN_ID }}
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: ${{ env.TARGET_PR_NUMBER }}