-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
129 lines (108 loc) · 4.77 KB
/
index.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const core = require('@actions/core');
const client = require('ssh2-sftp-client');
const checkNodeVersion = require('./checkNodeVersion');
const createInfo = require('./createInfo');
const prefixRepair = require('./prefixRepair');
const suffixRepair = require('./suffixRepair');
const isSecurePassword = require('./isSecurePassword');
const buildFilter = require('./buildFilter');
/**
* @param input
*/
function debugHelper(input) {
if (!input.toString().startsWith('DEBUG')) {
core.info(input.toString());
}
}
/**
* The main function of the github action!
* @return {Promise<void>}
*/
async function run() {
try {
// node 14 is not supported due to a bug!
checkNodeVersion();
const isRequired = { required: true };
const host = core.getInput('host', isRequired);
const port = core.getInput('port', isRequired);
const username = core.getInput('username', isRequired);
const password = core.getInput('password', isRequired);
const rawLocalDir = core.getInput('localDir', isRequired);
const rawUploadPath = core.getInput('uploadPath', isRequired);
const exclude = JSON.parse(core.getInput('exclude', isRequired));
// check that password is secure (or throw error!)
isSecurePassword(password);
const localDir = prefixRepair(suffixRepair(rawLocalDir));
const uploadPath = prefixRepair(suffixRepair(rawUploadPath));
const config = {
host: host,
port: port,
username: username,
password: password,
// - - -
retries: 3,
retry_factor: 2,
retry_minTimeout: 2000,
};
if (core.getInput('debugMode') === 'on') {
config['debug'] = debugHelper;
}
let sftp = new client('upload-client');
sftp.connect(config)
.then(() => {
return sftp.cwd();
})
.then(base => {
core.info(`Remote base directory is ${base}`);
})
.then(async () => {
// create an info.json file
await createInfo(localDir);
// pre-check if an old upload folder exists
if (await sftp.exists(uploadPath + 'upload')) {
core.setFailed('A "upload" folder was found! Is there a second deployment process in parallel? If this is an accident, please delete the "Upload" folder on the server manually. THE ACTION ABORTS ITSELF!');
throw new Error('A "upload" folder was found!');
}
// build filter
const filter = buildFilter(exclude);
// upload the directory
core.info('UPLOAD - START');
const startUpload = Math.floor(new Date().getTime() / 1000);
await sftp.uploadDir(localDir, uploadPath + 'upload', filter);
const doneUpload = Math.floor(new Date().getTime() / 1000);
core.info('UPLOAD - DONE -> took ' + (doneUpload - startUpload) + ' seconds!');
// if NOT exist create folder
if (!await sftp.exists(uploadPath + 'active')) {
core.info('No "active" folder found (first time running this script?). The script tries to create it!');
await sftp.mkdir(uploadPath + 'active', true);
}
// rename (old) backup dir
if (await sftp.exists(uploadPath + 'backup')) {
core.info('Rename directory "backup" => "backup-remove"');
await sftp.rename(uploadPath + 'backup', uploadPath + 'backup-remove');
}
// rename active dir
const start = Math.floor(new Date().getTime() / 1000);
core.info('Rename directory "active" => "backup"');
await sftp.rename(uploadPath + 'active', uploadPath + 'backup');
// rename upload dir
core.info('Rename directory "upload" => "active"');
await sftp.rename(uploadPath + 'upload', uploadPath + 'active');
const done = Math.floor(new Date().getTime() / 1000);
core.info('### Deployment is done - Folder swap took ' + (done - start) + ' seconds! ###');
// remove backup-remove
core.info('Auto-cleanup - backup-remove is now gonna be removed from server.')
await sftp.rmdir(uploadPath + 'backup-remove', true);
core.info('### DONE ###')
})
.catch(err => {
core.setFailed(err.message);
})
.finally(() => {
sftp.end();
});
} catch (error) {
core.setFailed(error.message);
}
}
run();