如何为测试和发布环境构建 Azure Devops 管道?

How to structure Azure Devops Pipelines for test & Release environments?

我是 Azure DevOps 的新手,希望为我的项目创建正确的 pipeline/release 结构。我正在努力了解如何构建我的代码,为我的构建配置传递正确的值。

我已经创建了我的仓库和分支:

main (main branch for released/to be released code)

feature/add_new_customer (feature branches per piece of work)

uat (a branch merging in multiple features commits specifically for testing prior to releases)

我创建了一个构建管道,这为我创建了一个“azure-pipelines.yaml”文件,我用它来构建和发布构建文件。这是从我的分支触发的:

trigger:
- main
- feature/*
- uat

到目前为止一切都很好。但这是通过在 yaml

中指定构建配置 statically/hardcoded
variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  **buildConfiguration: 'Release'**

我现在已经向我的管道 BuildConfig 添加了一个变量,并将其设置为“UAT”,这样我就可以在我的 yaml 中引用,如下所示:

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  **buildConfiguration: '$(BuildConfig)'**

这让我可以“动态”设置构建配置,但是如果我创建另一个管道,比如从主分支构建我的发布版本,这会创建一个 azure-pipelines-1.yaml 这一定是错误的因为我必须复制这些文件才能更改触发分支?

是否有一种“正确”的方法可以根据我正在签入的分支为不同的环境创建构建?我看过 Environments,但它们似乎只是为我提供 VM 或 Kubernetes?我只是想构建一个 .net 框架遗留 Web 表单应用程序,所以不需要任何花哨的东西。

我还没有开始部署过程!!! :D

当然,您可以有一个管道根据发布管道本身内的变量更改其行为。您需要在 运行 构建时执行此操作,而不是在根变量级别执行此操作。

您可以通过两种方式完成此操作:

  • 构建任务中的条件:参数
  • 有选择地设置变量的 YAML If 语句。

两者都适用于您的情况,我相信使用 if 语句最适合您的情况。这是一个简单示例中的样子:

name: Whosebug-Example-Variables
trigger:
    - main
    - feature/*
    - uat
stages:
    - stage: your_build_stage
      variables:
        - name: solution
          value: '**/*.sln'
        - name: buildPlatform
          value: 'Any CPU'
        - name: buildConfiguration
          ${{ if eq(variables['Build.SourceBranch'], 'refs/heads/main') }}:
            value: 'Release'
          ${{ if ne(variables['Build.SourceBranch'], 'refs/heads/main') }}:
            value: 'UAT'
      displayName: "Build Solution"
      jobs:
        - job: output_message_job
          displayName: "Output Message Job"
          pool:
            vmImage: "ubuntu-latest"
          steps:
            - powershell: |
                Write-Host ${{ variables.buildConfiguration }}
                Write-Host $(Build.SourceBranch)