# Solidity and Truffle Continuous Integration Setup

Author: Markus Waas

Published: 2020-06-05T22:19:43.000Z

Updated: 2026-09-13T14:25:30.000Z

Source: [https://soliditydeveloper.com/continuous-integration](<https://soliditydeveloper.com/continuous-integration>)

## Compatibility and review

Before you start

Historical Truffle CI guide. Truffle/Ganache and Node.js 10 are retired. The CircleCI example used retired Node.js 10 images and has been replaced with a small current configuration structure; migrate and test the old dependencies before relying on this setup.

[Official reference](<https://consensys.io/blog/consensys-announces-the-sunset-of-truffle-and-ganache-and-new-hardhat>)

Continuous integration (CI) with Truffle is great for developing once you have a basic set of tests implemented. It allows you to run very long tests, ensure all tests pass before merging a [pull request](<https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/creating-a-pull-request>) and to keep track of various statistics using additional tools.

We will use the [Truffle Metacoin Box](<https://www.trufflesuite.com/boxes/metacoin>) to setup our continuous integration. You can either choose Travis CI or Circle CI.

## Setting up Travis CI

Adding [Travis CI](<https://travis-ci.org/>) is straight-forward. You will only need to add a `.travis.yml` config file to the root folder of the project:

```yml
language: node_js
node_js:
  - 10

cache: npm

before_script:
  - echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p

script:
  - npm test
```

We are keeping it simple for now and are only running the test script which executes the Truffle unit tests. But we have one problem, there won't be a blockchain available on the Travis CI machine. A simple fix for this is to `npm install ganache-cli` and simply run it before the test. You can do this by adding a bash script with the line `npx ganache-cli  > /dev/null &` before the `npx truffle test` call. You can find the full example bash script [here](<https://github.com/gorgos/Truffle-CI-Example/blob/master/scripts/run_tests.sh>).

## Setting up Circle CI

[CircleCi](<https://circleci.com/>) requires a longer config file. The additional [npm ci](<https://docs.npmjs.com/cli/ci.html>) command is automatically done in Travis. It installs dependencies faster and more securely than `npm install` does. We again use the same script from the Travis version to run ganache-cli before the tests.

**2026 update:** The original CircleCI example used a retired Node.js 10 image. The following is a minimal configuration structure using the current image family. Choose and pin a supported Node version for your migrated dependencies, and make sure `npm test` starts the required local chain. The historical Node 10/Truffle setup has not been rerun.

```yml
version: 2.1

jobs:
  test:
    docker:
      # Select and pin a supported Node image for your migrated toolchain.
      - image: cimg/node:lts
    steps:
      - checkout
      - run: npm ci
      # npm test must start its required local test chain.
      - run: npm test

workflows:
  tests:
    jobs:
      - test
```

## Adding the eth-gas-reporter plugin

The eth-gas-reporter plugin is quite useful for keeping track of the gas costs of your smart contract functions. Having it in your CI will further be useful for showing diffs when adding pull requests.

**Step 1: Install the eth-gas-reporter plugin and codechecks:**

```bash
$ npm install --save-dev eth-gas-reporter
$ npm install --save-dev @codechecks/client
```

**Step 2: Add the plugin to the mocha settings inside your truffle-config.js (for options see [here](<https://github.com/cgewecke/eth-gas-reporter#options>)):**

```javascript
module.exports = {
  networks: { ... },
  mocha: {
    reporter: 'eth-gas-reporter',
    reporterOptions: {
      excludeContracts: ['Migrations']
    }
  }
};
```

**Step 3: Add a codechecks.yml to your project's root directory as below:**

```yml
checks:
  - name: eth-gas-reporter/codechecks
```

**Step 4: Run codechecks after the test command**

```yml
- npm test
- npx codechecks
```

**Step 5: Create a Codechecks account**

- Create an account with [Codechecks](<http://codechecks.io/>).
- Add the Github repo to it.
- Copy the secret and add the `CC_SECRET=COPIED SECRET` to your CI (see here for [Travis](<https://docs.travis-ci.com/user/environment-variables/>), here for [CircleCi](<https://circleci.com/docs/2.0/env-vars/#setting-an-environment-variable-in-a-project>)).
- Now go ahead and create a pull request.

That's it. You will now find a nice report about changes in gas costs of your pull request.

![gas reports](<https://cdn0.scrvt.com/b095ee27d37b3d7b6b150adba9ac6ec8/5bfc81decb5e57cf/fd430c9f9c41/v/2df57da920d1/gas-reports.png>)

## Adding the solidity-coverage plugin

With the solidity-coverage plugin you can check how much of your code paths are covered by your tests. Adding this to your CI makes is very convenient to use once it is set up.

**Step 1: Create a metacoin project & install coverage tools**

```bash
$ npm install --save-dev truffle
$ npm install --save-dev coveralls
$ npm install --save-dev solidity-coverage
```

**Step 2: Add solidity-coverage to the plugins array in truffle-config.js:**

```javascript
module.exports = {
  networks: {...},
  plugins: ["solidity-coverage"]
}
```

**Step 3: Add the coverage commands to the .travis.yml or Circle CI config.yml:**

```bash
- npx truffle run coverage
- cat coverage/lcov.info | npx coveralls
```

Solidity coverage starts its own ganache-cli, so we don't have to worry about this. Do not replace the regular test command though, coverage's ganache-cli works differently and is therefore no replacement for running regular unit tests.

**Step 4: Add repository to coveralls**

- Create an account with [Coveralls](<https://coveralls.io/>).
- Add the Github repo to it.
- Now go ahead and create a pull request.

![Coveralls](<https://cdn0.scrvt.com/b095ee27d37b3d7b6b150adba9ac6ec8/cf820a5d93b60c1b/f82def499107/v/cde0872bc054/coverall.png>)

## Further ideas

- [MythX](<https://mythx.io/>): With MythX you can automatically analyze your smart contract security. So it makes a lot of sense to [add this to your CI](<https://blog.mythx.io/howto/mythx-and-continuous-integration-part-1-circleci/>).
- [Linting](<https://en.wikipedia.org/wiki/Lint_%28software%29>): Good code can be enforced to some degree with linting tools. [Eslint](<https://eslint.org/>) works great for Javascript, is [easy to setup](<https://eslint.org/docs/user-guide/getting-started>), while [Solhint](<https://protofire.github.io/solhint/>) can be used for Solidity.
- Long tests: Sometimes you may want to add extreme tests, e.g., testing a contracts with hundreds of users. This takes a lot of time. Instead of running those in every test run, add them to the CI.

There you have it. Continuous integration is a very useful strategy for your developments. You can check out a full example at [Truffle-CI-Example](<https://github.com/gorgos/Truffle-CI-Example>). Just make sure to remove Circle-CI or Travis, one is enough!
