# Deploy a bundle from anywhere

Deploy a Next.js app on pooled hosting from a build made on your own machine or in any CI, with no repository and no image, in one command or three API calls.

An app on pooled hosting is deployed from a **bundle**: what `next build` wrote, packed as a
tar.gz. The starter template's workflow makes and uploads it on every push, but nothing ties a
bundle to our CI: build wherever you like and hand the result over. The app needs no repository
and no image. Replace app-id with the app's id or slug.

## Prerequisites

- An app with pooled hosting (`hosting: pooled` when you create it). An app on its own machines
  deploys from a repository or an image instead; see [Deploy an app](deploy-an-app.md).
- Permission for `deploy:DeployApp` on the app, and `deploy:DescribeApp` to look it up by its slug.
- A Next.js project with `output: 'standalone'` in its `next.config`.

## Build the app

From Next 16.2 on, the platform reads the build through Next's Adapters API: a small hook that
writes `.next/ews-outputs.json` during `next build` and serves nothing. Save it as
`ews/next-export.cjs` in your project:

```js
// Writes Next's description of every build output to .next/ews-outputs.json.
// Loaded by Next with require() (NEXT_ADAPTER_PATH=<this file>), so it stays CommonJS.
const fs = require('node:fs');
const path = require('node:path');
module.exports = {
  name: 'ews-output-export',
  async onBuildComplete(ctx) {
    const out = {
      nextVersion: ctx.nextVersion,
      buildId: ctx.buildId,
      projectDir: ctx.projectDir,
      routing: ctx.routing,
      outputs: ctx.outputs,
      config: {
        basePath: ctx.config.basePath,
        i18n: ctx.config.i18n,
        trailingSlash: ctx.config.trailingSlash,
        images: ctx.config.images,
      },
    };
    fs.writeFileSync(
      path.join(ctx.distDir, 'ews-outputs.json'),
      JSON.stringify(out, (k, v) => (v instanceof Map ? Object.fromEntries(v) : v), 2),
    );
  },
};
```

Then build with the hook:

```sh
NEXT_ADAPTER_PATH=$PWD/ews/next-export.cjs npx next build
```

Next 15.x to 16.1 need no hook: the pool host reads their build manifests itself.

## Deploy the build

**CLI**
From the project directory after the build:

```sh
dcloud deploy apps deploy-bundle app-id --dir .
```

The command checks the build first (`.next/standalone`, `.next/server`, and from Next 16.2 on
`.next/ews-outputs.json`) and stops with what to fix before anything is uploaded. It packs
`.next/standalone`, `.next/static`, `.next/server`, `.next/ews-outputs.json` and `public` (without
`.next/cache`), uploads the tar.gz, completes the deployment and waits until a pool host serves it,
then prints the URL. Flags:

| Flag              | What it does                                                                                        |
| ----------------- | --------------------------------------------------------------------------------------------------- |
| `--file <tgz>`    | Deploy a tar.gz you packed yourself instead of `--dir`                                              |
| `--commit <id>`   | What the build is from; names its preview URL. Default: the git commit of `--dir`, else a random id |
| `--branch <name>` | The app's branch (default `main`) or none deploys to production; any other branch makes a preview   |
| `--no-wait`       | Return once the upload is complete                                                                  |
| `--timeout <dur>` | How long to wait for a pool host to serve it (default `5m`)                                         |

It exits `0` once served, and non-zero when the platform refuses the bundle or the wait runs out.

**API**
1. Pack the build:

   ```sh
   tar -czf bundle.tgz --exclude=.next/cache .next/standalone .next/static .next/server .next/ews-outputs.json public
   ```

2. Announce it with its size and SHA-256. The answer holds `deploymentId` and `uploadUrl`, valid
   for an hour:

   ```http
   POST /deploy/api/apps/3f1c2b4a-5d6e-4f70-8a91-b2c3d4e5f601/bundles
   { "commitSha": "a1b2c3d", "branch": "main", "sha256": "9f2c4e1b7a6d3058c1e2f4a6b8d0e2f4a6c8e0a2b4d6f8a0c2e4f6a8b0d2e4f6", "sizeBytes": 36382720 }
   ```

3. Upload the file to `uploadUrl` with `PUT` and `Content-Type: application/gzip`.

4. Complete the deployment. The answer holds `urls.production` (null for a preview) and
   `urls.preview`:

   ```http
   POST /deploy/api/apps/3f1c2b4a-5d6e-4f70-8a91-b2c3d4e5f601/bundles/7c9e6679-7425-40de-944b-e07fc1f90ae7/complete
   ```

The pool host fetches the bundle at its next pass, within seconds; `GET …/hosting` lists the
deployment once it is served.

**Console:** not available. The console shows the deployments of a pooled app, but does not upload a bundle.
**Terraform:** not available. A deployment is an action, not configuration.

Complete is refused with `409` "The bundle has not been uploaded" before the upload, and with `409`
"The bundle is … bytes, … were announced" when the uploaded size differs from `sizeBytes`. The
pool host checks the SHA-256 again after download and does not serve a bundle that does not match.

## Related

- [Under the hood](under-the-hood.md): how a pool host serves the bundle
- [Use a database on the pool host](use-a-pool-host-database.md)
- `deploy.hosting.create-bundle` · `deploy.hosting.complete-bundle`
