skip to content

What does the Angular CLI's `ng deploy` command actually run, and where does its behaviour come from?

level: middleimportance: nice to knowfreq 25%

answer

  1. just another architect target
  2. the name is deploy
  3. ng add writes it
  4. missing target prompts a package

basics

~20 s

ng deploy runs whatever builder the project's deploy target in angular.json names; the CLI ships none. A hosting package installed with ng add usually writes that target, and its builder builds the app and uploads the output.

solid answer

~50 s

`ng deploy [project]` is an architect command like `ng build`: it runs the `deploy` target of the named or default project. The Angular CLI ships no deploy builder of its own; a hosting provider's package adds one when you install it with `ng add <package>`, whose schematic writes something like `"deploy": { "builder": "<package>:deploy", "options": {} }` into `angular.json`. What happens next is up to that builder, typically building the production configuration and uploading the output, often after interactive prompts. If the project has no `deploy` target, the CLI prints `Cannot find "deploy" target for the specified project.` with example packages and, in an interactive terminal, offers to `ng add` one. For your own infrastructure you can write a custom builder and register it as the `deploy` target, or skip `ng deploy` and ship `ng build` output from CI.

code

json · 14 lines
json
{
  "projects": {
    "shop": {
      "architect": {
        "deploy": {
          "builder": "@acme/ng-deploy:deploy",
          "options": {
            "buildTarget": "shop:build:production"
          }
        }
      }
    }
  }
}

go deeper

for a junior

Recall that ng deploy needs a package added with ng add, and that it runs the deploy target in angular.json.

for a middle

Explain the architect target and builder model behind ng deploy, and what the CLI does when the target is missing.

for a senior

Show judgement on deployment ownership: a deploy builder for developer convenience versus CI jobs that build and ship the output.

for a principal

Weigh standardising deployment through a shared custom builder against platform-specific pipelines owned by an infrastructure team.

## An architect command with no built-in builder The Angular CLI's commands `ng build`, `ng serve`, `ng test` and `ng deploy` are **architect commands**: each runs a named **target** of a project in `angular.json`, and the target names a **builder** that does the work. `ng deploy` runs the target called `deploy`. What makes `ng deploy` different is that a new workspace has **no `deploy` target**, and the Angular CLI ships no deploy builder. The command is a hook for third-party packages. ## Where the target comes from Hosting providers and community projects publish packages that contain both an `ng add` schematic and a deploy builder. Running `ng add <package>`: 1. installs the package; 2. runs its schematic, which adds a `deploy` target to the selected project in `angular.json`, for example `"deploy": { "builder": "<package>:deploy", "options": { ... } }`; 3. may prompt for account or project details and write them into the options. From then on, `ng deploy` runs that builder. The command's own documentation puts it this way: "use `ng add` to add a package that implements deployment capabilities to your favorite platform". ## What a typical deploy builder does The builder is ordinary code with access to the workspace, so behaviour varies, but most follow the same pattern: - build the project, usually with the `production` configuration; - upload the build output (the `browser` folder, or the whole output for server builds) to the provider; - print the resulting URL. Options such as the target site, the build configuration or a base href are the builder's own; read the package's documentation, because `ng deploy` passes them through untouched. ## When there is no deploy target Running `ng deploy` in a project without the target does not fail silently. The CLI reports: ```text Cannot find "deploy" target for the specified project. You can add a package that implements these capabilities. ``` It follows with example `ng add` commands, and in an interactive terminal it prompts you to pick one and runs `ng add` for it. In CI, with no terminal, it just prints the message and exits with an error. ## Deploying to your own infrastructure You have two reasonable options: | Approach | When it fits | |---|---| | A custom builder registered as the `deploy` target | several projects share the same deployment logic and developers expect `ng deploy` to work | | A CI job running `ng build` and copying `dist/<project>` | most teams; deployment stays in the pipeline, visible and auditable | Writing the builder itself (the builder API, `createBuilder` and builder schemas) is the custom-builders topic. For the deploy target, only the contract matters: the target's `builder` names your package's builder and its `options` are validated by that builder's schema. ## Using it safely in a team - **Keep credentials out of `angular.json`.** Deploy targets are committed with the workspace; tokens belong in environment variables or the CI secret store, as the package allows. - **Pin the deploy package's version.** Its behaviour is the package's, so an unpinned upgrade can change what `ng deploy` does. - **Prefer non-interactive runs in CI.** Interactive prompts that work on a laptop stall or fail in a pipeline; check which options the builder offers to skip them. - **Know which configuration it builds.** Some builders build the production configuration themselves, others deploy whatever output already exists. ## Interview angle The point interviewers look for is that `ng deploy` is **not magic**: it is one line in `angular.json` pointing at someone else's code. That explains why its flags differ between providers, why it can be replaced by a script, and why a deploy that suddenly behaves differently after an `npm update` usually traces back to the deploy package, not to the Angular CLI.

  • Why does `ng deploy` behave differently from one project to another?
    Because the Angular CLI only resolves and runs the `deploy` target; everything after that, including which options exist and whether it builds first, is defined by the builder package that `ng add` installed. Two projects using different deploy packages share nothing but the command name.
  • What happens if you run `ng deploy` in CI for a project with no deploy target?
    The CLI prints that it cannot find a "deploy" target for the project, lists example packages you could add, and exits with an error. It only offers to run `ng add` interactively when a terminal is attached, so in CI the job simply fails.

saying these in an interview costs you the question

  • ng deploy ships with a built-in Angular deploy builder.
  • ng deploy always runs ng build first, whatever the builder.
  • Deploy options are defined by the Angular CLI for every provider.
  • Without a deploy target ng deploy uploads dist to a default host.