1
1
import fs from 'fs' ;
2
2
import path from 'path' ;
3
- import rimraf from 'rimraf' ;
4
3
import signale from 'signale' ;
5
4
import { exec } from 'child_process' ;
6
5
import { Context } from '@actions/github/lib/context' ;
@@ -12,41 +11,54 @@ export const deploy = async (branch: string, context: Context) => {
12
11
const pushDir = path . resolve ( workDir , 'push' ) ;
13
12
signale . info ( `Deploying branch %s to %s` , branch , getRepository ( context ) ) ;
14
13
15
- rimraf ( workDir , ( ) => {
16
- } ) ;
17
14
fs . mkdirSync ( pushDir , { recursive : true } ) ;
18
- await prepareFiles ( buildDir , pushDir , context ) ;
19
- await cloneForBranch ( pushDir , branch , context ) ;
20
- await copyFiles ( buildDir , pushDir ) ;
21
- await config ( pushDir ) ;
22
- await commit ( pushDir ) ;
15
+ if ( ! await cloneForBranch ( pushDir , branch , context ) ) return ;
16
+ if ( ! await prepareFiles ( buildDir , pushDir , context ) ) return ;
17
+ if ( ! await copyFiles ( buildDir , pushDir ) ) return ;
18
+ if ( ! await config ( pushDir ) ) return ;
19
+ if ( ! await commit ( pushDir ) ) return ;
23
20
await push ( pushDir , branch , context ) ;
24
21
} ;
25
22
26
- export const prepareFiles = async ( buildDir : string , pushDir : string , context : Context ) => {
23
+ export const prepareFiles = async ( buildDir : string , pushDir : string , context : Context ) : Promise < boolean > => {
27
24
signale . info ( 'Preparing files for release' ) ;
28
25
29
26
fs . mkdirSync ( buildDir , { recursive : true } ) ;
30
27
await cloneForBuild ( buildDir , context ) ;
31
28
await runBuild ( buildDir ) ;
29
+ return true ;
32
30
} ;
33
31
34
- const cloneForBranch = async ( pushDir : string , branch : string , context : Context ) => {
32
+ const cloneForBranch = async ( pushDir : string , branch : string , context : Context ) : Promise < boolean > => {
35
33
signale . info ( `Cloning the branch %s from the remote repo` , branch ) ;
36
34
37
35
const url = getGitUrl ( context ) ;
38
36
await execAsync ( `git -C ${ pushDir } clone --quiet --branch=${ branch } --depth=1 ${ url } .` , true , 'git clone' , true ) ;
39
- if ( ! fs . existsSync ( path . resolve ( pushDir , '.git' ) ) ) {
37
+
38
+ const clonedBranch = await getBranchName ( pushDir ) ;
39
+ if ( branch !== clonedBranch ) {
40
+ signale . info ( `remote branch ${ branch } not found.` ) ;
41
+ signale . info ( `now branch: ${ clonedBranch } ` ) ;
42
+
43
+ await execAsync ( `rm -rdf ${ pushDir } ` ) ;
44
+ fs . mkdirSync ( pushDir , { recursive : true } ) ;
40
45
await gitInit ( pushDir ) ;
41
46
await gitCheckout ( pushDir , branch ) ;
42
47
}
48
+ return true ;
49
+ } ;
50
+
51
+ const getBranchName = async ( pushDir : string ) : Promise < string > => {
52
+ if ( ! fs . existsSync ( path . resolve ( pushDir , '.git' ) ) ) {
53
+ return '' ;
54
+ }
55
+ return ( await execAsync ( `git -C ${ pushDir } branch -a | grep -E '^\\*' | cut -b 3-` ) ) . trim ( ) ;
43
56
} ;
44
57
45
58
const gitInit = async ( pushDir : string ) => {
46
59
signale . info ( 'Initializing local git repo' ) ;
47
60
48
61
await execAsync ( `git -C ${ pushDir } init .` ) ;
49
-
50
62
} ;
51
63
52
64
const gitCheckout = async ( pushDir : string , branch : string ) => {
@@ -55,27 +67,33 @@ const gitCheckout = async (pushDir: string, branch: string) => {
55
67
await execAsync ( `git -C ${ pushDir } checkout --orphan "${ branch } "` ) ;
56
68
} ;
57
69
58
- const config = async ( pushDir : string ) => {
70
+ const config = async ( pushDir : string ) : Promise < boolean > => {
59
71
const name = getCommitName ( ) ;
60
72
const email = getCommitEmail ( ) ;
61
73
signale . info ( 'Configuring git committer to be %s <%s>' , name , email ) ;
62
74
63
75
await execAsync ( `git -C ${ pushDir } config user.name "${ name } "` ) ;
64
76
await execAsync ( `git -C ${ pushDir } config user.email "${ email } "` ) ;
77
+ return true ;
65
78
} ;
66
79
67
- const commit = async ( pushDir : string ) => {
80
+ const commit = async ( pushDir : string ) : Promise < boolean > => {
68
81
const message = getCommitMessage ( ) ;
69
82
await execAsync ( `git -C ${ pushDir } add --all --force` ) ;
83
+ if ( ! await checkDiff ( pushDir ) ) {
84
+ signale . info ( 'There is no diff.' ) ;
85
+ return false ;
86
+ }
70
87
await execAsync ( `git -C ${ pushDir } commit -qm "${ message } "` ) ;
71
88
await execAsync ( `git -C ${ pushDir } show --stat-count=10 HEAD` ) ;
89
+ return true ;
72
90
} ;
73
91
74
92
const push = async ( pushDir : string , branch : string , context : Context ) => {
75
93
signale . info ( 'Pushing to %s@%s' , getRepository ( context ) , branch ) ;
76
94
77
95
const url = getGitUrl ( context ) ;
78
- await execAsync ( `git -C ${ pushDir } push --quiet "${ url } " "${ branch } ":"${ branch } "` , true , 'git push' ) ;
96
+ await execAsync ( `git -C ${ pushDir } push --quiet "${ url } " "${ branch } ":"refs/heads/ ${ branch } "` , true , 'git push' ) ;
79
97
} ;
80
98
81
99
const cloneForBuild = async ( buildDir : string , context : Context ) => {
@@ -97,19 +115,25 @@ const runBuild = async (buildDir: string) => {
97
115
await execAsync ( `cd ${ current } ` ) ;
98
116
} ;
99
117
100
- const copyFiles = async ( buildDir : string , pushDir : string ) => {
118
+ const copyFiles = async ( buildDir : string , pushDir : string ) : Promise < boolean > => {
101
119
signale . info ( '=== Copying %s contents to %s ===' , buildDir , pushDir ) ;
102
120
103
121
await execAsync ( `rsync -rl --exclude .git --delete "${ buildDir } /" ${ pushDir } ` ) ;
122
+ return true ;
123
+ } ;
124
+
125
+ const checkDiff = async ( pushDir : string ) : Promise < boolean > => {
126
+ return ( await execAsync ( `git -C ${ pushDir } status --short -uno` ) ) . split ( / \r \n | \n / ) . filter ( line => line . match ( / ^ [ M D A ] \s + / ) ) . length > 0 ;
104
127
} ;
105
128
106
129
const execAsync = ( command : string , quiet : boolean = false , altCommand : string | null = null , suppressError : boolean = false ) => new Promise < string > ( ( resolve , reject ) => {
107
130
if ( 'string' === typeof altCommand ) signale . info ( `Run command: ${ altCommand } ` ) ;
108
131
else if ( ! quiet ) signale . info ( `Run command: ${ command } ` ) ;
109
132
exec ( command + ( quiet ? ' > /dev/null 2>&1' : '' ) + ( suppressError ? ' || :' : '' ) , ( error , stdout ) => {
110
133
if ( error ) {
111
- if ( 'string' === typeof altCommand ) reject ( new Error ( `command [${ altCommand } ] exited with code ${ error . code } .` ) ) ;
112
- else if ( ! quiet ) reject ( new Error ( `command [${ command } ] exited with code ${ error . code } .` ) ) ;
134
+ if ( 'string' === typeof altCommand && ! quiet ) reject ( new Error ( `command [${ altCommand } ] exited with code ${ error . code } . message: ${ error . message } ` ) ) ;
135
+ else if ( 'string' === typeof altCommand ) reject ( new Error ( `command [${ altCommand } ] exited with code ${ error . code } .` ) ) ;
136
+ else if ( ! quiet ) reject ( new Error ( `command [${ command } ] exited with code ${ error . code } . message: ${ error . message } ` ) ) ;
113
137
else reject ( new Error ( `command exited with code ${ error . code } .` ) ) ;
114
138
} else {
115
139
if ( ! quiet ) console . log ( stdout ) ;
0 commit comments