Created
March 27, 2025 19:52
-
-
Save xacrimon/fdeffab65b2a7a779c6a777aa3ef623e to your computer and use it in GitHub Desktop.
ci workflow for repo synced to the typst web editor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: build | |
on: | |
push: | |
branches: | |
- main | |
concurrency: | |
group: build-workflow | |
cancel-in-progress: false | |
permissions: | |
contents: write | |
jobs: | |
render: | |
runs-on: ubuntu-latest | |
outputs: | |
no_build: ${{ steps.diff.outputs.no_build }} | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Create build directory | |
run: mkdir -p build | |
- uses: actions/cache/restore@v4 | |
id: restore_rendered | |
with: | |
path: build/rendered | |
key: build-rendered-${{ hashFiles('projects/**') }} | |
restore-keys: | | |
build-rendered- | |
- id: diff | |
env: | |
exact_hit: ${{ steps.restore_rendered.outputs.cache-hit == 'true' }} | |
key_req: ${{ steps.restore_rendered.outputs.cache-primary-key }} | |
key_got: ${{ steps.restore_rendered.outputs.cache-matched-key }} | |
run: | | |
exact_hit="$exact_hit" | |
key_req="$key_req" | |
key_got="$key_got" | |
if [ "$exact_hit" = 'true' ]; then | |
echo "exact cache hit" | |
echo "no_build=true" >> "$GITHUB_OUTPUT" | |
fi | |
if [ "$exact_hit" = 'false' ] && [ -z "$key_got" ]; then | |
echo "cache miss" | |
cp project-list.txt build/do_update.txt | |
fi | |
if [ "$exact_hit" = 'false' ] && [ -n "$key_got" ] && [ "$key_req" != "$key_got" ]; then | |
echo "partial cache hit" | |
commit_sha=${{ github.sha }} | |
cache_commit_sha=$(cat build/rendered/commit_sha.txt | xargs echo -n) | |
git diff --name-only ${commit_sha} ${cache_commit_sha} > build/changed_files.txt | |
grep -A 1 "projects/.*" build/changed_files.txt | cut -d'/' -f2 | uniq > build/do_update.txt | |
fi | |
- name: Compute Typst dependencies | |
if: ${{ steps.diff.outputs.no_build != 'true' }} | |
run: | | |
touch build/amalgam.typ | |
while IFS="" read -r p || [ -n "$p" ] | |
do | |
find "projects/$p" -type f -name "*.typ" -exec cat {} + >> build/amalgam.typ | |
done < build/do_update.txt | |
grep -A 1 "#import .*" build/amalgam.typ | uniq > build/requirements.typ | |
- uses: typst-community/setup-typst@v4 | |
if: ${{ steps.diff.outputs.no_build != 'true' }} | |
with: | |
cache-dependency-path: build/requirements.typ | |
- name: Build projects | |
if: ${{ steps.diff.outputs.no_build != 'true' }} | |
run: | | |
mkdir -p build/rendered | |
while IFS="" read -r p || [ -n "$p" ] | |
do | |
typst compile "projects/$p/main.typ" "build/rendered/${p}.pdf" | |
done < build/do_update.txt | |
echo "${{ github.sha }}" > build/rendered/commit_sha.txt | |
- uses: actions/cache/save@v4 | |
if: ${{ steps.diff.outputs.no_build != 'true' }} | |
with: | |
path: build/rendered | |
key: build-rendered-${{ hashFiles('projects/**') }} | |
- uses: actions/upload-artifact@v4 | |
with: | |
name: build | |
path: build | |
tag: | |
needs: render | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- uses: actions/download-artifact@v4 | |
with: | |
name: build | |
path: build | |
- run: | | |
mv build/rendered . | |
rm -r build | |
- name: Create build tag | |
run: | | |
commit_sha=${{ github.sha }} | |
commit_sha_short="${commit_sha:0:7}" | |
git config --local user.email "github-actions[bot]@users.noreply.github.com" | |
git config --local user.name "github-actions[bot]" | |
git add rendered | |
git commit -m "build ${commit_sha_short}" | |
git tag "build-${commit_sha_short}" | |
git tag -f build-latest | |
- uses: ad-m/github-push-action@master | |
with: | |
force: true | |
tags: true | |
branch: build |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment