DEV Community

Huy Tr.
Huy Tr.

Posted on

Setup Travis CI for Rust Diesel project

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, 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
  - nightly
  // or
  - 1.22.0
  // or
  - stable
Enter fullscreen mode Exit fullscreen mode

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

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

  - postgresql
Enter fullscreen mode Exit fullscreen mode

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:

  - 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
Enter fullscreen mode Exit fullscreen mode

Step 4: Build and Test

Finally, build and test your project with cargo:

  - cargo build --verbose --all
  - cargo test --verbose --all
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

See more about dependencies caching here.

Top comments (0)