diff --git a/docs/commands/deploy.md b/docs/commands/deploy.md index 573bdadc8f0..a1aaddb5d02 100644 --- a/docs/commands/deploy.md +++ b/docs/commands/deploy.md @@ -30,7 +30,7 @@ netlify deploy - `alias` (*string*) - Specifies the alias for deployment, the string at the beginning of the deploy subdomain. Useful for creating predictable deployment URLs. Avoid setting an alias string to the same value as a deployed branch. `alias` doesn’t create a branch deploy and can’t be used in conjunction with the branch subdomain feature. Maximum 37 characters. - `allow-anonymous` (*boolean*) - If not logged in, deploy anonymously and create a claimable site instead of requiring authentication -- `context` (*string*) - Specify a deploy context for environment variables read during the build ("production", "deploy-preview", "branch-deploy", "dev") or `branch:your-branch` where `your-branch` is the name of a branch (default: dev) +- `context` (*string*) - Specify a deploy context for environment variables read during the build ("production", "deploy-preview", "branch-deploy", "dev") or `branch:your-branch` where `your-branch` is the name of a branch (default: value of CONTEXT or "production") - `created-via` (*string*) - Specify the source of the deploy (e.g., "cli", "drop") - `dir` (*string*) - Specify a folder to deploy - `env` (*string*) - Set an environment variable for this deploy only. Applies to deployed functions only. Can be specified multiple times. diff --git a/eslint-suppressions.json b/eslint-suppressions.json index 73ba6024678..a4f2f79293f 100644 --- a/eslint-suppressions.json +++ b/eslint-suppressions.json @@ -45,7 +45,7 @@ "count": 7 }, "@typescript-eslint/no-unsafe-assignment": { - "count": 13 + "count": 12 }, "@typescript-eslint/no-unsafe-call": { "count": 2 diff --git a/src/commands/base-command.ts b/src/commands/base-command.ts index c2294613d13..09c9c368526 100644 --- a/src/commands/base-command.ts +++ b/src/commands/base-command.ts @@ -867,7 +867,7 @@ export default class BaseCommand extends Command { packagePath: packagePath, repositoryRoot: repositoryRoot, cwd: cwd, - context: flags.context || process.env.CONTEXT || this.getDefaultContext(), + context: this.getContext(), debug: flags.debug, siteId: this.siteId, token: token, @@ -912,13 +912,26 @@ export default class BaseCommand extends Command { return join(this.workspacePackage || '', '.netlify', ...paths) } + getContext(): string { + return this.opts<{ context?: string }>().context || process.env.CONTEXT || this.getDefaultContext() + } + /** * Returns the context that should be used in case one hasn't been explicitly * set. The default context is `dev` most of the time, but some commands may * wish to override that. */ getDefaultContext(): 'production' | 'dev' { - return this.name() === 'serve' ? 'production' : 'dev' + if (this.name() === 'serve') { + return 'production' + } + if (this.name() === 'deploy') { + const { prod, prodIfUnlocked } = this.opts<{ prod?: boolean; prodIfUnlocked?: boolean }>() + if (prod || prodIfUnlocked) { + return 'production' + } + } + return 'dev' } /** diff --git a/src/commands/deploy/deploy.ts b/src/commands/deploy/deploy.ts index 3b1973f38f8..8b5b8884f4a 100644 --- a/src/commands/deploy/deploy.ts +++ b/src/commands/deploy/deploy.ts @@ -1042,7 +1042,7 @@ const prepAndRunDeploy = async ({ const siteEnv = await getEnvelopeEnv({ api, - context: options.context, + context: command.netlify.cachedConfig.context, env: command.netlify.cachedConfig.env, raw: true, scope: 'functions', diff --git a/src/commands/deploy/index.ts b/src/commands/deploy/index.ts index f55fdb05da1..e41e30291f9 100644 --- a/src/commands/deploy/index.ts +++ b/src/commands/deploy/index.ts @@ -91,7 +91,7 @@ For detailed configuration options, see the Netlify documentation.`, ) .option( '--context ', - 'Specify a deploy context for environment variables read during the build ("production", "deploy-preview", "branch-deploy", "dev") or `branch:your-branch` where `your-branch` is the name of a branch (default: dev)', + 'Specify a deploy context for environment variables read during the build ("production", "deploy-preview", "branch-deploy", "dev") or `branch:your-branch` where `your-branch` is the name of a branch (default: value of CONTEXT or "production")', normalizeContext, ) .option( diff --git a/tests/integration/commands/deploy/deploy.test.ts b/tests/integration/commands/deploy/deploy.test.ts index 8fc0905ee1b..b9fdf081b7e 100644 --- a/tests/integration/commands/deploy/deploy.test.ts +++ b/tests/integration/commands/deploy/deploy.test.ts @@ -133,6 +133,36 @@ describe.concurrent('deploy command', () => { }) }) + test.for([ + { args: ['--prod'], published: 'production.html', skipped: 'draft.html' }, + { args: [], published: 'draft.html', skipped: 'production.html' }, + ])('should resolve config with the deploy context ($args)', async ({ args, published, skipped }, t) => { + await withMockDeploy(async (mockApi, deployState) => { + await withSiteBuilder(t, async (builder) => { + builder + .withContentFile({ path: 'public/draft.html', content: 'draft' }) + .withContentFile({ path: 'public-production/production.html', content: 'production' }) + .withNetlifyToml({ + config: { + build: { publish: 'public' }, + context: { production: { publish: 'public-production' } }, + }, + }) + + await builder.build() + + await callCli( + ['deploy', '--json', '--no-build', ...args], + getCLIOptions({ apiUrl: mockApi.apiUrl, builder, env: { CONTEXT: '' } }), + ) + + const files = Object.keys(deployState.getDeployBody()?.files ?? {}) + expect(files).toContain(published) + expect(files).not.toContain(skipped) + }) + }) + }) + test('should deploy Edge Functions when directory exists after running a build', async (t) => { await withMockDeploy(async (mockApi, deployState) => { await withSiteBuilder(t, async (builder) => { diff --git a/tests/unit/commands/deploy/context.test.ts b/tests/unit/commands/deploy/context.test.ts new file mode 100644 index 00000000000..8e5df501256 --- /dev/null +++ b/tests/unit/commands/deploy/context.test.ts @@ -0,0 +1,41 @@ +import { afterEach, describe, expect, test, vi } from 'vitest' + +import BaseCommand from '../../../../src/commands/base-command.js' +import { createDeployCommand } from '../../../../src/commands/deploy/index.js' + +const parseDeploy = (args: string[]) => { + const deploy = createDeployCommand(new BaseCommand('netlify')) + deploy.parseOptions(args) + return deploy +} + +describe('deploy context', () => { + afterEach(() => { + vi.unstubAllEnvs() + }) + + test.each([ + { args: [], expected: 'dev' }, + { args: ['--draft'], expected: 'dev' }, + { args: ['--prod'], expected: 'production' }, + { args: ['--prod-if-unlocked'], expected: 'production' }, + { args: ['--prod', '--context', 'deploy-preview'], expected: 'deploy-preview' }, + { args: ['--prod', '--context', 'dev'], expected: 'dev' }, + ])('$args resolves to $expected', ({ args, expected }) => { + vi.stubEnv('CONTEXT', '') + + expect(parseDeploy(args).getContext()).toBe(expected) + }) + + test.each([{ args: [] }, { args: ['--prod'] }])('the CONTEXT env var wins over the default for $args', ({ args }) => { + vi.stubEnv('CONTEXT', 'branch-deploy') + + expect(parseDeploy(args).getContext()).toBe('branch-deploy') + }) + + test('--context wins over the CONTEXT env var', () => { + vi.stubEnv('CONTEXT', 'branch-deploy') + + expect(parseDeploy(['--prod', '--context', 'deploy-preview']).getContext()).toBe('deploy-preview') + }) +})