Migrating from Travis CI

So, you’ve decided to try Codefresh? Welcome on board!

We’ll help you get up to speed with basic functionality such as: compiling, testing and building Docker images.

Repository

Fork this repository to compare the Travis CI and Codefresh.

Looking into .travis.yml

In the root of this repository you’ll find a file named .travis.yml this is Travis build descriptor and it describes the different steps that comprise general process (install, script, etc). Let’s quickly review the contents of this file:

.travis.yml

sudo: false

language: node_js

node_js:
    - "7"

before_install:
    - export PORT=5000

services:
    - mongodb

install:
    - npm install

before_script:
    - npm install -g mocha
    - npm install -g istanbul
    - npm install -g gulp
    - npm install -g debug

script:
    - gulp test

94376d0-travis-ci-build.png

Looking into codefresh.yml

In this file, we will look at on build, freestyle, composition steps to see how to use them to build, test and deploy your repository. See more details about codefresh.yml steps here.

codefresh.yml

version: '1.0'
steps:

  build_step:
    type: build
    dockerfile: Dockerfile
    image-name: demochat
    tag: ${{CF_BRANCH}}

  unit_tests:
    image: ${{build_step}}
    fail-fast: false
    commands:
      - npm install
      - gulp test

  integration_step:
    type: composition
    composition:
      version: '2'
      services:
        app:
          image: ${{build_step}}
          links:
            - mongo
          ports:
            - 5000
        mongo:
          image: mongo
    composition-candidates:
      main:
        image: nhoag/curl
        command: bash -c "sleep 30 && curl http://app:5000/" | echo 'works'

  deploy_to_ecs:
      image: codefresh/cf-deploy-ecs
      commands:
        - cfecs-update --image-name demochat --image-tag ${{CF_BRANCH}} eu-west-1 demochat-cluster demochat-webapp
      environment:
        - AWS_ACCESS_KEY_ID=${{AWS_ACCESS_KEY_ID}}
        - AWS_SECRET_ACCESS_KEY=${{AWS_SECRET_ACCESS_KEY}}
      when:
        branch:
          only:
            - master

codefresh_processes.png

Getting Started

Let’s start to do the first actions with .travis.yml example. This file tells Travis CI that this project is written in PHP, and to test Test.php with phpunit against PHP versions 5.6, 7.0

.travis.yml

language: php
php:
- 5.6
- 7.0
script: phpunit Test.php

In Codefresh you can use the freestyle step to describe it.

Note, in Codefresh you can run Unit tests and Integration tests as separate steps of codefresh.yml

codefresh.yml

unit_tests_5.6:
    image: php:5.6
    commands:
      - phpunit Test.php
unit_tests_7.0:
    image: php:7.0
    commands:
      - phpunit Test.php

In case if you want to run your tests in parallel you need to create two codefresh.yml files

codefresh.5.6.yml

version: '1.0'
steps:
  unit_tests:
    image: php:5.6
    commands:
      - phpunit Test.php

codefresh.7.0.yml

version: '1.0'
steps:
  unit_tests:
    image: php:7.0
    commands:
      - phpunit Test.php

Now go to the pipelines of the repository and set the path to certain codefresh.yml for each of pipelines. Add webhook if you want to start the build automatically after commit and push.

codefresh_pipelines.png

The Build Lifycycle: Installing packages

In Codefresh we use the following steps to describe the flow

  • Build
  • Push
  • Git Clone
  • Composition
  • Launch Composition
More information

Codefresh YAML Steps

Using the steps above we can easily replace the commands that are used in Travis

before_install, install, before_script, script, after_success / after_failure, before_deploy, deploy, after_deploy, after_script

For instance, if you want to install something in your image you can describe it in Dockerfile using the command RUN:

Dockerfile

FROM ubuntu:14.04

RUN apt-get update

# Add oracle java 8 repository
RUN echo oracle-java8-installer shared/accepted-oracle-license-v1-1 select true | debconf-set-selections
RUN apt-get -y install software-properties-common
RUN add-apt-repository -y ppa:webupd8team/java
RUN apt-get update
RUN apt-get install -y oracle-java8-installer maven wget
RUN rm -rf /var/lib/apt/lists/*
RUN rm -rf /var/cache/oracle-jdk8-installer
ENV JAVA_HOME /usr/lib/jvm/java-8-oracle

# Install Maven, wget
RUN apt-get -y install maven wget

or you can install all necessary dependencies before running your test script.

codefresh.yml

unit_tests:
    image: php:5.6
    working_directory: ${{main_clone}}
    commands:
      - install-dependencies.sh
      - phpunit Test.php

Expression Condition: Skipping the step, Building specific branches

For instance, to skip the install in Travis you use install: true We suggest you describe the condition when you want to skip the certain step.

when:
    branch:
        only:
          - master
when:
    branch:
        ignore:
          - master
          - develop
when:
    condition:
        all:
          noSkipCiInCommitMessage: 'includes(lower("${{CF_COMMIT_MESSAGE}}"), "skip ci") == false'
          masterBranch: '"${{CF_BRANCH}}" == "master"'
Condition Description
when:
    branch:
        only:
          - master
Only execute the step for the master branch
when:
    branch:
        ignore:
          - master
          - develop
Ignore the develop branch and master branch
when:
    condition:
          all:
            noSkipCiInCommitMessage: 'includes(lower("$"), "skip ci") == false'
            masterBranch: '"$" == "master"'
Execute if the string [skip ci] is not part of the main repository commit message AND if the branch is master
More information about conditions?

Expression Condition Syntax Conditional Execution of Steps

Breaking the build

If any of the commands in the first four stages of the build lifecycle return a non-zero exit code, the build is broken. In Travis you can use the after_success, after_failure, after_script to execute post operations. In Codefresh we have the similar post-step operations on_success, on_finish, on_fail.

Or you can just add the capability fail_fast: false to skip step if it failed and continue performing the following steps.

More information about post-step operations

Post-Step Operations

Environment variables

Travis and Codefresh have the similar syntax to describe the environment variables.

.travis.yml

language: ruby
rvm:
- 1.9.3
env:
- DB=mongodb

codefresh.yml

step_name:
  image: zedtux/ruby-1.9.3
  environment:
    - DB=mongodb

Note: Codefresh allows to use encrypted variables.

codefresh.yml

step_name:
  image: zedtux/ruby-1.9.3
  environment:
    - ENCR_VAR=${{ENCR_VAR}}

codefresh-encr_vars.png

See which user provided variables exist in Codefresh

User provided variables

Push to docker registry

.travis.yml

after_success:
  - if [ "$TRAVIS_BRANCH" == "master" ]; then
    docker login -u="$DOCKER_USERNAME" -p="$DOCKER_PASSWORD";
    docker push USER/REPO;
    fi

codefresh.yml

  build_step_name:
    type: build
    image-name: USER/REPO
    dockerfile: Dockerfile
    tag: latest

  push_to_dockerhub:
    type: push
    title: Step Title
    description: Free text description
    candidate: ${{build_step_name}}
    tag: latest
    credentials:
      username: ${{DOCKER_USERNAME}}
      password: ${{DOCKER_PASSWORD}}
    when:
      branch:
        only:
          - master
More about push step

Push step

Deploy on example of AWS Elastic Beanstalk

To deploy to AWS Elastic Beanstalk need to use the following step in Travis and Codefresh

.travis.yml

deploy:
  provider: elasticbeanstalk
  access_key_id: <access-key-id>
  secret_access_key:
    secure: "Encypted <secret-access-key>="
  region: "us-east-1"  
  app: "example-app-name"
  env: "example-app-environment"

codefresh.yml

deploy-to-beanstalk:
    image: garland/aws-cli-docker:latest
    commands:
     - sh -c  "aws configure set region '${{AWS_REGION}}' && aws elasticbeanstalk update-environment --environment-name '${{AWS_ENV_NAME}}' --version-label '${{AWS_VERSION}}' "
    when:
      condition:
        all:
          masterBranch: "'${{CF_BRANCH}}' == '${{AWS_BRANCH}}'"

In the pipeline of repository, need to provide the following environment variables

codefresh_eb_env_vars.png

Command line client

Travis Codefresh
Travis command line client Codefresh command line client

See our examples: how to use Codefresh command line as step of codefresh.yml

Validating yml files

Travis Codefresh
Validating .travis.yml files Validating codefresh.yml files