@naturalcycles/backend-lib 9.79.0 → 9.81.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/deploy/deployHealthCheck.d.ts +1 -5
- package/dist/deploy/deployHealthCheck.js +3 -34
- package/dist/deploy/index.d.ts +2 -9
- package/dist/deploy/index.js +1 -6
- package/dist/express/createDefaultApp.d.ts +23 -0
- package/dist/express/createDefaultApp.js +5 -3
- package/dist/server/serverStatusMiddleware.d.ts +2 -2
- package/dist/server/serverStatusMiddleware.js +5 -14
- package/dist/util.d.ts +0 -1
- package/dist/util.js +1 -4
- package/package.json +3 -13
- package/src/admin/login.html +103 -76
- package/src/deploy/deployHealthCheck.ts +2 -48
- package/src/deploy/index.ts +2 -17
- package/src/express/createDefaultApp.ts +35 -3
- package/src/server/serverStatusMiddleware.ts +4 -20
- package/src/util.ts +1 -5
- package/bin/deploy-gae.js +0 -2
- package/bin/deploy-prepare.js +0 -2
- package/bin/undeploy-gae.js +0 -2
- package/dist/bin/deploy-gae.d.ts +0 -1
- package/dist/bin/deploy-gae.js +0 -12
- package/dist/bin/deploy-prepare.d.ts +0 -1
- package/dist/bin/deploy-prepare.js +0 -19
- package/dist/bin/undeploy-gae.d.ts +0 -1
- package/dist/bin/undeploy-gae.js +0 -13
- package/dist/deploy/backend.cfg.util.d.ts +0 -30
- package/dist/deploy/backend.cfg.util.js +0 -23
- package/dist/deploy/deploy.model.d.ts +0 -29
- package/dist/deploy/deploy.model.js +0 -1
- package/dist/deploy/deploy.util.d.ts +0 -7
- package/dist/deploy/deploy.util.js +0 -151
- package/dist/deploy/deployGae.d.ts +0 -10
- package/dist/deploy/deployGae.js +0 -90
- package/dist/deploy/deployInfo.util.d.ts +0 -1
- package/dist/deploy/deployInfo.util.js +0 -25
- package/dist/deploy/deployPrepare.d.ts +0 -13
- package/dist/deploy/deployPrepare.js +0 -85
- package/src/bin/deploy-gae.ts +0 -14
- package/src/bin/deploy-prepare.ts +0 -24
- package/src/bin/undeploy-gae.ts +0 -15
- package/src/deploy/backend.cfg.util.ts +0 -63
- package/src/deploy/deploy.model.ts +0 -32
- package/src/deploy/deploy.util.ts +0 -214
- package/src/deploy/deployGae.ts +0 -127
- package/src/deploy/deployInfo.util.ts +0 -27
- package/src/deploy/deployPrepare.ts +0 -113
- package/src/deploy/files-default/.gcloudignore +0 -7
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import { loadEnvFileIfExists } from '@naturalcycles/nodejs-lib';
|
|
2
|
-
import { dimGrey } from '@naturalcycles/nodejs-lib/colors';
|
|
3
|
-
import { fs2 } from '@naturalcycles/nodejs-lib/fs2';
|
|
4
|
-
import { kpySync } from '@naturalcycles/nodejs-lib/kpy';
|
|
5
|
-
import { srcDir } from '../paths.cnst.js';
|
|
6
|
-
import { getBackendCfg } from './backend.cfg.util.js';
|
|
7
|
-
import { createAndSaveAppYaml, createAndSaveDeployInfo } from './deploy.util.js';
|
|
8
|
-
export const deployPrepareCliOptions = {
|
|
9
|
-
projectDir: {
|
|
10
|
-
type: 'string',
|
|
11
|
-
default: '.',
|
|
12
|
-
},
|
|
13
|
-
targetDir: {
|
|
14
|
-
type: 'string',
|
|
15
|
-
default: `./tmp/deploy`,
|
|
16
|
-
},
|
|
17
|
-
createNpmrc: {
|
|
18
|
-
type: 'boolean',
|
|
19
|
-
desc: 'Create .npmrc in targetDir if NPM_TOKEN env var is set',
|
|
20
|
-
default: true,
|
|
21
|
-
},
|
|
22
|
-
appYamlPassEnv: {
|
|
23
|
-
type: 'string',
|
|
24
|
-
desc: 'Comma-separated list of env variables that will be passed to app.yaml from process.env',
|
|
25
|
-
},
|
|
26
|
-
};
|
|
27
|
-
const DEFAULT_FILES = [
|
|
28
|
-
'dist',
|
|
29
|
-
// '!dist/test',
|
|
30
|
-
// '!dist/**/*.test.*',
|
|
31
|
-
// '!dist/**/*.mock.*',
|
|
32
|
-
'!dist/**/*.script.*',
|
|
33
|
-
'src', // For Sentry
|
|
34
|
-
'!src/test',
|
|
35
|
-
'!src/**/*.test.*',
|
|
36
|
-
'!src/**/*.mock.*',
|
|
37
|
-
'!src/**/*.script.*',
|
|
38
|
-
'!**/__snapshots__',
|
|
39
|
-
'!**/__exclude',
|
|
40
|
-
'static',
|
|
41
|
-
'secret/**/*.enc', // encrypted secrets
|
|
42
|
-
'package.json',
|
|
43
|
-
'yarn.lock',
|
|
44
|
-
'.yarnrc',
|
|
45
|
-
'pnpm-lock.yaml',
|
|
46
|
-
'pnpm-workspace.yaml',
|
|
47
|
-
// 'tsconfig.json', // for path-mapping to work!
|
|
48
|
-
// 'tsconfig.dist.json',
|
|
49
|
-
'.gcloudignore',
|
|
50
|
-
'app.yaml',
|
|
51
|
-
'patches', // to allow patch-package
|
|
52
|
-
'resources',
|
|
53
|
-
];
|
|
54
|
-
const defaultFilesDir = `${srcDir}/deploy/files-default`;
|
|
55
|
-
export async function deployPrepare(opt = {}) {
|
|
56
|
-
loadEnvFileIfExists();
|
|
57
|
-
const { projectDir = '.', targetDir = './tmp/deploy', createNpmrc = true } = opt;
|
|
58
|
-
const backendCfg = getBackendCfg(projectDir);
|
|
59
|
-
const inputPatterns = backendCfg.files || DEFAULT_FILES;
|
|
60
|
-
const appYamlPassEnv = opt.appYamlPassEnv || backendCfg.appYamlPassEnv;
|
|
61
|
-
console.log(`1. Copy files to ${dimGrey(targetDir)}`);
|
|
62
|
-
// Clean targetDir
|
|
63
|
-
fs2.emptyDir(targetDir);
|
|
64
|
-
kpySync({
|
|
65
|
-
baseDir: defaultFilesDir,
|
|
66
|
-
outputDir: targetDir,
|
|
67
|
-
dotfiles: true,
|
|
68
|
-
});
|
|
69
|
-
kpySync({
|
|
70
|
-
baseDir: projectDir,
|
|
71
|
-
inputPatterns,
|
|
72
|
-
outputDir: targetDir,
|
|
73
|
-
dotfiles: true,
|
|
74
|
-
});
|
|
75
|
-
const { NPM_TOKEN } = process.env;
|
|
76
|
-
if (NPM_TOKEN && createNpmrc) {
|
|
77
|
-
const npmrcPath = `${targetDir}/.npmrc`;
|
|
78
|
-
const npmrc = `//registry.npmjs.org/:_authToken=${NPM_TOKEN}`;
|
|
79
|
-
fs2.writeFile(npmrcPath, npmrc);
|
|
80
|
-
}
|
|
81
|
-
console.log(`2. Generate ${dimGrey('deployInfo.json')} and ${dimGrey('app.yaml')} in targetDir`);
|
|
82
|
-
const deployInfo = await createAndSaveDeployInfo(backendCfg, targetDir);
|
|
83
|
-
createAndSaveAppYaml(backendCfg, deployInfo, projectDir, targetDir, appYamlPassEnv);
|
|
84
|
-
return deployInfo;
|
|
85
|
-
}
|
package/src/bin/deploy-gae.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { _parseArgs } from '@naturalcycles/nodejs-lib/args'
|
|
2
|
-
import { runScript } from '@naturalcycles/nodejs-lib/runScript'
|
|
3
|
-
import { deployGae } from '../deploy/deployGae.js'
|
|
4
|
-
import { deployHealthCheckCliOptions } from '../deploy/deployHealthCheck.js'
|
|
5
|
-
import { deployPrepareCliOptions } from '../deploy/deployPrepare.js'
|
|
6
|
-
|
|
7
|
-
runScript(async () => {
|
|
8
|
-
const opt = _parseArgs({
|
|
9
|
-
...deployPrepareCliOptions,
|
|
10
|
-
...deployHealthCheckCliOptions,
|
|
11
|
-
})
|
|
12
|
-
|
|
13
|
-
await deployGae(opt)
|
|
14
|
-
})
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
|
|
3
|
-
yarn deploy-prepare
|
|
4
|
-
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { _parseArgs } from '@naturalcycles/nodejs-lib/args'
|
|
8
|
-
import { runScript } from '@naturalcycles/nodejs-lib/runScript'
|
|
9
|
-
import { deployPrepare, deployPrepareCliOptions } from '../deploy/deployPrepare.js'
|
|
10
|
-
|
|
11
|
-
runScript(async () => {
|
|
12
|
-
const opt = _parseArgs(deployPrepareCliOptions)
|
|
13
|
-
|
|
14
|
-
await deployPrepare(opt)
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
// deploy strategy
|
|
18
|
-
// gae project: from config
|
|
19
|
-
// gae service: from config -- branch name
|
|
20
|
-
// gae version: automatic form date
|
|
21
|
-
|
|
22
|
-
// yarn build-prod
|
|
23
|
-
// yarn deploy-prepare && ./tmp/deploy/app.yaml && ./tmp/deploy/deployInfo.json && json2env
|
|
24
|
-
//
|
package/src/bin/undeploy-gae.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { _parseArgs } from '@naturalcycles/nodejs-lib/args'
|
|
2
|
-
import { runScript } from '@naturalcycles/nodejs-lib/runScript'
|
|
3
|
-
import { undeployGae } from '../deploy/deployGae.js'
|
|
4
|
-
|
|
5
|
-
runScript(async () => {
|
|
6
|
-
const { branch } = _parseArgs({
|
|
7
|
-
branch: {
|
|
8
|
-
type: 'string',
|
|
9
|
-
demandOption: true,
|
|
10
|
-
desc: `Because Github Actions delete event happens after the branch is already deleted - you need to pass it manually`,
|
|
11
|
-
},
|
|
12
|
-
})
|
|
13
|
-
|
|
14
|
-
await undeployGae(branch)
|
|
15
|
-
})
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import type { StringMap } from '@naturalcycles/js-lib/types'
|
|
2
|
-
import { j } from '@naturalcycles/nodejs-lib/ajv'
|
|
3
|
-
import { fs2 } from '@naturalcycles/nodejs-lib/fs2'
|
|
4
|
-
import { yaml2 } from '@naturalcycles/nodejs-lib/yaml2'
|
|
5
|
-
|
|
6
|
-
export interface BackendCfg {
|
|
7
|
-
gaeProject: string
|
|
8
|
-
gaeProjectByBranch?: StringMap
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* @example default
|
|
12
|
-
*/
|
|
13
|
-
gaeService: string
|
|
14
|
-
gaeServiceByBranch?: StringMap
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* List of file patterns to include in deployment.
|
|
18
|
-
*/
|
|
19
|
-
files?: string[]
|
|
20
|
-
|
|
21
|
-
appEnvDefault: string
|
|
22
|
-
appEnvByBranch?: StringMap
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* List of branches to use timestamps in gae version names (to keep previous versions).
|
|
26
|
-
*/
|
|
27
|
-
branchesWithTimestampVersions?: string[]
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* If true - branch names are not passed into deployed urls as is, but are hashed.
|
|
31
|
-
*/
|
|
32
|
-
hashedBranches?: boolean
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Comma-separated list of env variables that will be passed to app.yaml from process.env
|
|
36
|
-
*/
|
|
37
|
-
appYamlPassEnv?: string
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export const backendCfgSchema = j.object<BackendCfg>({
|
|
41
|
-
gaeProject: j.string(),
|
|
42
|
-
gaeProjectByBranch: j.object.stringMap(j.string()).optional(),
|
|
43
|
-
gaeService: j.string(),
|
|
44
|
-
gaeServiceByBranch: j.object.stringMap(j.string()).optional(),
|
|
45
|
-
files: j.array(j.string()).optional(),
|
|
46
|
-
appEnvDefault: j.string(),
|
|
47
|
-
appEnvByBranch: j.object.stringMap(j.string()).optional(),
|
|
48
|
-
branchesWithTimestampVersions: j.array(j.string()).optional(),
|
|
49
|
-
hashedBranches: j.boolean().optional(),
|
|
50
|
-
appYamlPassEnv: j.string().optional(),
|
|
51
|
-
})
|
|
52
|
-
|
|
53
|
-
export function getBackendCfg(projectDir = '.'): BackendCfg {
|
|
54
|
-
const backendCfgYamlPath = `${projectDir}/backend.cfg.yaml`
|
|
55
|
-
|
|
56
|
-
fs2.requireFileToExist(backendCfgYamlPath)
|
|
57
|
-
|
|
58
|
-
const backendCfg: BackendCfg = {
|
|
59
|
-
...yaml2.readYaml(backendCfgYamlPath),
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
return backendCfgSchema.validate(backendCfg)
|
|
63
|
-
}
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import type { UnixTimestamp } from '@naturalcycles/js-lib/types'
|
|
2
|
-
|
|
3
|
-
export interface DeployInfo {
|
|
4
|
-
gaeProject: string
|
|
5
|
-
gaeService: string
|
|
6
|
-
gaeVersion: string
|
|
7
|
-
versionUrl: string
|
|
8
|
-
serviceUrl: string
|
|
9
|
-
gitRev: string
|
|
10
|
-
gitBranch: string
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Unix timestamp of deployInfo.json being generated.
|
|
14
|
-
*/
|
|
15
|
-
ts: UnixTimestamp
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export interface AppYaml extends Record<string, any> {
|
|
19
|
-
runtime: string
|
|
20
|
-
service: string
|
|
21
|
-
inbound_services?: string[]
|
|
22
|
-
instance_class?: string
|
|
23
|
-
automatic_scaling?: {
|
|
24
|
-
min_instances?: number
|
|
25
|
-
max_instances?: number
|
|
26
|
-
}
|
|
27
|
-
env_variables: {
|
|
28
|
-
APP_ENV: string
|
|
29
|
-
DEBUG: string
|
|
30
|
-
[k: string]: string
|
|
31
|
-
}
|
|
32
|
-
}
|
|
@@ -1,214 +0,0 @@
|
|
|
1
|
-
import { localTime } from '@naturalcycles/js-lib/datetime/localTime.js'
|
|
2
|
-
import { _assert } from '@naturalcycles/js-lib/error/assert.js'
|
|
3
|
-
import { _mapValues, _merge } from '@naturalcycles/js-lib/object/object.util.js'
|
|
4
|
-
import { _truncate } from '@naturalcycles/js-lib/string/string.util.js'
|
|
5
|
-
import { sha256 } from '@naturalcycles/nodejs-lib'
|
|
6
|
-
import { dimGrey, white } from '@naturalcycles/nodejs-lib/colors'
|
|
7
|
-
import { fs2 } from '@naturalcycles/nodejs-lib/fs2'
|
|
8
|
-
import { yaml2 } from '@naturalcycles/nodejs-lib/yaml2'
|
|
9
|
-
import type { BackendCfg } from './backend.cfg.util.js'
|
|
10
|
-
import type { AppYaml, DeployInfo } from './deploy.model.js'
|
|
11
|
-
|
|
12
|
-
const APP_YAML_DEFAULT = (): AppYaml => ({
|
|
13
|
-
runtime: 'nodejs22',
|
|
14
|
-
service: 'default',
|
|
15
|
-
inbound_services: ['warmup'],
|
|
16
|
-
instance_class: 'F1',
|
|
17
|
-
automatic_scaling: {
|
|
18
|
-
min_instances: 0,
|
|
19
|
-
max_instances: 1,
|
|
20
|
-
},
|
|
21
|
-
env_variables: {
|
|
22
|
-
APP_ENV: 'prod',
|
|
23
|
-
DEBUG: 'app*,nc:*',
|
|
24
|
-
DEBUG_HIDE_DATE: 'true',
|
|
25
|
-
// DEBUG_COLORS: 'true',
|
|
26
|
-
TZ: 'UTC',
|
|
27
|
-
},
|
|
28
|
-
})
|
|
29
|
-
|
|
30
|
-
export async function createAndSaveDeployInfo(
|
|
31
|
-
backendCfg: BackendCfg,
|
|
32
|
-
targetDir: string,
|
|
33
|
-
): Promise<DeployInfo> {
|
|
34
|
-
const deployInfo = await createDeployInfo(backendCfg)
|
|
35
|
-
const deployInfoPath = `${targetDir}/deployInfo.json`
|
|
36
|
-
fs2.writeJson(deployInfoPath, deployInfo, { spaces: 2 })
|
|
37
|
-
console.log(`saved ${dimGrey(deployInfoPath)}`)
|
|
38
|
-
return deployInfo
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export async function createDeployInfo(
|
|
42
|
-
backendCfg: BackendCfg,
|
|
43
|
-
overrideBranch?: string,
|
|
44
|
-
): Promise<DeployInfo> {
|
|
45
|
-
const { simpleGit } = await import('simple-git') // lazy load
|
|
46
|
-
const git = simpleGit('.')
|
|
47
|
-
|
|
48
|
-
const now = localTime.now()
|
|
49
|
-
const gitBranch = overrideBranch || (await git.status()).current!
|
|
50
|
-
const gitRev = (await git.revparse(['HEAD'])).slice(0, 7)
|
|
51
|
-
|
|
52
|
-
let {
|
|
53
|
-
gaeProject,
|
|
54
|
-
gaeProjectByBranch = {},
|
|
55
|
-
gaeService,
|
|
56
|
-
gaeServiceByBranch = {},
|
|
57
|
-
branchesWithTimestampVersions = [],
|
|
58
|
-
} = backendCfg
|
|
59
|
-
|
|
60
|
-
gaeProject = gaeProjectByBranch[gitBranch] || gaeProject
|
|
61
|
-
|
|
62
|
-
if (gaeServiceByBranch[gitBranch]) {
|
|
63
|
-
gaeService = validateGAEServiceName(gaeServiceByBranch[gitBranch])
|
|
64
|
-
} else {
|
|
65
|
-
let branchName = gitBranch
|
|
66
|
-
|
|
67
|
-
if (backendCfg.hashedBranches) {
|
|
68
|
-
// Obfuscates the branch name by hashing it.
|
|
69
|
-
// If there are Jira issue names in the branch name, the first one found will be used as a prefix.
|
|
70
|
-
const jiraIssue = gitBranch.match(/([Dd][Ee][Vv]-\d+)/)?.[0]
|
|
71
|
-
const branchHash = sha256(gitBranch).slice(0, 10)
|
|
72
|
-
branchName = [jiraIssue, branchHash].filter(Boolean).join('-')
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
gaeService = validateGAEServiceName([branchName, gaeService].join('--'))
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
let gaeVersion = '1'
|
|
79
|
-
|
|
80
|
-
if (branchesWithTimestampVersions.includes(gitBranch)) {
|
|
81
|
-
// May only contain lowercase letters, digits, and hyphens. Must begin and end with a letter or digit. Must not exceed 63 characters.
|
|
82
|
-
gaeVersion = [
|
|
83
|
-
// now.format('YYYYMMDD-HHmm'), // 20190521-1721
|
|
84
|
-
now.toStringCompact().replace('_', '-'),
|
|
85
|
-
gitRev,
|
|
86
|
-
].join('-')
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
const versionUrl = `https://${[gaeVersion, gaeService, gaeProject].join('-dot-')}.appspot.com`
|
|
90
|
-
|
|
91
|
-
// Check the 63-char limit
|
|
92
|
-
const versionUrlString = [gaeVersion, gaeService, gaeProject].join('-dot-')
|
|
93
|
-
_assert(
|
|
94
|
-
versionUrlString.length <= 63,
|
|
95
|
-
`versionUrl length should be <= 63 characters, but it's ${versionUrlString.length} instead: ${versionUrlString}`,
|
|
96
|
-
)
|
|
97
|
-
|
|
98
|
-
const serviceUrl = `https://${[gaeService, gaeProject].join('-dot-')}.appspot.com`
|
|
99
|
-
|
|
100
|
-
const deployInfo: DeployInfo = {
|
|
101
|
-
gaeProject,
|
|
102
|
-
gaeService,
|
|
103
|
-
gaeVersion,
|
|
104
|
-
versionUrl,
|
|
105
|
-
serviceUrl,
|
|
106
|
-
gitBranch,
|
|
107
|
-
gitRev,
|
|
108
|
-
ts: now.unix,
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
console.log({ deployInfo })
|
|
112
|
-
|
|
113
|
-
return deployInfo
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
export function createAndSaveAppYaml(
|
|
117
|
-
backendCfg: BackendCfg,
|
|
118
|
-
deployInfo: DeployInfo,
|
|
119
|
-
projectDir: string,
|
|
120
|
-
targetDir: string,
|
|
121
|
-
appYamlPassEnv = '',
|
|
122
|
-
): AppYaml {
|
|
123
|
-
const appYaml = createAppYaml(backendCfg, deployInfo, projectDir, appYamlPassEnv)
|
|
124
|
-
const appYamlPath = `${targetDir}/app.yaml`
|
|
125
|
-
yaml2.writeYaml(appYamlPath, appYaml)
|
|
126
|
-
console.log(`saved ${dimGrey(appYamlPath)}`)
|
|
127
|
-
return appYaml
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
export function createAppYaml(
|
|
131
|
-
backendCfg: BackendCfg,
|
|
132
|
-
deployInfo: DeployInfo,
|
|
133
|
-
projectDir: string,
|
|
134
|
-
appYamlPassEnv = '',
|
|
135
|
-
): AppYaml {
|
|
136
|
-
const { appEnvDefault, appEnvByBranch = {} } = backendCfg
|
|
137
|
-
const { gaeService: service, gitBranch } = deployInfo
|
|
138
|
-
|
|
139
|
-
const { APP_ENV: processAppEnv } = process.env
|
|
140
|
-
const APP_ENV = processAppEnv || appEnvByBranch[gitBranch] || appEnvDefault
|
|
141
|
-
if (processAppEnv) {
|
|
142
|
-
console.log(`using APP_ENV=${dimGrey(processAppEnv)} from process.env`)
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
const appYaml = APP_YAML_DEFAULT()
|
|
146
|
-
|
|
147
|
-
// Check existing app.yaml
|
|
148
|
-
const appYamlPath = `${projectDir}/app.yaml`
|
|
149
|
-
if (fs2.pathExists(appYamlPath)) {
|
|
150
|
-
console.log(`merging-in ${dimGrey(appYamlPath)}`)
|
|
151
|
-
_merge(appYaml, yaml2.readYaml(appYamlPath))
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
const appEnvYamlPath = `${projectDir}/app.${APP_ENV}.yaml`
|
|
155
|
-
if (fs2.pathExists(appEnvYamlPath)) {
|
|
156
|
-
console.log(`merging-in ${dimGrey(appEnvYamlPath)}`)
|
|
157
|
-
_merge(appYaml, yaml2.readYaml(appEnvYamlPath))
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
// appYamlPassEnv
|
|
161
|
-
const passEnv = appYamlPassEnv
|
|
162
|
-
.split(',')
|
|
163
|
-
.filter(Boolean)
|
|
164
|
-
// oxlint-disable-next-line unicorn/no-array-reduce
|
|
165
|
-
.reduce<Record<string, string>>((map, key) => {
|
|
166
|
-
const v = process.env[key]
|
|
167
|
-
if (!v) {
|
|
168
|
-
throw new Error(
|
|
169
|
-
`appYamlPassEnv.${key} is requested, but process.env.${key} is not defined!`,
|
|
170
|
-
)
|
|
171
|
-
}
|
|
172
|
-
map[key] = v
|
|
173
|
-
return map
|
|
174
|
-
}, {})
|
|
175
|
-
|
|
176
|
-
if (Object.keys(passEnv).length) {
|
|
177
|
-
console.log(
|
|
178
|
-
`will merge ${white(
|
|
179
|
-
String(Object.keys(passEnv).length),
|
|
180
|
-
)} process.env keys to app.yaml: ${dimGrey(Object.keys(passEnv).join(', '))}`,
|
|
181
|
-
)
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
_merge(appYaml, {
|
|
185
|
-
service,
|
|
186
|
-
env_variables: {
|
|
187
|
-
APP_ENV,
|
|
188
|
-
...passEnv,
|
|
189
|
-
},
|
|
190
|
-
})
|
|
191
|
-
|
|
192
|
-
// Redacted appYaml to not show up secrets
|
|
193
|
-
console.log({
|
|
194
|
-
appYaml: redactedAppYaml(appYaml),
|
|
195
|
-
})
|
|
196
|
-
|
|
197
|
-
return appYaml
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
function redactedAppYaml(appYaml: AppYaml): AppYaml {
|
|
201
|
-
return {
|
|
202
|
-
...appYaml,
|
|
203
|
-
env_variables: _mapValues(appYaml.env_variables || {}, (_k, v) => _truncate(v, 7)),
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
export function validateGAEServiceName(serviceName: string): string {
|
|
208
|
-
// May only contain lowercase letters, digits, and hyphens. Must begin and end with a letter or digit. Must not exceed 63 characters.
|
|
209
|
-
return serviceName
|
|
210
|
-
.replaceAll('_', '-')
|
|
211
|
-
.toLowerCase()
|
|
212
|
-
.replaceAll(/[^0-9a-z-]/gi, '')
|
|
213
|
-
.slice(0, 40)
|
|
214
|
-
}
|
package/src/deploy/deployGae.ts
DELETED
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
import { _anyToError } from '@naturalcycles/js-lib/error'
|
|
2
|
-
import { pRetry } from '@naturalcycles/js-lib/promise/pRetry.js'
|
|
3
|
-
import { _objectAssign } from '@naturalcycles/js-lib/types'
|
|
4
|
-
import { appendToGithubSummary } from '@naturalcycles/nodejs-lib/env'
|
|
5
|
-
import { exec2 } from '@naturalcycles/nodejs-lib/exec2'
|
|
6
|
-
import { fs2 } from '@naturalcycles/nodejs-lib/fs2'
|
|
7
|
-
import { getBackendCfg } from './backend.cfg.util.js'
|
|
8
|
-
import { createDeployInfo } from './deploy.util.js'
|
|
9
|
-
import type { DeployHealthCheckOptions } from './deployHealthCheck.js'
|
|
10
|
-
import { deployHealthCheck } from './deployHealthCheck.js'
|
|
11
|
-
import type { DeployPrepareOptions } from './deployPrepare.js'
|
|
12
|
-
import { deployPrepare } from './deployPrepare.js'
|
|
13
|
-
|
|
14
|
-
export interface DeployGaeOptions extends DeployPrepareOptions, DeployHealthCheckOptions {}
|
|
15
|
-
|
|
16
|
-
export async function deployGae(opt: DeployGaeOptions = {}): Promise<void> {
|
|
17
|
-
const { logOnFailure, logOnSuccess } = opt
|
|
18
|
-
|
|
19
|
-
// 1. build
|
|
20
|
-
|
|
21
|
-
const packageManager = detectPackageManager()
|
|
22
|
-
|
|
23
|
-
exec2.spawn(`${packageManager} build`)
|
|
24
|
-
|
|
25
|
-
// 2. deploy-prepare
|
|
26
|
-
|
|
27
|
-
const deployInfo = await deployPrepare()
|
|
28
|
-
|
|
29
|
-
const targetDir = './tmp/deploy'
|
|
30
|
-
const appYamlPath = `${targetDir}/app.yaml`
|
|
31
|
-
|
|
32
|
-
const { gaeProject, gaeService, gaeVersion, versionUrl, serviceUrl } = deployInfo
|
|
33
|
-
_objectAssign(opt, {
|
|
34
|
-
gaeProject,
|
|
35
|
-
gaeService,
|
|
36
|
-
gaeVersion,
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
await pRetry(
|
|
40
|
-
async () => {
|
|
41
|
-
try {
|
|
42
|
-
exec2.spawn(
|
|
43
|
-
`gcloud app deploy ${appYamlPath} --project ${gaeProject} --version ${gaeVersion} --quiet --no-promote`,
|
|
44
|
-
)
|
|
45
|
-
} catch (err) {
|
|
46
|
-
if (logOnFailure) {
|
|
47
|
-
logs(gaeProject, gaeService, gaeVersion)
|
|
48
|
-
}
|
|
49
|
-
throw err
|
|
50
|
-
}
|
|
51
|
-
},
|
|
52
|
-
{
|
|
53
|
-
name: 'deploy',
|
|
54
|
-
maxAttempts: 2,
|
|
55
|
-
delay: 30_000,
|
|
56
|
-
// todo: this doesn't work, as the error is different from what is logged.
|
|
57
|
-
// We shoud somehow capture the logged text
|
|
58
|
-
predicate: err => _anyToError(err).message.includes('operation is already in progress'),
|
|
59
|
-
},
|
|
60
|
-
)
|
|
61
|
-
|
|
62
|
-
// Health check (versionUrl)
|
|
63
|
-
// yarn deploy-health-check --url $deployInfo_versionUrl --repeat 3 --timeoutSec 180 --intervalSec 2
|
|
64
|
-
await deployHealthCheck(versionUrl, opt)
|
|
65
|
-
|
|
66
|
-
// Only if "timestamped version" is used ('1' is default)
|
|
67
|
-
if (gaeVersion !== '1') {
|
|
68
|
-
// Rollout (promote versionUrl to serviceUrl)
|
|
69
|
-
// gcloud app services set-traffic $deployInfo_gaeService --project $deployInfo_gaeProject --splits $deployInfo_gaeVersion=1 --quiet
|
|
70
|
-
exec2.spawn(
|
|
71
|
-
`gcloud app services set-traffic ${gaeService} --project ${gaeProject} --splits ${gaeVersion}=1 --quiet`,
|
|
72
|
-
)
|
|
73
|
-
|
|
74
|
-
// Health check (serviceUrl)
|
|
75
|
-
// yarn deploy-health-check --url $deployInfo_serviceUrl --repeat 3 --timeoutSec 60 --intervalSec 2
|
|
76
|
-
await deployHealthCheck(serviceUrl, opt)
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
appendToGithubSummary(`Deployed ${serviceUrl}`)
|
|
80
|
-
|
|
81
|
-
// Logs
|
|
82
|
-
if (logOnSuccess) {
|
|
83
|
-
logs(gaeProject, gaeService, gaeVersion)
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Undeploys/removes the GAE service/version, using the same rules as deployGae.
|
|
89
|
-
* Detects the service/version by the same criteria: branch name, backend.cfg.yaml, etc.
|
|
90
|
-
*/
|
|
91
|
-
export async function undeployGae(branch: string): Promise<void> {
|
|
92
|
-
const projectDir = '.'
|
|
93
|
-
const backendCfg = getBackendCfg(projectDir)
|
|
94
|
-
|
|
95
|
-
const { gaeProject, gaeService, gaeVersion } = await createDeployInfo(backendCfg, branch)
|
|
96
|
-
|
|
97
|
-
// if (prod) {
|
|
98
|
-
// console.log(`undeployGae (branch: ${branch}): not removing prod version (safety check)`)
|
|
99
|
-
// return
|
|
100
|
-
// }
|
|
101
|
-
|
|
102
|
-
console.log(
|
|
103
|
-
`undeployGae (branch: ${branch}): going to remove ${gaeProject}/${gaeService}/${gaeVersion}`,
|
|
104
|
-
)
|
|
105
|
-
|
|
106
|
-
exec2.spawn(
|
|
107
|
-
`gcloud app versions delete --project ${gaeProject} --service ${gaeService} ${gaeVersion} --quiet`,
|
|
108
|
-
)
|
|
109
|
-
|
|
110
|
-
appendToGithubSummary(`removed ${gaeProject}/${gaeService}/${gaeVersion}`)
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
function logs(gaeProject: string, gaeService: string, gaeVersion: string): void {
|
|
114
|
-
try {
|
|
115
|
-
exec2.spawn(
|
|
116
|
-
`gcloud app logs read --project ${gaeProject} --service ${gaeService} --version ${gaeVersion}`,
|
|
117
|
-
)
|
|
118
|
-
} catch {}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
function detectPackageManager(): string {
|
|
122
|
-
const pj = fs2.readText('package.json')
|
|
123
|
-
if (pj.includes('pnpm')) {
|
|
124
|
-
return 'pnpm'
|
|
125
|
-
}
|
|
126
|
-
return 'yarn' // yarn is still the default
|
|
127
|
-
}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { localTime } from '@naturalcycles/js-lib/datetime/localTime.js'
|
|
2
|
-
import { _memoized } from '@naturalcycles/js-lib/decorators'
|
|
3
|
-
import { fs2 } from '@naturalcycles/nodejs-lib/fs2'
|
|
4
|
-
import type { DeployInfo } from './deploy.model.js'
|
|
5
|
-
|
|
6
|
-
export const getDeployInfo = _memoized((projectDir: string): DeployInfo => {
|
|
7
|
-
const deployInfoPath = `${projectDir}/deployInfo.json`
|
|
8
|
-
try {
|
|
9
|
-
return fs2.readJson(deployInfoPath)
|
|
10
|
-
} catch {
|
|
11
|
-
// console.error(`cannot read ${deployInfoPath}, returning empty version`)
|
|
12
|
-
return getDeployInfoStub()
|
|
13
|
-
}
|
|
14
|
-
})
|
|
15
|
-
|
|
16
|
-
function getDeployInfoStub(stub = ''): DeployInfo {
|
|
17
|
-
return {
|
|
18
|
-
gaeProject: stub,
|
|
19
|
-
gaeService: stub,
|
|
20
|
-
gaeVersion: stub,
|
|
21
|
-
serviceUrl: stub,
|
|
22
|
-
versionUrl: stub,
|
|
23
|
-
gitBranch: stub,
|
|
24
|
-
gitRev: stub,
|
|
25
|
-
ts: localTime.nowUnix(),
|
|
26
|
-
}
|
|
27
|
-
}
|