forked from kriasoft/graphql-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-deploy.js
More file actions
51 lines (44 loc) · 1.71 KB
/
api-deploy.js
File metadata and controls
51 lines (44 loc) · 1.71 KB
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
/* SPDX-FileCopyrightText: 2016-present Kriasoft <hello@kriasoft.com> */
/* SPDX-License-Identifier: MIT */
/**
* Deploys the "api" package to Google Cloud Functions (GCF). Usage:
*
* $ yarn api:deploy [--env #0]
*
* @see https://cloud.google.com/functions
* @see https://cloud.google.com/sdk/gcloud/reference/functions/deploy
*/
import envars from "envars";
import minimist from "minimist";
import { $ } from "zx";
// Load environment variables (PGHOST, PGUSER, etc.)
const args = minimist(process.argv.slice(2));
process.env.NODE_ENV = "production";
process.env.APP_ENV = args.env ?? process.env.APP_ENV ?? "test";
envars.config({ env: process.env.APP_ENV });
// Load the list of environment variables required by the app (api/env.ts)
/** @type {import("api/env").default} */
const env = await import("api/dist/index.js").then((x) => ({ ...x.env }));
// Use Cloud SQL Proxy in Google Cloud Functions (GCF) environment
const region = process.env.GOOGLE_CLOUD_REGION;
env.PGHOST = `/cloudsql/${env.PGSERVERNAME.replace(":", `:${region}:`)}`;
env.PGAPPNAME = `api ${env.APP_ENV} ${new Date().toISOString()}`;
delete env.PGSSLMODE;
delete env.PGSSLCERT;
delete env.PGSSLKEY;
delete env.PGSSLROOTCERT;
delete env.PGSERVERNAME;
const name = args.version ? `api_${args.version}` : `api`;
await $`gcloud functions deploy ${name} ${[
`--project=${process.env.GOOGLE_CLOUD_PROJECT}`,
`--region=${region}`,
`--allow-unauthenticated`,
`--entry-point=api`,
`--memory=1GB`,
`--runtime=nodejs16`,
`--source=./dist`,
`--timeout=30`,
`--trigger-http`,
`--set-env-vars=NODE_OPTIONS=--require=./.pnp.cjs --require=source-map-support/register --no-warnings`,
...Object.keys(env).map((key) => `--set-env-vars=${key}=${env[key]}`),
]}`;