Skip to content

Deploy preview environment #553

Deploy preview environment

Deploy preview environment #553

name: Deploy preview environment
on:
workflow_run:
workflows: [CI]
types: [completed]
workflow_dispatch:
inputs:
pr:
required: true
type: string
description: PR number
pull_request_target:
types: [labeled]
jobs:
deploy:
if: github.event_name != 'workflow_run' || github.event.workflow_run.conclusion == 'success'
runs-on: ubuntu-latest
steps:
- name: Download site build
if: github.event_name == 'workflow_run'
uses: actions/github-script@v6
id: download-from-workflow-run
with:
result-encoding: string
script: |
const allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: context.payload.workflow_run.id,
});
const artifact = allArtifacts.data.artifacts.find(artifact => {
return artifact.name.startsWith('site-');
});
if (!artifact) {
console.log('No site artifact found');
return;
}
const prNumber = artifact.name.split('-')[1];
console.log('Detected PR number from artifact: ', prNumber);
// Check if the PR has the "deploy-preview" label
const pr = (await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
})).data;
if (!pr.labels.some(label => label.name === 'deploy-preview')) {
console.log('PR does not have the "deploy-preview" label');
return;
}
const download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: artifact.id,
archive_format: 'zip',
});
const fs = require('fs');
fs.writeFileSync('site.zip', Buffer.from(download.data));
return prNumber;
- name: Download site build
if: github.event_name == 'pull_request_target' || github.event_name == 'workflow_dispatch'
uses: actions/github-script@v6
id: download-from-pr
with:
result-encoding: string
script: |
const prNumber = context.eventName === 'pull_request_target'
? context.payload.pull_request.number
: context.payload.inputs.pr;
if (context.eventName === 'pull_request_target' && context.payload.label.name !== 'deploy-preview') {
console.log('Label is not "deploy-preview"');
return;
}
let pr;
if (context.eventName === 'workflow_dispatch') {
pr = (await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
})).data;
} else {
pr = context.payload.pull_request;
}
// https://github.com/orgs/community/discussions/24709
const workflowRun = (await github.rest.actions.listWorkflowRuns({
owner: context.repo.owner,
repo: context.repo.repo,
workflow_id: 'CI.yml',
branch: pr.head.ref,
event: 'pull_request',
status: 'success',
per_page: 1,
})).data.workflow_runs[0];
if (!workflowRun) {
console.log('No matching workflow run found from branch ', pr.head.ref);
return;
}
const allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: workflowRun.id,
});
const artifact = allArtifacts.data.artifacts.find(artifact => {
return artifact.name.startsWith('site-');
});
if (!artifact) {
console.log('No site artifact found');
return;
}
const download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: artifact.id,
archive_format: 'zip',
});
const fs = require('fs');
fs.writeFileSync('site.zip', Buffer.from(download.data));
return prNumber;
- name: Unzip site build
if: steps.download-from-workflow-run.outputs.result || steps.download-from-pr.outputs.result
run: unzip site.zip -d site
- run: ls -R
if: steps.download-from-workflow-run.outputs.result || steps.download-from-pr.outputs.result
working-directory: site
- name: Deploy
id: deploy
if: steps.download-from-workflow-run.outputs.result || steps.download-from-pr.outputs.result
uses: Azure/static-web-apps-deploy@v1
with:
azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN_PROD }}
repo_token: ${{ secrets.GITHUB_TOKEN }}
action: "upload"
app_location: "site"
skip_app_build: true
production_branch: v2
deployment_environment: ${{ steps.download-from-workflow-run.outputs.result || steps.download-from-pr.outputs.result }}
- name: Comment on PR
# azure/static-web-apps-deploy seems to comment itself when the event is pull_request_target
if: github.event_name != 'pull_request_target' && (steps.download-from-workflow-run.outputs.result || steps.download-from-pr.outputs.result)
uses: actions/github-script@v6
env:
PR_NUMBER: ${{ steps.download-from-workflow-run.outputs.result || steps.download-from-pr.outputs.result }}
SITE_URL: ${{ steps.deploy.outputs.static_web_app_url }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: process.env.PR_NUMBER,
body: `Azure Static Web Apps: Your stage site is ready! Visit it here: ${process.env.SITE_URL}`
});