Skip to content

Docs Preview

Docs Preview #80

Workflow file for this run

name: Docs Preview
on:
workflow_run:
workflows:
- CI
types:
- completed
branches-ignore:
- master
permissions:
actions: read
contents: write
issues: write
pull-requests: write
jobs:
comment:
if: github.event.workflow_run.event == 'pull_request'
name: Deploy documentation preview
runs-on: ubuntu-latest
concurrency:
group: docs-gh-pages
cancel-in-progress: false
steps:
- name: Find pull request
id: pr
uses: actions/github-script@v7
with:
script: |
const pullRequests = context.payload.workflow_run.pull_requests;
if (!pullRequests || pullRequests.length !== 1) {
core.info(`Expected one pull request, found ${pullRequests?.length ?? 0}.`);
return;
}
const pr = pullRequests[0];
const { data } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pr.number,
});
if (data.state !== 'open') {
core.info('Pull request is not open.');
return;
}
if (data.head.repo.full_name !== `${context.repo.owner}/${context.repo.repo}`) {
core.info('Pull request is not from this repository.');
return;
}
if (!data.labels.some(label => label.name === 'documentation')) {
core.info('Pull request does not have the documentation label.');
return;
}
if (data.head.sha !== context.payload.workflow_run.head_sha) {
core.info('Workflow run is not for the current pull request head.');
return;
}
const jobs = await github.paginate(github.rest.actions.listJobsForWorkflowRun, {
owner: context.repo.owner,
repo: context.repo.repo,
run_id: context.payload.workflow_run.id,
per_page: 100,
});
const docsJob = jobs.find(job => job.name === 'Documentation');
if (docsJob?.conclusion !== 'success') {
core.info(`Documentation job conclusion: ${docsJob?.conclusion ?? 'missing'}.`);
return;
}
core.setOutput('number', pr.number);
- uses: actions/checkout@v6
if: steps.pr.outputs.number != ''
with:
ref: refs/pull/${{ steps.pr.outputs.number }}/merge
fetch-depth: 0
- uses: julia-actions/setup-julia@v2
if: steps.pr.outputs.number != ''
with:
version: '1'
- name: "Documentation dev environment setup"
if: steps.pr.outputs.number != ''
run: |
julia --project=docs -e '
using Pkg
Pkg.develop([
PackageSpec(path=pwd(), subdir="."),
PackageSpec(path=pwd(), subdir="KomaMRIBase"),
PackageSpec(path=pwd(), subdir="KomaMRICore"),
PackageSpec(path=pwd(), subdir="KomaMRIFiles"),
PackageSpec(path=pwd(), subdir="KomaMRIPlots")
])
Pkg.instantiate()'
- name: Build and deploy preview
if: steps.pr.outputs.number != ''
run: |
GITHUB_EVENT_NAME=pull_request \
GITHUB_REF=refs/pull/${{ steps.pr.outputs.number }}/merge \
julia --project=docs docs/make.jl push_preview
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
DOCUMENTER_KEY: ${{ secrets.DOCUMENTER_KEY }}
- name: Comment preview URL
if: steps.pr.outputs.number != ''
uses: actions/github-script@v7
with:
script: |
const prNumber = Number('${{ steps.pr.outputs.number }}');
const previewUrl = `https://juliahealth.github.io/KomaMRI.jl/previews/PR${prNumber}/`;
const marker = '<!-- komamri-docs-preview-link -->';
const previewSha = context.payload.workflow_run.head_sha.slice(0, 7);
const runUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`;
const body = `${marker}\nPreview available: ${previewUrl}\n\nLast preview build: ${previewSha}, [Docs Preview run ${context.runId}](${runUrl})`;
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
per_page: 100,
});
const existing = comments.find(comment =>
comment.user?.type === 'Bot' && comment.body?.includes(marker)
);
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body,
});
}