Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module.exports = function(grunt) {
- var fs = require('fs');
- var azureDeploy = require('azure-deploy');
- var pathExists = function(path) {
- try {
- fs.accessSync(path, fs.F_OK);
- return true;
- } catch(e) {
- return false;
- }
- };
- grunt.registerMultiTask('azureDeploy',
- "Deploys a directory to an Azure website.",
- function () {
- var options = this.options({
- credential_file: '~/.azure_deploy.json'
- });
- var credFileExists = pathExists(options.credential_file);
- grunt.log.writeln("Validating target data.");
- // 1. Make sure the user has the right directory
- if (typeof this.data.directory === 'undefined') {
- grunt.log.error("Directory isn't specified for this target!");
- return;
- }
- // test for directory existence
- if (!pathExists(this.data.directory)) {
- grunt.log.error("Directory " + this.data.directory + " isn't accessible.");
- return;
- }
- var targetDirectory = this.data.directory;
- // 2. check for valid credentials
- var azureUsername = null;
- var azurePassword = null;
- if (credFileExists) {
- var creds = grunt.file.readJSON(options.credential_file)
- if (typeof creds.username !== 'undefined' &&
- typeof creds.password !== 'undefined') {
- azureUsername = creds.username
- azurePassword = creds.password
- } else {
- grunt.log.error("Couldn't get username and password from credential file.");
- return;
- }
- } else {
- grunt.log.error("I don't have a credential file. Please check that it exists.");
- return;
- }
- // 3. Check for azure username
- if (typeof this.data.website_name === 'undefined') {
- grunt.log.error("Task target does not have a defined Azure website name. We need this!");
- return;
- } else {
- var azureWebsiteName = this.data.website_name;
- }
- var deploymentManager = new azureDeploy.AzureWebSiteDeploymentManager(
- azureWebsiteName, azureUsername, azurePassword);
- grunt.log.writeln("Starting Azure deployment...");
- var done = this.async();
- deploymentManager.deploy(targetDirectory)
- .then(function() {
- grunt.log.ok("Deployment successful.");
- done();
- })
- .catch(function(e) {
- grunt.log.error('Deployment finished with errors.');
- grunt.log.writeln(e);
- done(false);
- });
- });
- // config
- grunt.initConfig({
- azureDeploy: {
- middleman: {
- options: {
- credential_file: './creds.json'
- },
- directory: './build',
- website_name: 'middleman-azure-cdn-test'
- }
- }
- });
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement