Truffle – How to Pass Custom Parameters in Truffle Command Line for Contract Deployment

contract-developmenttruffle-migration

I am having 3 different environments:

  • dev
  • test
  • live

I have one contract deployed on three different addresses, that I want to use based on environment, since for every environment we will store different data into smart contract.

I am using teamcity for CI, but I am interested how to differentiate environment with Truffle migrations, so based on environment to pick different address from inside of migration?

Example:
Assume I have migration file:

    const test = artifacts.require("./test")


    module.exports = function(deployer, custom_argument_that_i_can_pass_via_teamcity) {

console.log(custom_argument_that_i_can_pass_via_teamcity);
        deployer
            .deploy(test);
    };

Best Answer

The official truffle documentation addresses your issue:

Your configuration file is called truffle.js and is located at the root of your project directory. This file is a Javascript file and can execute any code necessary to create your configuration. It must export an object representing your project configuration like the example below.

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

There you add your different environment configurations.

Below in the doc, it says how to send the env to use to truffle:

truffle migrate --network live

Update:

I think I understand your question better now that you've added more details to it.

You have access to the process.argv global variable inside migration files, so you can use minimist to parse it and extract your custom_param from it:

const argv = require('minimist')(process.argv.slice(2), {string: ['custom_argument']});

module.exports = function(deployer) {
  console.log("----migration custom argument: ",argv['custom_argument']);
  ......
};

And then executing truffle migrate --custom_argument "hello beautiful world"

prints ----migration custom argument: hello beautiful world.

Hope this helps.

Related Topic