|
1 | | -# On every push this script is executed |
2 | 1 | on: |
3 | 2 | push: |
4 | 3 | workflow_dispatch: |
| 4 | + repository_dispatch: |
| 5 | + types: [homework-update] |
| 6 | + |
5 | 7 | name: Build and deploy GH Pages |
| 8 | + |
6 | 9 | jobs: |
7 | | - build: |
| 10 | + discover-labs: |
8 | 11 | runs-on: ubuntu-latest |
| 12 | + outputs: |
| 13 | + matrix: ${{ steps.set-matrix.outputs.matrix }} |
| 14 | + homeworks-hash: ${{ steps.homeworks-hash.outputs.hash }} |
9 | 15 | steps: |
10 | 16 | - name: checkout |
11 | | - uses: actions/checkout@v3 |
| 17 | + uses: actions/checkout@v5 |
| 18 | + |
| 19 | + - name: Get homeworks repository commit hash |
| 20 | + id: homeworks-hash |
| 21 | + run: | |
| 22 | + HOMEWORKS_HASH=$(git ls-remote https://github.com/rust-stuco/homeworks.git HEAD | cut -f1) |
| 23 | + echo "hash=$HOMEWORKS_HASH" >> $GITHUB_OUTPUT |
| 24 | +
|
| 25 | + - name: Cache complete homework build |
| 26 | + id: cache-homework-build |
| 27 | + uses: actions/cache@v4 |
| 28 | + with: |
| 29 | + path: hw |
| 30 | + key: homework-complete-${{ steps.homeworks-hash.outputs.hash }} |
| 31 | + |
| 32 | + - name: Clone homeworks repository |
| 33 | + if: steps.cache-homework-build.outputs.cache-hit != 'true' |
| 34 | + run: | |
| 35 | + git clone --depth 1 https://github.com/rust-stuco/homeworks.git homeworks-temp |
| 36 | +
|
| 37 | + - name: Discover labs |
| 38 | + id: set-matrix |
| 39 | + run: | |
| 40 | + chmod +x .github/scripts/discover-labs.sh |
| 41 | + if [ -d "homeworks-temp" ]; then |
| 42 | + matrix=$(.github/scripts/discover-labs.sh) |
| 43 | + else |
| 44 | + # Use empty matrix to skip lab building |
| 45 | + matrix="[]" |
| 46 | + fi |
| 47 | + echo "matrix<<EOF" >> $GITHUB_OUTPUT |
| 48 | + echo "$matrix" >> $GITHUB_OUTPUT |
| 49 | + echo "EOF" >> $GITHUB_OUTPUT |
| 50 | +
|
| 51 | + # When homework files are cached (no changes in homeworks repo), |
| 52 | + # upload the cached files as an artifact. This is necessary because |
| 53 | + # build-labs and collect-labs jobs get skipped when matrix is empty, |
| 54 | + # but build-website still needs the homework files |
| 55 | + - name: Upload cached homework files |
| 56 | + if: steps.cache-homework-build.outputs.cache-hit == 'true' |
| 57 | + uses: actions/upload-artifact@v4 |
| 58 | + with: |
| 59 | + name: homework-complete |
| 60 | + path: hw |
| 61 | + |
| 62 | + build-labs: |
| 63 | + needs: discover-labs |
| 64 | + runs-on: ubuntu-latest |
| 65 | + if: needs.discover-labs.outputs.matrix != '[]' |
| 66 | + strategy: |
| 67 | + matrix: |
| 68 | + include: ${{fromJson(needs.discover-labs.outputs.matrix)}} |
| 69 | + steps: |
| 70 | + - name: Checkout repository |
| 71 | + uses: actions/checkout@v5 |
| 72 | + |
| 73 | + - name: Clone homeworks repository |
| 74 | + run: | |
| 75 | + git clone --depth 1 https://github.com/rust-stuco/homeworks.git homeworks-temp |
| 76 | +
|
| 77 | + - name: Get lab content hash |
| 78 | + id: lab-hash |
| 79 | + run: | |
| 80 | + lab_hash=$(find homeworks-temp/${{ matrix.week }}/${{ matrix.lab }} -type f \( -name "*.rs" -o -name "*.toml" -o -name "*.md" \) -exec sha256sum {} \; 2>/dev/null | sort | sha256sum | cut -d' ' -f1) |
| 81 | + echo "hash=$lab_hash" >> $GITHUB_OUTPUT |
| 82 | +
|
| 83 | + - name: Cache lab artifacts |
| 84 | + uses: actions/cache@v4 |
| 85 | + with: |
| 86 | + path: hw/${{ matrix.lab }} |
| 87 | + key: lab-${{ matrix.lab }}-${{ steps.lab-hash.outputs.hash }} |
| 88 | + |
| 89 | + - name: Setup Rust |
| 90 | + uses: actions-rust-lang/setup-rust-toolchain@v1 |
| 91 | + with: |
| 92 | + toolchain: stable |
| 93 | + override: true |
12 | 94 |
|
13 | | - - name: big build |
| 95 | + - name: Build lab documentation |
14 | 96 | run: | |
15 | | - npm install |
16 | | - npm run-script build |
| 97 | + chmod +x .github/scripts/build-lab.sh |
| 98 | + .github/scripts/build-lab.sh ${{ matrix.week }} ${{ matrix.lab }} |
| 99 | +
|
| 100 | + - name: Upload lab artifacts |
| 101 | + uses: actions/upload-artifact@v4 |
| 102 | + with: |
| 103 | + name: ${{ matrix.lab }} |
| 104 | + path: hw/${{ matrix.lab }} |
| 105 | + |
| 106 | + collect-labs: |
| 107 | + needs: [discover-labs, build-labs] |
| 108 | + runs-on: ubuntu-latest |
| 109 | + if: needs.discover-labs.outputs.matrix != '[]' |
| 110 | + steps: |
| 111 | + - name: Download all lab artifacts |
| 112 | + uses: actions/download-artifact@v5 |
| 113 | + with: |
| 114 | + path: hw |
| 115 | + |
| 116 | + - name: Reorganize artifacts |
| 117 | + run: | |
| 118 | + find hw -name "*.zip" -o -name "doc" -type d | while read item; do |
| 119 | + parent_dir=$(dirname "$item") |
| 120 | + lab_name=$(basename "$parent_dir") |
| 121 | + if [ "$parent_dir" != "hw/$lab_name" ]; then |
| 122 | + mkdir -p "hw/$lab_name" |
| 123 | + mv "$item" "hw/$lab_name/" |
| 124 | + fi |
| 125 | + done |
| 126 | +
|
| 127 | + - name: Cache complete homework build |
| 128 | + uses: actions/cache/save@v4 |
| 129 | + with: |
| 130 | + path: hw |
| 131 | + key: homework-complete-${{ needs.discover-labs.outputs.homeworks-hash }} |
| 132 | + |
| 133 | + - name: Upload complete homework artifacts |
| 134 | + uses: actions/upload-artifact@v4 |
| 135 | + with: |
| 136 | + name: homework-complete |
| 137 | + path: hw |
| 138 | + |
| 139 | + build-website: |
| 140 | + needs: [discover-labs, collect-labs] |
| 141 | + runs-on: ubuntu-latest |
| 142 | + if: always() && !cancelled() |
| 143 | + permissions: |
| 144 | + pages: write |
| 145 | + id-token: write |
| 146 | + environment: |
| 147 | + name: github-pages |
| 148 | + url: ${{ steps.deployment.outputs.page_url }} |
| 149 | + steps: |
| 150 | + - name: Checkout repository |
| 151 | + uses: actions/checkout@v5 |
| 152 | + |
| 153 | + - name: Clean existing homework folder |
| 154 | + run: rm -rf hw |
| 155 | + |
| 156 | + - name: Download homework artifacts |
| 157 | + uses: actions/download-artifact@v5 |
| 158 | + with: |
| 159 | + name: homework-complete |
| 160 | + path: hw |
| 161 | + continue-on-error: true |
| 162 | + |
| 163 | + - name: Setup Bun |
| 164 | + uses: oven-sh/setup-bun@v2 |
| 165 | + |
| 166 | + - name: Build website |
| 167 | + run: | |
| 168 | + # Only set path prefix for forks |
| 169 | + if [ "${{ github.repository }}" != "rust-stuco/rust-stuco.github.io" ]; then |
| 170 | + export ELEVENTY_PATH_PREFIX="/${{ github.event.repository.name }}/" |
| 171 | + fi |
| 172 | + bun install |
| 173 | + bun run build |
| 174 | +
|
| 175 | + - name: Setup Pages |
| 176 | + uses: actions/configure-pages@v5 |
| 177 | + |
| 178 | + - name: Upload to GitHub Pages |
| 179 | + uses: actions/upload-pages-artifact@v4 |
| 180 | + with: |
| 181 | + path: _site |
17 | 182 |
|
18 | | - - name: Push to gh-pages branch |
19 | | - uses: s0/git-publish-subdir-action@develop |
20 | | - env: |
21 | | - REPO: self |
22 | | - BRANCH: gh-pages # The branch name where you want to push the assets |
23 | | - FOLDER: _site # The directory where your assets are generated |
24 | | - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # GitHub will automatically add this - you don't need to bother getting a token |
25 | | - MESSAGE: "Build: ({sha}) {msg}" # The commit message |
| 183 | + - name: Deploy to GitHub Pages |
| 184 | + id: deployment |
| 185 | + uses: actions/deploy-pages@v4 |
0 commit comments