-
Notifications
You must be signed in to change notification settings - Fork 1
201 lines (167 loc) · 7.32 KB
/
ci.yml
File metadata and controls
201 lines (167 loc) · 7.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
name: Build Typst Projects
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
workflow_dispatch:
jobs:
check_changes:
runs-on: ubuntu-22.04
outputs:
should_build: ${{ steps.check_changes.outputs.should_build }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch all history for git log commands
- name: Check for changes in Typst projects or README
id: check_changes
run: |
# Get the hash of the previous commit
if [ "${{ github.event_name }}" == "pull_request" ]; then
# For pull requests, compare against the base commit
PREVIOUS_COMMIT=${{ github.event.pull_request.base.sha }}
else
# For pushes, compare against the previous commit
PREVIOUS_COMMIT=$(git rev-parse HEAD^1 || echo "")
fi
if [ -z "$PREVIOUS_COMMIT" ]; then
# If this is the first commit, we should build
echo "should_build=true" >> $GITHUB_OUTPUT
echo "First commit, building all projects"
exit 0
fi
# Check if any .typ or .typst files or README.md has changed
CHANGED_FILES=$(git diff --name-only $PREVIOUS_COMMIT HEAD)
TYPST_CHANGES=$(echo "$CHANGED_FILES" | grep -E '\.typ(st)?$' || true)
README_CHANGES=$(echo "$CHANGED_FILES" | grep -E 'README\.md$' || true)
ASSETS_CHANGES=$(echo "$CHANGED_FILES" | grep -E 'assets/' || true)
if [ -n "$TYPST_CHANGES" ] || [ -n "$README_CHANGES" ] || [ -n "$ASSETS_CHANGES" ] || [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
echo "should_build=true" >> $GITHUB_OUTPUT
echo "Changes detected in Typst files or README.md, proceeding with build"
else
echo "should_build=false" >> $GITHUB_OUTPUT
echo "No changes detected in Typst files or README.md, skipping build"
fi
build:
needs: check_changes
if: needs.check_changes.outputs.should_build == 'true'
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch all history for git log commands
- name: Install packages
run: |
sudo apt-get update
sudo apt-get install -y poppler-utils
- name: Setup Typst
uses: typst-community/setup-typst@v3
with:
typst-version: latest
- name: Find Typst projects with main.typst
id: find_projects
run: |
echo "TYPST_DIRS=$(find . -name "main.typst" -o -name "main.typ" | xargs dirname | tr '\n' ' ')" >> $GITHUB_ENV
mkdir -p build
- name: Copy assets
id: copy_assets
run: |
cp -r ./assets ./build
- name: Build PDFs from Typst projects
run: |
# Create a file to store modification dates
touch modification_dates.txt
for dir in $TYPST_DIRS; do
if [ -f "$dir/main.typst" ]; then
main_file="$dir/main.typst"
else
main_file="$dir/main.typ"
fi
# Get relative path from repository root
rel_dir="${dir#./}"
echo "Building $main_file to build/$rel_dir/main.pdf"
mkdir -p "build/$rel_dir"
# Try to compile, continue if there's an error
typst compile "$main_file" "build/$rel_dir/main.pdf" || echo "Failed to build $main_file, continuing..."
# If build was successful, get the last modified date based on content changes
if [ -f "build/$rel_dir/main.pdf" ]; then
# Get the last commit date that modified ANY file in this directory or subdirectories
# This captures changes to includes, images, data files, etc.
last_modified=$(git log -1 --format="%ad" --date=format:"%Y-%m-%d" -- "$dir")
# If git log fails (e.g., directory not yet committed), use file system date
if [ -z "$last_modified" ]; then
last_modified=$(find "$dir" -type f -printf "%TY-%Tm-%Td\n" | sort -r | head -n1)
fi
# Store the file path and its last modified date
echo "$rel_dir/main.pdf|$last_modified" >> modification_dates.txt
fi
done
- name: Generate Enhanced README
run: |
# Create header with site info
if [ -f "README.md" ]; then
echo "$(head -n 1 README.md)" >> build/README.md
fi
cat > build/README.md << 'EOF'
<div align="center">
<h3>Automatically Generated PDF Documents</h3>
<p>
<em>This site hosts the compiled PDF files from the Typst project source files of <a href="https://github.com/Huy-DNA/MPiSC/tree/main">MPiSC</a>.</em>
</p>
</div>
---
## 📄 Available Documents
<table>
<thead>
<tr>
<th align="left">Document</th>
<th align="left">Last Content Update</th>
<th align="center">View</th>
<th align="center">Download</th>
</tr>
</thead>
<tbody>
EOF
# Add table rows for each PDF
while IFS="|" read -r file_path last_mod_date; do
pdf_name=$(pdfinfo "build/$file_path" | grep "Title")
pdf_name=${pdf_name:6}
echo " <tr>" >> build/README.md
echo " <td><strong>$pdf_name</strong></td>" >> build/README.md
echo " <td>$last_mod_date</td>" >> build/README.md
echo " <td align=\"center\"><a href=\"$file_path\">📕 View</a></td>" >> build/README.md
echo " <td align=\"center\"><a href=\"$file_path\" download>⬇️ PDF</a></td>" >> build/README.md
echo " </tr>" >> build/README.md
done < modification_dates.txt
# Close the table
echo " </tbody>" >> build/README.md
echo " </table>" >> build/README.md
echo "" >> build/README.md
# Add original README content if it exists
echo "## 📝 Project Information" >> build/README.md
echo "" >> build/README.md
if [ -f "README.md" ]; then
echo "" >> build/README.md
cat README.md | tail -n +2 | sed -E 's/^(#+)/\1#/' >> build/README.md
echo "" >> build/README.md
fi
# Add footer with build info
cat >> build/README.md << EOF
---
<div align="center">
<p>
<small>Last build: $(date)</small><br>
<small>Generated by GitHub Actions • <a href="https://github.com/${GITHUB_REPOSITORY}/tree/main">View Source</a></small>
</p>
</div>
EOF
- name: Deploy to GitHub Pages
uses: JamesIves/github-pages-deploy-action@v4
with:
folder: build
branch: gh-pages
clean: true
commit-message: "Deploy Typst PDFs to GitHub Pages [skip ci]"