8000 feat: 支持部署到其他repo · cauu/github-pages-deploy-action@c874c8e · GitHub
[go: up one dir, main page]

Skip to content

Commit c874c8e

Browse files
author
caiyufu
committed
feat: 支持部署到其他repo
1 parent c74c1d2 commit c874c8e

File tree

2 files changed

+54
-12
lines changed

2 files changed

+54
-12
lines changed

src/constants.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export const action = {
1818
gitHubToken: core.getInput("GITHUB_TOKEN"),
1919
accessToken: core.getInput("ACCESS_TOKEN"),
2020
branch: core.getInput("BRANCH"),
21+
targetRepo: core.getInput("TARGET_REPO"),
2122
targetFolder: core.getInput("TARGET_FOLDER"),
2223
baseBranch: core.getInput("BASE_BRANCH"),
2324
defaultBranch: process.env.GITHUB_SHA ? process.env.GITHUB_SHA : "master",
@@ -41,3 +42,8 @@ export const repositoryPath = `https://${action.accessToken ||
4142
`x-access-token:${action.gitHubToken}`}@github.com/${
4243
action.gitHubRepository
4344
}.git`;
45+
46+
export const targetRepositoryPath = action.targetRepo ? `https://${action.accessToken ||
47+
`x-access-token:${action.gitHubToken}`}@github.com/${
48+
action.targetRepo
49+
}.git` : '';

src/git.ts

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as core from "@actions/core";
22
import { execute } from "./util";
3-
import { workspace, action, root, repositoryPath, isTest } from "./constants";
3+
import { workspace, action, root, repositoryPath, targetRepositoryPath, isTest } from "./constants";
44

55
/** Generates the branch if it doesn't exist on the remote.
66
* @returns {Promise}
@@ -67,6 +67,37 @@ export async function generateBranch(): Promise<any> {
6767
}
6868
}
6969

70+
export async function deployToAnotherRepo(): Promise<any> {
71+
const temporaryDeploymentDirectory = "gh-action-temp-deployment-folder";
72+
73+
await execute(`git clone ${targetRepositoryPath} ${temporaryDeploymentDirectory}`, workspace)
74+
75+
const targetRepoBranchExists = await execute(
76+
`git ls-remote --heads ${targetRepositoryPath} ${action.branch} | wc -l`,
77+
workspace
78+
)
79+
80+
if (!targetRepoBranchExists) {
81+
/** Create target branch on target repository */
82+
try {
83+
console.log("Deployment branch does not exist. Creating....");
84+
await execute(`git switch --orphan ${action.branch}`, temporaryDeploymentDirectory)
85+
await execute(`git reset --hard`, temporaryDeploymentDirectory);
86+
await execute(
87+
`git commit --allow-empty -m "Initial ${action.branch} commit."`,
88+
temporaryDeploymentDirectory
89+
);
90+
await execute(`git push ${targetRepositoryPath} ${action.branch}`, temporaryDeploymentDirectory);
91+
} catch (error) {
92+
core.setFailed(
93+
`There was an error creating the deployment branch: ${error} on ${action.targetRepo} ❌`
94+
);
95+
} finally {
96+
return Promise.resolve(`Deployment branch ${action.branch} on ${action.targetRepo} creation step complete... ✅`);
97+
}
98+
}
99+
}
100+
70101
/** Runs the necessary steps to make the deployment.
71102
* @returns {Promise}
72103
*/
@@ -77,22 +108,27 @@ export async function deploy(): Promise<any> {
77108
Checks to see if the remote exists prior to deploying.
78109
If the branch doesn't exist it gets created here as an orphan.
79110
*/
80-
const branchExists = await execute(
81-
`git ls-remote --heads ${repositoryPath} ${action.branch} | wc -l`,
82-
workspace
83-
);
84-
if (!branchExists) {
85-
console.log("Deployment branch does not exist. Creating....");
86-
await generateBranch();
111+
if (targetRepositoryPath) {
112+
await deployToAnotherRepo()
113+
} else {
114+
const branchExists = await execute(
115+
`git ls-remote --heads ${repositoryPath} ${action.branch} | wc -l`,
116+
workspace
117+
);
118+
if (!branchExists) {
119+
console.log("Deployment branch does not exist. Creating....");
120+
await generateBranch();
121+
}
122+
123+
await execute(
124+
`git worktree add --checkout ${temporaryDeploymentDirectory} origin/${action.branch}`,
125+
workspace
126+
);
87127
}
88128

89129
// Checks out the base branch to begin the deployment process.
90130
await switchToBaseBranch();
91131
await execute(`git fetch ${repositoryPath}`, workspace);
92-
await execute(
93-
`git worktree add --checkout ${temporaryDeploymentDirectory} origin/${action.branch}`,
94-
workspace
95-
);
96132

97133
// Ensures that items that need to be excluded from the clean job get parsed.
98134
let excludes = "";

0 commit comments

Comments
 (0)
0