Skip to content

Commit 80591ea

Browse files
authored
feat(cli): Deploy, added --replace-env flag (#9095)
1 parent cef0714 commit 80591ea

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

packages/cubejs-backend-cloud/src/cloud.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fetch, { RequestInit } from 'node-fetch';
22
import FormData from 'form-data';
33
import path from 'path';
44
import { ReadStream } from 'fs';
5+
import { DotenvParseOutput } from '@cubejs-backend/dotenv';
56

67
export type AuthObject = {
78
auth: string,
@@ -145,9 +146,10 @@ export class CubeCloudClient {
145146
});
146147
}
147148

148-
public setEnvVars({ envVariables, auth }: { envVariables: any, auth?: AuthObject }) {
149+
public setEnvVars({ envVariables, auth, replaceEnv }: { envVariables: DotenvParseOutput, auth?: AuthObject, replaceEnv?: boolean }) {
150+
const params = new URLSearchParams({ replaceEnv: Boolean(replaceEnv).toString() });
149151
return this.request({
150-
url: (deploymentId) => `build/deploy/${deploymentId}/set-env`,
152+
url: (deploymentId) => `build/deploy/${deploymentId}/set-env?${params.toString()}`,
151153
method: 'POST',
152154
body: JSON.stringify({ envVariables: JSON.stringify(envVariables) }),
153155
headers: {

packages/cubejs-backend-cloud/src/deploy.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export interface DeployResponse {
7979
export class DeployController {
8080
public constructor(
8181
protected readonly cubeCloudClient: CubeCloudClient,
82-
protected readonly envVariables: DotenvParseOutput = {},
82+
protected readonly envs: { envVariables?: DotenvParseOutput, replaceEnv?: boolean } = {},
8383
protected readonly hooks: DeployHooks = {}
8484
) {
8585
}
@@ -92,8 +92,11 @@ export class DeployController {
9292
const upstreamHashes = await this.cubeCloudClient.getUpstreamHashes();
9393
const { transaction, deploymentName } = await this.cubeCloudClient.startUpload();
9494

95-
if (Object.keys(this.envVariables).length) {
96-
await this.cubeCloudClient.setEnvVars({ envVariables: this.envVariables });
95+
if (this.envs.envVariables) {
96+
const { envVariables, replaceEnv } = this.envs;
97+
if (Object.keys(this.envs.envVariables).length) {
98+
await this.cubeCloudClient.setEnvVars({ envVariables, replaceEnv });
99+
}
97100
}
98101

99102
const files = Object.keys(fileHashes);

packages/cubejs-cli/src/command/deploy.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,20 @@ import fs from 'fs-extra';
22
import path from 'path';
33
import cliProgress from 'cli-progress';
44
import { CommanderStatic } from 'commander';
5-
import { CubeCloudClient, DeployController } from '@cubejs-backend/cloud';
5+
import { AuthObject, CubeCloudClient, DeployController } from '@cubejs-backend/cloud';
66
import { ConfigCli } from '../config';
77

88
import { logStage, displayError, event } from '../utils';
99

10-
const deploy = async ({ directory, auth, uploadEnv, token }: any) => {
10+
type DeployOptions = {
11+
directory: string,
12+
auth: AuthObject,
13+
uploadEnv: boolean,
14+
replaceEnv: boolean,
15+
token: string
16+
};
17+
18+
const deploy = async ({ directory, auth, uploadEnv, replaceEnv, token }: DeployOptions) => {
1119
if (!(await fs.pathExists(path.join(process.cwd(), 'node_modules', '@cubejs-backend/server-core')))) {
1220
await displayError(
1321
'@cubejs-backend/server-core dependency not found. Please run deploy command from project root directory and ensure npm install has been run.'
@@ -28,10 +36,10 @@ const deploy = async ({ directory, auth, uploadEnv, token }: any) => {
2836
hideCursor: true
2937
});
3038

31-
const envVariables = uploadEnv ? await config.envFile(`${directory}/.env`) : {};
39+
const envVariables = uploadEnv || replaceEnv ? await config.envFile(`${directory}/.env`) : {};
3240

3341
const cubeCloudClient = new CubeCloudClient(auth || (await config.deployAuthForCurrentDir()));
34-
const deployController = new DeployController(cubeCloudClient, envVariables, {
42+
const deployController = new DeployController(cubeCloudClient, { envVariables, replaceEnv }, {
3543
onStart: async (deploymentName, files) => {
3644
await logStage(`Deploying ${deploymentName}...`, 'Cube Cloud CLI Deploy');
3745
bar.start(files.length, 0, {
@@ -57,7 +65,8 @@ export function configureDeployCommand(program: CommanderStatic) {
5765
program
5866
.command('deploy')
5967
.description('Deploy project to Cube Cloud')
60-
.option('--upload-env', 'Upload .env file to CubeCloud')
68+
.option('--upload-env', 'Use .env file to populate environment variables in Cube Cloud. Only set them once during the very first upload for this deployment')
69+
.option('--replace-env', 'Use .env file to populate environment variables in Cube Cloud. Replace them with new ones during every upload for this deployment')
6170
.option('--token <token>', 'Add auth token to CubeCloud')
6271
.option('--directory [path]', 'Specify path to conf directory', './')
6372
.action(

0 commit comments

Comments
 (0)