Continuous Deployment with Truffle

Brendan Asselstine
MedX Protocol
Published in
3 min readSep 25, 2018

TLDR; I’ve developed a tool called Truffle Deploy Registry that separates your deployed contract addresses by network from the Truffle complication artifacts. Compatible with all versions of Truffle (up to the latest 5.0)

Continuous deployment is achieved when changes to a codebase are automatically pushed live, whether to a staging or production environment.

Typically:

  1. Code is pushed to a repository
  2. Tests are run on a continuous integration (CI) server
  3. If tests pass, code is deployed.

Continuous deployment allows us to iterate rapidly with a product.

Truffle is a fantastic tool for smart contract development that makes testing and deploying contracts a breeze. The only issue preventing it from smoothly integrating with continuous deployment is the fact that it generates very heavy artifacts.

Truffle artifacts contain the addresses at which the contracts are deployed. These artifacts can be pulled in or referenced by front-end code so that we can interface with them. However, the artifacts also include local filesystem paths that make them unsuitable to commit to the repository. Continuous deployment can only pull information from committed files, so how do we get around this?

I’ve developed a tool called Truffle Deploy Registry that separates the deployed contract address from the artifacts into files separated by network. You can .gitignore the local development networks, and commit testnet files so that the CI server can pick them up.

Truffle Deploy Registry

Truffle Deploy Registry works in two stages:

  1. New deployment entries are recorded in a network-specific JSON file.
  2. The latest deployment entries are merged with the truffle artifacts after compilation.

Typically with Truffle you’ll be using migrations to deploy and update your smart contracts. To add contracts to the deploy registry, you must append them within your migrations:

// migrations/1_initial_migration.js

var append = require('truffle-deploy-registry').append
var Migrations = artifacts.require("./Migrations.sol");

module.exports = function(deployer, network) {
deployer.deploy(Migrations).then(() => {
return append(
deployer.network_id,
{ contractName: 'Migrations', address: Migrations.address }
)
})
}

Now that contract address will be appended to the registry for the deployed network. If you have deployed to mainnet and Ropsten, you will now have the files:

networks/
1.json
3.json

Inside each of those files will be a list of deployed contracts. You would see something like:

# networks/1.json
[
{
contractName: 'Migrations',
address: '0x3383c29542b8c96eafed98c4aafe789ddb256e19'
}
]

Now you have a registry of where those contracts are stored. To merge those addresses back into the Truffle compilation artifacts you would use the apply-registry command. It takes the path to the build artifacts as a parameter:

$ apply-registry build/contracts

This ensures that the artifacts contain all of the *newest* addresses defined in the registries in the networks directory.

Now, when your CI server builds your front end code and pulls in the artifacts they will all have the correct addresses.

I hope this helps you in some way. Check out Truffle Deploy Registry on Github!

Brendan Asselstine

Medium: Brendan Asselstine

Github: https://github.com/asselstine

--

--

Brendan Asselstine
MedX Protocol

Ethereum Blockchain Engineer, PoolTogether, Delta Camp