Skip to content

Commit

Permalink
feat(command-deploy): allow support for deploying by site name (#4327)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
tinfoil-knight and erezrokah authored Mar 1, 2022
1 parent 01c0e6c commit 84b98e5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
3 changes: 2 additions & 1 deletion docs/commands/deploy.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ netlify deploy
- `open` (*boolean*) - Open site after deploy
- `prod` (*boolean*) - Deploy to production
- `prodIfUnlocked` (*boolean*) - Deploy to production if unlocked, create a draft otherwise
- `site` (*string*) - A site ID to deploy to
- `site` (*string*) - A site name or ID to deploy to
- `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
- `timeout` (*string*) - Timeout to wait for deployment to finish
- `trigger` (*boolean*) - Trigger a new build of your site on Netlify without uploading local files
Expand All @@ -106,6 +106,7 @@ netlify deploy

```bash
netlify deploy
netlify deploy --site my-first-site
netlify deploy --prod
netlify deploy --prod --open
netlify deploy --prodIfUnlocked
Expand Down
22 changes: 20 additions & 2 deletions src/commands/deploy/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ const runDeploy = async ({
}) => {
let results
let deployId

try {
if (deployToProduction) {
await prepareProductionDeploy({ siteData, api })
Expand Down Expand Up @@ -462,10 +463,26 @@ const deploy = async (options, command) => {
await command.authenticate(options.auth)

let siteId = options.site || site.id

let siteData = {}
if (siteId) {
try {
siteData = await api.getSite({ siteId })
const [{ siteError, siteFoundById }, sites] = await Promise.all([
api
.getSite({ siteId })
.then((data) => ({ siteFoundById: data }))
.catch((error_) => ({ siteError: error_ })),
api.listSites({ name: options.site, filter: 'all' }),
])
const siteFoundByName = sites.find((filteredSite) => filteredSite.name === options.site)
if (siteFoundById) {
siteData = siteFoundById
} else if (siteFoundByName) {
siteData = siteFoundByName
siteId = siteFoundByName.id
} else {
throw siteError
}
} catch (error_) {
// TODO specifically handle known cases (e.g. no account access)
if (error_.status === 404) {
Expand Down Expand Up @@ -674,7 +691,7 @@ Support for package.json's main field, and intrinsic index.js entrypoints are co
.option('-o, --open', 'Open site after deploy', false)
.option('-m, --message <message>', 'A short message to include in the deploy log')
.option('-a, --auth <token>', 'Netlify auth token to deploy with', env.NETLIFY_AUTH_TOKEN)
.option('-s, --site <id>', 'A site ID to deploy to', env.NETLIFY_SITE_ID)
.option('-s, --site <name-or-id>', 'A site name or ID to deploy to', env.NETLIFY_SITE_ID)
.option('--json', 'Output deployment data as JSON')
.option('--timeout <number>', 'Timeout to wait for deployment to finish', (value) => Number.parseInt(value))
.option('--trigger', 'Trigger a new build of your site on Netlify without uploading local files')
Expand All @@ -687,6 +704,7 @@ Support for package.json's main field, and intrinsic index.js entrypoints are co
)
.addExamples([
'netlify deploy',
'netlify deploy --site my-first-site',
'netlify deploy --prod',
'netlify deploy --prod --open',
'netlify deploy --prodIfUnlocked',
Expand Down
24 changes: 24 additions & 0 deletions tests/integration/210.command.deploy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,30 @@ if (process.env.NETLIFY_TEST_DISABLE_LIVE !== 'true') {
})
})

test.serial('should deploy site by name', async (t) => {
await withSiteBuilder('site-with-public-folder', async (builder) => {
const content = '<h1>⊂◉‿◉つ</h1>'
builder
.withContentFile({
path: 'public/index.html',
content,
})
.withNetlifyToml({
config: {
build: { publish: 'public' },
},
})

await builder.buildAsync()

const deploy = await callCli(['deploy', '--json', '--site', SITE_NAME], {
cwd: builder.directory,
}).then((output) => JSON.parse(output))

await validateDeploy({ deploy, siteName: SITE_NAME, content, t })
})
})

test.serial('should deploy site when publish directory set in netlify.toml', async (t) => {
await withSiteBuilder('site-with-public-folder', async (builder) => {
const content = '<h1>⊂◉‿◉つ</h1>'
Expand Down

1 comment on commit 84b98e5

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📊 Benchmark results

Package size: 443 MB

Please sign in to comment.