Easy migration from Travis to GitHub Actions

Published on Jan. 5, 2022 by Gabriel Bordeaux

Introduction

As of 2021, Travis has stopped giving access to its CI resources to open source projects for free.

GitHub Actions is a free alternative for open source projects that offers great performance and flexibility.

Migrating the CI pipeline from Travis to GitHub actions

Here is a basic .travis.yml file:

language: python
python:
  - "3.7"
  - "3.8"
before_install:
  - sudo apt-get update
  - sudo apt-get install --yes gcc python3-dev libsqlcipher-dev
  - pip install pycodestyle codecov
  - python3 setup.py install
script:
  - pycodestyle
  - pytest
  - coverage report -m
after_success:
  - codecov

To reproduce that file with GitHub actions, you need to create a file .github/workflows/ci.yml in your repository:

mkdir -p .github/workflows
touch .github/workflows/ci.yml

To test against a single version of a programming language (for example Python 3.9 as seen below), your ci.yml file should follow this syntax:

name: CI

on: [push, pull_request]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      - name: Set up Python 3.9
        uses: actions/setup-python@v1
        with:
          python-version: 3.9
      - name: Display Python version
        run: python -c "import sys; print(sys.version)"
      - name: Before tests (same as "before_install")
        run: |
          sudo apt-get update
          sudo apt-get install --yes gcc python3-dev libsqlcipher-dev
          pip install pycodestyle codecov pytest
          python3 setup.py install
      - name: Run test suite (same as "script")
        run: |
          pycodestyle
          pytest
          coverage report -m
      - name: Finalization (same as "after_success")
        run: |
          codecov

If you would like to test against multiple versions of a programming language, you would replace:

    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      - name: Set up Python 3.9
        uses: actions/setup-python@v1
        with:
          python-version: 3.9

with:

    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: ['3.7', '3.8', '3.9']

    steps:
      - uses: actions/checkout@v2
      - name: Set up Python
        uses: actions/setup-python@v2
        with:
          python-version: ${{ matrix.python-version }}
          architecture: x64

Common dependencies

Use MySQL in GitHub actions:

Add the following step to your ci.yml file:

      - name: Set up MySQL
        run: |
          sudo systemctl start mysql.service
          mysql -e 'CREATE DATABASE my_db;' -uroot -proot

The credentials will be:

  • Host: localhost
  • Username: root
  • Password: root
  • Database: my_db (can be configured in ci.yml)

Use PostgreSQL in GitHub actions:

Add the following step to your ci.yml file:

      - name: Start PostgreSQL
        run: |
          sudo systemctl start postgresql.service
          pg_isready
      - name: Create PostgreSQL user
        run: |
          sudo -u postgres psql --command="CREATE USER db_user PASSWORD 'db_password'" --command="\du"
      - name: Create PostgreSQL database
        run: |
          sudo -u postgres createdb --owner=db_user my_db

The credentials will be:

  • Host: localhost
  • Username: db_user (can be configured in ci.yml)
  • Password: db_password (can be configured in ci.yml)
  • Database: my_db (can be configured in ci.yml)

Use Redis in GitHub actions:

Add the following step to your ci.yml file:

      - uses: shogo82148/actions-setup-redis@v1
        with:
          redis-version: '6.x'

The credentials will be:

  • Host: localhost
  • Password: no password