@@ -6,14 +6,16 @@ let { join } = require('path')
6
6
let admZip = require ( 'adm-zip' )
7
7
let aws = require ( 'aws-sdk' )
8
8
9
- let { DEPLOY , S3_BUCKET : Bucket , LATEST_API : FunctionName } = process . env
9
+ let { DEPLOY , CF_DISTRO : DistributionId , S3_BUCKET : Bucket , LATEST_API : FunctionName } = process . env
10
10
if ( ! Bucket ) throw ReferenceError ( 'Missing S3_BUCKET config!' )
11
11
if ( ! DEPLOY ) throw ReferenceError ( 'Missing DEPLOY config!' )
12
+ if ( ! DistributionId ) throw ReferenceError ( 'Missing CF_DISTRO config!' )
12
13
if ( ! FunctionName ) throw ReferenceError ( 'Missing LATEST_API config!' )
13
14
let valid = [ 'prod' , 'main' ]
14
15
if ( ! valid . includes ( DEPLOY ) ) throw ReferenceError ( `DEPLOY must be one of: ${ valid . join ( ', ' ) } ` )
15
16
16
17
let region = 'us-west-2'
18
+ let cf = new aws . CloudFront ( )
17
19
let lambda = new aws . Lambda ( { region } )
18
20
let s3 = new aws . S3 ( { region } )
19
21
let ssm = new aws . SSM ( { region } )
@@ -50,8 +52,7 @@ let kb = buf => Math.floor(buf.length / 1000)
50
52
}
51
53
let channel = isProd ? 'latest' : 'main'
52
54
let ACL = 'public-read'
53
- // TODO: increase to 24 hours, clear cache on publish
54
- let CacheControl = 'max-age=500'
55
+ let CacheControl = 's-maxage=3600, max-age=3600, must-revalidate' // 1 hour
55
56
56
57
for ( let [ local , { name, zip } ] of Object . entries ( files ) ) {
57
58
let path = join ( __dirname , '..' , 'build' , local )
@@ -75,6 +76,17 @@ let kb = buf => Math.floor(buf.length / 1000)
75
76
}
76
77
}
77
78
79
+ // Flush CDN release channel
80
+ let releasePath = `/releases/${ channel } /*`
81
+ await cf . createInvalidation ( {
82
+ DistributionId,
83
+ InvalidationBatch : {
84
+ CallerReference : `${ channel } -${ new Date ( ) . toISOString ( ) } ` ,
85
+ Paths : { Quantity : 1 , Items : [ releasePath ] }
86
+ }
87
+ } ) . promise ( )
88
+ console . log ( `Invalidated ${ releasePath } ` )
89
+
78
90
let uri = ext => `https://dl.begin.com/releases/${ channel } /begin-x64-${ ext } `
79
91
let data = {
80
92
[ `ver_${ channel } ` . toUpperCase ( ) ] : version ,
@@ -105,7 +117,7 @@ let kb = buf => Math.floor(buf.length / 1000)
105
117
console . log ( `Updated ${ key } ` )
106
118
}
107
119
108
- // Fetch Lambda config
120
+ // Update `dl.begin.com/versions` config
109
121
let config = await lambda . getFunctionConfiguration ( { FunctionName } ) . promise ( )
110
122
let Environment = { Variables : { ...config . Environment . Variables , ...data } }
111
123
await lambda . updateFunctionConfiguration ( { FunctionName, Environment } ) . promise ( )
0 commit comments