Deploy PR Storybooks #3202
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: Deploy PR Storybooks | |
on: | |
workflow_run: | |
workflows: ['Build PR Storybooks'] | |
types: | |
- completed | |
jobs: | |
upload: | |
runs-on: ubuntu-latest | |
if: > | |
github.event.workflow_run.event == 'pull_request' && | |
github.event.workflow_run.conclusion == 'success' | |
steps: | |
- name: Checkout 🛎️ | |
uses: actions/checkout@v3 | |
- name: Get token | |
id: get_token | |
uses: actions/create-github-app-token@v1 | |
with: | |
app-id: ${{ secrets.GREEN_BOT_APP_ID }} | |
private-key: ${{ secrets.GREEN_BOT_PRIVATE_KEY }} | |
- name: 'Download artifact' | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
var artifacts = await github.rest.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.rest.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 pr.zip -d artifact | |
- name: Get PR number | |
id: pr_number | |
uses: actions/github-script@v3 | |
with: | |
script: | | |
var fs = require('fs'); | |
return Number(fs.readFileSync('./artifact/PR_NUMBER')); | |
- name: Deploy built storybooks to GitHub Pages | |
uses: JamesIves/github-pages-deploy-action@v4.5.0 | |
with: | |
token: ${{ steps.get_token.outputs.token }} | |
branch: gh-pages # The branch the action should deploy to. | |
folder: artifact # The folder the action should deploy. | |
target-folder: pr-${{ steps.pr_number.outputs.result }} # The folder the action should deploy to. | |
- name: Add Storybook link to pull request | |
uses: actions/github-script@v5 | |
id: comment | |
with: | |
github-token: ${{ steps.get_token.outputs.token }} | |
script: | | |
// Get list of folders | |
const updatedStorybooks = require('fs').readdirSync('./artifact') | |
.filter((file) => require('fs').statSync(`./artifact/${file}`).isDirectory()); | |
// Create link list | |
const links = updatedStorybooks | |
.map((storybook) => | |
`- [${storybook}](https://storybook.seb.io/pr-${{ steps.pr_number.outputs.result }}/${storybook}/)`) | |
.join('\n'); | |
// Create comment body | |
return `👋 Thanks for creating a pull request! | |
🚀 Checkout the ${updatedStorybooks.length > 1 ? 'storybooks' : 'storybook'} we've created for it: | |
${links}`; | |
- name: Comment on PR | |
uses: mshick/add-pr-comment@v2 | |
with: | |
repo-token: ${{ steps.get_token.outputs.token }} | |
issue: ${{ steps.pr_number.outputs.result }} | |
message: ${{ fromJSON(steps.comment.outputs.result) }} | |
message-id: storybook-comment |