1
1
import * as core from "@actions/core" ;
2
2
import { execute } from "./util" ;
3
- import { workspace , action , root , repositoryPath , isTest } from "./constants" ;
3
+ import { workspace , action , root , repositoryPath , targetRepositoryPath , isTest } from "./constants" ;
4
4
5
5
/** Generates the branch if it doesn't exist on the remote.
6
6
* @returns {Promise }
@@ -67,6 +67,37 @@ export async function generateBranch(): Promise<any> {
67
67
}
68
68
}
69
69
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
+
70
101
/** Runs the necessary steps to make the deployment.
71
102
* @returns {Promise }
72
103
*/
@@ -77,22 +108,27 @@ export async function deploy(): Promise<any> {
77
108
Checks to see if the remote exists prior to deploying.
78
109
If the branch doesn't exist it gets created here as an orphan.
79
110
*/
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
+ ) ;
87
127
}
88
128
89
129
// Checks out the base branch to begin the deployment process.
90
130
await switchToBaseBranch ( ) ;
91
131
await execute ( `git fetch ${ repositoryPath } ` , workspace ) ;
92
- await execute (
93
- `git worktree add --checkout ${ temporaryDeploymentDirectory } origin/${ action . branch } ` ,
94
- workspace
95
- ) ;
96
132
97
133
// Ensures that items that need to be excluded from the clean job get parsed.
98
134
let excludes = "" ;
0 commit comments