Skip to content

LaTeX GitLab / GitHub Worker

This is worker for automatic compilation of LaTeX documents (.tex) in GitLab or GitHub repo with pipelines.

Github CI

The path to the worker is .github/workflows/{name of worker}.yml

In order for the worker to upload the compiled PDF to the repo, the write permission must be granted.

For each repo, it is located at Settings > Actions > General > Workflow permissions and set to Read and write permissions.

name: LaTeX to PDF

on:
  push:
    paths-ignore:
      - '**.pdf'
      - '.github/**'

jobs:
  build_latex:
    runs-on: ubuntu-latest
    steps:
      - name: Check out repository
        uses: actions/checkout@v4

      - name: Compile LaTeX document
        uses: xu-cheng/latex-action@v3
        with:
          root_file: main.tex

      - name: Commit PDF
        id: commit
        run: |
                git config --local user.email "action[bot]@github.com"
                git config --local user.name "github-actions[bot]"
                git add main.pdf
                git commit -m "[bot] Update PDF from commit: ${{ github.event.head_commit.message }}"

      - name: Push changes
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}

GitLab CI

The path to the worker is .gitlab-ci.yml

Due to gitlab restrictions that I cannot bypass, I must first obtain a personal access token with repo write permissions and set it as a variable named pushtoken in the GitLab CI/CD settings. Then the worker can use the token to push the compiled PDF back to the repository.

# .gitlab-ci.yml

stages:
  - build

build_pdf:
  stage: build
  image: aergus/latex
  rules:
    - if: '$CI_COMMIT_MESSAGE =~ /^\[bot\]/'
      when: never
    - if: '$CI_PIPELINE_SOURCE == "push"'
      changes:
        - cv.tex
      when: on_success
    - when: never

  before_script:
    - git config user.name "${GITLAB_USER_NAME} [CI Bot]"
    - git config user.email "${GITLAB_USER_EMAIL}"

  script:
    - pdflatex -interaction=nonstopmode -jobname=resume cv.tex
    - echo "PDF compiled successfully as resume.pdf"

    - git add resume.pdf
    - 'git diff-index --quiet HEAD || git commit -m "[bot] Update PDF from commit: ${CI_COMMIT_MESSAGE}"'
    - git push "https://gitlab-ci-token:${pushtoken}@${CI_SERVER_HOST}/${CI_PROJECT_PATH}.git" HEAD:${CI_COMMIT_REF_NAME}
    - echo "Pushed resume.pdf back to the repository."