Skip to content

Commit 49a0aed

Browse files
authored
Fix deployment of docs to netlify (#34984)
Normally, on github, netlify takes care of the build + deploy fully automatically. However, our build takes to long so that we would hit the free build mins relatively quickly. So we continue deploying the docs via github actions. But PR workflows don't have access to secrets, so we need to split the upload of the docs in a new workflow, see https://securitylab.github.com/research/github-actions-preventing-pwn-requests/. Moreover, the netlify deploy cli doesn't take care of adding a PR comment and status check linking to the deployed website (netlify does this automatically if it would build + deploy the page). So we add this functionality manually.
1 parent 3a044ad commit 49a0aed

File tree

2 files changed

+102
-34
lines changed

2 files changed

+102
-34
lines changed

.github/workflows/doc-build.yml

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@ jobs:
1515
build-docs:
1616
runs-on: ubuntu-latest
1717
container: ghcr.io/sagemath/sage/sage-docker-ubuntu-focal-standard-with-targets:dev
18-
env:
19-
CAN_DEPLOY: ${{ secrets.NETLIFY_AUTH_TOKEN != '' }}
2018
steps:
2119
- name: Checkout
2220
uses: actions/checkout@v3
2321

2422
- name: Prepare
2523
run: |
24+
apt-get update && apt-get install -y zip
2625
# Reuse built SAGE_LOCAL contained in the Docker image
2726
./bootstrap
2827
./configure --enable-build-as-root --prefix=/sage/local --with-sage-venv --enable-download-from-upstream-url
@@ -34,43 +33,17 @@ jobs:
3433
SAGE_NUM_THREADS: 2
3534

3635
- name: Copy docs
37-
if: env.CAN_DEPLOY == 'true'
3836
run: |
3937
# For some reason the deploy step below cannot find /sage/...
4038
# So copy everything from there to local folder
4139
# We also need to replace the symlinks because netlify is not following them
4240
mkdir -p ./docs
4341
cp -r -L /sage/local/share/doc/sage/html/en/* ./docs
42+
# Zip everything for increased performance
43+
zip -r docs.zip docs
4444
45-
- name: Deploy to Netlify preview
46-
id: preview-netlify
47-
if: env.CAN_DEPLOY == 'true' && github.ref != 'refs/heads/develop'
48-
uses: netlify/actions/cli@master
45+
- name: Upload docs
46+
uses: actions/upload-artifact@v3
4947
with:
50-
args: deploy --dir=docs --alias="${NETLIFY_ALIAS}"
51-
env:
52-
# Set deployment url to commit hash to easily link from the trac.
53-
# We could also set NETLIFY_ALIAS to the branch name.
54-
# However, netlify currently doesn't support updates to a deployment with the same alias
55-
# https://github.com/netlify/cli/issues/948
56-
# https://github.com/netlify/cli/issues/1984
57-
# Note that even if this feature is implemented, one would also need to first process the branch name
58-
# to workaround the bug https://github.com/netlify/cli/issues/969.
59-
NETLIFY_ALIAS: ${{ github.sha }}
60-
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
61-
NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
62-
63-
- name: Deploy to Netlify production
64-
id: deploy-netlify
65-
if: env.CAN_DEPLOY == 'true' && github.ref == 'refs/heads/develop'
66-
uses: netlify/actions/cli@master
67-
with:
68-
args: deploy --dir=docs --prod
69-
env:
70-
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
71-
NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
72-
73-
- name: Report deployment url
74-
if: env.CAN_DEPLOY == 'true'
75-
run: |
76-
echo "::notice::The documentation has being automatically deployed to Netlify. %0A ✅ Preview: ${{ steps.preview-netlify.outputs.NETLIFY_URL || steps.deploy-netlify.outputs.NETLIFY_URL }}"
48+
name: docs
49+
path: docs.zip

.github/workflows/doc-publish.yml

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Triggers after the documentation build has finished,
2+
# taking the artifact and uploading it to netlify
3+
name: Publish documentation
4+
5+
on:
6+
workflow_run:
7+
workflows: ["Build documentation"]
8+
types:
9+
- completed
10+
11+
permissions:
12+
statuses: write
13+
checks: write
14+
pull-requests: write
15+
16+
jobs:
17+
upload-docs:
18+
runs-on: ubuntu-latest
19+
if: github.event.workflow_run.conclusion == 'success'
20+
steps:
21+
- name: Get information about workflow origin
22+
uses: potiuk/get-workflow-origin@v1_5
23+
id: source-run-info
24+
with:
25+
token: ${{ secrets.GITHUB_TOKEN }}
26+
sourceRunId: ${{ github.event.workflow_run.id }}
27+
28+
# Once https://github.com/actions/download-artifact/issues/172 and/or https://github.com/actions/download-artifact/issues/60 is implemented, we can use the official download-artifact action
29+
# For now use the solution from https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#using-data-from-the-triggering-workflow
30+
- name: Download docs
31+
uses: actions/[email protected]
32+
with:
33+
script: |
34+
var artifacts = await github.actions.listWorkflowRunArtifacts({
35+
owner: context.repo.owner,
36+
repo: context.repo.repo,
37+
run_id: ${{github.event.workflow_run.id }},
38+
});
39+
var matchArtifact = artifacts.data.artifacts.filter((artifact) => {
40+
return artifact.name == "docs"
41+
})[0];
42+
var download = await github.actions.downloadArtifact({
43+
owner: context.repo.owner,
44+
repo: context.repo.repo,
45+
artifact_id: matchArtifact.id,
46+
archive_format: 'zip',
47+
});
48+
var fs = require('fs');
49+
fs.writeFileSync('${{github.workspace}}/docs.zip', Buffer.from(download.data));
50+
51+
- name: Extract docs
52+
run: unzip docs.zip -d docs && unzip docs/docs.zip -d docs/docs
53+
54+
- name: Deploy to Netlify
55+
id: deploy-netlify
56+
uses: netlify/actions/cli@master
57+
with:
58+
args: deploy --dir=docs/docs/docs ${NETLIFY_PRODUCTION:+"--prod"} --message ${NETLIFY_MESSAGE} --alias ${NETLIFY_ALIAS}
59+
env:
60+
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
61+
NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
62+
NETLIFY_PRODUCTION: ${{ github.ref == 'refs/heads/develop' }}
63+
NETLIFY_MESSAGE: ${{ steps.source-run-info.outputs.pullRequestNumber }}
64+
NETLIFY_ALIAS: deploy-preview-${{ steps.source-run-info.outputs.pullRequestNumber }}
65+
66+
# Add deployment as status check, PR comment and annotation
67+
# we could use the nwtgck/actions-netlify action for that, except for that it is not (yet) working in workflow_run context: https://github.com/nwtgck/actions-netlify/issues/545
68+
- name: Add/Update deployment status PR comment
69+
uses: marocchino/sticky-pull-request-comment@v2
70+
with:
71+
number: ${{ steps.source-run-info.outputs.pullRequestNumber }}
72+
header: preview-comment
73+
recreate: true
74+
message: |
75+
[Documentation preview for this PR](${{ steps.deploy-netlify.outputs.NETLIFY_URL }}) is ready! :tada:
76+
Built with commit: ${{ steps.source-run-info.outputs.sourceHeadSha }}
77+
78+
- name: Update deployment status PR check
79+
uses: myrotvorets/[email protected]
80+
if: ${{ always() }}
81+
env:
82+
DEPLOY_SUCCESS: Successfully deployed preview.
83+
DEPLOY_FAILURE: Failed to deploy preview.
84+
with:
85+
token: ${{ secrets.GITHUB_TOKEN }}
86+
status: ${{ job.status == 'success' && 'success' || 'failure' }}
87+
sha: ${{ github.event.workflow_run.head_sha }}
88+
context: Deploy Documentation
89+
targetUrl: ${{ steps.deploy-netlify.outputs.NETLIFY_URL }}
90+
description: ${{ job.status == 'success' && env.DEPLOY_SUCCESS || env.DEPLOY_FAILURE }}
91+
92+
- name: Report deployment url
93+
run: |
94+
echo "::notice::The documentation has being automatically deployed to Netlify. %0A ✅ Preview: ${{ steps.deploy-netlify.outputs.NETLIFY_URL }}"
95+

0 commit comments

Comments
 (0)