Skip to main content
INTELLIA
GuideDevOpsAzure DevOpsNext.jsCI/CD

Next.js CI/CD on Azure DevOps with a self-hosted agent

A practical pipeline for deploying Next.js to private servers with Azure DevOps: self-hosted agents, standalone builds, release directories, health checks and rollback.

By INTELLIA Engineering Team

Published

Last updated

2 min read

Short answer

To deploy Next.js through Azure DevOps to servers on a private network, install a self-hosted agent inside that network, build the app with output set to standalone, publish the build as a pipeline artifact, then have a deployment stage extract it into a new release directory, start it, health-check it and switch traffic, keeping the previous release for instant rollback.

How to set up Next.js CI/CD on Azure DevOps: step by step

  1. 1

    Install a self-hosted agent

    Register an Azure Pipelines agent on a machine inside the private network and add it to an agent pool.

  2. 2

    Configure standalone output

    Set output to standalone in next.config so the build includes a minimal Node.js server.

  3. 3

    Build and test in CI

    Install dependencies, lint, type-check, test and build, then publish the standalone folder as an artifact.

  4. 4

    Deploy to a new release directory

    Extract the artifact into a timestamped folder on the target server and install environment configuration.

  5. 5

    Start, health-check and switch

    Start the new release on a spare port, check a health route, then switch the reverse proxy or symlink.

  6. 6

    Keep rollback ready

    Retain the previous releases so rollback is a single switch.

The problem this solves

Many organizations in Pakistan and the GCC host applications on servers inside their own data center or a private cloud. Those servers are deliberately unreachable from the internet, which rules out cloud-hosted build agents pushing deployments to them. The usual fallback is manual deployment by copying files, which is slow, inconsistent and causes downtime.

Architecture

Component Role
Azure Repos or GitHub Source code and pull requests
Azure Pipelines (cloud) Orchestrates stages and stores artifacts
Self-hosted agent (private network) Runs deployment jobs on or near target servers
Target servers Run Node.js processes under a process manager
Reverse proxy (Nginx) Routes traffic to the active release

The agent only makes outbound HTTPS connections to Azure DevOps. No inbound firewall rules are needed.

Step 1: Standalone build

In next.config.ts, set output: "standalone". The build produces .next/standalone with a server.js and only the required dependencies. Copy public and .next/static alongside it, because they are not included automatically.

Step 2: The CI stage

A typical CI stage:

  1. Use a pinned Node.js version.
  2. Install dependencies with npm ci and a cache.
  3. Run lint, type-check and unit tests.
  4. Run next build.
  5. Assemble the standalone folder with public and .next/static.
  6. Publish it as a pipeline artifact named with the build number.

Pull requests run the same steps without publishing, so broken code never reaches the main branch.

Step 3: The deployment stage

Run the deployment job in the self-hosted agent pool and target an Azure DevOps environment so approvals and history are recorded. The job:

  1. Downloads the artifact.
  2. Extracts it into /srv/app/releases/<build-number>.
  3. Writes environment configuration from a secure variable group.
  4. Starts the new release on a spare port with the process manager.
  5. Calls a health route, for example one that renders a real page, until it returns success or times out.
  6. Switches the reverse proxy upstream or the current symlink and reloads gracefully.
  7. Stops the old process after connections drain.
  8. Deletes releases older than the last few.

Rollback

Because previous releases remain on disk, rollback is a switch back to the prior directory and a reload, measured in seconds. Expose it as a separate manual pipeline so on-call engineers do not need server access.

Secrets and configuration

  • Store secrets in Azure DevOps variable groups linked to Azure Key Vault where possible.
  • Mark secret variables so they are masked in logs.
  • Give the agent's service account only the permissions it needs on target servers.

Common pitfalls

  • Forgetting static assets: without .next/static, pages render but styles and scripts return 404.
  • Health checks that only test the process: a running process can still fail to render pages; check a real route.
  • Shared caches between releases: keep each release self-contained.
  • Agent as a single point of failure: register at least two agents in the pool for production.

Results to measure

Track deployment duration, downtime per release, change failure rate and time to restore. These are standard DevOps delivery metrics and make the value of the pipeline visible.

We applied this pattern in the zero-downtime Next.js deployment case study. Our cloud and DevOps service sets up the same approach for client applications.

Frequently asked questions

Why use a self-hosted agent instead of Microsoft-hosted agents?

Microsoft-hosted agents run in Microsoft's cloud and cannot reach servers on a private network without exposing them. A self-hosted agent runs inside your network, makes only outbound connections to Azure DevOps, and can deploy to internal servers.

What does output standalone do in Next.js?

It makes the build produce a folder containing a minimal Node.js server and only the dependencies the app needs, so you can deploy without the full node_modules directory.

How do you achieve zero downtime?

Start the new version alongside the old one, confirm it is healthy, and only then move traffic to it through the reverse proxy or a process manager reload. If health checks fail, traffic stays on the old version.

Can the same pipeline deploy to staging and production?

Yes. Use separate stages and environments, with approvals or checks before production, and environment-specific variable groups.

About the author

INTELLIA Engineering Team

Software engineering, Karachi

The engineers who build and support INTELLIA's products and client systems, from WhatsApp integrations and contact-center telephony to ERP rollouts and CI/CD pipelines.

Named author byline pending

Have a project in mind?

Tell us what you are trying to achieve. A solution architect will reply within one business day with next steps — not a sales script.

CallWhatsAppGet a quote