Continuous integration with Travis CI

It is often the case in large software systems that for every change to our code, we want both our unit and integration tests to run automatically. Moreover, in a collaborative project, the manual way is just not practical. Fortunately, Continuous Integration is a practice that aims to automate those aspects of software development. Travis CI is a public continuous integration service that allows you to run your project's tests automatically in the cloud, based on event hooks. One example of an event hook is when new commits are pushed.

Travis is generally used to automate running builds and tests and to report failed builds, but can also be used for creating releases and even deploying them in staging or production environments. We'll focus on one aspect of Travis in this section, performing automated runs of our tests for our project. GitHub already has integration with Travis that can run tests for new commits in our project. To make this happen, we need the following:

  • Our project on GitHub
  • An account in Travis, which is made by logging in with GitHub
  • Your project enabled for builds in Travis
  • A .travis.yml file at the root of your repository that tells Travis what to run on

The first step is to go to https://travis-ci.org/ and log in with your GitHub credentials. From there, we can add our GitHub repository in Travis. Travis has good native support for Rust projects and keeps its Rust compiler continuously up to date. It provides a basic version of the .travis.yml file for Rust projects, which is as follows:

language: rust 
rust:
- stable
- beta
- nightly
matrix:
allow_failures:
- rust: nightly

The Rust project recommends testing against beta and nightly channels too, but you may choose to target just a single version by removing the corresponding lines. This recommended setup runs the tests on all three versions, but allows the fast-moving nightly compiler to fail.

With this .travis.yml file in your repository, GitHub will inform Travis CI every time you push your code and run your tests automatically. We can also attach build status badges to our repository's README.md file, which shows a green badge when tests pass and a red badge in when tests fail.

Let's integrate Travis with our logic_gates crate. For this, we have to add a .travis.yml file at our crate root. The following is the contents of the .travis.yml file:

language: rust
rust:
- stable
- beta
- nightly
matrix:
allow_failures:
- rust: nightly
fast_finish: true
cache: cargo

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

After pushing this to GitHub, we then need to enable Travis for our project on their page, as follows:

The preceding screenshot is from my TravisCI account. Now, we'll make a commit to our logic_gates repository by adding a simple README.md file to trigger the Travis build runner. While we do this, let's also add a build badge to our README.md file that will show the status of our repository to consumers. To do this, we'll click the build passing badge on the right:

This opens up a popup menu with the badge link:

We will copy this link and add it to the top in our README.md file as follows:

[![Build Status](https://travis-ci.org/$USERNAME/$REPO_NAME.svg?branch=master)](https://travis-ci.org/creativcoder/logic_gates)

You need to replace $USERNAME and $REPO_NAME with your details.

After this change and committing the README.md file, we will start to see the Travis build starting and succeeding:

Awesome! If you are feeling more ambitious, you can also try hosting the logic_gates crate's documentation on your repository's gh-pages branch on GitHub. You can do this by using the cargo-travis project, which is available at https://github.com/roblabla/cargo-travis.

For an even more versatile CI setup that covers major platforms, you can use the template provided by the trust project, which is available at https://github.com/japaric/trust.

Finally, to publish your crate on crates.io, you can follow the directions given in Cargo's reference documentation: https://doc.rust-lang.org/cargo/reference/publishing.html.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset