-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.js
75 lines (63 loc) · 1.69 KB
/
deploy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { existsSync } from 'fs';
import fsPromises from 'fs/promises';
import path from 'path';
import envalid from 'envalid';
import execa from 'execa';
const version = process.argv[2];
if (!version) {
console.error('usage: node deploy.js <version>');
process.exit(1);
}
const { str } = envalid;
const { DEPLOY_BUCKET_NAME, DEPLOY_DIRECTORY, DEPLOY_SYMLINK } =
envalid.cleanEnv(
process.env,
{
DEPLOY_BUCKET_NAME: str(),
DEPLOY_DIRECTORY: str(),
DEPLOY_SYMLINK: str(),
},
{
strict: true,
},
);
const root = '/data';
const releaseDir = path.join(root, DEPLOY_DIRECTORY);
const symlinkPath = path.join(root, DEPLOY_SYMLINK);
await fsPromises.mkdir(releaseDir, { recursive: true });
// Verify auth
const result = await execa('gcloud', ['auth', 'list']);
if (result.stderr && result.stderr.includes('No credentialed accounts.')) {
console.error('You need to run the login command first.');
process.exit(1);
}
console.log(`Deploying ${version}`);
const exec = execa('gsutil', [
'-m',
'rsync',
'-r',
'-d',
`gs://${DEPLOY_BUCKET_NAME}`,
releaseDir,
]);
exec.stdout.pipe(process.stdout);
await exec;
const exists = existsSync(path.join(releaseDir, version));
if (!exists) {
console.error(`Release ${version} does not exist`);
process.exit(1);
}
// Remove previous symlink if exists
try {
await fsPromises.unlink(symlinkPath);
} catch (e) {
if (e.code !== 'ENOENT') {
console.error(e.message);
process.exit(1);
}
}
// Create new symlink
const relativePath = path.relative(root, path.join(releaseDir, version));
console.log(`Creating symlink for ${version}`);
await fsPromises.symlink(relativePath, symlinkPath);
console.log('Finished');