This repository has been archived by the owner on Jun 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
trigger-webjob.js
81 lines (69 loc) · 2.15 KB
/
trigger-webjob.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
module.exports = {
friendlyName: 'Trigger Webjob',
description: 'Triggers a webjob on a given website.',
extendedDescription: '',
inputs: {
deploymentUser: {
description: 'The deployment username for the website',
example: 'johndoe',
required: true
},
deploymentPassword: {
description: 'The deployment password for the website',
example: 'p@ssw0rd',
required: true
},
name: {
description: 'The name of the script that should be triggered',
example: 'myscript.ps1',
required: true
},
website: {
description: 'The name of the website the script should be triggered on',
example: 'mysite',
required: true
}
},
defaultExit: 'success',
exits: {
error: {
description: 'Unexpected error occurred.'
},
success: {
description: 'Done.'
}
},
fn: function (inputs,exits) {
var child_process = require('child_process');
var cliPath = require('path').resolve(__dirname, '../node_modules/azure-cli/bin/azure');
var _ = require('lodash');
var request = require('request');
var checkForError = function (response) {
response = (response[0] && response[0].headers) ? response[0] : response;
if (response.headers && response.headers['content-type'] && response.headers['content-type'] === 'text/html') {
// 'Azure returned text/html (which it shouldn't), checking for errors
if (response.body && response.body.indexOf('401 - Unauthorized') > -1) {
return 'Invalid Credentials: The Azure Website rejected the given username or password.';
}
}
return false;
}
var targetUrl = 'https://' + inputs.website + '.scm.azurewebsites.net/api/triggeredwebjobs/' + inputs.name + '/run';
request.post(targetUrl, {
'auth': {
'user': inputs.deploymentUser,
'pass': inputs.deploymentPassword
}
},
function (err, result) {
if (err) {
return exits.error(err);
}
errorCheck = checkForError(result);
if (errorCheck) {
return exits.error(errorCheck);
}
return exits.success(result);
});
},
};