Skip to content

Instantly share code, notes, and snippets.

@eddywashere
Forked from siygle/grunt config
Created November 30, 2012 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eddywashere/4178099 to your computer and use it in GitHub Desktop.
Save eddywashere/4178099 to your computer and use it in GitHub Desktop.
deploy to s3
grunt.registerMultiTask('deploy', 'deploy program to cloud', function() {
var pkgcloud = require('pkgcloud')
, walk = require('walk')
, fs = require('fs')
, done = this.async()
, self = this;
grunt.log.writeln('Prepare upload to cloud (Prepare deploy configuration)');
var content, amazon;
try {
content = JSON.parse(fs.readFileSync('./s3.json'));
amazon = pkgcloud.storage.createClient({
provider: 'amazon',
key: content.secret,
keyId: content.key
});
} catch (e) {
grunt.fail.fatal('Can not get s3 key to deploy files');
}
var walker = walk.walk(self.data.from);
walker.on('file', function(root, fileStat, next) {
var readpath = root + '/' + fileStat.name;
var prefix = root.substr((self.data.from.length + 1));
var filepath = '';
if (prefix === '') {
filepath = fileStat.name;
} else {
filepath = prefix + '/' + fileStat.name;
}
fs.createReadStream(readpath).pipe(amazon.upload({
container: self.data.to,
remote: filepath
}, function(err) {
if (err) {
grunt.log.error(readpath + ' upload failed!');
} else {
grunt.log.error('Upload from ' + readpath);
grunt.log.error('to ' + filepath);
}
next();
}));
});
walker.on('errors', function(root, fileStat, next) {
grunt.log.error('Can not read ' + fileStat.name + ' correctly');
next();
});
walker.on('end', function() {
grunt.log.error('Deploy all programs to cloud');
done();
});
});
deploy: {
from: 'FILE_PATH',
to: 'S3_BUCKET'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment