loading...

Setup Travis CI for Rust Diesel project

huytd profile image Huy Tr. ・2 min read

Rust is supported on Travis CI, there are three main channels of Rust you can use: stable, beta and nightly. You can just follow their instruction to get your Rust project build smoothly.

But when it come to diesel.rs, it will be a bit tricky.

The reason is, in order to build a diesel-based project, you will need to have:

  • A database on your build machine
  • A .env file with a configuration to connect to that database
  • diesel-cli installed to run migration

So, it will require some more configuration.

Let's create a .travis.yml file in your project's root folder.

Step 1: Rust version

You can select any version of Rust in your Travis CI, put this to your config file:

language: rust
rust:
  - nightly
  // or
  - 1.22.0
  // or
  - stable

Step 2: PostgreSQL (or what ever database you use)

Next, we need to add PostgreSQL as an extra service:

services:
  - postgresql

If you're using a different database, just change it. For the complete list of supported database, please take a look at this document.

Step 3: Init your database and migration

Now we need to do a lot of things, create a databse, install diesel-cli, create a database and run migration, let create a before_script section:

before_script:
  - psql -c 'create database build_db;' -U postgres
  - echo "DATABASE_URL=postgres://postgres@localhost/build_db" > .env
  - cargo install diesel_cli --no-default-features --features=postgres
  - diesel migration run

Step 4: Build and Test

Finally, build and test your project with cargo:

script:
  - cargo build --verbose --all
  - cargo test --verbose --all

That's all you need!

If you are too lazy to follow the four steps, this is a gist for you.

P/S: It's worth to mention that due to a lot of cargo run, the build time is so freaking slow, with the above gist, my project took approximately 8 to 9 mins per build.

You might want to add some caching for cargo in your .travis.yml:

cache: cargo

See more about dependencies caching here.

Posted on Oct 6 '17 by:

huytd profile

Huy Tr.

@huytd

Write code, drawing things

Discussion

markdown guide