mirror of
https://github.com/JamesIves/github-pages-deploy-action.git
synced 2023-12-15 20:03:39 +08:00
Some additional integration test fixes
This commit is contained in:
parent
d51113f1c1
commit
41de4b88ee
2
.github/workflows/integration.yml
vendored
2
.github/workflows/integration.yml
vendored
@ -36,7 +36,7 @@ jobs:
|
||||
- name: Build and Deploy
|
||||
uses: JamesIves/github-pages-deploy-action@releases/v3-test
|
||||
with:
|
||||
ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
BRANCH: gh-pages
|
||||
FOLDER: integration
|
||||
BASE_BRANCH: dev
|
||||
|
23
__tests__/execute.test.ts
Normal file
23
__tests__/execute.test.ts
Normal file
@ -0,0 +1,23 @@
|
||||
import {execute} from '../src/execute';
|
||||
import {exec} from '@actions/exec';
|
||||
|
||||
jest.mock('@actions/exec', () => ({
|
||||
exec: jest.fn()
|
||||
}))
|
||||
|
||||
describe('execute', () => {
|
||||
describe('execute', () => {
|
||||
it('should be called with the correct arguements', async() => {
|
||||
await execute('echo Montezuma', './')
|
||||
|
||||
expect(exec).toBeCalledWith(
|
||||
"echo Montezuma", [], {
|
||||
cwd: "./",
|
||||
listeners: {
|
||||
stdout: expect.any(Function)
|
||||
}
|
||||
}
|
||||
)
|
||||
});
|
||||
})
|
||||
})
|
@ -5,11 +5,11 @@ process.env["GITHUB_SHA"] = "123";
|
||||
import _ from "lodash";
|
||||
import { action } from "../src/constants";
|
||||
import { deploy, generateBranch, init, switchToBaseBranch } from "../src/git";
|
||||
import { execute } from "../src/util";
|
||||
import { execute } from "../src/execute";
|
||||
|
||||
const originalAction = _.cloneDeep(action);
|
||||
|
||||
jest.mock("../src/util", () => ({
|
||||
jest.mock("../src/execute", () => ({
|
||||
execute: jest.fn()
|
||||
}));
|
||||
|
||||
@ -30,7 +30,7 @@ describe("git", () => {
|
||||
});
|
||||
|
||||
const call = await init();
|
||||
expect(execute).toBeCalledTimes(3);
|
||||
expect(execute).toBeCalledTimes(4);
|
||||
expect(call).toBe("Initialization step complete...");
|
||||
});
|
||||
|
||||
@ -46,7 +46,7 @@ describe("git", () => {
|
||||
|
||||
const call = await init();
|
||||
|
||||
expect(execute).toBeCalledTimes(3);
|
||||
expect(execute).toBeCalledTimes(4);
|
||||
expect(call).toBe("Initialization step complete...");
|
||||
});
|
||||
|
||||
@ -109,7 +109,7 @@ describe("git", () => {
|
||||
|
||||
const call = await init();
|
||||
|
||||
expect(execute).toBeCalledTimes(3);
|
||||
expect(execute).toBeCalledTimes(4);
|
||||
expect(call).toBe("Initialization step complete...");
|
||||
});
|
||||
});
|
||||
|
@ -1,23 +1,20 @@
|
||||
import {execute} from '../src/util';
|
||||
import {exec} from '@actions/exec';
|
||||
import {isNullOrUndefined} from '../src/util';
|
||||
|
||||
jest.mock('@actions/exec', () => ({
|
||||
exec: jest.fn()
|
||||
}))
|
||||
|
||||
describe('util', () => {
|
||||
describe('execute', () => {
|
||||
it('should be called with the correct arguements', async() => {
|
||||
await execute('echo Montezuma', './')
|
||||
describe('isNullOrUndefined', () => {
|
||||
it('should return true if the value is null', async() => {
|
||||
const value = null;
|
||||
expect(isNullOrUndefined(value)).toBeTruthy()
|
||||
|
||||
expect(exec).toBeCalledWith(
|
||||
"echo Montezuma", [], {
|
||||
cwd: "./",
|
||||
listeners: {
|
||||
stdout: expect.any(Function)
|
||||
}
|
||||
}
|
||||
)
|
||||
it('should return true if the value is undefined', async() => {
|
||||
const value = undefined;
|
||||
expect(isNullOrUndefined(value)).toBeTruthy()
|
||||
});
|
||||
|
||||
it('should return false if the value is defined', async() => {
|
||||
const value = 'montezuma';
|
||||
expect(isNullOrUndefined(value)).toBeFalsy()
|
||||
});
|
||||
})
|
||||
})
|
33
lib/execute.js
Normal file
33
lib/execute.js
Normal file
@ -0,0 +1,33 @@
|
||||
"use strict";
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const exec_1 = require("@actions/exec");
|
||||
/** Wrapper around the GitHub toolkit exec command which returns the output.
|
||||
* Also allows you to easily toggle the current working directory.
|
||||
* @param {String} cmd = The command to execute.
|
||||
* @param {String} cwd - The current working directory.
|
||||
* @returns {Promise} - The output from the command.
|
||||
*/
|
||||
function execute(cmd, cwd) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
let output = "";
|
||||
yield exec_1.exec(cmd, [], {
|
||||
cwd,
|
||||
listeners: {
|
||||
stdout: (data) => {
|
||||
output += data.toString().trim();
|
||||
}
|
||||
}
|
||||
});
|
||||
return Promise.resolve(output);
|
||||
});
|
||||
}
|
||||
exports.execute = execute;
|
47
lib/git.js
47
lib/git.js
@ -17,6 +17,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const core = __importStar(require("@actions/core"));
|
||||
const execute_1 = require("./execute");
|
||||
const util_1 = require("./util");
|
||||
const constants_1 = require("./constants");
|
||||
/** Generates the branch if it doesn't exist on the remote.
|
||||
@ -25,16 +26,18 @@ const constants_1 = require("./constants");
|
||||
function init() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
try {
|
||||
if (!constants_1.action.accessToken && !constants_1.action.gitHubToken) {
|
||||
if (util_1.isNullOrUndefined(constants_1.action.accessToken) &&
|
||||
util_1.isNullOrUndefined(constants_1.action.gitHubToken)) {
|
||||
return core.setFailed("You must provide the action with either a Personal Access Token or the GitHub Token secret in order to deploy.");
|
||||
}
|
||||
if (constants_1.action.build.startsWith("/") || constants_1.action.build.startsWith("./")) {
|
||||
console.log("2");
|
||||
return core.setFailed(`The deployment folder cannot be prefixed with '/' or './'. Instead reference the folder name directly.`);
|
||||
}
|
||||
yield util_1.execute(`git init`, constants_1.workspace);
|
||||
yield util_1.execute(`git config user.name ${constants_1.action.name}`, constants_1.workspace);
|
||||
yield util_1.execute(`git config user.email ${constants_1.action.email}`, constants_1.workspace);
|
||||
yield util_1.execute(`git fetch`, constants_1.workspace);
|
||||
yield execute_1.execute(`git init`, constants_1.workspace);
|
||||
yield execute_1.execute(`git config user.name ${constants_1.action.name}`, constants_1.workspace);
|
||||
yield execute_1.execute(`git config user.email ${constants_1.action.email}`, constants_1.workspace);
|
||||
yield execute_1.execute(`git fetch`, constants_1.workspace);
|
||||
}
|
||||
catch (error) {
|
||||
core.setFailed(`There was an error initializing the repository: ${error}`);
|
||||
@ -50,7 +53,7 @@ exports.init = init;
|
||||
*/
|
||||
function switchToBaseBranch() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
yield util_1.execute(constants_1.action.baseBranch
|
||||
yield execute_1.execute(constants_1.action.baseBranch
|
||||
? `git switch ${constants_1.action.baseBranch}`
|
||||
: `git checkout --progress --force ${constants_1.action.defaultBranch}`, constants_1.workspace);
|
||||
return Promise.resolve("Switched to the base branch...");
|
||||
@ -65,11 +68,11 @@ function generateBranch() {
|
||||
try {
|
||||
console.log(`Creating ${constants_1.action.branch} branch... 🔧`);
|
||||
yield switchToBaseBranch();
|
||||
yield util_1.execute(`git switch --orphan ${constants_1.action.branch}`, constants_1.workspace);
|
||||
yield util_1.execute(`git reset --hard`, constants_1.workspace);
|
||||
yield util_1.execute(`git commit --allow-empty -m "Initial ${constants_1.action.branch} commit."`, constants_1.workspace);
|
||||
yield util_1.execute(`git push ${constants_1.repositoryPath} ${constants_1.action.branch}`, constants_1.workspace);
|
||||
yield util_1.execute(`git fetch`, constants_1.workspace);
|
||||
yield execute_1.execute(`git switch --orphan ${constants_1.action.branch}`, constants_1.workspace);
|
||||
yield execute_1.execute(`git reset --hard`, constants_1.workspace);
|
||||
yield execute_1.execute(`git commit --allow-empty -m "Initial ${constants_1.action.branch} commit."`, constants_1.workspace);
|
||||
yield execute_1.execute(`git push ${constants_1.repositoryPath} ${constants_1.action.branch}`, constants_1.workspace);
|
||||
yield execute_1.execute(`git fetch`, constants_1.workspace);
|
||||
}
|
||||
catch (error) {
|
||||
core.setFailed(`There was an error creating the deployment branch: ${error} ❌`);
|
||||
@ -91,15 +94,15 @@ function deploy() {
|
||||
Checks to see if the remote exists prior to deploying.
|
||||
If the branch doesn't exist it gets created here as an orphan.
|
||||
*/
|
||||
const branchExists = yield util_1.execute(`git ls-remote --heads ${constants_1.repositoryPath} ${constants_1.action.branch} | wc -l`, constants_1.workspace);
|
||||
const branchExists = yield execute_1.execute(`git ls-remote --heads ${constants_1.repositoryPath} ${constants_1.action.branch} | wc -l`, constants_1.workspace);
|
||||
if (!branchExists) {
|
||||
console.log("Deployment branch does not exist. Creating....");
|
||||
yield generateBranch();
|
||||
}
|
||||
// Checks out the base branch to begin the deployment process.
|
||||
yield switchToBaseBranch();
|
||||
yield util_1.execute(`git fetch ${constants_1.repositoryPath}`, constants_1.workspace);
|
||||
yield util_1.execute(`git worktree add --checkout ${temporaryDeploymentDirectory} origin/${constants_1.action.branch}`, constants_1.workspace);
|
||||
yield execute_1.execute(`git fetch ${constants_1.repositoryPath}`, constants_1.workspace);
|
||||
yield execute_1.execute(`git worktree add --checkout ${temporaryDeploymentDirectory} origin/${constants_1.action.branch}`, constants_1.workspace);
|
||||
// Ensures that items that need to be excluded from the clean job get parsed.
|
||||
let excludes = "";
|
||||
if (constants_1.action.clean && constants_1.action.cleanExclude) {
|
||||
@ -115,25 +118,25 @@ function deploy() {
|
||||
Pushes all of the build files into the deployment directory.
|
||||
Allows the user to specify the root if '.' is provided.
|
||||
rysync is used to prevent file duplication. */
|
||||
yield util_1.execute(`rsync -q -av --progress ${constants_1.action.build}/. ${constants_1.action.targetFolder
|
||||
yield execute_1.execute(`rsync -q -av --progress ${constants_1.action.build}/. ${constants_1.action.targetFolder
|
||||
? `${temporaryDeploymentDirectory}/${constants_1.action.targetFolder}`
|
||||
: temporaryDeploymentDirectory} ${constants_1.action.clean
|
||||
? `--delete ${excludes} --exclude CNAME --exclude .nojekyll`
|
||||
: ""} --exclude .git --exclude .github ${constants_1.action.build === constants_1.root ? `--exclude ${temporaryDeploymentDirectory}` : ""}`, constants_1.workspace);
|
||||
const hasFilesToCommit = yield util_1.execute(`git status --porcelain`, temporaryDeploymentDirectory);
|
||||
const hasFilesToCommit = yield execute_1.execute(`git status --porcelain`, temporaryDeploymentDirectory);
|
||||
if (!hasFilesToCommit && !constants_1.isTest) {
|
||||
console.log("There is nothing to commit. Exiting... ✅");
|
||||
return Promise.resolve();
|
||||
}
|
||||
// Commits to GitHub.
|
||||
yield util_1.execute(`git add --all .`, temporaryDeploymentDirectory);
|
||||
yield util_1.execute(`git switch -c ${temporaryDeploymentBranch}`, temporaryDeploymentDirectory);
|
||||
yield util_1.execute(`git commit -m "Deploying to ${constants_1.action.branch} from ${constants_1.action.baseBranch} ${process.env.GITHUB_SHA}" --quiet`, temporaryDeploymentDirectory);
|
||||
yield util_1.execute(`git push --force ${constants_1.repositoryPath} ${temporaryDeploymentBranch}:${constants_1.action.branch}`, temporaryDeploymentDirectory);
|
||||
yield execute_1.execute(`git add --all .`, temporaryDeploymentDirectory);
|
||||
yield execute_1.execute(`git switch -c ${temporaryDeploymentBranch}`, temporaryDeploymentDirectory);
|
||||
yield execute_1.execute(`git commit -m "Deploying to ${constants_1.action.branch} from ${constants_1.action.baseBranch} ${process.env.GITHUB_SHA}" --quiet`, temporaryDeploymentDirectory);
|
||||
yield execute_1.execute(`git push --force ${constants_1.repositoryPath} ${temporaryDeploymentBranch}:${constants_1.action.branch}`, temporaryDeploymentDirectory);
|
||||
// Cleans up temporary files/folders and restores the git state.
|
||||
console.log("Running post deployment cleanup jobs... 🔧");
|
||||
yield util_1.execute(`rm -rf ${temporaryDeploymentDirectory}`, constants_1.workspace);
|
||||
yield util_1.execute(`git checkout --progress --force ${constants_1.action.defaultBranch}`, constants_1.workspace);
|
||||
yield execute_1.execute(`rm -rf ${temporaryDeploymentDirectory}`, constants_1.workspace);
|
||||
yield execute_1.execute(`git checkout --progress --force ${constants_1.action.defaultBranch}`, constants_1.workspace);
|
||||
return Promise.resolve("Commit step complete...");
|
||||
});
|
||||
}
|
||||
|
43
lib/src/constants.js
Normal file
43
lib/src/constants.js
Normal file
@ -0,0 +1,43 @@
|
||||
"use strict";
|
||||
var __importStar = (this && this.__importStar) || function (mod) {
|
||||
if (mod && mod.__esModule) return mod;
|
||||
var result = {};
|
||||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
||||
result["default"] = mod;
|
||||
return result;
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const core = __importStar(require("@actions/core"));
|
||||
const github = __importStar(require("@actions/github"));
|
||||
const { pusher, repository } = github.context.payload;
|
||||
exports.workspace = process.env.GITHUB_WORKSPACE;
|
||||
exports.folder = core.getInput("FOLDER", { required: true });
|
||||
exports.root = ".";
|
||||
exports.isTest = process.env.UNIT_TEST;
|
||||
// Required action data.
|
||||
exports.action = {
|
||||
build: exports.folder,
|
||||
gitHubRepository: repository && repository.full_name
|
||||
? repository.full_name
|
||||
: process.env.GITHUB_REPOSITORY,
|
||||
gitHubToken: core.getInput("GITHUB_TOKEN"),
|
||||
accessToken: core.getInput("ACCESS_TOKEN"),
|
||||
branch: core.getInput("BRANCH"),
|
||||
targetFolder: core.getInput("TARGET_FOLDER"),
|
||||
baseBranch: core.getInput("BASE_BRANCH"),
|
||||
defaultBranch: process.env.GITHUB_SHA ? process.env.GITHUB_SHA : "master",
|
||||
name: pusher && pusher.name
|
||||
? pusher.name
|
||||
: process.env.GITHUB_ACTOR
|
||||
? process.env.GITHUB_ACTOR
|
||||
: "GitHub Pages Deploy Action",
|
||||
email: pusher && pusher.email
|
||||
? pusher.email
|
||||
: `${process.env.GITHUB_ACTOR ||
|
||||
"github-pages-deploy-action"}@users.noreply.github.com`,
|
||||
clean: core.getInput("CLEAN"),
|
||||
cleanExclude: core.getInput("CLEAN_EXCLUDE")
|
||||
};
|
||||
// Repository path used for commits/pushes.
|
||||
exports.repositoryPath = `https://${exports.action.accessToken ||
|
||||
`x-access-token:${exports.action.gitHubToken}`}@github.com/${exports.action.gitHubRepository}.git`;
|
33
lib/src/execute.js
Normal file
33
lib/src/execute.js
Normal file
@ -0,0 +1,33 @@
|
||||
"use strict";
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const exec_1 = require("@actions/exec");
|
||||
/** Wrapper around the GitHub toolkit exec command which returns the output.
|
||||
* Also allows you to easily toggle the current working directory.
|
||||
* @param {String} cmd = The command to execute.
|
||||
* @param {String} cwd - The current working directory.
|
||||
* @returns {Promise} - The output from the command.
|
||||
*/
|
||||
function execute(cmd, cwd) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
let output = "";
|
||||
yield exec_1.exec(cmd, [], {
|
||||
cwd,
|
||||
listeners: {
|
||||
stdout: (data) => {
|
||||
output += data.toString().trim();
|
||||
}
|
||||
}
|
||||
});
|
||||
return Promise.resolve(output);
|
||||
});
|
||||
}
|
||||
exports.execute = execute;
|
143
lib/src/git.js
Normal file
143
lib/src/git.js
Normal file
@ -0,0 +1,143 @@
|
||||
"use strict";
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var __importStar = (this && this.__importStar) || function (mod) {
|
||||
if (mod && mod.__esModule) return mod;
|
||||
var result = {};
|
||||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
||||
result["default"] = mod;
|
||||
return result;
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const core = __importStar(require("@actions/core"));
|
||||
const execute_1 = require("./execute");
|
||||
const util_1 = require("./util");
|
||||
const constants_1 = require("./constants");
|
||||
/** Generates the branch if it doesn't exist on the remote.
|
||||
* @returns {Promise}
|
||||
*/
|
||||
function init() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
try {
|
||||
if (util_1.isNullOrUndefined(constants_1.action.accessToken) &&
|
||||
util_1.isNullOrUndefined(constants_1.action.gitHubToken)) {
|
||||
return core.setFailed("You must provide the action with either a Personal Access Token or the GitHub Token secret in order to deploy.");
|
||||
}
|
||||
if (constants_1.action.build.startsWith("/") || constants_1.action.build.startsWith("./")) {
|
||||
console.log("2");
|
||||
return core.setFailed(`The deployment folder cannot be prefixed with '/' or './'. Instead reference the folder name directly.`);
|
||||
}
|
||||
yield execute_1.execute(`git init`, constants_1.workspace);
|
||||
yield execute_1.execute(`git config user.name ${constants_1.action.name}`, constants_1.workspace);
|
||||
yield execute_1.execute(`git config user.email ${constants_1.action.email}`, constants_1.workspace);
|
||||
yield execute_1.execute(`git fetch`, constants_1.workspace);
|
||||
}
|
||||
catch (error) {
|
||||
core.setFailed(`There was an error initializing the repository: ${error}`);
|
||||
}
|
||||
finally {
|
||||
return Promise.resolve("Initialization step complete...");
|
||||
}
|
||||
});
|
||||
}
|
||||
exports.init = init;
|
||||
/** Switches to the base branch.
|
||||
* @returns {Promise}
|
||||
*/
|
||||
function switchToBaseBranch() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
yield execute_1.execute(constants_1.action.baseBranch
|
||||
? `git switch ${constants_1.action.baseBranch}`
|
||||
: `git checkout --progress --force ${constants_1.action.defaultBranch}`, constants_1.workspace);
|
||||
return Promise.resolve("Switched to the base branch...");
|
||||
});
|
||||
}
|
||||
exports.switchToBaseBranch = switchToBaseBranch;
|
||||
/** Generates the branch if it doesn't exist on the remote.
|
||||
* @returns {Promise}
|
||||
*/
|
||||
function generateBranch() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
try {
|
||||
console.log(`Creating ${constants_1.action.branch} branch... 🔧`);
|
||||
yield switchToBaseBranch();
|
||||
yield execute_1.execute(`git switch --orphan ${constants_1.action.branch}`, constants_1.workspace);
|
||||
yield execute_1.execute(`git reset --hard`, constants_1.workspace);
|
||||
yield execute_1.execute(`git commit --allow-empty -m "Initial ${constants_1.action.branch} commit."`, constants_1.workspace);
|
||||
yield execute_1.execute(`git push ${constants_1.repositoryPath} ${constants_1.action.branch}`, constants_1.workspace);
|
||||
yield execute_1.execute(`git fetch`, constants_1.workspace);
|
||||
}
|
||||
catch (error) {
|
||||
core.setFailed(`There was an error creating the deployment branch: ${error} ❌`);
|
||||
}
|
||||
finally {
|
||||
return Promise.resolve("Deployment branch creation step complete... ✅");
|
||||
}
|
||||
});
|
||||
}
|
||||
exports.generateBranch = generateBranch;
|
||||
/** Runs the necessary steps to make the deployment.
|
||||
* @returns {Promise}
|
||||
*/
|
||||
function deploy() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
const temporaryDeploymentDirectory = "gh-action-temp-deployment-folder";
|
||||
const temporaryDeploymentBranch = "gh-action-temp-deployment-branch";
|
||||
/*
|
||||
Checks to see if the remote exists prior to deploying.
|
||||
If the branch doesn't exist it gets created here as an orphan.
|
||||
*/
|
||||
const branchExists = yield execute_1.execute(`git ls-remote --heads ${constants_1.repositoryPath} ${constants_1.action.branch} | wc -l`, constants_1.workspace);
|
||||
if (!branchExists) {
|
||||
console.log("Deployment branch does not exist. Creating....");
|
||||
yield generateBranch();
|
||||
}
|
||||
// Checks out the base branch to begin the deployment process.
|
||||
yield switchToBaseBranch();
|
||||
yield execute_1.execute(`git fetch ${constants_1.repositoryPath}`, constants_1.workspace);
|
||||
yield execute_1.execute(`git worktree add --checkout ${temporaryDeploymentDirectory} origin/${constants_1.action.branch}`, constants_1.workspace);
|
||||
// Ensures that items that need to be excluded from the clean job get parsed.
|
||||
let excludes = "";
|
||||
if (constants_1.action.clean && constants_1.action.cleanExclude) {
|
||||
try {
|
||||
const excludedItems = JSON.parse(constants_1.action.cleanExclude);
|
||||
excludedItems.forEach((item) => (excludes += `--exclude ${item} `));
|
||||
}
|
||||
catch (_a) {
|
||||
console.log("There was an error parsing your CLEAN_EXCLUDE items. Please refer to the README for more details. ❌");
|
||||
}
|
||||
}
|
||||
/*
|
||||
Pushes all of the build files into the deployment directory.
|
||||
Allows the user to specify the root if '.' is provided.
|
||||
rysync is used to prevent file duplication. */
|
||||
yield execute_1.execute(`rsync -q -av --progress ${constants_1.action.build}/. ${constants_1.action.targetFolder
|
||||
? `${temporaryDeploymentDirectory}/${constants_1.action.targetFolder}`
|
||||
: temporaryDeploymentDirectory} ${constants_1.action.clean
|
||||
? `--delete ${excludes} --exclude CNAME --exclude .nojekyll`
|
||||
: ""} --exclude .git --exclude .github ${constants_1.action.build === constants_1.root ? `--exclude ${temporaryDeploymentDirectory}` : ""}`, constants_1.workspace);
|
||||
const hasFilesToCommit = yield execute_1.execute(`git status --porcelain`, temporaryDeploymentDirectory);
|
||||
if (!hasFilesToCommit && !constants_1.isTest) {
|
||||
console.log("There is nothing to commit. Exiting... ✅");
|
||||
return Promise.resolve();
|
||||
}
|
||||
// Commits to GitHub.
|
||||
yield execute_1.execute(`git add --all .`, temporaryDeploymentDirectory);
|
||||
yield execute_1.execute(`git switch -c ${temporaryDeploymentBranch}`, temporaryDeploymentDirectory);
|
||||
yield execute_1.execute(`git commit -m "Deploying to ${constants_1.action.branch} from ${constants_1.action.baseBranch} ${process.env.GITHUB_SHA}" --quiet`, temporaryDeploymentDirectory);
|
||||
yield execute_1.execute(`git push --force ${constants_1.repositoryPath} ${temporaryDeploymentBranch}:${constants_1.action.branch}`, temporaryDeploymentDirectory);
|
||||
// Cleans up temporary files/folders and restores the git state.
|
||||
console.log("Running post deployment cleanup jobs... 🔧");
|
||||
yield execute_1.execute(`rm -rf ${temporaryDeploymentDirectory}`, constants_1.workspace);
|
||||
yield execute_1.execute(`git checkout --progress --force ${constants_1.action.defaultBranch}`, constants_1.workspace);
|
||||
return Promise.resolve("Commit step complete...");
|
||||
});
|
||||
}
|
||||
exports.deploy = deploy;
|
36
lib/src/main.js
Normal file
36
lib/src/main.js
Normal file
@ -0,0 +1,36 @@
|
||||
"use strict";
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var __importStar = (this && this.__importStar) || function (mod) {
|
||||
if (mod && mod.__esModule) return mod;
|
||||
var result = {};
|
||||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
||||
result["default"] = mod;
|
||||
return result;
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const core = __importStar(require("@actions/core"));
|
||||
const git_1 = require("./git");
|
||||
/** Initializes and runs the action. */
|
||||
(function () {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
try {
|
||||
yield git_1.init();
|
||||
yield git_1.deploy();
|
||||
}
|
||||
catch (error) {
|
||||
console.log("The deployment encountered an error. ❌");
|
||||
core.setFailed(error.message);
|
||||
}
|
||||
finally {
|
||||
console.log("Completed Deployment ✅");
|
||||
}
|
||||
});
|
||||
})();
|
10
lib/src/util.js
Normal file
10
lib/src/util.js
Normal file
@ -0,0 +1,10 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
/** Utility function that checks to see if a value is undefined or not.
|
||||
* @param {*} value = The value to check.
|
||||
* @returns {boolean}
|
||||
*/
|
||||
function isNullOrUndefined(value) {
|
||||
return typeof value === "undefined" || value === null || value === "";
|
||||
}
|
||||
exports.isNullOrUndefined = isNullOrUndefined;
|
35
lib/util.js
35
lib/util.js
@ -1,33 +1,10 @@
|
||||
"use strict";
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const exec_1 = require("@actions/exec");
|
||||
/** Wrapper around the GitHub toolkit exec command which returns the output.
|
||||
* Also allows you to easily toggle the current working directory.
|
||||
* @param {String} cmd = The command to execute.
|
||||
* @param {String} cwd - The current working directory.
|
||||
* @returns {Promise} - The output from the command.
|
||||
/** Utility function that checks to see if a value is undefined or not.
|
||||
* @param {*} value = The value to check.
|
||||
* @returns {boolean}
|
||||
*/
|
||||
function execute(cmd, cwd) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
let output = "";
|
||||
yield exec_1.exec(cmd, [], {
|
||||
cwd,
|
||||
listeners: {
|
||||
stdout: (data) => {
|
||||
output += data.toString().trim();
|
||||
function isNullOrUndefined(value) {
|
||||
return typeof value === "undefined" || value === null || value === "";
|
||||
}
|
||||
}
|
||||
});
|
||||
return Promise.resolve(output);
|
||||
});
|
||||
}
|
||||
exports.execute = execute;
|
||||
exports.isNullOrUndefined = isNullOrUndefined;
|
||||
|
22
src/execute.ts
Normal file
22
src/execute.ts
Normal file
@ -0,0 +1,22 @@
|
||||
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.
|
||||
* @param {String} cmd = The command to execute.
|
||||
* @param {String} cwd - The current working directory.
|
||||
* @returns {Promise} - The output from the command.
|
||||
*/
|
||||
export async function execute(cmd: string, cwd: string): Promise<any> {
|
||||
let output = "";
|
||||
|
||||
await exec(cmd, [], {
|
||||
cwd,
|
||||
listeners: {
|
||||
stdout: (data: Buffer) => {
|
||||
output += data.toString().trim();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return Promise.resolve(output);
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
import * as core from "@actions/core";
|
||||
import { execute } from "./util";
|
||||
import { execute } from "./execute";
|
||||
import { isNullOrUndefined } from "./util";
|
||||
import { workspace, action, root, repositoryPath, isTest } from "./constants";
|
||||
|
||||
/** Generates the branch if it doesn't exist on the remote.
|
||||
@ -7,13 +8,17 @@ import { workspace, action, root, repositoryPath, isTest } from "./constants";
|
||||
*/
|
||||
export async function init(): Promise<any> {
|
||||
try {
|
||||
if (!action.accessToken && !action.gitHubToken) {
|
||||
if (
|
||||
isNullOrUndefined(action.accessToken) &&
|
||||
isNullOrUndefined(action.gitHubToken)
|
||||
) {
|
||||
return core.setFailed(
|
||||
"You must provide the action with either a Personal Access Token or the GitHub Token secret in order to deploy."
|
||||
);
|
||||
}
|
||||
|
||||
if (action.build.startsWith("/") || action.build.startsWith("./")) {
|
||||
console.log("2");
|
||||
return core.setFailed(
|
||||
`The deployment folder cannot be prefixed with '/' or './'. Instead reference the folder name directly.`
|
||||
);
|
||||
|
25
src/util.ts
25
src/util.ts
@ -1,22 +1,7 @@
|
||||
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.
|
||||
* @param {String} cmd = The command to execute.
|
||||
* @param {String} cwd - The current working directory.
|
||||
* @returns {Promise} - The output from the command.
|
||||
/** Utility function that checks to see if a value is undefined or not.
|
||||
* @param {*} value = The value to check.
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export async function execute(cmd: string, cwd: string): Promise<any> {
|
||||
let output = "";
|
||||
|
||||
await exec(cmd, [], {
|
||||
cwd,
|
||||
listeners: {
|
||||
stdout: (data: Buffer) => {
|
||||
output += data.toString().trim();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return Promise.resolve(output);
|
||||
export function isNullOrUndefined(value: any): boolean {
|
||||
return typeof value === "undefined" || value === null || value === "";
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user