Azure DevOps构建流水线中的两个构建任务

30次阅读
没有评论

问题描述

正在编写一个Azure DevOps构建流水线,用于构建一个C#应用程序。但是,他遇到了一个问题,因为开发人员将.sln文件的构建设置如下:
– Debug(用于开发使用)
– Release(用于开发使用以获取所有构建产物)
– Installers(必须在Release之后运行,以便正确创建.msi文件)

他需要先以Release模式构建应用程序,然后以Installers模式构建应用程序。是否有一种方法可以在触发Installers步骤时,仍然可以访问在触发Release步骤时创建的所有文件?

以下是YAML代码片段,以便更清楚地理解:

- task: VSBuild@1
  displayName: 'Build solution in release mode'
  inputs:
    solution: '$(Build.SourcesDirectory)/work.sln'
    msbuildArgs: '/P:Version=$(Build_No);ReleaseType=$(ReleaseType);RunWixToolsOutOfProc=true'
    platform: '$(BuildPlatform)'
    configuration: '$(BuildConfigurationRelease)'
    msbuildArchitecture: 'x64'
- task: VSBuild@1
  displayName: 'Build solution on installer mode'
  inputs:
    solution: '$(Build.SourcesDirectory)/work.sln'
    msbuildArgs: '/P:Version=$(Build_No);ReleaseType=$(ReleaseType);RunWixToolsOutOfProc=true'
    platform: '$(BuildPlatform)'
    configuration: '$(BuildConfigurationInstallers)'
    msbuildArchitecture: 'x64'

解决方案

请注意以下操作注意版本差异及修改前做好备份。

方案1

根据您提供的YAML代码片段,您可以通过以下步骤在Azure DevOps构建流水线中实现先以Release模式构建应用程序,然后以Installers模式构建应用程序:
1. 打开Azure DevOps构建流水线。
2. 找到并编辑与您的项目相关的构建流水线。
3. 在构建流水线中,找到第一个构建任务(Build solution in release mode)。
4. 确保该任务的输入参数中的configuration值为$(BuildConfigurationRelease),以确保以Release模式构建应用程序。
5. 在构建流水线中,找到第二个构建任务(Build solution on installer mode)。
6. 确保该任务的输入参数中的configuration值为$(BuildConfigurationInstallers),以确保以Installers模式构建应用程序。
7. 保存并触发构建流水线。

通过以上步骤,您将能够先以Release模式构建应用程序,然后以Installers模式构建应用程序。在Installers步骤触发时,它仍然可以访问在Release步骤触发时创建的所有文件。

方案2

使用脚本或工具来管理构建任务的顺序可能会增加复杂性,并且需要确保依赖关系正确设置。
另一种方法是编写脚本或使用工具来控制构建任务的运行顺序。您可以使用Azure DevOps中的自定义脚本任务或一些第三方工具来管理构建任务的依赖关系。

以下是一个简单的bash脚本示例,可以在Release模式构建完成后再执行Installers模式构建:

#!/bin/bash
# 执行Release模式构建
az pipelines build queue --definition-name "Your_Build_Pipeline_Name" --branch "Your_Branch_Name" --variables "BuildConfiguration=$(BuildConfigurationRelease)"
# 等待Release模式构建完成
while [ "$(az pipelines build list --definition-name "Your_Build_Pipeline_Name" --branch "Your_Branch_Name" --status completed --top 1 --query '[0].result' -o tsv)" != "succeeded" ]; do
  sleep 1
done
# 执行Installers模式构建
az pipelines build queue --definition-name "Your_Build_Pipeline_Name" --branch "Your_Branch_Name" --variables "BuildConfiguration=$(BuildConfigurationInstallers)"

在这个示例中,我们首先使用Azure CLI命令az pipelines build queue来触发Release模式构建。然后,使用一个循环来等待Release模式构建完成(这里是通过查询构建状态来判断)。一旦Release模式构建成功,我们再使用Azure CLI命令az pipelines build queue来触发Installers模式构建。

请注意,这只是一个示例,您需要根据您的实际情况进行相应的调整和配置。

以上是两种实现先以Release模式构建应用程序,然后以Installers模式构建应用程序的方法。您可以根据您的需求选择适合您的方法来解决问题。

正文完