@@ -2,22 +2,25 @@ import fs from 'fs';
2
2
import path from 'path' ;
3
3
import signale from 'signale' ;
4
4
import { exec } from 'child_process' ;
5
+ import { GitHub } from '@actions/github/lib/github' ;
5
6
import { Context } from '@actions/github/lib/context' ;
6
- import { getGitUrl , getRepository , getBuildCommands , getWorkspace , getCommitMessage , getCommitName , getCommitEmail , detectBuildCommand } from './misc' ;
7
+ import { getGitUrl , getRepository , getBuildCommands , getWorkspace , getCommitMessage , getCommitName , getCommitEmail , getBranchName } from './misc' ;
7
8
8
- export const deploy = async ( branch : string , context : Context ) => {
9
+ export const deploy = async ( tagName : string , octokit : GitHub , context : Context ) => {
9
10
const workDir = path . resolve ( getWorkspace ( ) , '.work' ) ;
10
11
const buildDir = path . resolve ( workDir , 'build' ) ;
11
12
const pushDir = path . resolve ( workDir , 'push' ) ;
12
- signale . info ( `Deploying branch %s to %s` , branch , getRepository ( context ) ) ;
13
+ const branchName = getBranchName ( ) ;
14
+ signale . info ( 'Deploying branch %s to %s' , branchName , getRepository ( context ) ) ;
13
15
14
16
fs . mkdirSync ( pushDir , { recursive : true } ) ;
15
- if ( ! await cloneForBranch ( pushDir , branch , context ) ) return ;
17
+ if ( ! await cloneForBranch ( pushDir , branchName , context ) ) return ;
16
18
if ( ! await prepareFiles ( buildDir , pushDir , context ) ) return ;
17
19
if ( ! await copyFiles ( buildDir , pushDir ) ) return ;
18
20
if ( ! await config ( pushDir ) ) return ;
19
21
if ( ! await commit ( pushDir ) ) return ;
20
- await push ( pushDir , branch , context ) ;
22
+ if ( ! await push ( pushDir , tagName , branchName , context ) ) return ;
23
+ await updateRelease ( tagName , octokit , context ) ;
21
24
} ;
22
25
23
26
export const prepareFiles = async ( buildDir : string , pushDir : string , context : Context ) : Promise < boolean > => {
@@ -29,26 +32,26 @@ export const prepareFiles = async (buildDir: string, pushDir: string, context: C
29
32
return true ;
30
33
} ;
31
34
32
- const cloneForBranch = async ( pushDir : string , branch : string , context : Context ) : Promise < boolean > => {
33
- signale . info ( ` Cloning the branch %s from the remote repo` , branch ) ;
35
+ const cloneForBranch = async ( pushDir : string , branchName : string , context : Context ) : Promise < boolean > => {
36
+ signale . info ( ' Cloning the branch %s from the remote repo' , branchName ) ;
34
37
35
38
const url = getGitUrl ( context ) ;
36
- await execAsync ( `git -C ${ pushDir } clone --quiet --branch=${ branch } --depth=1 ${ url } .` , true , 'git clone' , true ) ;
39
+ await execAsync ( `git -C ${ pushDir } clone --quiet --branch=${ branchName } --depth=1 ${ url } .` , true , 'git clone' , true ) ;
37
40
38
- const clonedBranch = await getBranchName ( pushDir ) ;
39
- if ( branch !== clonedBranch ) {
40
- signale . info ( ` remote branch ${ branch } not found.` ) ;
41
- signale . info ( ` now branch: ${ clonedBranch } ` ) ;
41
+ const clonedBranch = await getCurrentBranchName ( pushDir ) ;
42
+ if ( branchName !== clonedBranch ) {
43
+ signale . info ( ' remote branch %s not found.' , branchName ) ;
44
+ signale . info ( ' now branch: %s' , clonedBranch ) ;
42
45
43
46
await execAsync ( `rm -rdf ${ pushDir } ` ) ;
44
47
fs . mkdirSync ( pushDir , { recursive : true } ) ;
45
48
await gitInit ( pushDir ) ;
46
- await gitCheckout ( pushDir , branch ) ;
49
+ await gitCheckout ( pushDir , branchName ) ;
47
50
}
48
51
return true ;
49
52
} ;
50
53
51
- const getBranchName = async ( pushDir : string ) : Promise < string > => {
54
+ const getCurrentBranchName = async ( pushDir : string ) : Promise < string > => {
52
55
if ( ! fs . existsSync ( path . resolve ( pushDir , '.git' ) ) ) {
53
56
return '' ;
54
57
}
@@ -61,10 +64,10 @@ const gitInit = async (pushDir: string) => {
61
64
await execAsync ( `git -C ${ pushDir } init .` ) ;
62
65
} ;
63
66
64
- const gitCheckout = async ( pushDir : string , branch : string ) => {
65
- signale . info ( 'Checking out orphan branch %s' , branch ) ;
67
+ const gitCheckout = async ( pushDir : string , branchName : string ) => {
68
+ signale . info ( 'Checking out orphan branch %s' , branchName ) ;
66
69
67
- await execAsync ( `git -C ${ pushDir } checkout --orphan "${ branch } "` ) ;
70
+ await execAsync ( `git -C ${ pushDir } checkout --orphan "${ branchName } "` ) ;
68
71
} ;
69
72
70
73
const config = async ( pushDir : string ) : Promise < boolean > => {
@@ -89,11 +92,36 @@ const commit = async (pushDir: string): Promise<boolean> => {
89
92
return true ;
90
93
} ;
91
94
92
- const push = async ( pushDir : string , branch : string , context : Context ) => {
93
- signale . info ( 'Pushing to %s@%s' , getRepository ( context ) , branch ) ;
95
+ const push = async ( pushDir : string , tagName : string , branchName : string , context : Context ) : Promise < boolean > => {
96
+ signale . info ( 'Pushing to %s@%s (tag: %s) ' , getRepository ( context ) , branchName , tagName ) ;
94
97
95
98
const url = getGitUrl ( context ) ;
96
- await execAsync ( `git -C ${ pushDir } push --quiet "${ url } " "${ branch } ":"refs/heads/${ branch } "` , true , 'git push' ) ;
99
+ await execAsync ( `git -C ${ pushDir } push --delete origin tag ${ tagName } ` ) ;
100
+ await execAsync ( `git -C ${ pushDir } tag -l | xargs git -C ${ pushDir } tag -d` ) ;
101
+ await execAsync ( `git -C ${ pushDir } fetch origin --tags` ) ;
102
+ await execAsync ( `git -C ${ pushDir } tag ${ tagName } ` ) ;
103
+ await execAsync ( `git -C ${ pushDir } push --quiet --tags "${ url } " "${ branchName } ":"refs/heads/${ branchName } "` , true , 'git push' ) ;
104
+ return true ;
105
+ } ;
106
+
107
+ const updateRelease = async ( tagName : string , octokit : GitHub , context : Context ) : Promise < boolean > => {
108
+ const releases = await octokit . repos . listReleases ( {
109
+ owner : context . repo . owner ,
110
+ repo : context . repo . repo ,
111
+ } ) ;
112
+ const release = releases . data . find ( release => release . tag_name === tagName ) ;
113
+ if ( ! release ) {
114
+ signale . warn ( 'There is no release that has tag name: %s' , tagName ) ;
115
+ return false ;
116
+ }
117
+
118
+ await octokit . repos . updateRelease ( {
119
+ owner : context . repo . owner ,
120
+ repo : context . repo . repo ,
121
+ release_id : release . id ,
122
+ draft : false ,
123
+ } ) ;
124
+ return true ;
97
125
} ;
98
126
99
127
const cloneForBuild = async ( buildDir : string , context : Context ) => {
@@ -127,8 +155,8 @@ const checkDiff = async (pushDir: string): Promise<boolean> => {
127
155
} ;
128
156
129
157
const execAsync = ( command : string , quiet : boolean = false , altCommand : string | null = null , suppressError : boolean = false ) => new Promise < string > ( ( resolve , reject ) => {
130
- if ( 'string' === typeof altCommand ) signale . info ( ` Run command: ${ altCommand } ` ) ;
131
- else if ( ! quiet ) signale . info ( ` Run command: ${ command } ` ) ;
158
+ if ( 'string' === typeof altCommand ) signale . info ( ' Run command: %s' , altCommand ) ;
159
+ else if ( ! quiet ) signale . info ( ' Run command: %s' , command ) ;
132
160
exec ( command + ( quiet ? ' > /dev/null 2>&1' : '' ) + ( suppressError ? ' || :' : '' ) , ( error , stdout ) => {
133
161
if ( error ) {
134
162
if ( 'string' === typeof altCommand && ! quiet ) reject ( new Error ( `command [${ altCommand } ] exited with code ${ error . code } . message: ${ error . message } ` ) ) ;
0 commit comments