Travis CI Cheat Sheet
Reference for .travis.yml build lifecycle, language environments, stages, and deployment configuration.
Basic .travis.yml
Minimal configuration for a Node.js project.
language: node_jsnode_js: - "20" - "18"install: - npm ciscript: - npm testcache: directories: - node_modules
Build Lifecycle Phases
Order of phases Travis CI executes per build.
- before_install- Commands run before dependency installation (e.g. apt-get, nvm use)
- install- Installs project dependencies
- before_script- Setup commands run before the main script phase
- script- Main build/test commands; a failure here fails the build
- after_success / after_failure- Runs based on the result of the script phase
- deploy- Deployment provider configuration triggered after a successful build
Stages & Deployment
Defining ordered stages and a deploy provider.
jobs: include: - stage: test script: npm test - stage: deploy script: skip deploy: provider: pages skip_cleanup: true github_token: $GITHUB_TOKEN local_dir: dist on: branch: main
Build Matrix with env and exclude
Expand a build across multiple language versions and environment combinations, excluding invalid pairs.
language: node_jsnode_js: - "18" - "20"env: - DB=postgres - DB=mysqlmatrix: exclude: - node_js: "18" env: DB=mysql allow_failures: - node_js: "20" env: DB=mysql fast_finish: true
Conditional Stages with if:
Restrict a deploy stage to run only on the main branch and skip pull request builds.
jobs: include: - stage: lint script: npm run lint - stage: deploy if: branch = main AND type != pull_request script: skip deploy: provider: script script: bash scripts/deploy.sh on: branch: main
Encrypting Secrets with the Travis CLI
Encrypt a value or an entire env line so it can be committed safely in .travis.yml.
gem install travistravis login --com# Encrypt a single secure valuetravis encrypt DEPLOY_TOKEN="s3cr3t" --add env.global# Encrypt a whole file (e.g. a service account key) into an env vartravis encrypt-file service-account.json --add
Caching Across Language Toolchains
Cache directories for multiple package managers in a polyglot build.
cache: directories: - $HOME/.npm - $HOME/.cache/pip - vendor/bundle bundler: true pip: truebefore_cache: - rm -rf $HOME/.cache/pip/log
Advanced Configuration Keys
Less common .travis.yml keys that control build behavior in larger projects.
- matrix.include- Add explicit, hand-picked build combinations on top of the generated matrix
- allow_failures- Marks specific matrix rows as non-blocking (build still passes overall) while still reporting their status
- fast_finish- Mark the build as finished as soon as required jobs pass, without waiting on allowed-failure jobs
- addons- Declaratively install apt packages, Chrome/Firefox, or Postgres/Redis services
- notifications- Configure email/slack/webhooks notifications on build state transitions
- cron builds- Scheduled builds configured per-branch in repo settings (or via the API), independent of pushes
- build stages- Group jobs into ordered stages (e.g. test -> deploy) where later stages run only if earlier ones pass
Set 'dist: jammy' (or your preferred Ubuntu release) explicitly rather than relying on the default image, since the default distribution can change and silently break builds.