1
1
const gulp = require ( 'gulp' ) ;
2
2
const sri = require ( 'gulp-sri' ) ;
3
- const branch = require ( 'git-branch ' ) ;
4
- const pkgVersion = require ( './package.json' ) . version ;
5
- const path = require ( 'path ' ) ;
6
-
7
- // S3 CLIENT CONFIG
8
- const s3 = require ( 'gulp-s3-upload' ) ( { useIAM : true } ) ;
3
+ const git = require ( 'git-rev-sync ' ) ;
4
+ const pkg = require ( './package.json' ) ;
5
+ const { upload } = require ( 'gulp-s3-publish ' ) ;
6
+ const { S3 } = require ( 'aws-sdk' ) ;
7
+ const util = require ( 'util' ) ;
8
+ const exec = util . promisify ( require ( 'child_process' ) . exec ) ;
9
9
10
10
// DEPLOYMENT CONFIGURATION OPTIONS
11
- const source = [ 'dist/*.js' , 'dist/*.map' , 'dist/*.json' ] ; // source for deploy
12
- const sourceSRI = [ 'dist/*.js' , 'dist/*.map' ] ; // source for sri generation
13
- const Bucket = 'static.filestackapi.com' // upload bucked
14
- const ACL = 'public-read' ; // upload acl
15
- const deployPath = 'filestack-react' ; // upload path
16
- const deploymentBranch = 'master' ; // branch for upload production version
17
- const cacheControll = { // cache controll for each version
11
+ const pkgName = pkg . name ;
12
+ const pkgCurrentVersion = pkg . version ;
13
+
14
+ const source = [ 'dist/*.js' , 'dist/*.js.map' , 'dist/*.css' , 'dist/*.json' ] ; // source for deploy
15
+ const sourceSRI = [ 'dist/*.js' , 'dist/*.css' ] ; // source for sri generation
16
+ const bucket = process . env . DEPLOY_BUCKET || 'static.filestackapi.com' // upload bucked
17
+ const betaBranch = process . env . BETA_BRANCH || 'develop' ;
18
+ const dryRun = process . env . DRY_RUN || false ;
19
+
20
+ const putObjectParams = {
21
+ ACL : 'public-read'
22
+ } ;
23
+ const deployPath = pkgName ; // upload path
24
+ const cacheControl = { // cache controll for each version
18
25
latest : 1 ,
19
26
version : 30 ,
20
27
beta : 0 ,
21
28
} ;
22
29
23
30
// HELPERS
24
- const getDeployPath = ( inputPath , version ) => `${ deployPath } /${ version } /${ path . basename ( inputPath ) } ` ;
31
+ let currentTag ;
32
+ const currentBranch = git . branch ( ) ;
33
+ const isCi = process . env . CI || false ;
34
+ const forceDeploy = process . env . FORCE_DEPLOY || false ;
35
+
36
+ try {
37
+ currentTag = git . tag ( ) ;
38
+ } catch ( e ) {
39
+ console . log ( 'Current Git Tag not found. Beta will be released' ) ;
40
+ }
41
+
42
+ // Get major version for "version deploy" ie: 1.x.x
25
43
const getMajorVersion = ( version ) => version . split ( '.' ) [ 0 ] ;
26
44
45
+ // get current pkg version from npm (we dont need to update)
46
+ const getCurrentReleasedVersion = async ( ) => {
47
+ const { stdout } = await exec ( `npm view ${ pkgName } version` ) ;
48
+ return stdout . trim ( ) ;
49
+ } ;
50
+
51
+ if ( forceDeploy ) {
52
+ console . info ( '!!!!!!!!!!!!!!!!!!!!!!!!!!!!' ) ;
53
+ console . info ( '!!!!!!FORCE DEPLOYMENT!!!!!!' ) ;
54
+ console . info ( '!!!!!!!!!!!!!!!!!!!!!!!!!!!!' ) ;
55
+ }
56
+
57
+ console . info ( 'Current GIT Branch is' , currentBranch ) ;
58
+ console . info ( `Current GIT Tag is` , currentTag ) ;
59
+ console . info ( `Is Continuous Integration Env` , isCi ) ;
60
+
61
+ // S3
62
+ const S3Client = new S3 ( ) ;
63
+
64
+ const uploadFile = ( version , CacheControl ) => {
65
+ const options = {
66
+ bucket,
67
+ putObjectParams : {
68
+ ...putObjectParams ,
69
+ CacheControl : `max-age=${ CacheControl * 86400 } ` ,
70
+ } ,
71
+ uploadPath : `${ deployPath } /${ version } ` ,
72
+ dryRun,
73
+ } ;
74
+
75
+ console . info ( 'Upload files with option:' , options )
76
+
77
+ return upload ( S3Client , options ) ;
78
+ }
79
+
80
+ /**
81
+ * Check if we can deploy code to production CDN
82
+ *
83
+ * - pgk should be released only from CI
84
+ * - pkg version should be different thant this on NPM repository
85
+ * - git tag should be set
86
+ * - current git tag should be equal to provided in package.json
87
+ */
88
+ const canBeDeployedProd = async ( ) => {
89
+ if ( ! forceDeploy ) {
90
+ const currentVersion = await getCurrentReleasedVersion ( ) ;
91
+
92
+ if ( ! isCi ) {
93
+ console . info ( 'Publish can be run only from CI. You can bypass it using FORCE flag' ) ;
94
+ return Promise . resolve ( false ) ;
95
+ }
96
+
97
+ if ( currentVersion === pkgCurrentVersion ) {
98
+ console . info ( `Version ${ pkgCurrentVersion } is already published (in npm). Skipping` ) ;
99
+ return Promise . resolve ( false ) ;
100
+ }
101
+
102
+ if ( ! currentTag ) {
103
+ console . info ( 'Current tag is missing' ) ;
104
+ return Promise . resolve ( false ) ;
105
+ }
106
+
107
+ if ( currentTag !== pkgCurrentVersion ) {
108
+ console . info ( `Package version ${ pkgCurrentVersion } and GIT Tag (${ currentTag } ) are not equal. Skipping` ) ;
109
+ return Promise . resolve ( false ) ;
110
+ }
111
+
112
+ }
113
+
114
+ return Promise . resolve ( true ) ;
115
+ }
116
+
27
117
// GENERATE SRI TAG
28
118
gulp . task ( 'sri' , ( ) => {
29
119
return gulp . src ( sourceSRI )
@@ -42,46 +132,32 @@ gulp.task('sri', () => {
42
132
} ) ;
43
133
44
134
// DEPLOYMENTS
45
- gulp . task ( 'publish:beta' , ( ) => {
46
- return gulp . src ( source )
47
- . pipe ( s3 ( {
48
- Bucket,
49
- ACL ,
50
- CacheControl : `max-age=${ cacheControll . beta * 86400 } ` ,
51
- keyTransform : ( path ) => getDeployPath ( path , 'beta' ) ,
52
- } ) ) ;
53
- } ) ;
135
+ gulp . task ( 'publish:beta' , ( done ) => {
136
+ // beta can be deployed only from provided branch
137
+ if ( currentBranch !== betaBranch ) {
138
+ console . warn ( `Skipping publish:beta task. Incorrect branch ${ currentBranch } . Beta can be released from ${ betaBranch } ` ) ;
139
+ return done ( ) ;
140
+ }
54
141
55
- gulp . task ( 'publish:latest' , ( ) => {
56
- return gulp . src ( source )
57
- . pipe ( s3 ( {
58
- Bucket,
59
- ACL ,
60
- CacheControl : `max-age=${ cacheControll . latest * 86400 } ` ,
61
- keyTransform : ( path ) => getDeployPath ( path , `${ getMajorVersion ( pkgVersion ) } .x.x` ) ,
62
- } ) ) ;
142
+ return gulp . src ( source ) . pipe ( uploadFile ( 'beta' , cacheControl . beta ) )
63
143
} ) ;
64
144
65
- gulp . task ( 'publish' , gulp . series ( ( ) => {
66
- const currentBranch = branch . sync ( ) ;
67
- console . info ( `Current branch is "${ currentBranch } "` )
68
-
69
- if ( currentBranch !== deploymentBranch ) {
70
- return gulp . start ( 'publish:beta' ) ;
145
+ gulp . task ( 'publish:latest' , async ( ) => {
146
+ if ( ! ( await canBeDeployedProd ( ) ) ) {
147
+ console . warn ( 'Skipping publish:latest task' ) ;
148
+ return Promise . resolve ( ) ;
71
149
}
72
150
73
- return gulp . src ( source )
74
- . pipe ( s3 ( {
75
- Bucket,
76
- ACL ,
77
- CacheControl : `max-age=${ cacheControll . version * 86400 } ` ,
78
- keyTransform : ( path ) => getDeployPath ( path , pkgVersion ) ,
79
- } ) ) ;
80
- } , ( ) => {
81
- const currentBranch = branch . sync ( ) ;
82
- if ( currentBranch !== deploymentBranch ) {
83
- return ;
151
+ return gulp . src ( source ) . pipe ( uploadFile ( `${ getMajorVersion ( pkgCurrentVersion ) } .x.x` , cacheControl . latest ) ) ;
152
+ } ) ;
153
+
154
+ gulp . task ( 'publish:version' , async ( ) => {
155
+ if ( ! ( await canBeDeployedProd ( ) ) ) {
156
+ console . warn ( 'Skipping publish:version task' ) ;
157
+ return Promise . resolve ( ) ;
84
158
}
85
159
86
- return gulp . start ( 'publish:latest' ) ;
87
- } ) ) ;
160
+ return gulp . src ( source ) . pipe ( uploadFile ( pkgCurrentVersion , cacheControl . version ) ) ;
161
+ } ) ;
162
+
163
+ gulp . task ( 'publish' , gulp . series ( 'sri' , 'publish:beta' , 'publish:version' , 'publish:latest' ) ) ;
0 commit comments