Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/commands/deploy.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion eslint-suppressions.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"count": 7
},
"@typescript-eslint/no-unsafe-assignment": {
"count": 13
"count": 12
},
"@typescript-eslint/no-unsafe-call": {
"count": 2
Expand Down
17 changes: 15 additions & 2 deletions src/commands/base-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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'
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/commands/deploy/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion src/commands/deploy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ For detailed configuration options, see the Netlify documentation.`,
)
.option(
'--context <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(
Expand Down
30 changes: 30 additions & 0 deletions tests/integration/commands/deploy/deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
41 changes: 41 additions & 0 deletions tests/unit/commands/deploy/context.test.ts
Original file line number Diff line number Diff line change
@@ -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')
})
})