PR Preview Deploy #49
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: PR Preview Deploy | |
on: | |
workflow_run: | |
workflows: ["PR Preview Build"] | |
types: | |
- completed | |
jobs: | |
deploy-pr-preview: | |
runs-on: ubuntu-latest | |
if: > | |
github.event.workflow_run.event == 'pull_request' && | |
github.event.workflow_run.conclusion == 'success' | |
steps: | |
- name: Download build | |
uses: actions/github-script@v3 | |
with: | |
script: | | |
var artifacts = await github.actions.listWorkflowRunArtifacts({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
run_id: ${{github.event.workflow_run.id }}, | |
}); | |
var matchArtifact = artifacts.data.artifacts.filter((artifact) => { | |
return artifact.name == "build" | |
})[0]; | |
var download = await github.actions.downloadArtifact({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
artifact_id: matchArtifact.id, | |
archive_format: 'zip', | |
}); | |
var fs = require('fs'); | |
fs.writeFileSync('${{github.workspace}}/build.zip', Buffer.from(download.data)); | |
- name: Download PR number | |
uses: actions/github-script@v3 | |
with: | |
script: | | |
var artifacts = await github.actions.listWorkflowRunArtifacts({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
run_id: ${{github.event.workflow_run.id }}, | |
}); | |
var matchArtifact = artifacts.data.artifacts.filter((artifact) => { | |
return artifact.name == "pr" | |
})[0]; | |
var download = await github.actions.downloadArtifact({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
artifact_id: matchArtifact.id, | |
archive_format: 'zip', | |
}); | |
var fs = require('fs'); | |
fs.writeFileSync('${{github.workspace}}/pr.zip', Buffer.from(download.data)); | |
- run: unzip build.zip -d build/ | |
- run: unzip pr.zip | |
- name: Generate issue_number | |
uses: actions/github-script@v3 | |
id: issue_number | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
var fs = require('fs'); | |
return Number(fs.readFileSync('./NR')); | |
result-encoding: string | |
- name: Generate Surge URL | |
uses: actions/github-script@v3 | |
id: surge-url | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const issue_number = ${{steps.issue_number.outputs.result}}; | |
return `trustification.github.io-pr-${issue_number}-preview.surge.sh`; | |
result-encoding: string | |
- name: Install Surge | |
run: npm install -g surge | |
- name: Deploy to Surge | |
run: surge ./build/ "${{steps.surge-url.outputs.result}}" --token ${{ secrets.SURGE_TOKEN }} | |
- name: Post URL as PR comment | |
uses: mshick/add-pr-comment@v2 | |
with: | |
message: "🚀 Deployed Preview: http://${{steps.surge-url.outputs.result}} ✨" | |
repo-token: ${{ secrets.PREVIEW_BOT_TOKEN }} | |
issue: ${{steps.issue_number.outputs.result}} |