Travis CI
CI/CD Tool
Travis CI
Overview
Travis CI is a veteran CI/CD service for open-source projects. It features GitHub integration, simple configuration, and multi-language support, enabling straightforward CI/CD pipeline construction through .travis.yml configuration.
Details
Travis CI is a cloud-based CI/CD (Continuous Integration/Continuous Delivery) service established in 2011. As a pioneer in CI/CD for open-source projects, it has been beloved by many developers through its GitHub-focused design. Build configuration is defined in the .travis.yml file within repositories, enabling version-controlled CI/CD settings. In March 2024, updates for developer productivity improvements were implemented, adding the ability to define job log length within .travis.yml configuration files. It natively supports major languages including Java, Node.js, Python, Ruby, and PHP, providing optimized build environments for each language. It supports multiple platforms including Linux, macOS, and Windows, and improves management efficiency for large projects through Build Config Imports shared configuration functionality. It provides free plans for open-source projects and paid plans for private projects with advanced features.
Pros and Cons
Pros
- Complete GitHub Integration: Tight integration with Pull Requests, Issues, Releases
- Simple Configuration: Intuitive setup through .travis.yml files
- Multi-language Support: Optimized build environments for major languages
- Open Source Friendly: Free plans supporting OSS projects
- Platform Coverage: Linux, macOS, Windows environments
- Community Support: Long-established user base and abundant information
- Shared Configuration: Configuration reuse through Build Config Imports
- History and Track Record: Over 10 years of operational experience and stability
Cons
- Feature Limitations: Limited functionality compared to other CI/CD tools
- Performance: Challenges with build speed and parallel processing capabilities
- Customization Constraints: Limitations in advanced workflow control
- Pricing Structure: Costs for private projects
- Lack of Enterprise Features: Insufficient enterprise-grade functionality
- Platform Limitations: Challenges with full Windows support
- Competitive Disadvantage: Feature gap with emerging services
Key Links
- Travis CI Official Site
- Travis CI Official Documentation
- Travis CI Build Configuration
- Travis CI GitHub Repository
- Travis CI Community
- Travis CI Blog
Code Examples
Basic Node.js Configuration
# .travis.yml
language: node_js
node_js:
- "18"
- "20"
- "22"
cache:
directories:
- node_modules
before_script:
- npm install
script:
- npm run test
- npm run lint
- npm run build
after_success:
- npm run coverage
notifications:
email:
recipients:
- [email protected]
on_success: change
on_failure: always
Multi-language Project
# .travis.yml
matrix:
include:
- language: node_js
node_js: "18"
script:
- npm install
- npm test
- language: python
python: "3.9"
install:
- pip install -r requirements.txt
script:
- python -m pytest
- language: java
jdk: openjdk11
script:
- ./gradlew test
env:
- NODE_ENV=test
- PYTHON_ENV=test
- JAVA_OPTS="-Xmx1024m"
before_install:
- echo "Setting up multi-language environment"
cache:
directories:
- node_modules
- $HOME/.cache/pip
- $HOME/.gradle/caches/
Docker Integration Build
# .travis.yml
language: minimal
services:
- docker
env:
- DOCKER_COMPOSE_VERSION=2.21.0
before_install:
- sudo rm /usr/local/bin/docker-compose
- curl -L https://github.com/docker/compose/releases/download/${DOCKER_COMPOSE_VERSION}/docker-compose-`uname -s`-`uname -m` > docker-compose
- chmod +x docker-compose
- sudo mv docker-compose /usr/local/bin
script:
- docker build -t myapp:latest .
- docker run --rm myapp:latest npm test
- docker-compose up -d
- docker-compose exec web npm run test:integration
- docker-compose down
after_success:
- echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin
- docker tag myapp:latest $DOCKER_USERNAME/myapp:$TRAVIS_BUILD_NUMBER
- docker tag myapp:latest $DOCKER_USERNAME/myapp:latest
- docker push $DOCKER_USERNAME/myapp:$TRAVIS_BUILD_NUMBER
- docker push $DOCKER_USERNAME/myapp:latest
branches:
only:
- main
- develop
Deployment Configuration
# .travis.yml
language: node_js
node_js: "18"
cache:
directories:
- node_modules
script:
- npm test
- npm run build
deploy:
- provider: pages
skip_cleanup: true
github_token: $GITHUB_TOKEN
local_dir: dist
on:
branch: main
- provider: heroku
app: myapp-staging
api_key: $HEROKU_API_KEY
on:
branch: develop
- provider: heroku
app: myapp-production
api_key: $HEROKU_API_KEY
on:
branch: main
- provider: script
script: bash scripts/deploy.sh
on:
branch: main
condition: $TRAVIS_TAG =~ ^v[0-9]+
env:
global:
- NODE_ENV=production
- secure: "encrypted_api_key_here"
notifications:
slack:
rooms:
- secure: "encrypted_slack_token"
on_success: change
on_failure: always
Branch Control and Stage Separation
# .travis.yml
language: node_js
node_js: "18"
# Branch control
branches:
only:
- main
- develop
- /^feature\/.*$/
except:
- legacy
- experimental
# Build stage definition
stages:
- name: test
- name: build
if: branch IN (main, develop)
- name: deploy
if: branch = main
jobs:
include:
# Test stage
- stage: test
name: "Unit Tests"
script:
- npm run test:unit
- stage: test
name: "Integration Tests"
script:
- npm run test:integration
- stage: test
name: "Linting"
script:
- npm run lint
- npm run type-check
# Build stage
- stage: build
name: "Build Application"
script:
- npm run build
after_success:
- npm run build:analyze
# Deploy stage
- stage: deploy
name: "Deploy to Production"
script: skip
deploy:
provider: script
script: bash scripts/deploy-production.sh
on:
branch: main
before_install:
- nvm install $TRAVIS_NODE_VERSION
- npm install -g npm@latest
install:
- npm ci
cache:
directories:
- node_modules
- $HOME/.npm
env:
global:
- NODE_ENV=test
matrix:
- TEST_SUITE=unit
- TEST_SUITE=integration
Security and Secret Management
# .travis.yml
language: node_js
node_js: "18"
before_script:
- npm audit
- npm run security:check
script:
- npm test
- npm run build
after_script:
- npm run coverage:report
# Encrypted environment variables
env:
global:
- secure: "encrypted_api_key_1"
- secure: "encrypted_api_key_2"
- NODE_ENV=test
# Encrypted files
before_install:
- openssl aes-256-cbc -K $encrypted_key -iv $encrypted_iv -in secrets.tar.enc -out secrets.tar -d
- tar xvf secrets.tar
addons:
sonarcloud:
organization: "myorg"
token:
secure: "encrypted_sonar_token"
code_climate:
repo_token:
secure: "encrypted_codeclimate_token"
after_success:
- bash <(curl -s https://codecov.io/bash)
- sonar-scanner
notifications:
email:
recipients:
- secure: "encrypted_email_1"
- secure: "encrypted_email_2"
on_success: change
on_failure: always
webhooks:
urls:
- secure: "encrypted_webhook_url"
on_success: change
on_failure: always