Publish PR Preview #141
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
# Making changes? https://github.com/nektos/act may help you test locally | |
# The logic for working with GH Actions is mostly based on https://github.com/ant-design/ant-design-pro/blob/master/.github/workflows/preview-deploy.yml | |
name: "Publish PR Preview" | |
on: | |
workflow_run: | |
workflows: | |
- "Build PR to Artifact" | |
types: | |
- "completed" | |
concurrency: | |
group: "${{ github.workflow }}" | |
cancel-in-progress: false | |
jobs: | |
docs: | |
runs-on: "ubuntu-latest" | |
if: "github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success'" | |
steps: | |
- uses: "actions/checkout@v3" # for pages publishing | |
- name: "download PR" | |
uses: "dawidd6/action-download-artifact@v2" | |
with: | |
workflow: "${{ github.event.workflow_run.workflow_id }}" | |
run_id: "${{ github.event.workflow_run.id }}" | |
name: "pr-num" | |
path: "." | |
- name: "download dist artifact" | |
uses: "dawidd6/action-download-artifact@v2" | |
with: | |
workflow: "${{ github.event.workflow_run.workflow_id }}" | |
run_id: "${{ github.event.workflow_run.id }}" | |
name: "docs-html" | |
path: "build/html" | |
- name: "generate data blob" | |
id: "data" | |
uses: "actions/github-script@v6" | |
with: | |
script: | | |
let fs = require("fs/promises"); | |
let dirPromise = fs.mkdir('build/html/', {recursive: true}); | |
let prNum = (await fs.readFile('pr-num.txt', {encoding: 'UTF-8'})).trim(); | |
let time = new Date().toISOString(); | |
let data = { | |
pr: prNum, | |
time: time | |
}; | |
await dirPromise; | |
await fs.writeFile('build/html/_preview_data.json', JSON.stringify(data)); | |
return data | |
- name: "publish to pages" | |
id: "publish" | |
uses: "JamesIves/[email protected]" | |
with: | |
branch: "main" | |
folder: "build/html" | |
ssh-key: "${{ secrets.DOCS_PREVIEW_DEPLOY_KEY }}" | |
repository-name: "KyoriPowered/adventure-docs-previews" | |
target-folder: "pull/${{ fromJSON(steps.data.outputs.result).pr }}" | |
clean: false | |
single-commit: true | |
force: true | |
- name: "notify" | |
if: "${{ steps.publish.outputs.deployment-status == 'success' }}" | |
uses: "actions/github-script@v6" | |
env: | |
PR: "${{ fromJSON(steps.data.outputs.result).pr }}" | |
COMMIT: "${{ github.event.workflow_run.head_sha }}" | |
with: | |
script: | | |
const { PR, COMMIT } = process.env | |
// Format body | |
const header = "Your pull request has been made available for preview at:" | |
const body = `${header} | |
https://kyoripowered.github.io/adventure-docs-previews/pull/${PR}/ | |
<sub>Last deployed: ${COMMIT}. This deployment will expire after 90 days.</sub> | |
` | |
// Check for existing comment | |
const repo = context.repo; | |
const comments = await github.rest.issues.listComments({ | |
owner: repo.owner, | |
repo: repo.repo, | |
issue_number: Number(PR) | |
}) | |
let commentId = -1; | |
for (const comment of comments.data) { | |
if (comment.body.startsWith(header) && comment.user.login == 'github-actions[bot]' && comment.user.type == 'Bot') { | |
commentId = comment.id; | |
break; | |
} | |
} | |
// If comment exists: edit it, otherwise create | |
if (commentId != -1) { | |
await github.rest.issues.updateComment({ | |
owner: repo.owner, | |
repo: repo.repo, | |
comment_id: commentId, | |
body: body | |
}); | |
} else { | |
await github.rest.issues.createComment({ | |
owner: repo.owner, | |
repo: repo.repo, | |
issue_number: Number(PR), | |
body: body | |
}); | |
} | |