如何使用Grunt从特定环境访问变量

时间:2013-12-03 17:04:55

标签: gruntjs grunt-ssh

我正在使用以下Grunt配置在测试或生产服务器上运行某些命令:

...
secret_test: grunt.file.readJSON('./deploy/test.json'),
secret_prod: grunt.file.readJSON('./deploy/prod.json'),
sshconfig: {
  test: {
    host: '<%= secret_test.host %>',
    port: '<%= secret_test.port %>',
    username: '<%= secret_test.username %>',
    password: '<%= secret_test.password %>',
    path: '<%= secret_test.path %>',
    createDirectories: true
  },
  prod: {
    host: '<%= secret_prod.host %>',
    port: '<%= secret_prod.port %>',
    username: '<%= secret_prod.username %>',
    password: '<%= secret_prod.password %>',
    path: '<%= secret_prod.path %>',
    createDirectories: true
  },
},
sshexec: {
  uptime: {
    command: 'uptime', // working fine
  },
  ls: {
     command: '???' // I'd like to list the content of the path provided (test or prod)
  },
  ...
},

secret_test和secret_prod具有以下格式:

{
"host" : "MY_SERVER",
"port": 22,
"path" : "SOME_PATH",
"username" : "USERNAME",
"password": "PWD"

}

要在测试环境中运行'ls'任务,我使用:

grunt ls --config test

路径在test和prod中有所不同,我如何访问链接到我指定的配置的路径的值?

1 个答案:

答案 0 :(得分:1)

我会使用grunt-env插件 使用grunt env:

env:
 dev:
  host: ...
  port: ...
 prod:
  host: ...
  port: ...

然后只需注册任务......

grunt.registerTask('dev', 'env:dev lint server watch');
grunt.registerTask('build', 'env:build lint other:build:tasks');

您可以通过

访问变量
process.env.VARIABLE_NAME