Skip to content

Commit 84b98e5

Browse files
tinfoil-knighterezrokah
andauthoredMar 1, 2022
feat(command-deploy): allow support for deploying by site name (#4327)
* feat(command-deploy): allow support for deploying by site name * docs: add new example and update description for deploy site flag * test: use serial instead of only * refactor: get site by name or id else throw errors * fix(command-deploy): list all partial matches for getting site by name Co-authored-by: Erez Rokah <erezrokah@users.noreply.github.com>
1 parent 01c0e6c commit 84b98e5

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed
 

‎docs/commands/deploy.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ netlify deploy
9494
- `open` (*boolean*) - Open site after deploy
9595
- `prod` (*boolean*) - Deploy to production
9696
- `prodIfUnlocked` (*boolean*) - Deploy to production if unlocked, create a draft otherwise
97-
- `site` (*string*) - A site ID to deploy to
97+
- `site` (*string*) - A site name or ID to deploy to
9898
- `skip-functions-cache` (*boolean*) - Ignore any functions created as part of a previous `build` or `deploy` commands, forcing them to be bundled again as part of the deployment
9999
- `timeout` (*string*) - Timeout to wait for deployment to finish
100100
- `trigger` (*boolean*) - Trigger a new build of your site on Netlify without uploading local files
@@ -106,6 +106,7 @@ netlify deploy
106106

107107
```bash
108108
netlify deploy
109+
netlify deploy --site my-first-site
109110
netlify deploy --prod
110111
netlify deploy --prod --open
111112
netlify deploy --prodIfUnlocked

‎src/commands/deploy/deploy.js

+20-2
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ const runDeploy = async ({
302302
}) => {
303303
let results
304304
let deployId
305+
305306
try {
306307
if (deployToProduction) {
307308
await prepareProductionDeploy({ siteData, api })
@@ -462,10 +463,26 @@ const deploy = async (options, command) => {
462463
await command.authenticate(options.auth)
463464

464465
let siteId = options.site || site.id
466+
465467
let siteData = {}
466468
if (siteId) {
467469
try {
468-
siteData = await api.getSite({ siteId })
470+
const [{ siteError, siteFoundById }, sites] = await Promise.all([
471+
api
472+
.getSite({ siteId })
473+
.then((data) => ({ siteFoundById: data }))
474+
.catch((error_) => ({ siteError: error_ })),
475+
api.listSites({ name: options.site, filter: 'all' }),
476+
])
477+
const siteFoundByName = sites.find((filteredSite) => filteredSite.name === options.site)
478+
if (siteFoundById) {
479+
siteData = siteFoundById
480+
} else if (siteFoundByName) {
481+
siteData = siteFoundByName
482+
siteId = siteFoundByName.id
483+
} else {
484+
throw siteError
485+
}
469486
} catch (error_) {
470487
// TODO specifically handle known cases (e.g. no account access)
471488
if (error_.status === 404) {
@@ -674,7 +691,7 @@ Support for package.json's main field, and intrinsic index.js entrypoints are co
674691
.option('-o, --open', 'Open site after deploy', false)
675692
.option('-m, --message <message>', 'A short message to include in the deploy log')
676693
.option('-a, --auth <token>', 'Netlify auth token to deploy with', env.NETLIFY_AUTH_TOKEN)
677-
.option('-s, --site <id>', 'A site ID to deploy to', env.NETLIFY_SITE_ID)
694+
.option('-s, --site <name-or-id>', 'A site name or ID to deploy to', env.NETLIFY_SITE_ID)
678695
.option('--json', 'Output deployment data as JSON')
679696
.option('--timeout <number>', 'Timeout to wait for deployment to finish', (value) => Number.parseInt(value))
680697
.option('--trigger', 'Trigger a new build of your site on Netlify without uploading local files')
@@ -687,6 +704,7 @@ Support for package.json's main field, and intrinsic index.js entrypoints are co
687704
)
688705
.addExamples([
689706
'netlify deploy',
707+
'netlify deploy --site my-first-site',
690708
'netlify deploy --prod',
691709
'netlify deploy --prod --open',
692710
'netlify deploy --prodIfUnlocked',

‎tests/integration/210.command.deploy.test.js

+24
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,30 @@ if (process.env.NETLIFY_TEST_DISABLE_LIVE !== 'true') {
6868
})
6969
})
7070

71+
test.serial('should deploy site by name', async (t) => {
72+
await withSiteBuilder('site-with-public-folder', async (builder) => {
73+
const content = '<h1>⊂◉‿◉つ</h1>'
74+
builder
75+
.withContentFile({
76+
path: 'public/index.html',
77+
content,
78+
})
79+
.withNetlifyToml({
80+
config: {
81+
build: { publish: 'public' },
82+
},
83+
})
84+
85+
await builder.buildAsync()
86+
87+
const deploy = await callCli(['deploy', '--json', '--site', SITE_NAME], {
88+
cwd: builder.directory,
89+
}).then((output) => JSON.parse(output))
90+
91+
await validateDeploy({ deploy, siteName: SITE_NAME, content, t })
92+
})
93+
})
94+
7195
test.serial('should deploy site when publish directory set in netlify.toml', async (t) => {
7296
await withSiteBuilder('site-with-public-folder', async (builder) => {
7397
const content = '<h1>⊂◉‿◉つ</h1>'

0 commit comments

Comments
 (0)