|
4 | 4 | branches: [ master, development ] |
5 | 5 | pull_request: |
6 | 6 | branches: [ master, development ] |
| 7 | + types: [ opened, synchronize, reopened, closed ] |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: write |
| 11 | + pull-requests: write |
7 | 12 |
|
8 | 13 | env: |
9 | 14 | REPO_METADATA_SHAS: repo-metadata-shas.txt |
10 | 15 |
|
11 | 16 | jobs: |
12 | 17 | build: |
| 18 | + if: github.event.action != 'closed' |
13 | 19 | runs-on: ubuntu-latest |
14 | 20 | steps: |
15 | 21 | - uses: actions/checkout@v4 |
|
21 | 27 | timeout_minutes: 15 |
22 | 28 | command: | |
23 | 29 | sudo apt-get update |
24 | | - sudo apt-get install -yqq build-essential libxml2-dev zlib1g-dev bison flex |
| 30 | + sudo apt-get install -yqq build-essential libxml2-dev zlib1g-dev bison flex libcairo2-dev pkg-config |
25 | 31 |
|
26 | 32 | - name: Set up Python install env |
27 | 33 | run: | |
|
51 | 57 | export GITHUB_TOKEN=${{ github.token }} |
52 | 58 | make build |
53 | 59 |
|
| 60 | + - name: Deploy branch preview |
| 61 | + if: github.event_name == 'pull_request' |
| 62 | + run: | |
| 63 | + BRANCH_NAME="${{ github.head_ref }}" |
| 64 | + SAFE_BRANCH=$(echo "$BRANCH_NAME" | sed 's/[^a-zA-Z0-9-]/-/g') |
| 65 | +
|
| 66 | + . venv/bin/activate |
| 67 | +
|
| 68 | + # Build with site_url set so navigation works in subdirectory |
| 69 | + cat > mkdocs-preview.yml << EOF |
| 70 | + INHERIT: mkdocs.yml |
| 71 | + site_url: https://docs.openworm.org/preview/$SAFE_BRANCH/ |
| 72 | + EOF |
| 73 | + python -m mkdocs build -f mkdocs-preview.yml --site-dir /tmp/site_preview |
| 74 | +
|
| 75 | + # Push to gh-pages under preview/<branch>/ |
| 76 | + git fetch origin gh-pages:gh-pages |
| 77 | + git config user.name github-actions |
| 78 | + git config user.email github-actions@github.com |
| 79 | + git checkout gh-pages |
| 80 | +
|
| 81 | + mkdir -p preview/$SAFE_BRANCH |
| 82 | + rm -rf preview/$SAFE_BRANCH/* |
| 83 | + cp -r /tmp/site_preview/* preview/$SAFE_BRANCH/ |
| 84 | +
|
| 85 | + git add preview/$SAFE_BRANCH |
| 86 | + git commit -m "Deploy preview: $BRANCH_NAME @ ${GITHUB_SHA::7} [ci skip]" || echo "No changes to commit" |
| 87 | + git remote set-url origin https://${{ github.token }}@github.com/${{ github.repository_owner }}/openworm_docs.git |
| 88 | + git push origin gh-pages |
| 89 | +
|
| 90 | + - name: Comment preview URL on PR |
| 91 | + if: github.event_name == 'pull_request' |
| 92 | + uses: actions/github-script@v7 |
| 93 | + with: |
| 94 | + script: | |
| 95 | + const branch = '${{ github.head_ref }}'.replace(/[^a-zA-Z0-9-]/g, '-'); |
| 96 | + const url = `https://docs.openworm.org/preview/${branch}/`; |
| 97 | + const marker = '<!-- preview-url -->'; |
| 98 | + const body = `${marker}\n**Preview:** ${url}`; |
| 99 | +
|
| 100 | + const { data: comments } = await github.rest.issues.listComments({ |
| 101 | + owner: context.repo.owner, |
| 102 | + repo: context.repo.repo, |
| 103 | + issue_number: context.issue.number, |
| 104 | + }); |
| 105 | + const existing = comments.find(c => c.body.includes(marker)); |
| 106 | + if (existing) { |
| 107 | + await github.rest.issues.updateComment({ |
| 108 | + owner: context.repo.owner, |
| 109 | + repo: context.repo.repo, |
| 110 | + comment_id: existing.id, |
| 111 | + body, |
| 112 | + }); |
| 113 | + } else { |
| 114 | + await github.rest.issues.createComment({ |
| 115 | + owner: context.repo.owner, |
| 116 | + repo: context.repo.repo, |
| 117 | + issue_number: context.issue.number, |
| 118 | + body, |
| 119 | + }); |
| 120 | + } |
| 121 | +
|
54 | 122 | - name: Archive production artifacts |
55 | 123 | if: github.ref == 'refs/heads/master' |
56 | 124 | uses: actions/upload-artifact@v4 |
@@ -87,3 +155,26 @@ jobs: |
87 | 155 | git remote set-url origin https://${{ github.token }}@github.com/${{ github.repository_owner }}/openworm_docs.git |
88 | 156 | git push -f origin gh-pages |
89 | 157 | fi |
| 158 | +
|
| 159 | + cleanup-preview: |
| 160 | + if: github.event_name == 'pull_request' && github.event.action == 'closed' |
| 161 | + runs-on: ubuntu-latest |
| 162 | + steps: |
| 163 | + - uses: actions/checkout@v4 |
| 164 | + - name: Remove preview from gh-pages |
| 165 | + run: | |
| 166 | + BRANCH_NAME="${{ github.head_ref }}" |
| 167 | + SAFE_BRANCH=$(echo "$BRANCH_NAME" | sed 's/[^a-zA-Z0-9-]/-/g') |
| 168 | +
|
| 169 | + git fetch origin gh-pages:gh-pages |
| 170 | + git config user.name github-actions |
| 171 | + git config user.email github-actions@github.com |
| 172 | + git checkout gh-pages |
| 173 | +
|
| 174 | + if [ -d "preview/$SAFE_BRANCH" ]; then |
| 175 | + rm -rf preview/$SAFE_BRANCH |
| 176 | + git add -A preview/ |
| 177 | + git commit -m "Remove preview: $BRANCH_NAME [ci skip]" |
| 178 | + git remote set-url origin https://${{ github.token }}@github.com/${{ github.repository_owner }}/openworm_docs.git |
| 179 | + git push origin gh-pages |
| 180 | + fi |
0 commit comments