Chip Off The Ol’ Blockchain – Part 3: How to Setup a CI/CD Pipeline with VSTS and Truffle

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.

  1. With your code checked in, click +New to add a new build definition to VSTS.

    New Build Definition
    New Build Definition
  2. Select your source. You’ll want to also select the repository and branch you want to build from too, then click Continue.

    Select Source
    Select Source
  3. On the next step, click on Empty process.

    Empty Process
    Empty Process
  4. Name the build something descriptive and select Hosted Linux Preview for the Agent Queue.

    Name and Agent
    Name and Agent
  5. 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.

    Add NPM
    Add NPM
  6. Search for shell script and click Add next to shell script in the search results.

    Add Shell Script
    Add Shell Script
  7. 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.

    Edit NPM
    Edit NPM
  8. 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 the truffle.jsfile in the root of the truffle project that is checked in. Edit the truffle.json file and add the test network if you don’t already have a ganache-cli network defined in the truffle.js file. It needs to have the host set to 127.0.0.1, the port to 8545, and network_id to *.

     module.exports = {
       networks: {
         test: {
           host: "127.0.0.1",
           port: 8545,
           network_id: "*" // Match any network id
         }   
       }
     };
    

    Edit Shell Scriptq
    Edit Shell Script
  9. From the Save & queue menu, select Save, and name the build definition.

    Save Build
    Save Build

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.

Stay Informed

Sign up for the latest blogs, events, and insights.

We deliver solutions that accelerate the value of Azure.
Ready to experience the full power of Microsoft Azure?

Atmosera is thrilled to announce that we have been named GitHub AI Partner of the Year.

X