Skip to content

Commit

Permalink
Add --env-file for cli deploy command (#1743)
Browse files Browse the repository at this point in the history
  • Loading branch information
aswamy authored Feb 14, 2024
1 parent 7a724e4 commit faeba9f
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 69 deletions.
7 changes: 7 additions & 0 deletions .changeset/chatty-donuts-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@shopify/cli-hydrogen': patch
---

Add `--env-file` flag to the `deploy` command

Optionally provide the path to a local .env file to override the environment variables set on Admin.
140 changes: 72 additions & 68 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions packages/cli/oclif.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@
"required": false,
"multiple": false
},
"env-file": {
"name": "env-file",
"type": "option",
"description": "Path to an environment file to override existing environment variables for the deployment.",
"required": false,
"multiple": false
},
"preview": {
"name": "preview",
"type": "boolean",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"@shopify/cli-kit": "3.52.0",
"@shopify/hydrogen-codegen": "^0.2.1",
"@shopify/mini-oxygen": "^2.2.5",
"@shopify/oxygen-cli": "^4.0.0",
"@shopify/oxygen-cli": "^4.1.0",
"ansi-escapes": "^6.2.0",
"cli-truncate": "^4.0.0",
"diff": "^5.1.0",
Expand Down
48 changes: 48 additions & 0 deletions packages/cli/src/commands/hydrogen/deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ import {
createDeploy,
parseToken,
} from '@shopify/oxygen-cli/deploy';
import {loadEnvironmentVariableFile} from '@shopify/oxygen-cli/utils';
import {ciPlatform} from '@shopify/cli-kit/node/context/local';

vi.mock('../../lib/get-oxygen-deployment-data.js');
vi.mock('@shopify/oxygen-cli/deploy');
vi.mock('@shopify/oxygen-cli/utils');
vi.mock('@shopify/cli-kit/node/fs');
vi.mock('@shopify/cli-kit/node/context/local');
vi.mock('../../lib/auth.js');
Expand Down Expand Up @@ -186,6 +188,52 @@ describe('deploy', () => {
expect(vi.mocked(renderSuccess)).toHaveBeenCalled;
});

it('calls createDeploy with overridden variables in environment file', async () => {
const overriddenEnvironmentVariables = [
{
key: 'fake-key',
value: 'fake-value',
isSecret: true,
},
];

vi.mocked(loadEnvironmentVariableFile).mockReturnValue(
overriddenEnvironmentVariables,
);

await oxygenDeploy({
...deployParams,
environmentFile: 'fake-env-file',
});

expect(vi.mocked(createDeploy)).toHaveBeenCalledWith({
config: {
...expectedConfig,
overriddenEnvironmentVariables,
},
hooks: expectedHooks,
logger: deploymentLogger,
});
});

it('errors when supplied environment file does not exist', async () => {
vi.mocked(loadEnvironmentVariableFile).mockImplementation(
(_path: string) => {
throw new AbortError('File not found');
},
);

try {
await oxygenDeploy({
...deployParams,
environmentFile: 'fake-env-file',
});
expect(true).toBe(false);
} catch (err) {
expect(err).toBeInstanceOf(AbortError);
}
});

it('errors when there are uncommited changes', async () => {
vi.mocked(ensureIsClean).mockRejectedValue(
new GitDirectoryNotCleanError('Uncommitted changes'),
Expand Down
23 changes: 23 additions & 0 deletions packages/cli/src/commands/hydrogen/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
DeploymentVerificationDetailsResponse,
parseToken,
} from '@shopify/oxygen-cli/deploy';
import {loadEnvironmentVariableFile} from '@shopify/oxygen-cli/utils';

import {commonFlags, flagsToCamelObject} from '../../lib/flags.js';
import {getOxygenDeploymentData} from '../../lib/get-oxygen-deployment-data.js';
Expand All @@ -53,6 +54,11 @@ export default class Deploy extends Command {
description: 'Environment branch (tag) for environment to deploy to.',
required: false,
}),
'env-file': Flags.string({
description:
'Path to an environment file to override existing environment variables for the deployment.',
required: false,
}),
preview: Flags.boolean({
description:
'Deploys to the Preview environment. Overrides --env-branch and Git metadata.',
Expand Down Expand Up @@ -137,6 +143,7 @@ export default class Deploy extends Command {
...camelFlags,
defaultEnvironment: flags.preview,
environmentTag: flags['env-branch'],
environmentFile: flags['env-file'],
path: flags.path ? resolvePath(flags.path) : process.cwd(),
} as OxygenDeploymentOptions;
}
Expand All @@ -146,6 +153,7 @@ interface OxygenDeploymentOptions {
authBypassToken: boolean;
defaultEnvironment: boolean;
environmentTag?: string;
environmentFile?: string;
force: boolean;
noJsonOutput: boolean;
path: string;
Expand Down Expand Up @@ -188,6 +196,7 @@ export async function oxygenDeploy(
authBypassToken: generateAuthBypassToken,
defaultEnvironment,
environmentTag,
environmentFile,
force: forceOnUncommitedChanges,
noJsonOutput,
path,
Expand Down Expand Up @@ -248,6 +257,19 @@ export async function oxygenDeploy(
metadataDescription = `${commitHash} with additional changes`;
}

let overriddenEnvironmentVariables;

if (environmentFile) {
try {
overriddenEnvironmentVariables =
loadEnvironmentVariableFile(environmentFile);
} catch (error) {
throw new AbortError(
`Could not load environment file at ${environmentFile}`,
);
}
}

if (!isCI) {
deploymentData = await getOxygenDeploymentData({
root: path,
Expand Down Expand Up @@ -343,6 +365,7 @@ export async function oxygenDeploy(
skipBuild: false,
workerOnly: false,
workerDir: 'dist/worker',
overriddenEnvironmentVariables,
};

let resolveUpload: () => void;
Expand Down

0 comments on commit faeba9f

Please sign in to comment.