diff --git a/action.yml b/action.yml index 1c30b61..4c6ea5c 100644 --- a/action.yml +++ b/action.yml @@ -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 diff --git a/package-lock.json b/package-lock.json index 7cc9222..7b059e3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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" diff --git a/src/config.js b/src/config.js new file mode 100644 index 0000000..fda0591 --- /dev/null +++ b/src/config.js @@ -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 +}; diff --git a/src/inputs.js b/src/inputs.js index 2c5dd19..83117bd 100644 --- a/src/inputs.js +++ b/src/inputs.js @@ -1,4 +1,7 @@ const { snakeToCamel } = require('./helpers'); +const { initConfig } = require('./config'); + +initConfig(); const inputNames = [ 'REMOTE_HOST', 'REMOTE_USER', 'REMOTE_PORT',