logo
eng-flag

Travis CI Cheatsheet

Table of Contents

  1. Travis CI Basics
  2. Travis CI Configuration
  3. Build Lifecycle
  4. Language Support
  5. Environment Variables
  6. Deployment
  7. Caching
  8. Build Matrix
  9. Notifications
  10. Travis CI with Docker
  11. Travis CI with Databases
  12. Conditional Builds
  13. Travis CI API
  14. Travis CI Best Practices
  15. Troubleshooting Travis CI

Travis CI Basics

What is Travis CI?

Travis CI is a hosted continuous integration service used to build and test software projects hosted on GitHub and Bitbucket.

Key Components

  • .travis.yml file: Defines the CI/CD pipeline
  • Builds: Individual test runs
  • Jobs: Phases in a build
  • Stages: Groups of jobs that run sequentially

Travis CI Configuration

Basic .travis.yml structure

language: ruby
rvm:
  - 2.7
script:
  - bundle exec rake test

Specify build environment

os: linux
dist: focal

Build Lifecycle

Main phases

  1. before_install
  2. install
  3. before_script
  4. script
  5. after_success or after_failure
  6. before_deploy (optional)
  7. deploy (optional)
  8. after_deploy (optional)
  9. after_script

Example lifecycle configuration

before_install:
  - sudo apt-get update
install:
  - bundle install
before_script:
  - bundle exec rake db:create
script:
  - bundle exec rake test
after_success:
  - coveralls

Language Support

Ruby

language: ruby
rvm:
  - 2.7
  - 3.0

Node.js

language: node_js
node_js:
  - 14
  - 16

Python

language: python
python:
  - "3.8"
  - "3.9"

Environment Variables

Define environment variables

env:
  - DB=postgres
  - SH=bash
  - PACKAGE_VERSION="1.0.*"

Encrypted environment variables

Use travis encrypt command:

travis encrypt SOMEVAR="secretvalue" --add env.global

Use environment variables

script:
  - echo $DB

Deployment

Deploy to Heroku

deploy:
  provider: heroku
  api_key: "YOUR API KEY"
  app: "YOUR APP NAME"

Deploy to AWS S3

deploy:
  provider: s3
  access_key_id: "YOUR AWS ACCESS KEY"
  secret_access_key: "YOUR AWS SECRET KEY"
  bucket: "S3 Bucket"
  skip_cleanup: true

Caching

Cache dependencies

cache:
  directories:
    - $HOME/.cache/pip
    - node_modules

Cache for specific languages

language: node_js
cache: npm

Build Matrix

Define build matrix

language: python
python:
  - "3.8"
  - "3.9"
env:
  - DJANGO=2.2
  - DJANGO=3.0

Exclude specific combinations

matrix:
  exclude:
    - python: "3.8"
      env: DJANGO=3.0

Notifications

Email notifications

notifications:
  email:
    recipients:
      - one@example.com
      - other@example.com
    on_success: never
    on_failure: always

Slack notifications

notifications:
  slack: "YOUR SLACK AUTH TOKEN"

Travis CI with Docker

Use Docker in builds

services:
  - docker

before_install:
  - docker build -t myapp .
  - docker run -d -p 127.0.0.1:80:4567 myapp

Push Docker images

after_success:
  - echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin
  - docker push USER/REPO

Travis CI with Databases

Use MySQL

services:
  - mysql

before_script:
  - mysql -e 'CREATE DATABASE IF NOT EXISTS test;'

Use PostgreSQL

services:
  - postgresql

before_script:
  - psql -c 'CREATE DATABASE travis_ci_test;' -U postgres

Conditional Builds

Build specific branches

branches:
  only:
    - main
    - stable

Conditional jobs

jobs:
  include:
    - if: branch = main
      script: npm run deploy

Travis CI API

Trigger a build via API

body='{
"request": {
  "branch":"main"
}}'

curl -s -X POST   -H "Content-Type: application/json"   -H "Accept: application/json"   -H "Travis-API-Version: 3"   -H "Authorization: token YOUR-TRAVIS-API-TOKEN"   -d "$body"   https://api.travis-ci.com/repo/OWNER%2FREPO/requests

Get build status

curl -H "Travis-API-Version: 3"   -H "Authorization: token YOUR-TRAVIS-API-TOKEN"   https://api.travis-ci.com/repo/OWNER%2FREPO/builds

Travis CI Best Practices

  1. Keep your .travis.yml file simple and readable
  2. Use build stages for complex pipelines
  3. Cache dependencies to speed up builds
  4. Use environment variables for configuration
  5. Implement proper error handling and notifications
  6. Use conditional builds to optimize CI process
  7. Regularly update your Travis CI configuration
  8. Use build matrices for testing multiple versions/configurations
  9. Encrypt sensitive data
  10. Use Travis CI CLI for local validation

Troubleshooting Travis CI

Validate .travis.yml

Use Travis CI CLI:

travis lint

Debug build

Add to your .travis.yml:

before_install:
  - env
  - ls -la

Common issues

  • Build timeouts: Optimize build steps or increase timeout in job configuration
  • Dependency issues: Check your project's dependencies and Travis CI's pre-installed packages
  • Environment variable problems: Verify variable names and values in Travis CI settings
  • Build matrix issues: Check for conflicting configurations in your build matrix

2024 © All rights reserved - buraxta.com