This commit is contained in:
SamKirkland
2020-08-26 01:28:40 -05:00
parent 1053c3278e
commit 3576f3af9c
23 changed files with 7764 additions and 5829 deletions

View File

@@ -1,91 +1,31 @@
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import fs from 'fs';
import { promisify } from 'util';
import { IActionArguments } from './types';
import * as core from "@actions/core";
import { deploy } from "@samkirkland/ftp-deploy";
import { IFtpDeployArguments } from "@samkirkland/ftp-deploy/dist/module/types";
const writeFileAsync = promisify(fs.writeFile);
const errorDeploying = "⚠️ Error deploying";
async function run() {
try {
const userArguments = getUserArguments();
await configureHost(userArguments);
await syncFiles(userArguments);
console.log("✅ Deploy Complete");
}
catch (error) {
console.error(errorDeploying);
core.setFailed(error.message);
}
}
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");
core.setFailed(error.message);
}
}
function getUserArguments(): IActionArguments {
return {
ftp_server: core.getInput("ftp-server", { required: true }),
ftp_username: core.getInput("ftp-username", { required: true }),
ftp_password: core.getInput("ftp-password", { required: true }),
local_dir: withDefault(core.getInput("local-dir"), "./"),
gitFtpArgs: withDefault(core.getInput("git-ftp-args"), ""),
knownHosts: withDefault(core.getInput("known-hosts"), "")
async function runDeployment() {
const args: IFtpDeployArguments = {
server: core.getInput("server", { required: true }),
username: core.getInput("username", { required: true }),
password: core.getInput("password", { required: true }),
protocol: core.getInput("protocol") as any, // todo fix
port: core.getInput("port") as any, // todo fix
"local-dir": core.getInput("local-dir") as any, // todo fix
"server-dir": core.getInput("server-dir") as any, // todo fix
"state-name": core.getInput("state-name") as any, // todo fix
"dry-run": core.getInput("dry-run") as any, // todo fix
"dangerous-clean-slate": core.getInput("dangerous-clean-slate") as any, // todo fix
"include": core.getInput("include") as any, // todo fix
"exclude": core.getInput("exclude") as any, // todo fix
"log-level": core.getInput("log-level") as any // todo fix
};
}
function withDefault(value: string, defaultValue: string) {
if (value === "" || value === null || value === undefined) {
return defaultValue;
}
return value;
}
/**
* Sync changed files
*/
async function syncFiles(args: IActionArguments) {
try {
await core.group("Uploading files", async () => {
return await exec.exec(
"git ftp push",
[
"--force",
"--auto-init",
"--verbose",
`--syncroot=${args.local_dir}`,
`--user=${args.ftp_username}`,
`--passwd=${args.ftp_password}`,
args.gitFtpArgs!,
args.ftp_server!
]
);
});
await deploy(args);
}
catch (error) {
core.setFailed(error.message);
core.setFailed(error);
}
}
}
runDeployment();

View File

@@ -1,31 +0,0 @@
export interface IActionArguments {
ftp_server: string | undefined;
ftp_username: string | undefined;
ftp_password: string | undefined;
/** @default "." */
local_dir: string | undefined;
/** @default "" */
gitFtpArgs: string | undefined;
/** @default "" */
knownHosts: string | undefined;
}
/**
* @see https://github.com/git-ftp/git-ftp/blob/master/man/git-ftp.1.md#exit-codes
*/
export enum gitFTPExitCode {
Successful = 0,
UnknownError = 1,
WrongUsage = 2,
MissingArguments = 3,
ErrorWhileUploading = 4,
ErrorWhileDownloading = 5,
UnknownProtocol = 6,
RemoteLocked = 7,
GitRelatedError = 8,
PreFTPPushHookFailed = 9,
LocalFileOperationFailed = 10
}