Visual Studio Team Services (VSTS) has quickly become one for enterprise and indie developers the tool of choice for managing code repositories, processes, and test, and builds. With the rise of Solidity as the language of choice, Inevitably the need for CI/CD pipelines for Solidity smart contracts will grow. This short tutorial outlines how to use VSTS to create a simple pipeline for Solidity and Truffle.
- With your code checked in, click +New to add a new build definition to VSTS.
- Select your source. You’ll want to also select the repository and branch you want to build from too, then click Continue.
- On the next step, click on Empty process.
- Name the build something descriptive and select Hosted Linux Preview for the Agent Queue.
- Next, click the + button next to Phase 1 to add a task. Search for npm, then select Add next to npm from the search results.
- Search for shell script and click Add next to shell script in the search results.
- Click on the npm task to edit it. This task will install Truffle on the Linux agent and also install ganache-cli to use as the test network. Set the Display name to install ganache-cli and truffle. For Command, select custom. Paste in
install -g truffle ganache-cli
into Command and arguments. - Click on the Shell Script task. This task will actually run your Solidity unit tests. Set the Version to 3.* , Display name to run tests, and Type to inline. Paste in the following code into the Script field.
#!/bin/bash # Starts the Ganache blockchain in the background. ganache-cli > /dev/null & # Deploys the contracts to the Ganache network truffle deploy --reset --network test # Tuns the tests for the contracts truffle test --network test # Kills the Ganache blockchain running in the background. kill -KILL $(jobs -p) exit 0
Note: the network
test
is defined in thetruffle.js
file in the root of the truffle project that is checked in. Edit the truffle.json file and add thetest
network if you don’t already have a ganache-cli network defined in thetruffle.js
file. It needs to have thehost
set to127.0.0.1
, theport
to8545
, andnetwork_id
to*
.module.exports = { networks: { test: { host: "127.0.0.1", port: 8545, network_id: "*" // Match any network id } } };
- From the Save & queue menu, select Save, and name the build definition.
That’s it. You can now test it by queuing a new build and watching the output. Also, you can add any additional tasks related to your CI/CD pipeline or turn on Enable Continuous Integration under the Triggers tab so that the build definition fires when new code is checked into the repository.