feat: add header.tex #250
Workflow file for this run
This file contains 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: 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] | |
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, | |
}); | |
} | |