mirror of
https://github.com/SamKirkland/FTP-Deploy-Action.git
synced 2025-08-14 14:05:05 +00:00
Update main.ts
Code Cleanup changes:
- Pulled out this PR into a new function `configureHost`
- Placed args.knownHosts empty at the top of the function, this is known as "fail fast" and helps make the code more readable. Instead of wrapping our code in if/else blocks.
- Converted fs.writeFile to a awaitable function to make it more readable
- Pulled out sshFolder into a const
- Removed `console.log('Wrote ' + process.env['HOME'] + '/.ssh/known_hosts');` logging. Probably not needed because we log `✅ Configured known_hosts` just 2 lines later
- Small formatting changes
Bug fixes:
- Converted fs.writeFile to a awaitable function, previously the code continued to execute so this could open up a race condition. Also it's more readable :)
- Race condition example: Previously we asked node to write the file `known_hosts` but we never verified the IO operation completed before modifying the files permission, or deploying the site. If this IO operation wasn't immediate the next function would throw or the deploy would error out.
- Any exception within the new method was swallowed because we had a catch without a throw. In this case let's end the program run instead of attempting to deploy
-
This commit is contained in:
parent
db3b78d8e7
commit
af948b8060
44
src/main.ts
44
src/main.ts
@ -1,30 +1,16 @@
|
||||
import * as core from '@actions/core';
|
||||
import * as exec from '@actions/exec';
|
||||
import fs from 'fs';
|
||||
import { promisify } from 'util';
|
||||
import { IActionArguments } from './types';
|
||||
|
||||
const writeFileAsync = promisify(fs.writeFile);
|
||||
|
||||
async function run() {
|
||||
const userArguments = getUserArguments();
|
||||
if ( '' !== userArguments.knownHosts ) {
|
||||
try {
|
||||
await exec.exec(`mkdir -v -p ${process.env['HOME']}/.ssh`);
|
||||
await exec.exec(`chmod 700 ${process.env['HOME']}/.ssh`);
|
||||
fs.writeFile(
|
||||
process.env['HOME'] + '/.ssh/known_hosts',
|
||||
userArguments.knownHosts,
|
||||
(err) => {
|
||||
if (err) throw err;
|
||||
console.log('Wrote ' + process.env['HOME'] + '/.ssh/known_hosts');
|
||||
}
|
||||
);
|
||||
await exec.exec(`chmod 755 ${process.env['HOME']}/.ssh/known_hosts`);
|
||||
console.log("✅ Configured known_hosts");
|
||||
} catch( error ) {
|
||||
console.error("⚠️ Error configuring known_hosts")
|
||||
core.setFailed(error.message);;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
await configureHost(userArguments);
|
||||
await syncFiles(userArguments);
|
||||
|
||||
console.log("✅ Deploy Complete");
|
||||
@ -37,6 +23,26 @@ async function run() {
|
||||
|
||||
run();
|
||||
|
||||
async function configureHost(args: IActionArguments): Promise<void> {
|
||||
if (args.knownHosts === "") {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const sshFolder = `${process.env['HOME']}/.ssh`;
|
||||
|
||||
await exec.exec(`mkdir -v -p ${sshFolder}`);
|
||||
await exec.exec(`chmod 700 ${sshFolder}`);
|
||||
writeFileAsync(`${sshFolder}/known_hosts`, args.knownHosts);
|
||||
await exec.exec(`chmod 755 ${sshFolder}/known_hosts`);
|
||||
|
||||
console.log("✅ Configured known_hosts");
|
||||
}
|
||||
catch (error) {
|
||||
console.error("⚠️ Error configuring known_hosts");
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
function getUserArguments(): IActionArguments {
|
||||
return {
|
||||
|
Loading…
x
Reference in New Issue
Block a user