|
| 1 | +# SPDX-FileCopyrightText: 2024 CSC - IT Center for Science Ltd. <www.csc.fi> |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +name: Deploy pages |
| 6 | + |
| 7 | +on: |
| 8 | + workflow_run: |
| 9 | + workflows: |
| 10 | + - "Build HTML slides" |
| 11 | + types: |
| 12 | + - completed |
| 13 | + |
| 14 | +# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages |
| 15 | +permissions: |
| 16 | + contents: read |
| 17 | + pages: write |
| 18 | + id-token: write |
| 19 | + |
| 20 | +# Only one deploy run at a time across all branches |
| 21 | +concurrency: |
| 22 | + group: pages-deploy |
| 23 | + cancel-in-progress: false |
| 24 | + |
| 25 | +jobs: |
| 26 | + deploy: |
| 27 | + if: ${{ github.event.workflow_run.conclusion == 'success' }} |
| 28 | + environment: |
| 29 | + name: github-pages |
| 30 | + url: ${{ steps.deployment.outputs.page_url }}${{ github.event.workflow_run.head_branch }} |
| 31 | + runs-on: ubuntu-latest |
| 32 | + steps: |
| 33 | + - name: Setup Pages |
| 34 | + id: pages |
| 35 | + uses: actions/configure-pages@v4 |
| 36 | + |
| 37 | + - name: Download slide artifact from build |
| 38 | + uses: actions/download-artifact@v4 |
| 39 | + with: |
| 40 | + name: slides |
| 41 | + path: ./build |
| 42 | + run-id: ${{ github.event.workflow_run.id }} |
| 43 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 44 | + |
| 45 | + - name: Merge previous site and new build |
| 46 | + env: |
| 47 | + SITE_ZIP: site.zip |
| 48 | + BRANCH: ${{ github.event.workflow_run.head_branch }} |
| 49 | + shell: bash |
| 50 | + run: | |
| 51 | + echo "Updating pages for branch $BRANCH:" |
| 52 | +
|
| 53 | + # Download previous site |
| 54 | + wget -q "https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}/$SITE_ZIP" || echo "- Downloading previous site failed - starting pages from scratch" |
| 55 | +
|
| 56 | + # Prepare site directory |
| 57 | + if [ -f $SITE_ZIP ]; then |
| 58 | + unzip -q $SITE_ZIP |
| 59 | + rm $SITE_ZIP |
| 60 | + else |
| 61 | + mkdir site |
| 62 | + fi |
| 63 | +
|
| 64 | + echo "- Updating existing pages" |
| 65 | + rm -rf site/$BRANCH |
| 66 | + mv build site/$BRANCH |
| 67 | +
|
| 68 | + # Add redirecting index.html at the root |
| 69 | + cat > site/index.html <<'EOF' |
| 70 | + <!DOCTYPE html> |
| 71 | + <html lang="en"> |
| 72 | + <head> |
| 73 | + <meta charset="UTF-8" /> |
| 74 | + <meta http-equiv="refresh" content="0; url=./main/" /> |
| 75 | + <script> |
| 76 | + window.location.replace('./main/'); |
| 77 | + </script> |
| 78 | + <title>Redirecting</title> |
| 79 | + </head> |
| 80 | + <body> |
| 81 | + <p>Redirecting to <a href="./main/">main</a></p> |
| 82 | + </body> |
| 83 | + </html> |
| 84 | + EOF |
| 85 | +
|
| 86 | + # Remove pages for deleted branches |
| 87 | + echo "Checking branches:" |
| 88 | + find site -mindepth 1 -maxdepth 1 -type d | while read -r dir; do |
| 89 | + branch=$(basename "$dir") |
| 90 | +
|
| 91 | + if wget -q --spider https://api.github.com/repos/${{ github.repository }}/branches/$branch; then |
| 92 | + echo "- $branch: exist - keeping in pages" |
| 93 | + else |
| 94 | + echo "- $branch: deleted - removing from pages" |
| 95 | + rm -rf "$dir" |
| 96 | + fi |
| 97 | + done |
| 98 | +
|
| 99 | + # Repack site |
| 100 | + zip -q -r $SITE_ZIP site/ |
| 101 | + mv $SITE_ZIP site/ |
| 102 | +
|
| 103 | + - name: Upload pages artifact |
| 104 | + uses: actions/upload-pages-artifact@v3 |
| 105 | + with: |
| 106 | + path: ./site |
| 107 | + |
| 108 | + - name: Deploy to GitHub Pages |
| 109 | + id: deployment |
| 110 | + uses: actions/deploy-pages@v4 |
0 commit comments