Skip to content

Publish PR Preview #174

Publish PR Preview

Publish PR Preview #174

Workflow file for this run

# 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@v4" # for pages publishing
- name: "download PR"
uses: "dawidd6/action-download-artifact@v3"
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@v3"
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@v7"
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/github-pages-deploy-action@v4.5.0"
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@v7"
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
});
}