Documentation Preview #106
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: Documentation Preview | |
on: | |
workflow_run: | |
workflows: [Build] | |
types: [completed] | |
concurrency: | |
group: ${{ github.event.workflow_run.head_branch }} | |
jobs: | |
documentation-preview: | |
runs-on: ubuntu-latest | |
permissions: | |
pull-requests: write | |
actions: read | |
steps: | |
- name: Get PR context | |
id: pr-context | |
env: | |
# Token required for GH CLI: | |
GH_TOKEN: ${{ github.token }} | |
# Best practice for scripts is to reference via ENV at runtime. Avoid using the expression syntax in the script content directly: | |
PR_TARGET_REPO: ${{ github.repository }} | |
# If the PR is from a fork, prefix it with `<owner-login>:`, otherwise only the PR branch name is relevant: | |
PR_BRANCH: |- | |
${{ | |
(github.event.workflow_run.head_repository.owner.login != github.event.workflow_run.repository.owner.login) | |
&& format('{0}:{1}', github.event.workflow_run.head_repository.owner.login, github.event.workflow_run.head_branch) | |
|| github.event.workflow_run.head_branch | |
}} | |
# Query the PR number by repo + branch, then assign to step output: | |
run: | | |
gh pr view --repo "${PR_TARGET_REPO}" "${PR_BRANCH}" \ | |
--json 'number' --jq '"number=\(.number)"' \ | |
>> "${GITHUB_OUTPUT}" | |
- name: Download Documentation | |
uses: actions/download-artifact@v4 | |
with: | |
name: Documentation Artifacts | |
github-token: ${{ github.token }} | |
run-id: ${{ github.event.workflow_run.id }} | |
- name: Deploy Documentation Preview | |
uses: appleboy/[email protected] | |
with: | |
username: ${{ secrets.SSH_USERNAME }} | |
host: ${{ secrets.SSH_HOST }} | |
port: ${{ secrets.SSH_PORT }} | |
key: ${{ secrets.SSH_KEY }} | |
rm: true | |
source: . | |
target: ~/NetCord/preview/html/${{ steps.pr-context.outputs.number }} | |
- name: Notify Documentation Preview | |
uses: actions/github-script@v7 | |
env: | |
PR_NUMBER: ${{ steps.pr-context.outputs.number }} | |
with: | |
script: | | |
const number = process.env.PR_NUMBER; | |
const { data: comments } = await github.rest.issues.listComments({ | |
issue_number: number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
}); | |
const body = `The documentation preview is available at https://preview.netcord.dev/${number}.`; | |
const duplicateComment = comments.find(comment => comment.user.id === 41898282 && comment.body === body); | |
if (!duplicateComment) { | |
await github.rest.issues.createComment({ | |
issue_number: number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: body, | |
}); | |
} |