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 github from '@actions/github';
import * as core from "@actions/core";
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;
// 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.
export const action = {
gitHubRepository: repository ? repository.full_name : '',
gitHubToken: core.getInput('GITHUB_TOKEN'),
accessToken: core.getInput('ACCESS_TOKEN'),
branch: core.getInput('BRANCH'),
baseBranch: core.getInput('BASE_BRANCH') || 'master',
pusher,
gitHubRepository: repository ? repository.full_name : "",
gitHubToken: core.getInput("GITHUB_TOKEN"),
accessToken: core.getInput("ACCESS_TOKEN"),
branch: core.getInput("BRANCH"),
baseBranch: core.getInput("BASE_BRANCH") || "master",
pusher
};
// Repository path used for commits/pushes.

View File

@ -1,25 +1,25 @@
import * as core from '@actions/core';
import {cp} from '@actions/io';
import {execute} from './util';
import {workspace, build, action, repositoryPath} from './constants';
import * as core from "@actions/core";
import { cp } from "@actions/io";
import { execute } from "./util";
import { workspace, build, action, repositoryPath } from "./constants";
/** Generates the branch if it doesn't exist on the remote.
* @returns {Promise}
*/
export async function init(): Promise<any> {
try {
const accessToken = core.getInput('ACCESS_TOKEN');
const gitHubToken = core.getInput('GITHUB_TOKEN');
const accessToken = core.getInput("ACCESS_TOKEN");
const gitHubToken = core.getInput("GITHUB_TOKEN");
if (!accessToken && !gitHubToken) {
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(
`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) {
core.setFailed(`There was an error initializing the repository: ${error}`);
} 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> {
try {
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 reset --hard`, workspace);
await execute(
`git commit --allow-empty -m "Initial ${action.branch} commit."`,
workspace,
workspace
);
await execute(`git push ${repositoryPath} ${action.branch}`, workspace);
} catch (error) {
core.setFailed(
`There was an error creating the deployment branch: ${error}`,
`There was an error creating the deployment branch: ${error}`
);
} 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}
*/
export async function deploy(): Promise<any> {
const temporaryDeploymentDirectory = 'temp-deployment-folder';
const temporaryDeploymentBranch = 'temp-deployment-branch';
const temporaryDeploymentDirectory = "temp-deployment-folder";
const temporaryDeploymentBranch = "temp-deployment-branch";
/*
Checks to see if the remote exists prior to deploying.
@ -69,19 +69,19 @@ export async function deploy(): Promise<any> {
*/
const branchExists = await execute(
`git ls-remote --heads ${repositoryPath} ${action.branch} | wc -l`,
workspace,
workspace
);
if (!branchExists) {
console.log('Deployment branch does not exist. Creating....');
console.log("Deployment branch does not exist. Creating....");
await generateBranch();
}
// 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 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. */
await cp(`${build}/.`, temporaryDeploymentDirectory, {
recursive: true,
force: true,
force: true
});
// Commits to GitHub.
await execute(`git add --all .`, temporaryDeploymentDirectory);
await execute(
`git checkout -b ${temporaryDeploymentBranch}`,
temporaryDeploymentDirectory,
temporaryDeploymentDirectory
);
await execute(
`git commit -m "Deploying to ${action.branch} from ${action.baseBranch} ${process.env.GITHUB_SHA}" --quiet`,
temporaryDeploymentDirectory,
temporaryDeploymentDirectory
);
await execute(
`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 {init, deploy} from './git';
import * as core from "@actions/core";
import { init, deploy } from "./git";
/** Initializes and runs the action. */
(async function() {
@ -10,6 +10,6 @@ import {init, deploy} from './git';
} catch (error) {
core.setFailed(error.message);
} 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.
* 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.
*/
export async function execute(cmd: string, cwd: string): Promise<any> {
let output = '';
let output = "";
await exec(cmd, [], {
cwd,
listeners: {
stdout: (data: Buffer) => {
output += data.toString().trim();
},
},
}
}
});
return Promise.resolve(output);