How to build my LaTeX automatically using Travis CI?

This answer contains a summary of two answers: one using Tectonic and Docker, and one using TeX Live and pdflatex. But there are much more options, for the complete guide including advantages/disadvantages (and including scripts for GitHub Actions) see this repository: github.com/PHPirates/travis-ci-latex-pdf.

1. Steps for building LaTeX using Docker and Tectonic

Tectonic is a LaTeX enginge (compiler) which downloads the needed packages automatically, as well as compiling as much times as needed taking BibTeX into account. Can also work with biber.

Docker provides the ability to quickly download a pre-installed Tectonic to the Travis server.

  1. Enable your repo for Travis: install the Travis GitHub App to your GitHub account by going to the Marketplace, scroll down, select Open Source (also when you want to use private repos) and select 'Install it for free', then 'Complete order and begin installation'.
  2. Now you should be in Personal settings | Applications | Travis CI | Configure and you can allow access to repositories, either select repos or all repos.
  3. Copy config file below to your repo, specify the tex file you want to compile, possibly change the directory your tex file is in after $TRAVIS_BUILD_DIR if it is not src/.
  4. commit and push, view your builds at travis-ci.com.

file .travis.yml:

sudo: required
language: generic
services: docker

script:
  # We use the docker image from https://hub.docker.com/r/dxjoke/tectonic-docker/
  - docker pull dxjoke/tectonic-docker
  # Compiling only main.tex:
  - docker run --mount src=$TRAVIS_BUILD_DIR/src,target=/usr/src/tex,type=bind dxjoke/tectonic-docker /bin/sh -c "tectonic main.tex"
  # Compiling multiple files as well as using biber:
#  - docker run --mount src=$TRAVIS_BUILD_DIR/src,target=/usr/src/tex,type=bind dxjoke/tectonic-docker /bin/sh -c "tectonic --keep-intermediates --reruns 0 biber-mwe.tex; biber biber-mwe; tectonic biber-mwe.tex; tectonic main.tex"

Tip: following this post Malcolm Ramsay has written up a pretty complete blog post for a similar use case.

Note that it is also possible to for example automatically deploy a pdf to your GitHub releases page when you tag a git commit, instructions in the overview repository github.com/PHPirates/travis-ci-latex-pdf.

2. Steps for building LaTeX using pdflatex and TeX Live

I adapted the LaTeX3 build file for use with pdflatex. A disadvantage of pdflatex is that a bigger TeX Live scheme (basic instead of infraonly) needs to be installed, because that includes pdflatex. On the other hand, Travis allows caching so you don't download it every time. Note that you could also use TeX Live and pdflatex from a Docker image, see the answer from Strauman.

If you use more packages, just add them in the file below. Also holds for .cls files.

texlive_install.sh:

#!/usr/bin/env sh

# Originally from https://github.com/latex3/latex3

# This script is used for testing using Travis
# It is intended to work on their VM set up: Ubuntu 12.04 LTS
# A minimal current TL is installed adding only the packages that are
# required

# See if there is a cached version of TL available
export PATH=/tmp/texlive/bin/x86_64-linux:$PATH
if ! command -v texlua > /dev/null; then
  # Obtain TeX Live
  wget http://mirror.ctan.org/systems/texlive/tlnet/install-tl-unx.tar.gz
  tar -xzf install-tl-unx.tar.gz
  cd install-tl-20*

  # Install a minimal system
  ./install-tl --profile=../texlive/texlive.profile

  cd ..
fi

# Just including texlua so the cache check above works
# Needed for any use of texlua even if not testing LuaTeX
tlmgr install luatex

# Other contrib packages: done as a block to avoid multiple calls to tlmgr
# texlive-latex-base is needed to run pdflatex
tlmgr install   \
  exam          \
  amsfonts      \
  stmaryrd      \
  amsmath

# Keep no backups (not required, simply makes cache bigger)
tlmgr option -- autobackup 0

# Update the TL install but add nothing new
tlmgr update --self --all --no-auto-install

texlive/texlive.profile:

selected_scheme scheme-basic
TEXDIR /tmp/texlive
TEXMFCONFIG ~/.texlive/texmf-config
TEXMFHOME ~/texmf
TEXMFLOCAL /tmp/texlive/texmf-local
TEXMFSYSCONFIG /tmp/texlive/texmf-config
TEXMFSYSVAR /tmp/texlive/texmf-var
TEXMFVAR ~/.texlive/texmf-var
option_doc 0
option_src 0

.travis.yml:

install:
 - source ./texlive_install.sh
cache:
  directories:
    - /tmp/texlive
    - $HOME/.texlive
script:
- mkdir _build
# Prefix command with travis_wait x so it times out after 3 mins
- travis_wait 3 pdflatex -output-directory _build ./src/nameofmytexfile.tex

Note that it is also possible to for example automatically deploy a pdf to your GitHub releases page when you tag a git commit, instructions in the overview repository github.com/PHPirates/travis-ci-latex-pdf.

Changelog

Update October 2018 @WtfJoke amended @rekka's Docker image to work with biber, updated incstructions

Update July 2018 @rekka provides a Docker image with Tectonic which makes the build faster and the build file much shorter, instructions added.

Update May 2018 Travis is migrating open-source from travis-ci.org to travis-ci.com, and they have introduced a GitHub App. Instructions are updated.

Update Jan 2018 I discovered the tectonic engine, added instructions.


LaTeX is not supported on Travis-CI but R is community-maintained. And since R uses LaTeX to run some vignettes and build manuals, you can access LaTeX through R and build it that way. With Yihui Xie's tinytex package, building a LaTeX document involves a pretty simple script.

For example, here's the .travis.yml file from a dummy repository which builds the first .tex file using pdflatex and then copies the PDF as a release:

language: r
sudo: true
latex: false
pandoc: false
warnings_are_errors: false

install: echo "Nothing occurs at installation, only script"
script:
- Rscript install_texlive.R
- Rscript -e 'tinytex::pdflatex(list.files(pattern = "\\.tex$")[1], bib_engine = "biber")'
branches:
  - master
  - travising
deploy:
  provider: releases
  skip_cleanup: true
  api_key:
    secure: <generated by `travis setup releases`>
  file: Report.pdf
  on:
    repo: HughParsonage/latex-travis

install_texlive.R

if (!requireNamespace("tinytex", quietly = TRUE)) {
    if (!requireNamespace("devtools", quietly = TRUE)) {
        install.packages("jsonlite", repos = "https://cran.rstudio.com/", quiet = TRUE)
        install.packages("httr", repos = "https://cran.rstudio.com/", quiet = TRUE)
        install.packages("memoise", repos = "https://cran.rstudio.com/", quiet = TRUE)
        install.packages("devtools", repos = "https://cran.rstudio.com/", quiet = TRUE)
        cat('devtools installed\n')
    }
  devtools::install_github(c('yihui/tinytex'), quiet = TRUE)
  tinytex::install_tinytex()
}

November 2018:

Build LaTeX using TeX Live from a Docker image

Minimal example

Use the following .travis.yml:

sudo:       required
language:   generic
services:   docker

tex-config:
- build-pattern=src/main.tex,src/mwe.tex
- packages=cancel, exam

script:
- docker run --mount src=$TRAVIS_BUILD_DIR/,target=/repo,type=bind strauman/travis-latexbuild:small

Complete answer

I made a version, which is available through this git repo and accompanying this docker. This is the quickstart:

After following the setup below, then your repository has the following functionality

  1. You push to a branch
  2. travis-ci automatically installs the packages specified in the config file, and runs tests the pdflatex via latexmk on the TeX files specified in the config file.
    • If the directory containing the .tex file has a file named wants-fail, then travis only succeeds if the .tex-file build fails.

Note: If you want travis to push a branch containing the built pdfs, check out the instructions on the master branch

Setup:

In your repo, you need two files from this branch: the .travis.yml-file and the .travis/tex-config.ini.

NB: You also have to add travis-ci.org to your repo, and your repo to travis-ci.org.

  1. Copy the .travis.yml file from this repo to the root of your git repository
  2. Copy the .travis/tex-config.ini file from this repo to .travis/tex-config.ini in your repo. Make sure to add packages in this file (after the packages= line: comma sepearated)
  3. Profits

Configure the .travis/tex-config.ini to your needs. For more advanced options (e.g. push pdfs back to git repo after tests) see the instructions in the master branch. The master branch also has in depth configuration reference.

NB if you want to use more advanced features (push back to git or a different TeX scheme) you MUST use the .travis.yml from the master branch!