This commit is contained in:
James Ives 2019-11-07 18:36:38 -05:00
parent 31699d5092
commit f157d8c731
4 changed files with 42 additions and 42 deletions

View File

@ -1,21 +1,21 @@
import * as core from '@actions/core'; import * as core from "@actions/core";
import * as github from '@actions/github'; import * as github from "@actions/github";
const { pusher, repository } = github.context.payload; const { pusher, repository } = github.context.payload;
export const workspace: any = process.env.GITHUB_WORKSPACE; export const workspace: any = process.env.GITHUB_WORKSPACE;
// The build folder that stores the deployment data. // The build folder that stores the deployment data.
export const build = core.getInput('FOLDER', {required: true}); export const build = core.getInput("FOLDER", { required: true });
// Required action data. // Required action data.
export const action = { export const action = {
gitHubRepository: repository ? repository.full_name : '', gitHubRepository: repository ? repository.full_name : "",
gitHubToken: core.getInput('GITHUB_TOKEN'), gitHubToken: core.getInput("GITHUB_TOKEN"),
accessToken: core.getInput('ACCESS_TOKEN'), accessToken: core.getInput("ACCESS_TOKEN"),
branch: core.getInput('BRANCH'), branch: core.getInput("BRANCH"),
baseBranch: core.getInput('BASE_BRANCH') || 'master', baseBranch: core.getInput("BASE_BRANCH") || "master",
pusher, pusher
}; };
// Repository path used for commits/pushes. // Repository path used for commits/pushes.

View File

@ -1,25 +1,25 @@
import * as core from '@actions/core'; import * as core from "@actions/core";
import {cp} from '@actions/io'; import { cp } from "@actions/io";
import {execute} from './util'; import { execute } from "./util";
import {workspace, build, action, repositoryPath} from './constants'; import { workspace, build, action, repositoryPath } from "./constants";
/** Generates the branch if it doesn't exist on the remote. /** Generates the branch if it doesn't exist on the remote.
* @returns {Promise} * @returns {Promise}
*/ */
export async function init(): Promise<any> { export async function init(): Promise<any> {
try { try {
const accessToken = core.getInput('ACCESS_TOKEN'); const accessToken = core.getInput("ACCESS_TOKEN");
const gitHubToken = core.getInput('GITHUB_TOKEN'); const gitHubToken = core.getInput("GITHUB_TOKEN");
if (!accessToken && !gitHubToken) { if (!accessToken && !gitHubToken) {
core.setFailed( core.setFailed(
'You must provide the action with either a Personal Access Token or the GitHub Token secret in order to deploy.', "You must provide the action with either a Personal Access Token or the GitHub Token secret in order to deploy."
); );
} }
if (build.startsWith('/') || build.startsWith('./')) { if (build.startsWith("/") || build.startsWith("./")) {
core.setFailed( core.setFailed(
`The deployment folder cannot be prefixed with '/' or './'. Instead reference the folder name directly.`, `The deployment folder cannot be prefixed with '/' or './'. Instead reference the folder name directly.`
); );
} }
@ -29,7 +29,7 @@ export async function init(): Promise<any> {
} catch (error) { } catch (error) {
core.setFailed(`There was an error initializing the repository: ${error}`); core.setFailed(`There was an error initializing the repository: ${error}`);
} finally { } finally {
Promise.resolve('Initializion step complete...'); Promise.resolve("Initializion step complete...");
} }
} }
@ -39,20 +39,20 @@ export async function init(): Promise<any> {
export async function generateBranch(): Promise<any> { export async function generateBranch(): Promise<any> {
try { try {
console.log(`Creating ${action.branch} branch...`); console.log(`Creating ${action.branch} branch...`);
await execute(`git checkout ${action.baseBranch || 'master'}`, workspace); await execute(`git checkout ${action.baseBranch || "master"}`, workspace);
await execute(`git checkout --orphan ${action.branch}`, workspace); await execute(`git checkout --orphan ${action.branch}`, workspace);
await execute(`git reset --hard`, workspace); await execute(`git reset --hard`, workspace);
await execute( await execute(
`git commit --allow-empty -m "Initial ${action.branch} commit."`, `git commit --allow-empty -m "Initial ${action.branch} commit."`,
workspace, workspace
); );
await execute(`git push ${repositoryPath} ${action.branch}`, workspace); await execute(`git push ${repositoryPath} ${action.branch}`, workspace);
} catch (error) { } catch (error) {
core.setFailed( core.setFailed(
`There was an error creating the deployment branch: ${error}`, `There was an error creating the deployment branch: ${error}`
); );
} finally { } finally {
Promise.resolve('Deployment branch creation step complete...'); Promise.resolve("Deployment branch creation step complete...");
} }
} }
@ -60,8 +60,8 @@ export async function generateBranch(): Promise<any> {
* @returns {Promise} * @returns {Promise}
*/ */
export async function deploy(): Promise<any> { export async function deploy(): Promise<any> {
const temporaryDeploymentDirectory = 'temp-deployment-folder'; const temporaryDeploymentDirectory = "temp-deployment-folder";
const temporaryDeploymentBranch = 'temp-deployment-branch'; const temporaryDeploymentBranch = "temp-deployment-branch";
/* /*
Checks to see if the remote exists prior to deploying. Checks to see if the remote exists prior to deploying.
@ -69,19 +69,19 @@ export async function deploy(): Promise<any> {
*/ */
const branchExists = await execute( const branchExists = await execute(
`git ls-remote --heads ${repositoryPath} ${action.branch} | wc -l`, `git ls-remote --heads ${repositoryPath} ${action.branch} | wc -l`,
workspace, workspace
); );
if (!branchExists) { if (!branchExists) {
console.log('Deployment branch does not exist. Creating....'); console.log("Deployment branch does not exist. Creating....");
await generateBranch(); await generateBranch();
} }
// Checks out the base branch to begin the deployment process. // Checks out the base branch to begin the deployment process.
await execute(`git checkout ${action.baseBranch || 'master'}`, workspace); await execute(`git checkout ${action.baseBranch || "master"}`, workspace);
await execute(`git fetch origin`, workspace); await execute(`git fetch origin`, workspace);
await execute( await execute(
`git worktree add --checkout ${temporaryDeploymentDirectory} origin/${action.branch}`, `git worktree add --checkout ${temporaryDeploymentDirectory} origin/${action.branch}`,
workspace, workspace
); );
/* /*
@ -89,23 +89,23 @@ export async function deploy(): Promise<any> {
Allows the user to specify the root if '.' is provided. */ Allows the user to specify the root if '.' is provided. */
await cp(`${build}/.`, temporaryDeploymentDirectory, { await cp(`${build}/.`, temporaryDeploymentDirectory, {
recursive: true, recursive: true,
force: true, force: true
}); });
// Commits to GitHub. // Commits to GitHub.
await execute(`git add --all .`, temporaryDeploymentDirectory); await execute(`git add --all .`, temporaryDeploymentDirectory);
await execute( await execute(
`git checkout -b ${temporaryDeploymentBranch}`, `git checkout -b ${temporaryDeploymentBranch}`,
temporaryDeploymentDirectory, temporaryDeploymentDirectory
); );
await execute( await execute(
`git commit -m "Deploying to ${action.branch} from ${action.baseBranch} ${process.env.GITHUB_SHA}" --quiet`, `git commit -m "Deploying to ${action.branch} from ${action.baseBranch} ${process.env.GITHUB_SHA}" --quiet`,
temporaryDeploymentDirectory, temporaryDeploymentDirectory
); );
await execute( await execute(
`git push ${repositoryPath} ${temporaryDeploymentBranch}:${action.branch}`, `git push ${repositoryPath} ${temporaryDeploymentBranch}:${action.branch}`,
temporaryDeploymentDirectory, temporaryDeploymentDirectory
); );
Promise.resolve('Commit step complete...'); Promise.resolve("Commit step complete...");
} }

View File

@ -1,5 +1,5 @@
import * as core from '@actions/core'; import * as core from "@actions/core";
import {init, deploy} from './git'; import { init, deploy } from "./git";
/** Initializes and runs the action. */ /** Initializes and runs the action. */
(async function() { (async function() {
@ -10,6 +10,6 @@ import {init, deploy} from './git';
} catch (error) { } catch (error) {
core.setFailed(error.message); core.setFailed(error.message);
} finally { } finally {
console.log('Completed Deployment'); console.log("Completed Deployment");
} }
})(); })();

View File

@ -1,4 +1,4 @@
import {exec} from '@actions/exec'; import { exec } from "@actions/exec";
/** Wrapper around the GitHub toolkit exec command which returns the output. /** Wrapper around the GitHub toolkit exec command which returns the output.
* Also allows you to easily toggle the current working directory. * Also allows you to easily toggle the current working directory.
@ -7,15 +7,15 @@ import {exec} from '@actions/exec';
* @returns {Promise} - The output from the command. * @returns {Promise} - The output from the command.
*/ */
export async function execute(cmd: string, cwd: string): Promise<any> { export async function execute(cmd: string, cwd: string): Promise<any> {
let output = ''; let output = "";
await exec(cmd, [], { await exec(cmd, [], {
cwd, cwd,
listeners: { listeners: {
stdout: (data: Buffer) => { stdout: (data: Buffer) => {
output += data.toString().trim(); output += data.toString().trim();
}, }
}, }
}); });
return Promise.resolve(output); return Promise.resolve(output);