Skip to content

Deploy PR previews

Deploy PR previews #11

Workflow file for this run

# .github/workflows/preview.yml
name: Deploy PR previews
# on:
# pull_request:
# types:
# - opened
# - reopened
# - synchronize
# - closed
on:
workflow_run:
workflows: ["Coverage"]
types:
- completed
permissions:
pull-requests: write
contents: write
concurrency: preview-${{ github.ref }}
jobs:
deploy-preview:
runs-on: ubuntu-20.04
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Install and Build
run: |
yarn install
yarn run storybook:build
- name: Download PR number artifact
uses: actions/github-script@v6
with:
script: |
let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: context.payload.workflow_run.id,
});
let matchArtifact = allArtifacts.data.artifacts.filter((artifact) => {
return artifact.name == "pr-number"
})[0];
let download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: matchArtifact.id,
archive_format: 'zip',
});
let fs = require('fs');
fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/pr-number.zip`, Buffer.from(download.data));
- name: Unzip PR number artifact
run: unzip $GITHUB_WORKSPACE/pr-number.zip -d $GITHUB_WORKSPACE
- name: Read PR number
id: pr
run: echo "::set-output name=number::$(cat pr_number.txt)"
- name: Set PR Number as pr=$pr
run: echo "pr=${{ steps.pr.outputs.number }}" >> $GITHUB_ENV
- name: Use PR Number
run: |
echo "The PR number is ${{ steps.pr.outputs.number }}"
- name: Deploy preview
id: deploy-preview
uses: rossjrw/pr-preview-action@v1
with:
source-dir: ./storybook-static/
preview-branch: preview
custom-url: dotting-preview.netlify.app
# umbrella-dir: docs/pr-preview
token: ${{ secrets.GITHUB_TOKEN }}
action: deploy # This prevents netlify from auto deleting url
env:
pr: ${{ steps.pr.outputs.number }}
targetdir: pr-preview/pr-${{ steps.pr.outputs.number }}
- name: Echo preview URL
run: |
echo "Preview URL: ${{ steps.deploy-preview.outputs.deployment-url }}"
- name: Post PR comment
uses: actions/github-script@v5
with:
script: |
const url = "${{ steps.deploy-preview.outputs.deployment-url }}";
if (url) {
github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: "${{ steps.pr.outputs.number }}",
body: "Preview URL: ${{ steps.deploy-preview.outputs.deployment-url }}"
});
} else {
core.setFailed('No preview URL found');
}