update config.js

This commit is contained in:
xmz 2024-04-20 13:35:06 +08:00
parent c5a3f7ea09
commit 0cd154ef66

View File

@ -1,8 +1,12 @@
const { readFileSync, existsSync } = require('fs'); const { readFileSync, existsSync } = require('fs');
const { validateRequiredInputs } = require('./helpers');
const githubWorkspace = process.env.GITHUB_WORKSPACE; const githubWorkspace = process.env.GITHUB_WORKSPACE;
const inputConfigKey = 'CONFIG_PATH'; const SSH_PRIVATE_KEY_PATH = 'SSH_PRIVATE_KEY_PATH';
const inputSshKeyPath = 'SSH_PRIVATE_KEY_PATH';
const DEPLOY_CONFIG_PATH = 'DEPLOY_CONFIG_PATH';
const SSH_CMD_CONFIG_PATH = 'SSH_CMD_CONFIG_PATH';
const sshCmdRemoteKey = 'sshCmdRemote';
const readConfig = (configPath) => { const readConfig = (configPath) => {
if (!existsSync(configPath)) { if (!existsSync(configPath)) {
@ -34,23 +38,28 @@ const readSshKey = (SshKeyPath) => {
}; };
const initConfig = () => { const initConfig = () => {
console.info('⚠️ [initConfig] start.'); console.info('[initConfig] start.');
const inputValue = process.env[inputConfigKey] || process.env[`INPUT_${inputConfigKey}`]; const deployConfigPath = process.env[DEPLOY_CONFIG_PATH] || process.env[`INPUT_${DEPLOY_CONFIG_PATH}`];
if (inputValue) { const sshCmdPath = process.env[SSH_CMD_CONFIG_PATH] || process.env[`INPUT_${SSH_CMD_CONFIG_PATH}`];
const path = `${githubWorkspace}/${inputValue}`; validateRequiredInputs({ deployConfigPath, sshCmdPath });
const conf = readConfig(path); const deployConfig = readConfig(`${githubWorkspace}/${deployConfigPath}`);
Object.keys(conf).forEach((k) => { const sshCmdConfig = readConfig(`${githubWorkspace}/${sshCmdPath}`);
if (k && conf[k]) { if (deployConfig && deployConfig[sshCmdRemoteKey]
process.env[k] = conf[k]; && sshCmdConfig && sshCmdConfig[deployConfig[sshCmdRemoteKey]]
if (k === inputSshKeyPath) { ) {
process.env.SSH_PRIVATE_KEY = readSshKey(conf[k]); const sshConfig = sshCmdConfig[deployConfig[sshCmdRemoteKey]];
Object.keys(sshConfig).forEach((k) => {
if (k && sshConfig[k]) {
process.env[k] = sshConfig[k];
if (k === SSH_PRIVATE_KEY_PATH) {
process.env.SSH_PRIVATE_KEY = readSshKey(sshConfig[k]);
} }
} }
}); });
console.info('⚠️ [initConfig] end.'); console.info('✅ [initConfig] success.');
return; return;
} }
console.warn('⚠️ [initConfig] CONFIG_PATH is not defined'); console.warn(`⚠️ [initConfig] config is not match. sshCmdRemote: ${deployConfig[sshCmdRemoteKey]}`);
}; };
module.exports = { module.exports = {