|
1 | | -name: Generate PDFs |
| 1 | +name: DevOps Cheatsheet PDF Generator |
2 | 2 |
|
3 | 3 | on: |
4 | 4 | push: |
5 | | - branches: |
6 | | - - master |
| 5 | + branches: [ main, master ] |
| 6 | + paths: |
| 7 | + - '**/*.md' |
| 8 | + - '.github/workflows/generate-pdf.yml' |
7 | 9 | pull_request: |
8 | | - branches: |
9 | | - - master |
| 10 | + branches: [ main, master ] |
| 11 | + paths: |
| 12 | + - '**/*.md' |
10 | 13 | workflow_dispatch: |
| 14 | + inputs: |
| 15 | + specific_category: |
| 16 | + description: 'Generate PDFs for specific category (leave empty for all)' |
| 17 | + required: false |
| 18 | + type: string |
11 | 19 |
|
12 | 20 | jobs: |
13 | | - pdf: |
| 21 | + generate-pdfs: |
| 22 | + name: Generate Cheatsheet PDFs |
14 | 23 | runs-on: ubuntu-latest |
15 | | - |
| 24 | + |
16 | 25 | steps: |
17 | | - - name: Checkout code |
| 26 | + - name: Checkout Repository |
18 | 27 | uses: actions/checkout@v4 |
| 28 | + with: |
| 29 | + fetch-depth: 0 |
| 30 | + |
| 31 | + - name: Setup Node.js |
| 32 | + uses: actions/setup-node@v4 |
| 33 | + with: |
| 34 | + node-version: '18' |
| 35 | + cache: 'npm' |
19 | 36 |
|
20 | | - - name: Install pandoc and LaTeX |
| 37 | + - name: Install Dependencies |
21 | 38 | run: | |
22 | 39 | sudo apt-get update |
23 | | - sudo apt-get install -y pandoc |
24 | | - sudo apt-get install -y texlive-xetex |
| 40 | + sudo apt-get install -y pandoc texlive-xetex texlive-fonts-recommended texlive-fonts-extra |
| 41 | +
|
| 42 | + - name: Cache PDF Generation |
| 43 | + uses: actions/cache@v3 |
| 44 | + with: |
| 45 | + path: | |
| 46 | + .pdf-cache |
| 47 | + key: ${{ runner.os }}-pdf-${{ hashFiles('**/*.md') }} |
| 48 | + restore-keys: | |
| 49 | + ${{ runner.os }}-pdf- |
| 50 | +
|
| 51 | + - name: Setup PDF Directory |
| 52 | + run: | |
| 53 | + mkdir -p pdfs |
| 54 | + mkdir -p .pdf-cache |
25 | 55 |
|
26 | | - - name: Generate PDF |
| 56 | + - name: Generate PDFs |
27 | 57 | run: | |
28 | | - for file in $(find . -name '*.md'); do |
29 | | - pandoc "$file" -o "${file%.md}.pdf" --pdf-engine=xelatex |
| 58 | + # Function to generate PDF with custom styling |
| 59 | + generate_pdf() { |
| 60 | + local input_file="$1" |
| 61 | + local output_file="$2" |
| 62 | + local category="$(dirname "$input_file" | sed 's/.\///')" |
| 63 | + local title="$(basename "$input_file" .md)" |
| 64 | + |
| 65 | + # Create header with metadata |
| 66 | + cat > header.yaml <<EOF |
| 67 | + --- |
| 68 | + title: "${title//-/ } Cheatsheet" |
| 69 | + author: "DevOps Cheatsheet Hub" |
| 70 | + date: "$(date +'%B %d, %Y')" |
| 71 | + geometry: margin=2cm |
| 72 | + colorlinks: true |
| 73 | + linkcolor: blue |
| 74 | + urlcolor: blue |
| 75 | + toccolor: blue |
| 76 | + toc: true |
| 77 | + toc-depth: 2 |
| 78 | + category: "${category}" |
| 79 | + header-includes: |
| 80 | + - \usepackage{fancyhdr} |
| 81 | + - \pagestyle{fancy} |
| 82 | + - \fancyhead[L]{DevOps Cheatsheet Hub} |
| 83 | + - \fancyhead[R]{${category}} |
| 84 | + - \fancyfoot[C]{\thepage} |
| 85 | + --- |
| 86 | + EOF |
| 87 | +
|
| 88 | + # Combine header with content and generate PDF |
| 89 | + cat header.yaml "$input_file" | pandoc \ |
| 90 | + -f markdown \ |
| 91 | + -t pdf \ |
| 92 | + --pdf-engine=xelatex \ |
| 93 | + --highlight-style=tango \ |
| 94 | + -o "$output_file" \ |
| 95 | + --toc \ |
| 96 | + --toc-depth=2 \ |
| 97 | + --variable=geometry:margin=2cm |
| 98 | + } |
| 99 | +
|
| 100 | + # Process files based on input |
| 101 | + if [ -n "${{ github.event.inputs.specific_category }}" ]; then |
| 102 | + category="${{ github.event.inputs.specific_category }}" |
| 103 | + echo "Generating PDFs for category: $category" |
| 104 | + for file in "$category"/*.md; do |
| 105 | + if [ -f "$file" ]; then |
| 106 | + output_file="pdfs/$(basename "${file%.md}.pdf")" |
| 107 | + if [ ! -f ".pdf-cache/$(basename "$output_file")" ] || [ "$file" -nt ".pdf-cache/$(basename "$output_file")" ]; then |
| 108 | + echo "Generating PDF for $file" |
| 109 | + generate_pdf "$file" "$output_file" |
| 110 | + cp "$output_file" ".pdf-cache/" |
| 111 | + else |
| 112 | + echo "Using cached version for $file" |
| 113 | + cp ".pdf-cache/$(basename "$output_file")" "$output_file" |
| 114 | + fi |
| 115 | + fi |
| 116 | + done |
| 117 | + else |
| 118 | + echo "Generating PDFs for all categories" |
| 119 | + for file in */*.md; do |
| 120 | + if [ -f "$file" ]; then |
| 121 | + output_file="pdfs/$(dirname "$file")-$(basename "${file%.md}.pdf")" |
| 122 | + if [ ! -f ".pdf-cache/$(basename "$output_file")" ] || [ "$file" -nt ".pdf-cache/$(basename "$output_file")" ]; then |
| 123 | + echo "Generating PDF for $file" |
| 124 | + generate_pdf "$file" "$output_file" |
| 125 | + cp "$output_file" ".pdf-cache/" |
| 126 | + else |
| 127 | + echo "Using cached version for $file" |
| 128 | + cp ".pdf-cache/$(basename "$output_file")" "$output_file" |
| 129 | + fi |
| 130 | + fi |
| 131 | + done |
| 132 | + fi |
| 133 | +
|
| 134 | + - name: Create PDF Index |
| 135 | + run: | |
| 136 | + echo "# DevOps Cheatsheet PDFs" > pdfs/index.md |
| 137 | + echo "Generated on: $(date +'%B %d, %Y')" >> pdfs/index.md |
| 138 | + echo "" >> pdfs/index.md |
| 139 | + |
| 140 | + for category in */; do |
| 141 | + if [ -d "$category" ]; then |
| 142 | + echo "## ${category%/}" >> pdfs/index.md |
| 143 | + for pdf in pdfs/${category%/}*.pdf; do |
| 144 | + if [ -f "$pdf" ]; then |
| 145 | + name=$(basename "$pdf" .pdf | sed 's/-/ /g') |
| 146 | + echo "- [$name]($pdf)" >> pdfs/index.md |
| 147 | + fi |
| 148 | + done |
| 149 | + echo "" >> pdfs/index.md |
| 150 | + fi |
30 | 151 | done |
31 | 152 |
|
32 | | - - name: Upload PDF artifacts |
| 153 | + - name: Upload Individual PDFs |
33 | 154 | uses: actions/upload-artifact@v4 |
34 | 155 | with: |
35 | | - name: pdfs |
36 | | - path: '**/*.pdf' |
| 156 | + name: cheatsheet-pdfs |
| 157 | + path: pdfs/*.pdf |
| 158 | + retention-days: 30 |
| 159 | + |
| 160 | + - name: Create Release |
| 161 | + if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master') |
| 162 | + run: | |
| 163 | + # Zip all PDFs |
| 164 | + cd pdfs |
| 165 | + zip -r ../cheatsheets.zip *.pdf |
| 166 | + cd .. |
| 167 | + |
| 168 | + # Create release tag |
| 169 | + TAG="v$(date +'%Y.%m.%d-%H%M')" |
| 170 | + gh release create "$TAG" \ |
| 171 | + --title "DevOps Cheatsheets $TAG" \ |
| 172 | + --notes-file pdfs/index.md \ |
| 173 | + cheatsheets.zip |
| 174 | + env: |
| 175 | + GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} |
| 176 | + |
| 177 | + - name: Update Documentation |
| 178 | + if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master') |
| 179 | + run: | |
| 180 | + git config --local user.email "[email protected]" |
| 181 | + git config --local user.name "GitHub Action" |
| 182 | + |
| 183 | + # Update README with latest PDF links |
| 184 | + sed -i '/## Available PDFs/q' README.md |
| 185 | + echo "" >> README.md |
| 186 | + cat pdfs/index.md >> README.md |
| 187 | + |
| 188 | + # Commit and push if there are changes |
| 189 | + git add README.md |
| 190 | + git diff --quiet && git diff --staged --quiet || (git commit -m "docs: Update PDF links [skip ci]" && git push) |
| 191 | + env: |
| 192 | + GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} |
0 commit comments