Search code examples
deploymentjenkinsgruntjsproduction

setup grunt with jenkins to scp depending upon the environment value i select in jenkins


i have a project in grunt and can scp to different servers and copy stuff with grunt scp but every time when i have to scp to either test or production i have to change the host name in gruntfile.js and then commit it back to svn in order to run it via jenkins.So what i want is to create some environment varible on the basis of which my i can add some parameters in jenkins and when i select any of the parameter e.g. test by build will get deploy to test and so on. my scp code is below

scp: {
      options: {
        host: 'testserver',
        username: 'user',
        privateKey: fs.readFileSync('/repository/server/.ssh/id_rsa_robot'),
        tryKeyboard: true
      },
      dist: {
        files: [{
          cwd: './dist',
          src: '**/*',
          filter: 'isFile',
           // path on the server
          dest: '/path/to/deploy/project.test'
        }]  
      }
     } 

i want to use something like

grunt deploy:test

or

   grunt deploy:production

and ask jenkins to do that scp

please help !!


Solution

  • Never used Grunt, but any parameter that you configure in Jenkins under "This build is parameterized" section is available as environment variables for the processes under that build (with the same name as configured)

    According to this answer, Read environment variables in Node.js, to read an environment variable in nodejs (and I understand that Grunt uses nodejs), all you need is:
    process.env.ENV_VARIABLE
    Where ENV_VARIABLE is the name of the environment variable, as configured in Jenkins.

    So, in your case:

    • Check mark "This build is parameterized" in Jenkins job config.
    • Add "Choice" parameter type.
    • Give it a name, like Hostname
    • Provide a list of possible values
    • In your file, use:
      host: process.env.Hostname,