$ cat "

Using Grunt to Publish Files via FTP in Active Mode

"

A key part in the automation of tasks in any project is to automate the deployment.

If you use Grunt for automation and FTP to deploy to production there are a few different tasks from which to choose. However, not all of these support configuration of Active/Passive mode. In my case I need to be able to deploy in Active mode, so the task I use is grunt-ftpscript.

To get started with grunt-ftpscript you first install the task into your project directory with npm install grunt-ftpscript --save-dev, which adds the necessary line to your packages.json file. You can then configure the task in your gruntfile.js. Here is an example:

module.exports = function(grunt) {

    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),

        'ftpscript': {
            publish: {
                options: {
                    host: 'myhost.com',
                    authKey: 'myhost',
                    passive: false
                },
                files: [
                    {
                        expand: true,
                        cwd: 'dist',
                        src: [
                            '**/*',
                            '!**/exclude.js' // Use ! to exclude files
                        ],
                        dest: '/some/path/at/the/destination/ftp/server/'
                    }
                ]
            }
        }
    });

    grunt.loadNpmTasks('grunt-ftpscript');

    grunt.registerTask('default', ['ftpscript:publish']);
};

The astute reader has probably already noticed that there are no credentials included in the configuration of the ftpscript task. Instead the credentials are stored in a separate file called .ftppass. The ´.ftppass´ file can include multiple sets of credentials and you use the authKey value to reference the correct set of credentials.

Here is an example of what a .ftppass file can look like:

{
  "myhost": {
    "username": "someusername",
    "password": "p4$sw0rd"
  }
}

Remember to think twice before deciding if you want to put your .ftppass file under version control. If you are working on an open source project you had better add .ftppass to your .gitignore before committing your new changes to your grunt config. :)

Written by Erik Öjebo 2014-11-22 21:28

    Comments