Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grunt - Watch a file and SFTP when it's changed

I'm trying to automatically upload a .css file, when it's compiled from Sass. This is what I've got in my Gruntfile.js:

module.exports = function(grunt) {

  // Project configuration.
  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    watch: {
      coffee: {
        files: ['**/*.coffee'],
        tasks: ['coffee']
      },
      scripts: {
        files: ['**/*.scss'],
        tasks: ['compass']
      },
      sftp: {
        files: ['**/*.css'],
        tasks: ['sftp-deploy']
      }
    },
    coffee: {
      compile: {
        files: {
          'testing.js': 'testing.coffee'
        }
      }
    },
    compass: {
      dist: {
        options: {
          config: 'config.rb'
        }
      }
    },

    'sftp-deploy': {
      build: {
        auth: {
          host: 'example.com',
          port: 22,
          authKey: 'key2'
        },
        src: 'styles/',
        dest: 'styles/',
        exclusions: ['**/.DS_Store'],
        server_sep: '/'
      }
    }
  });

  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-contrib-coffee');
  grunt.loadNpmTasks('grunt-contrib-compass');
  grunt.loadNpmTasks('grunt-sftp-deploy');


  // Default task(s).
  grunt.registerTask('default', ['watch']);

};

It compiles the .css but doesn't seem to upload it. Any ideas?

like image 557
Paul Mason Avatar asked Jun 02 '13 01:06

Paul Mason


2 Answers

I would like to confirm that the following grunt-ssh (https://github.com/andrewrjones/grunt-ssh) task config worked fine for me. Note that grunt accepts --verbose option which may help debug. Note that as of v0.6.2 grunt-ssh SFTP task did not seem to support sshconfig syntax, which was not very clear from the help page.

    sftpCredentials: grunt.file.readJSON('sftp-credentials.json'),
    sftp: {
        deploy: {
            files: {
                "./": "deploy/**"
            },
            options: {
                "path": "<%= sftpCredentials.path %>",
                "host": "<%= sftpCredentials.host %>",
                "username": "<%= sftpCredentials.username %>",
                "port": "<%= sftpCredentials.port %>",
                "password": "<%= sftpCredentials.password %>",
                "srcBasePath": "deploy/",
                "createDirectories": true
            }
        }
    }
like image 126
sbat Avatar answered Sep 25 '22 19:09

sbat


I was trying to do something almost identical using grunt-sftp and ran into similar errors. The logging wasn't the greatest so I ended up using grunt-shell and just ran scp upon compilation:

watch: {
    tumblr: {
        files:['sass/*.scss', 'sass/*/*.scss'],
        tasks: [ 'compass:tumblr', 'shell:tumblr' ]
    }
},

shell: {
  tumblr: {
    command: 'scp -P 2222 -r stylesheets "[email protected]:/var/www/foo/directory"'
  }
}

This worked like a charm.

like image 40
imjared Avatar answered Sep 23 '22 19:09

imjared