-
Notifications
You must be signed in to change notification settings - Fork 0
267 lines (222 loc) · 8.86 KB
/
latex.yml
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
name: LaTeX to PDF
on: [push, pull_request]
env:
OUTPUT_DIR: "output"
MAIN_TEX_FOLDER: "src"
IMPRINT_URL: "https://cdn.helpwave.de/imprint.json"
DEPENDENCIES: |
head.tex
version.tex
imprint.tex
logo.svg
header.tex
jobs:
get_tex_commit_history:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: 0
- name: Check if any dependencies changed
id: check_dependencies
run: |
changed_files=$(git diff --name-only HEAD^ HEAD)
echo "Changed files: $changed_files"
dependencies_pattern=$(echo "${{ env.DEPENDENCIES }}" | sed 's/ /\\|/g')
if echo "$changed_files" | grep -E "$dependencies_pattern"; then
echo "Dependencies changed, updating .tex files."
echo "dependencies_changed=true" >> $GITHUB_ENV
else
echo "No dependencies changed."
echo "dependencies_changed=false" >> $GITHUB_ENV
fi
- name: Touch all .tex files if dependencies changed
if: env.dependencies_changed == 'true'
run: |
mkdir -p tex_commit_history
commit_hash=$(git rev-parse HEAD)
echo "Dependencies changed. Updating all .tex files..."
for file in $(find ${{ env.MAIN_TEX_FOLDER }} -name "*.tex"); do
echo "$file=$commit_hash" >> tex_commit_history/commit_hashes.txt
echo "Update $file"
done
cat tex_commit_history/commit_hashes.txt
- name: Find latest commits for .tex files
if: env.dependencies_changed == 'false'
run: |
echo "Fetching latest commit hashes for .tex files..."
mkdir -p tex_commit_history
for file in $(find ${{ env.MAIN_TEX_FOLDER }} -name "*.tex"); do
commit_hash=$(git log -n 1 --pretty=format:%H -- "$file")
echo "$file=$commit_hash" >> tex_commit_history/commit_hashes.txt
done
cat tex_commit_history/commit_hashes.txt
- name: Upload commit hash file
uses: actions/upload-artifact@v3
with:
name: tex_commit_hashes
path: tex_commit_history/commit_hashes.txt
compile_latex:
runs-on: ubuntu-latest
needs: get_tex_commit_history
outputs:
commit_hash: ${{ steps.get_commit_hash.outputs.commit_hash }}
changed_files: ${{ steps.determine_changed_files.outputs.changed_files }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
- name: Download commit hash artifact
uses: actions/download-artifact@v3
with:
name: tex_commit_hashes
path: tex_commit_history
- name: Get commit hash
id: get_commit_hash
run: |
commit_hash=$(git rev-parse HEAD)
echo $commit_hash
echo "commit_hash=$commit_hash" >> $GITHUB_OUTPUT
- name: Determine changed .tex files
id: determine_changed_files
run: |
echo "Determining changed .tex files based on commit history..."
changed_files=$(grep "${{ steps.get_commit_hash.outputs.commit_hash }}" tex_commit_history/commit_hashes.txt | cut -d'=' -f1 | tr ' ' '\n')
echo "Changed files: $changed_files"
echo "changed_files<<EOF" >> $GITHUB_ENV
echo -e "$changed_files" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
echo "changed_files<<EOF" >> $GITHUB_OUTPUT
echo -e "$changed_files" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Inject version
run: |
sed -i "s/ffffff/${{ steps.get_commit_hash.outputs.commit_hash }}/g" version.tex
- name: Download JSON file
run: |
curl -o imprint.json ${{ env.IMPRINT_URL }}
- name: Replace placeholders in LaTeX file
run: |
FILE_TO_UPDATE="imprint.tex"
for key in $(jq -r 'keys[]' imprint.json); do
value=$(jq -r --arg key "$key" '.[$key]' imprint.json)
placeholder=$(echo $key | tr '.' '_')
sed -i "s|{$placeholder}|{$value}|g" $FILE_TO_UPDATE
done
- name: Compile changed LaTeX documents
if: env.changed_files != ''
uses: xu-cheng/latex-action@v3
with:
root_file: ${{ env.changed_files }}
extra_system_packages: "inkscape"
latexmk_shell_escape: true
- name: Create output directory and move PDFs
if: env.changed_files != ''
run: |
mkdir -p ${{ env.OUTPUT_DIR }}
mv *.pdf ${{ env.OUTPUT_DIR }}
echo ${{ github.sha }} > ${{ env.OUTPUT_DIR }}/version.txt
- name: Upload PDFs as artifacts
if: env.changed_files != ''
uses: actions/upload-artifact@v4
with:
name: pdfs
path: ${{ env.OUTPUT_DIR }}
upload_to_cdn:
runs-on: ubuntu-latest
needs: compile_latex
if: needs.compile_latex.outputs.commit_hash != '' && needs.compile_latex.outputs.changed_files != ''
steps:
- name: Download PDFs
uses: actions/download-artifact@v4
with:
name: pdfs
path: ${{ env.OUTPUT_DIR }}
- name: Upload to CDN build
uses: ryand56/r2-upload-action@latest
with:
r2-account-id: ${{ secrets.R2_ACCOUNT_ID }}
r2-access-key-id: ${{ secrets.R2_ACCESS_KEY_ID }}
r2-secret-access-key: ${{ secrets.R2_SECRET_ACCESS_KEY }}
r2-bucket: ${{ secrets.R2_BUCKET }}
source-dir: ${{ env.OUTPUT_DIR }}
destination-dir: ${{ needs.compile_latex.outputs.commit_hash }}
release_on_cdn:
runs-on: ubuntu-latest
needs: compile_latex
if: github.ref == 'refs/heads/main' && needs.compile_latex.outputs.commit_hash != '' && needs.compile_latex.outputs.changed_files != ''
steps:
- name: Download PDFs
uses: actions/download-artifact@v4
with:
name: pdfs
path: ${{ env.OUTPUT_DIR }}
- name: Release on CDN
uses: ryand56/r2-upload-action@latest
with:
r2-account-id: ${{ secrets.R2_ACCOUNT_ID }}
r2-access-key-id: ${{ secrets.R2_ACCESS_KEY_ID }}
r2-secret-access-key: ${{ secrets.R2_SECRET_ACCESS_KEY }}
r2-bucket: ${{ secrets.R2_BUCKET }}
source-dir: ${{ env.OUTPUT_DIR }}
destination-dir: main
keep-file-fresh: true
pr_comment:
runs-on: ubuntu-latest
if: github.event_name == 'pull_request'
needs: [get_tex_commit_history, upload_to_cdn]
steps:
- name: Download commit hash file
uses: actions/download-artifact@v3
with:
name: tex_commit_hashes
path: tex_commit_history
- name: Generate CDN links and comment on PR
uses: actions/github-script@v6
with:
script: |
const fs = require('fs');
const commitHashes = {};
const commitHashFile = fs.readFileSync('tex_commit_history/commit_hashes.txt', 'utf8');
commitHashFile.split('\n').forEach(line => {
if (line.trim()) {
const [file, hash] = line.split('=');
commitHashes[file] = hash;
}
});
const prNumber = context.payload.pull_request.number;
const cdnUrl = '${{ secrets.CDN_URL }}';
let commentBody = `☁️ **Compiled LaTeX Versions** ☁️\n\n| File | Commit Hash | PDF Link |\n|------|-------------|----------|\n`;
Object.keys(commitHashes).forEach(file => {
const fileName = file.replace(/^.*\//, '').replace('.tex', '');
const hash = commitHashes[file];
const url = `${cdnUrl}/${hash}/${fileName}.pdf`;
commentBody += `| \`${file}\` | \`${hash}\` | [📄 Download PDF](${url}) |\n`;
});
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
});
const existingComment = comments.find(comment =>
comment.user.login === 'github-actions[bot]' &&
comment.body.includes('☁️ **Compiled LaTeX Versions** ☁️')
);
if (existingComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existingComment.id,
body: commentBody,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: commentBody,
});
}