You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure DevOps模板分支条件配置咨询:基于分支部署对应环境

为Azure DevOps流水线添加分支条件

以下是添加了分支条件后的完整流水线配置,同时更新了触发器以包含development分支:

trigger:
  batch: true
  branches:
    include:
    - main
    - development

pool: 'On-Prem Pool'

variables:
  - group: ContainerRegistry
  - group: xyz
  - name: appName
    value: 'xyz'
  - name: dockerfilePath
    value: '**/Dockerfile'


stages:


# Build App and push to container registry

- stage: Build
  displayName: Build and push to container registry
  jobs:
  - job: Build
    displayName: Build job
    pool: 'On-Prem Pool'
    steps:
    - task: Docker@2
      displayName: Login to ACR
      inputs:
        command: login
        containerRegistry: $(registryServiceConnection)
    - task: Docker@2
      displayName: Build container image
      inputs:
        command: build
        repository: $(appName)
        dockerfile: $(dockerfilePath)
        containerRegistry: $(registryServiceConnection)
        tags: $(Build.BuildId)
    - task: Docker@2
      displayName: Push container image
      inputs:
        command: push
        repository: $(appName)
        containerRegistry: $(registryServiceConnection)
      condition: and(succeeded(), ne(variables['Build.Reason'], 'PullRequest')) # Don´t push to ACR on Pull Requests




# Infrastructure

# Lint the Bicep file.
- stage: Lint
  jobs: 
  - template: pipeline-templates/lint.yml

 # Deploy to the dev environment.
- template: pipeline-templates/deploy.yml
  condition: or(eq(variables['Build.SourceBranchName'], 'main'), eq(variables['Build.SourceBranchName'], 'development'))
  parameters:
    environmentType: Development
    resourceGroupName: rg-xx
    appSuffix: xyz
    costCenter: 'xyz'
    serviceConnectionName: 'SVCxx'
    dockerRegistryPassword: $(dockerRegistryPassword)
    dockerRegistryUserName: $(dockerRegistryUserName)
    containerTag: $(Build.BuildId)
    registryUrl: $(registryUrl)
    cpuCores: 0.5 
    memory: 1
    minReplicas: 0
    maxReplicas: 1



 # Deploy to the staging environment.
- template: pipeline-templates/deploy.yml
  condition: eq(variables['Build.SourceBranchName'], 'main')
  parameters:
    environmentType: Staging
    resourceGroupName: rg-xxx-001
    appSuffix: xyz
    costCenter: 'xyz'
    serviceConnectionName: 'SVCxxx'
    dockerRegistryPassword: $(dockerRegistryPassword)
    dockerRegistryUserName: $(dockerRegistryUserName)
    containerTag: $(Build.BuildId)
    registryUrl: $(registryUrl)
    cpuCores: 0.5 
    memory: 1 
    minReplicas: 1
    maxReplicas: 2

关键修改说明

  • 触发器更新: 在trigger.branches.include中添加了development分支,确保流水线在该分支有代码推送时自动触发
  • Development环境部署条件: 使用or(eq(variables['Build.SourceBranchName'], 'main'), eq(variables['Build.SourceBranchName'], 'development')),只有当当前分支是main或development时才会执行该部署阶段
  • Staging环境部署条件: 使用eq(variables['Build.SourceBranchName'], 'main'),仅当当前分支为main时才执行该部署阶段

内容的提问来源于stack exchange,提问作者objectclass

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 17:31:15