通过 build.cake 而不是 Azure Devops 发布构建工件

Publish build artifact through build.cake instead of Azure Devops

  1. 是否可以通过 build.cake 脚本将构建工件发布到 Azure Devops/TFS?
  2. 在转换为 cake 脚本时,应该在 build.cake 脚本中还是在 Azure DevOps 管道中配置发布构建工件的责任?

为了在我们的构建和发布管道中实现版本控制,我们决定将我们的(gitversion、clean、构建、测试...)任务转移到存储在每个存储库中的 cake 脚本中。

有没有办法用 cake.build 中的任务替换 publish build artifact(Azure DevOps) 任务?我已经搜索了 Azure 和 cake 的官方文档,但似乎找不到解决方案。 第一个任务,将构建工件复制到暂存目录是可能的,但是,发布工件 - 是它变得复杂的地方。

目前,我们 build.cake.

的片段
Task("Copy-Bin")
    .WithCriteria(!isLocalBuild)
    .Does(() =>
    {
        Information($"Creating directory {artifactStagingDir}/drop");
        CreateDirectory($"{artifactStagingDir}/drop");
        Information($"Copying all files from {solutionDir}/{moduleName}.ServiceHost/bin to {artifactStagingDir}/drop/bin");
        CopyDirectory($"{solutionDir}/{moduleName}.ServiceHost/bin", $"{artifactStagingDir}/drop/bin");
        // Now we should publish the artifact to TFS/Azure Devops
    });

解决方案

更新的 build.cake 的片段。

Task("Copy-And-Publish-Artifacts")
    .WithCriteria(BuildSystem.IsRunningOnAzurePipelinesHosted)
    .Does(() =>
    {
        Information($"Creating directory {artifactStagingDir}/drop");
        CreateDirectory($"{artifactStagingDir}/drop");
        Information($"Copying all files from {solutionDir}/{moduleName}.ServiceHost/bin to {artifactStagingDir}/drop/bin");
        CopyDirectory($"{solutionDir}/{moduleName}.ServiceHost/bin", $"{artifactStagingDir}/drop/bin");
        Information($"Uploading files from artifact directory: {artifactStagingDir}/drop to TFS");
        TFBuild.Commands.UploadArtifactDirectory($"{artifactStagingDir}/drop");
    });

是的,Cake 支持使用其内置的 tfbuild 构建系统提供程序上传工件

Task("UploadArtifacts")
 .IsDependentOn("ZipArtifacts")
 .WithCriteria(BuildSystem.IsRunningOnAzurePipelinesHosted)
 .Does(() => {
  TFBuild.Commands.UploadArtifact("website", zipFileName, "website"); 
  TFBuild.Commands.UploadArtifact("website", deployCakeFileName, "website"); 
});

cakebuild.net/api/Cake.Common.Build.TFBuild/TFBuildCommands

中记录的所有 TFBuild 命令

TFBuild 已重命名为 AzurePipelines 所以更新代码是,

Task("PublishArtifacts")
.WithCriteria(BuildSystem.IsRunningOnAzurePipelinesHosted)                                                               
.IsDependentOn("ZipArtifacts") //If you have different task to zip the artifacts
.WithCriteria(BuildSystem.IsRunningOnAzurePipelinesHosted)
.Does((Context) => {

 AzurePipelines.Commands.UploadArtifact("FolderNameWhereTheZipIs","ZippedFileName"); 

 });