[fix] textbook pdf missing in push #76
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: Generate PDF for Textbook | |
on: | |
push: | |
branches: | |
- main | |
pull_request: | |
branches: | |
- main | |
jobs: | |
build_and_generate_pdf: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Install Pandoc, Latex necessities, and pdftk | |
run: | | |
sudo apt-get install -y pandoc texlive texlive-latex-extra poppler-utils texlive-extra-utils pdftk | |
- name: Install Python | |
uses: actions/setup-python@v4 | |
with: | |
python-version: '3.11' | |
- name: Generate PDFs from the textbook Markdown files | |
run: | | |
mkdir -p pdf_output | |
file_list=( | |
"index.md" | |
"principles/index.md" | |
"principles/principles.md" | |
"memory-safety/index.md" | |
"memory-safety/x86.md" | |
"memory-safety/vulnerabilities.md" | |
"memory-safety/mitigations.md" | |
"crypto/index.md" | |
"crypto/intro.md" | |
"crypto/symmetric.md" | |
"crypto/hashes.md" | |
"crypto/macs.md" | |
"crypto/prng.md" | |
"crypto/key-exchange.md" | |
"crypto/public-key.md" | |
"crypto/signatures.md" | |
"crypto/certificates.md" | |
"crypto/passwords.md" | |
"crypto/case-studies.md" | |
"crypto/bitcoin.md" | |
"web/index.md" | |
"web/sqli.md" | |
"web/intro.md" | |
"web/sop.md" | |
"web/cookies.md" | |
"web/csrf.md" | |
"web/xss.md" | |
"web/ui-attacks.md" | |
"web/captchas.md" | |
"network/index.md" | |
"network/intro.md" | |
"network/arp.md" | |
"network/dhcp.md" | |
"network/wpa.md" | |
"network/bgp.md" | |
"network/transport.md" | |
"network/tls.md" | |
"network/dns.md" | |
"network/dnssec.md" | |
"network/dos.md" | |
"network/firewalls.md" | |
"network/intrusion-detection.md" | |
"network/abusing-intrusion-detection.md" | |
"network/malware.md" | |
"network/tor.md" | |
) | |
for md_file in "${file_list[@]}"; do | |
pdf_file_name="pdf_output/$(echo "$md_file" | sed 's/\//_/g' | sed 's/.md//g').pdf" | |
python3 pdf-generation/generate-pdf-edits.py "$md_file" | pandoc -V geometry:margin=1in -V colorlinks=true -V linkcolor=blue -V urlcolor=blue --pdf-engine=pdflatex -o "$pdf_file_name" | |
if [ ! -f "$pdf_file_name" ]; then | |
echo "Error: PDF file $pdf_file_name not created!" >&2 | |
exit 1 | |
fi | |
done | |
pdftk \ | |
"pdf_output/index.pdf" \ | |
"pdf_output/principles_index.pdf" \ | |
"pdf_output/principles_principles.pdf" \ | |
"pdf_output/memory-safety_index.pdf" \ | |
"pdf_output/memory-safety_x86.pdf" \ | |
"pdf_output/memory-safety_vulnerabilities.pdf" \ | |
"pdf_output/memory-safety_mitigations.pdf" \ | |
"pdf_output/crypto_index.pdf" \ | |
"pdf_output/crypto_intro.pdf" \ | |
"pdf_output/crypto_symmetric.pdf" \ | |
"pdf_output/crypto_hashes.pdf" \ | |
"pdf_output/crypto_macs.pdf" \ | |
"pdf_output/crypto_prng.pdf" \ | |
"pdf_output/crypto_key-exchange.pdf" \ | |
"pdf_output/crypto_public-key.pdf" \ | |
"pdf_output/crypto_signatures.pdf" \ | |
"pdf_output/crypto_certificates.pdf" \ | |
"pdf_output/crypto_passwords.pdf" \ | |
"pdf_output/crypto_case-studies.pdf" \ | |
"pdf_output/crypto_bitcoin.pdf" \ | |
"pdf_output/web_index.pdf" \ | |
"pdf_output/web_sqli.pdf" \ | |
"pdf_output/web_intro.pdf" \ | |
"pdf_output/web_sop.pdf" \ | |
"pdf_output/web_cookies.pdf" \ | |
"pdf_output/web_csrf.pdf" \ | |
"pdf_output/web_xss.pdf" \ | |
"pdf_output/web_ui-attacks.pdf" \ | |
"pdf_output/web_captchas.pdf" \ | |
"pdf_output/network_index.pdf" \ | |
"pdf_output/network_intro.pdf" \ | |
"pdf_output/network_arp.pdf" \ | |
"pdf_output/network_dhcp.pdf" \ | |
"pdf_output/network_wpa.pdf" \ | |
"pdf_output/network_bgp.pdf" \ | |
"pdf_output/network_transport.pdf" \ | |
"pdf_output/network_tls.pdf" \ | |
"pdf_output/network_dns.pdf" \ | |
"pdf_output/network_dnssec.pdf" \ | |
"pdf_output/network_dos.pdf" \ | |
"pdf_output/network_firewalls.pdf" \ | |
"pdf_output/network_intrusion-detection.pdf" \ | |
"pdf_output/network_abusing-intrusion-detection.pdf" \ | |
"pdf_output/network_malware.pdf" \ | |
"pdf_output/network_tor.pdf" \ | |
cat output "textbook_full_original.pdf" | |
- name: Inject page numbers back into the pdf | |
run: | | |
mkdir -p pdf_numbering_output | |
num_pages=$(pdftk textbook_full_original.pdf dump_data | grep NumberOfPages | awk '{print $2}') | |
sed -i "s/161/$num_pages/" pdf-generation/numbering.tex | |
# Don't continue if the sed failed | |
if [ $? -ne 0 ]; then | |
echo "Error: sed command failed" >&2 | |
exit 1 | |
fi | |
pdflatex pdf-generation/numbering.tex | |
pdftk numbering.pdf burst output pdf_numbering_output/number_%03d.pdf | |
pdftk textbook_full_original.pdf burst output pdf_numbering_output/page_%03d.pdf | |
for i in $(seq -f %03g 1 $num_pages) ; do pdftk pdf_numbering_output/page_$i.pdf background pdf_numbering_output/number_$i.pdf output pdf_numbering_output/new_$i.pdf; done | |
pdftk pdf_numbering_output/new_???.pdf output textbook_full.pdf | |
cp textbook_full.pdf textbook_full_cpy.pdf | |
sed -i "s/$num_pages/161/" pdf-generation/numbering.tex | |
- name: Upload PDF as an artifact | |
uses: actions/upload-artifact@v4 | |
with: | |
name: textbook-full-copy | |
path: textbook_full_cpy.pdf | |
- name: Remove temporary files | |
run: | | |
rm -rf pdf_output | |
rm -rf pdf_numbering_output | |
rm -rf textbook_full_original.pdf | |
rm -rf textbook_full_cpy.pdf | |
- name: Check if PDF files are created | |
run: | | |
if [ -f "textbook_full.pdf" ]; then echo "textbook_full.pdf here"; else echo "textbook_full.pdf gone"; fi | |
- name: Commit changes to a new branch | |
if: github.event_name == 'push' && !contains(github.event.head_commit.message, 'Update PDF of textbook') | |
run: | | |
git config --local user.email "github-actions[bot]@users.noreply.github.com" | |
git config --local user.name "GitHub Actions" | |
git checkout -b update-textbook-full-pdf | |
git add textbook_full.pdf | |
git commit -m "Update PDF of textbook on site after a merge to main" | |
git push origin HEAD | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
- name: Create a Pull Request | |
if: github.event_name == 'push' && !contains(github.event.head_commit.message, 'Update PDF of textbook') | |
uses: peter-evans/create-pull-request@v5 | |
with: | |
token: ${{ secrets.GITHUB_TOKEN }} | |
commit-message: "Update PDF of textbook on site" | |
branch: update-textbook-full-pdf | |
title: "Update full PDF of textbook" | |
body: "This PR updates the full PDF of the textbook." | |
base: main |