yaml管道中的部署条件

时间:2020-04-05 01:07:14

标签: azure-devops yaml azure-pipelines azure-pipelines-release-pipeline

我对Yaml条件有疑问。我遵循了以下文档:https://docs.microsoft.com/en-us/azure/devops/pipelines/process/conditions?view=azure-devops&tabs=yaml

我需要根据某些条件部署到每个环境。例如,

  • 仅在源分支为“开发”时才部署到“ int”环境
  • 仅当源分支为“ master”时才部署到“ ua”环境
  • 仅当源分支为“ master”时才部署到“ prod”环境

如果满足以下任何条件,请部署到该环境。

我写了一个条件如下:

condition: |
      or(
        and(
          eq(variables['Build.SourceBranch'], 'refs/heads/develop'),
          eq('${{ parameters.environmentAbbreviation }}', 'int')
        ),
        and(
          eq(variables['Build.SourceBranch'], 'refs/heads/master'),
          eq('${{ parameters.environmentAbbreviation }}', 'ua')
        ),
        and(
          eq(variables['Build.SourceBranch'], 'refs/heads/master'),
          eq('${{ parameters.environmentAbbreviation }}', 'prod')
        )
      )

这是我在运行构建/发行版时得到的:

评估:

or( and(eq(variables ['Build.SourceBranch'],'refs / heads / develop'), eq('int','int'))和(eq(variables ['Build.SourceBranch'], 'refs / heads / master'),eq('int','ua')), 和(eq(variables ['Build.SourceBranch'],'refs / heads / master'), eq('int','prod')))

展开:

or( and(eq('refs / heads / develop','refs / heads / develop'),eq('int', 'int'))和(eq('refs / heads / develop','refs / heads / master'), eq('int','ua'))和(eq('refs / heads / develop','refs / heads / master'), eq('int,'prod')))

因为第一个条件正确,所以我的假设是部署将在int环境中启动。但这并未部署到int环境。为什么呢?

1 个答案:

答案 0 :(得分:1)

不确定完整的YAML脚本是什么,但是该条件应该可以在int环境中进行部署。

我只是运行它,并且效果很好。这是我要测试的测试YAML,您可以通过自己的支票进行检查:

parameters:
- name: 'environmentAbbreviation'
  type: string
  default: int

stages:
- stage: DeployToDevelopment
  displayName: Deploy to 
  jobs:
  - deployment: DeployDev
    condition: or(and(eq(variables['Build.SourceBranch'], 'refs/heads/develop'), eq('${{ parameters.environmentAbbreviation }}', 'int')), and(eq(variables['Build.SourceBranch'], 'refs/heads/master'), eq('${{ parameters.environmentAbbreviation }}', 'ua')), and(eq(variables['Build.SourceBranch'], 'refs/heads/master'), eq('${{ parameters.environmentAbbreviation }}', 'prod')))
    environment: ${{ parameters.environmentAbbreviation }}
    strategy:
      runOnce:
        deploy:
          steps:
          - pwsh: |
              Write-Host ${{ parameters.environmentAbbreviation }}
            name: outputVars

enter image description here

相关问题