mirror of
https://github.com/JamesIves/github-pages-deploy-action.git
synced 2023-12-15 20:03:39 +08:00
Merge branch 'dev' into releases/v3
This commit is contained in:
commit
82eeb59f49
@ -107,7 +107,7 @@ Below you'll find a description of what each option does.
|
||||
| `GITHUB_TOKEN` | In order for GitHub to trigger the rebuild of your page you must provide the action with the repositories provided GitHub token. This can be referenced in the workflow `yml` file by using `${{ secrets.GITHUB_TOKEN }}`. Only required if an access token is **not** provided. **Please note there is currently an issue affecting the use of this token, [you can learn more here](https://github.com/JamesIves/github-pages-deploy-action/issues/5)**. | `secrets / with` | **Yes** |
|
||||
| `BRANCH` | This is the branch you wish to deploy to, for example `gh-pages` or `docs`. | `with` | **Yes** |
|
||||
| `FOLDER` | The folder in your repository that you want to deploy. If your build script compiles into a directory named `build` you'd put it here. **Folder paths cannot have a leading `/` or `./`**. If you wish to deploy the root directory you can place a `.` here. | `with` | **Yes** |
|
||||
| `BASE_BRANCH` | The base branch of your repository which you'd like to checkout prior to deploying. This defaults to `master`. | `with` | **No** |
|
||||
| `BASE_BRANCH` | The base branch of your repository which you'd like to checkout prior to deploying. This defaults to the current commit that triggered the build followed by `master`. | `with` | **No** |
|
||||
| `TARGET_FOLDER` | If you'd like to push the contents of the deployment folder into a specific directory on the deployment branch you can specify it here. | `with` | **No** |
|
||||
| `CLEAN` | If your project generates hashed files on build you can use this option to automatically delete them from the deployment branch with each deploy. This option can be toggled on by setting it to `true`. | `with` | **No** |
|
||||
| `CLEAN_EXCLUDE` | If you need to use `CLEAN` but you'd like to preserve certain files or folders you can use this option. This should be formatted as an array but stored as a string. For example: `'["filename.js", "folder"]'` | `with` | **No** |
|
||||
|
@ -1,11 +1,11 @@
|
||||
// Initial env variable setup for tests.
|
||||
process.env["INPUT_FOLDER"] = "build";
|
||||
process.env["GITHUB_SHA"] = "123"
|
||||
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 { init, generateBranch, deploy } from "../src/git";
|
||||
import {action} from '../src/constants'
|
||||
import _ from 'lodash';
|
||||
|
||||
const originalAction = _.cloneDeep(action);
|
||||
|
||||
@ -20,120 +20,132 @@ describe("git", () => {
|
||||
|
||||
describe("init", () => {
|
||||
it("should execute three commands if a GitHub token is provided", async () => {
|
||||
|
||||
|
||||
Object.assign(action, {
|
||||
build: 'build',
|
||||
gitHubToken: '123',
|
||||
pusher: {
|
||||
name: 'asd',
|
||||
email: 'as@cat'
|
||||
}})
|
||||
Object.assign(action, {
|
||||
build: "build",
|
||||
gitHubToken: "123",
|
||||
pusher: {
|
||||
name: "asd",
|
||||
email: "as@cat"
|
||||
}
|
||||
});
|
||||
|
||||
const call = await init();
|
||||
expect(execute).toBeCalledTimes(3);
|
||||
expect(call).toBe('Initialization step complete...')
|
||||
expect(call).toBe("Initialization step complete...");
|
||||
});
|
||||
|
||||
it("should execute three commands if a Access token is provided", async () => {
|
||||
|
||||
Object.assign(action, {
|
||||
build: 'build',
|
||||
accessToken: '123',
|
||||
build: "build",
|
||||
accessToken: "123",
|
||||
pusher: {
|
||||
name: 'asd',
|
||||
email: 'as@cat'
|
||||
}})
|
||||
|
||||
const call = await init();
|
||||
|
||||
expect(execute).toBeCalledTimes(3);
|
||||
expect(call).toBe('Initialization step complete...')
|
||||
name: "asd",
|
||||
email: "as@cat"
|
||||
}
|
||||
});
|
||||
|
||||
const call = await init();
|
||||
|
||||
expect(execute).toBeCalledTimes(3);
|
||||
expect(call).toBe("Initialization step complete...");
|
||||
});
|
||||
|
||||
it("should fail if there is no provided GitHub Token or Access Token", async () => {
|
||||
Object.assign(action, {
|
||||
build: 'build',
|
||||
build: "build",
|
||||
pusher: {
|
||||
name: 'asd',
|
||||
email: 'as@cat'
|
||||
}})
|
||||
name: "asd",
|
||||
email: "as@cat"
|
||||
}
|
||||
});
|
||||
|
||||
const call = await init()
|
||||
const call = await init();
|
||||
|
||||
expect(execute).toBeCalledTimes(0);
|
||||
expect(call).toBe('Initialization step complete...')
|
||||
})
|
||||
expect(execute).toBeCalledTimes(0);
|
||||
expect(call).toBe("Initialization step complete...");
|
||||
});
|
||||
|
||||
it("should fail if the build folder begins with a /", async () => {
|
||||
Object.assign(action, {
|
||||
accessToken: '123',
|
||||
build: '/',
|
||||
accessToken: "123",
|
||||
build: "/",
|
||||
pusher: {
|
||||
name: 'asd',
|
||||
email: 'as@cat'
|
||||
}})
|
||||
name: "asd",
|
||||
email: "as@cat"
|
||||
}
|
||||
});
|
||||
|
||||
const call = await init()
|
||||
const call = await init();
|
||||
|
||||
expect(execute).toBeCalledTimes(0);
|
||||
expect(call).toBe('Initialization step complete...')
|
||||
})
|
||||
expect(execute).toBeCalledTimes(0);
|
||||
expect(call).toBe("Initialization step complete...");
|
||||
});
|
||||
|
||||
it("should fail if the build folder begins with a ./", async () => {
|
||||
Object.assign(action, {
|
||||
accessToken: '123',
|
||||
build: './',
|
||||
accessToken: "123",
|
||||
build: "./",
|
||||
pusher: {
|
||||
name: 'asd',
|
||||
email: 'as@cat'
|
||||
}})
|
||||
name: "asd",
|
||||
email: "as@cat"
|
||||
}
|
||||
});
|
||||
|
||||
const call = await init()
|
||||
const call = await init();
|
||||
|
||||
expect(execute).toBeCalledTimes(0);
|
||||
expect(call).toBe('Initialization step complete...')
|
||||
})
|
||||
expect(execute).toBeCalledTimes(0);
|
||||
expect(call).toBe("Initialization step complete...");
|
||||
});
|
||||
|
||||
it("should not fail if root is used", async () => {
|
||||
Object.assign(action, {
|
||||
accessToken: '123',
|
||||
build: '.',
|
||||
accessToken: "123",
|
||||
build: ".",
|
||||
pusher: {
|
||||
name: 'asd',
|
||||
email: 'as@cat'
|
||||
}})
|
||||
name: "asd",
|
||||
email: "as@cat"
|
||||
}
|
||||
});
|
||||
|
||||
const call = await init()
|
||||
const call = await init();
|
||||
|
||||
expect(execute).toBeCalledTimes(3);
|
||||
expect(call).toBe('Initialization step complete...')
|
||||
})
|
||||
expect(execute).toBeCalledTimes(3);
|
||||
expect(call).toBe("Initialization step complete...");
|
||||
});
|
||||
});
|
||||
|
||||
describe('generateBranch', () => {
|
||||
it('should execute five commands', async () => {
|
||||
describe("generateBranch", () => {
|
||||
it("should execute five commands", async () => {
|
||||
const call = await generateBranch();
|
||||
expect(execute).toBeCalledTimes(6);
|
||||
expect(call).toBe('Deployment branch creation step complete... ✅')
|
||||
})
|
||||
})
|
||||
expect(call).toBe("Deployment branch creation step complete... ✅");
|
||||
});
|
||||
});
|
||||
|
||||
describe('deploy', () => {
|
||||
it('should execute five commands', async () => {
|
||||
describe("switchToBaseBranch", () => {
|
||||
it("should execute one command", async () => {
|
||||
const call = await switchToBaseBranch();
|
||||
expect(execute).toBeCalledTimes(1);
|
||||
expect(call).toBe("Switched to the base branch...");
|
||||
});
|
||||
});
|
||||
|
||||
describe("deploy", () => {
|
||||
it("should execute five commands", async () => {
|
||||
Object.assign(action, {
|
||||
build: 'build',
|
||||
gitHubToken: '123',
|
||||
build: "build",
|
||||
gitHubToken: "123",
|
||||
pusher: {
|
||||
name: 'asd',
|
||||
email: 'as@cat'
|
||||
}})
|
||||
|
||||
name: "asd",
|
||||
email: "as@cat"
|
||||
}
|
||||
});
|
||||
|
||||
const call = await deploy();
|
||||
|
||||
// Includes the call to generateBranch
|
||||
expect(execute).toBeCalledTimes(18);
|
||||
expect(call).toBe('Commit step complete...')
|
||||
})
|
||||
})
|
||||
expect(call).toBe("Commit step complete...");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -24,7 +24,8 @@ exports.action = {
|
||||
accessToken: core.getInput("ACCESS_TOKEN"),
|
||||
branch: core.getInput("BRANCH"),
|
||||
targetFolder: core.getInput("TARGET_FOLDER"),
|
||||
baseBranch: core.getInput("BASE_BRANCH") || "master",
|
||||
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
|
||||
|
26
lib/git.js
26
lib/git.js
@ -44,6 +44,18 @@ function init() {
|
||||
});
|
||||
}
|
||||
exports.init = init;
|
||||
/** Switches to the base branch.
|
||||
* @returns {Promise}
|
||||
*/
|
||||
function switchToBaseBranch() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
yield util_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}
|
||||
*/
|
||||
@ -51,13 +63,13 @@ function generateBranch() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
try {
|
||||
console.log(`Creating ${constants_1.action.branch} branch... 🔧`);
|
||||
yield util_1.execute(`git switch ${constants_1.action.baseBranch || "master"}`, constants_1.workspace);
|
||||
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);
|
||||
// Switches back to the base branch.
|
||||
yield util_1.execute(`git switch ${constants_1.action.baseBranch || "master"}`, constants_1.workspace);
|
||||
yield switchToBaseBranch();
|
||||
}
|
||||
catch (error) {
|
||||
core.setFailed(`There was an error creating the deployment branch: ${error} ❌`);
|
||||
@ -85,7 +97,7 @@ function deploy() {
|
||||
yield generateBranch();
|
||||
}
|
||||
// Checks out the base branch to begin the deployment process.
|
||||
yield util_1.execute(`git checkout ${constants_1.action.baseBranch || "master"}`, constants_1.workspace);
|
||||
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);
|
||||
// Ensures that items that need to be excluded from the clean job get parsed.
|
||||
@ -119,11 +131,9 @@ function deploy() {
|
||||
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);
|
||||
// Cleans up temporary files/folders and restores the git state.
|
||||
if (process.env.GITHUB_SHA) {
|
||||
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 ${process.env.GITHUB_SHA}`, constants_1.workspace);
|
||||
}
|
||||
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);
|
||||
return Promise.resolve("Commit step complete...");
|
||||
});
|
||||
}
|
||||
|
@ -19,7 +19,8 @@ export const action = {
|
||||
accessToken: core.getInput("ACCESS_TOKEN"),
|
||||
branch: core.getInput("BRANCH"),
|
||||
targetFolder: core.getInput("TARGET_FOLDER"),
|
||||
baseBranch: core.getInput("BASE_BRANCH") || "master",
|
||||
baseBranch: core.getInput("BASE_BRANCH"),
|
||||
defaultBranch: process.env.GITHUB_SHA ? process.env.GITHUB_SHA : "master",
|
||||
name:
|
||||
pusher && pusher.name
|
||||
? pusher.name
|
||||
|
36
src/git.ts
36
src/git.ts
@ -29,13 +29,27 @@ export async function init(): Promise<any> {
|
||||
}
|
||||
}
|
||||
|
||||
/** Switches to the base branch.
|
||||
* @returns {Promise}
|
||||
*/
|
||||
export async function switchToBaseBranch() {
|
||||
await execute(
|
||||
action.baseBranch
|
||||
? `git switch ${action.baseBranch}`
|
||||
: `git checkout --progress --force ${action.defaultBranch}`,
|
||||
workspace
|
||||
);
|
||||
|
||||
return Promise.resolve("Switched to the base branch...");
|
||||
}
|
||||
|
||||
/** Generates the branch if it doesn't exist on the remote.
|
||||
* @returns {Promise}
|
||||
*/
|
||||
export async function generateBranch(): Promise<any> {
|
||||
try {
|
||||
console.log(`Creating ${action.branch} branch... 🔧`);
|
||||
await execute(`git switch ${action.baseBranch || "master"}`, workspace);
|
||||
await switchToBaseBranch();
|
||||
await execute(`git switch --orphan ${action.branch}`, workspace);
|
||||
await execute(`git reset --hard`, workspace);
|
||||
await execute(
|
||||
@ -43,9 +57,7 @@ export async function generateBranch(): Promise<any> {
|
||||
workspace
|
||||
);
|
||||
await execute(`git push ${repositoryPath} ${action.branch}`, workspace);
|
||||
|
||||
// Switches back to the base branch.
|
||||
await execute(`git switch ${action.baseBranch || "master"}`, workspace);
|
||||
await switchToBaseBranch();
|
||||
} catch (error) {
|
||||
core.setFailed(
|
||||
`There was an error creating the deployment branch: ${error} ❌`
|
||||
@ -75,7 +87,7 @@ export async function deploy(): Promise<any> {
|
||||
}
|
||||
|
||||
// Checks out the base branch to begin the deployment process.
|
||||
await execute(`git checkout ${action.baseBranch || "master"}`, workspace);
|
||||
await switchToBaseBranch();
|
||||
await execute(`git fetch ${repositoryPath}`, workspace);
|
||||
await execute(
|
||||
`git worktree add --checkout ${temporaryDeploymentDirectory} origin/${action.branch}`,
|
||||
@ -142,14 +154,12 @@ export async function deploy(): Promise<any> {
|
||||
);
|
||||
|
||||
// Cleans up temporary files/folders and restores the git state.
|
||||
if (process.env.GITHUB_SHA) {
|
||||
console.log("Running post deployment cleanup jobs... 🔧");
|
||||
await execute(`rm -rf ${temporaryDeploymentDirectory}`, workspace);
|
||||
await execute(
|
||||
`git checkout --progress --force ${process.env.GITHUB_SHA}`,
|
||||
workspace
|
||||
);
|
||||
}
|
||||
console.log("Running post deployment cleanup jobs... 🔧");
|
||||
await execute(`rm -rf ${temporaryDeploymentDirectory}`, workspace);
|
||||
await execute(
|
||||
`git checkout --progress --force ${action.defaultBranch}`,
|
||||
workspace
|
||||
);
|
||||
|
||||
return Promise.resolve("Commit step complete...");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user