add config.js...

This commit is contained in:
xmz 2024-04-18 11:52:57 +08:00
parent 01a39e3348
commit c464f67edc
4 changed files with 72 additions and 5 deletions

View File

@ -2,15 +2,18 @@ name: "ssh deploy"
description: "NodeJS action for FAST deployment with rsync/ssh and remote script execution before/after rsync"
author: "easingthemes"
inputs:
CONFIG_PATH:
description: "config path"
required: false
SSH_PRIVATE_KEY:
description: "Private key part of an SSH key pair"
required: true
required: false
REMOTE_HOST:
description: "Remote host"
required: true
required: false
REMOTE_USER:
description: "Remote user"
required: true
required: false
REMOTE_PORT:
description: "Remote port"
required: false

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{
"name": "@draganfilipovic/ssh-deploy",
"version": "4.1.10",
"version": "5.0.3",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@draganfilipovic/ssh-deploy",
"version": "4.1.10",
"version": "5.0.3",
"license": "MIT",
"dependencies": {
"rsyncwrapper": "^3.0.1"

61
src/config.js Normal file
View File

@ -0,0 +1,61 @@
const { readFileSync, existsSync } = require('fs');
const { handleError } = require('./helpers');
const githubWorkspace = process.env.GITHUB_WORKSPACE;
const inputConfigKey = 'CONFIG_PATH';
const inputSshKeyPath = 'SSH_PRIVATE_KEY_PATH';
const readConfig = (configPath) => {
if (existsSync(configPath)) {
const message = `⚠️ [FILE] ${configPath} Required file exist.`;
handleError(message, true);
return null;
}
try {
console.log(`[FILE] reading ${configPath} file ...`);
const fileContents = readFileSync(configPath, 'utf8');
return JSON.parse(fileContents);
} catch (error) {
const message = `⚠️[FILE] reading file error. configPath: ${configPath}, message: ${error.message}`;
handleError(message, true);
return null;
}
};
const readSshKey = (SshKeyPath) => {
if (existsSync(SshKeyPath)) {
const message = `⚠️ [FILE] ${SshKeyPath} Required file exist.`;
handleError(message, true);
return null;
}
try {
console.log(`[FILE] reading ${SshKeyPath} file ...`);
return readFileSync(SshKeyPath, 'utf8');
} catch (error) {
const message = `⚠️[FILE] reading file error. configPath: ${SshKeyPath}, message: ${error.message}`;
handleError(message, true);
return '';
}
};
const initConfig = () => {
const inputValue = process.env[inputConfigKey] || process.env[`INPUT_${inputConfigKey}`];
if (inputValue) {
const path = `${githubWorkspace}/${inputValue}`;
const conf = readConfig(path);
Object.keys(conf).forEach((k) => {
if (k && conf[k]) {
process.env[k] = conf[k];
if (k === inputSshKeyPath) {
process.env.SSH_PRIVATE_KEY = readSshKey(conf[k]);
}
}
});
return;
}
console.warn('⚠️ [initConfig] CONFIG_PATH is not defined');
};
module.exports = {
initConfig
};

View File

@ -1,4 +1,7 @@
const { snakeToCamel } = require('./helpers');
const { initConfig } = require('./config');
initConfig();
const inputNames = [
'REMOTE_HOST', 'REMOTE_USER', 'REMOTE_PORT',