Azure DevOps

Azure Pipelines: PR Preview Environments

Applies to
Gatewayruntime for API, AI, and MCP gateways
Hosting
Managed EdgeManaged DedicatedSelf-HostedDiffers
1 difference by hosting option
Self-Hosted

Add --self-hosted-endpoint with your management API hostname to each zuplo deploy and zuplo delete command so it targets your cluster. See zuplo deploy.

Deploy preview environments for pull requests and clean up when they close.

YAMLazure-pipelines.yml
trigger: - main pr: - main pool: vmImage: ubuntu-latest steps: - task: NodeTool@0 inputs: versionSpec: "24.x" displayName: "Install Node.js" - script: npm install displayName: "Install dependencies" - script: | set -o pipefail # System.PullRequest.SourceBranch is a full ref (refs/heads/my-feature) # and is only set on pull request builds. Fall back to the build's branch. case "$PR_SOURCE_BRANCH" in refs/heads/*) BRANCH="${PR_SOURCE_BRANCH#refs/heads/}" ;; *) BRANCH="${BUILD_SOURCE_BRANCH#refs/heads/}" ;; esac echo "##vso[task.setvariable variable=PREVIEW_BRANCH]$BRANCH" npx zuplo deploy --branch "$BRANCH" --api-key $(ZUPLO_API_KEY) 2>&1 | tee ./DEPLOYMENT_STDOUT displayName: "Deploy to Zuplo" env: PR_SOURCE_BRANCH: $(System.PullRequest.SourceBranch) BUILD_SOURCE_BRANCH: $(Build.SourceBranch) - script: | DEPLOYMENT_URL=$(grep -oP 'Deployed to \K(https://[^ ]+)' ./DEPLOYMENT_STDOUT) npx zuplo test --endpoint "$DEPLOYMENT_URL" displayName: "Run tests" - script: | npx zuplo delete --branch "$PREVIEW_BRANCH" --api-key $(ZUPLO_API_KEY) --wait displayName: "Delete environment" env: PREVIEW_BRANCH: $(PREVIEW_BRANCH) condition: eq(variables['Build.Reason'], 'PullRequest')

The deploy step passes --branch with the pull request's source branch. Without it, a pull request build can create an environment named after the merge ref instead of your branch. Azure Pipelines sets System.PullRequest.SourceBranch to a full ref such as refs/heads/my-feature, so the script strips the refs/heads/ prefix. The variable is set only when a branch policy triggers the build, so the script falls back to Build.SourceBranch for other runs. The values reach the script through env instead of being written into it.

The cleanup step only runs for pull requests, deleting the preview environment by the same branch name after tests pass.

Next Steps

Last modified on