-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
88 lines (74 loc) · 2.62 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
const core = require('@actions/core');
const fs = require('fs');
const path = require('path');
let Client = require('ssh2-sftp-client');
let sftp = new Client();
const host = core.getInput('host');
const port = parseInt(core.getInput('port'));
const username = core.getInput('username');
const password = core.getInput('password');
const agent = core.getInput('agent');
const privateKeyIsFile = core.getInput('privateKeyIsFile');
const passphrase = core.getInput('passphrase');
var privateKey = core.getInput('privateKey');
core.setSecret(password);
if (passphrase != undefined) {
core.setSecret(passphrase);
}
if (privateKeyIsFile == "true") {
var privateKey = fs.readFileSync(privateKey);
core.setSecret(privateKey);
}
const localPath = core.getInput('localPath');
const remotePath = core.getInput('remotePath');
const additionalPaths = core.getInput('additionalPaths')
sftp.connect({
host: host,
port: port,
username: username,
password: password,
agent: agent,
privateKey: privateKey,
passphrase: passphrase
}).then(async () => {
console.log("Connection established.");
console.log("Current working directory: " + await sftp.cwd())
await processPath(localPath, remotePath) //TODO: Instead of localPath, remotePath use key/value to uplaod multiple files at once.
const parsedAdditionalPaths = (() => {
try {
const parsedAdditionalPaths = JSON.parse(additionalPaths)
return Object.entries(parsedAdditionalPaths)
}
catch (e) {
throw "Error parsing addtionalPaths. Make sure it is a valid JSON object (key/ value pairs)."
}
})()
for (const [local, remote] of parsedAdditionalPaths) {
await processPath(local, remote)
}
}).then(() => {
console.log("Upload finished.");
return sftp.end();
}).catch(err => {
core.setFailed(`Action failed with error ${err}`);
process.exit(1);
});
async function processPath(local, remote) {
console.log("Uploading: " + local + " to " + remote)
if (fs.lstatSync(local).isDirectory()) {
return sftp.uploadDir(local, remote);
} else {
var directory = await sftp.realPath(path.dirname(remote));
if (!(await sftp.exists(directory))) {
await sftp.mkdir(directory, true);
console.log("Created directories.");
}
var modifiedPath = remote;
if (await sftp.exists(remote)) {
if ((await sftp.stat(remote)).isDirectory) {
var modifiedPath = modifiedPath + path.basename(local);
}
}
return sftp.put(fs.createReadStream(local), modifiedPath);
}
}