What is Travis CI?
Travis CI for smart contracts
- If you still haven’t given Travis CI access to your repo, follow this guide. Otherwise, just go to the next step.
- Create a file in your GitHub repo called
.travis.yml. The purpose of this file is to detail which steps should be executed at each step of the build; if it’s not in your repo, Travis CI won’t trigger the build process. Your
.travis.yml file should look like this:
123456789101112131415161718192021222324dist: xenialsudo: requiredlanguage: node_jsnode_js:- "9.4.0"matrix:include:- os: linuxdist: xenialbefore_install:- sudo add-apt-repository ppa:ethereum/ethereum -y- sudo apt-get update- sudo apt-get install build-essential -y- sudo apt-get install gcc g++ libssl-dev libudev-dev pkg-config- curl -sL https://deb.nodesource.com/setup_9.x | sudo -E bash -- sudo apt-get install -y nodejs- rm -rf node_modulesinstall:- bash ./bin/travis-install.shscript:- bash ./bin/test.sh
You may have noticed that many of the commands here are specifically for Debian-based distros. Don’t worry if you’re using another distro or a different OS altogether — all of these commands will run on a VM provided by Travis CI (more info here). ./bin/travis-install.sh and ./bin/test.sh are just the scripts you’ll use for installing any necessary modules or dependencies and testing your smart contracts, respectively. As we will see in the following steps, you can name them whatever you want as long as they execute some specific commands and they’re properly referenced in .travis.yml.
- Create a file called
./bin/travis-install.sh. In our example, we put shell scripts in
./bin/ to keep the repo organized, but it isn’t strictly necessary. This is what it should look like:
123#!/bin/bashnpm installnpm install -g ganache-cli truffle
This will install all the Node packages you need for setting up an Ethereum network and running your tests.
- Create another file called
./bin/test.sh. This is what will actually run the tests.
1234567891011#!/bin/bashset -eganache-cli --gasLimit <yourLimit> 2> /dev/null 1> /dev/null &sleep 5 # to make sure ganache-cli is up and running before compilingrm -rf buildtruffle compiletruffle migrate --reset --network developmenttruffle testkill -9 $(lsof -t -i:8545)
- Make sure to replace <yourLimit> with whatever you’d like to set as the gas limit for the network. If you only want to execute some specific test cases, you can just replace truffle test with truffle test the same way you would if you were executing them in the console.
- Now everything should be set for pushing your changes!
- Optionally, add a Travis status badge to your repo’s README.md.
You can now easily see if your smart contracts are working just by pushing changes to Git. No more manual testing before updating your remote repo — Travis CI will let you know if your project is working properly.