5
votes
  1. Is it possible to publish an build artifact to Azure Devops/TFS through build.cake script?
  2. Where should the responsibility for publishing the build artifact be configured when converting to cake scripts, in the build.cake script or in the Azure DevOps pipeline?

To achieve versioning in our build and release pipelines we decided to move our (gitversion, clean, build, tests, ...) tasks to be handled by a cake script stored in each repository instead.

Is there a way to replace the publish build artifact(Azure DevOps) task with a task in the cake.build? I have searched the official documentation of both Azure and cake but can not seem to find a solution. The first task, copying the build artifacts to a staging directory is possible, however, publishing the artifact - is where it gets complicated.

Currently, a snippet of our 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
    });

Solution

A snippet of an updated 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");
    });
2

2 Answers

4
votes

Yes Cake supports uploading artifacts using it's built-in tfbuild build system provider

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

All TFBuild commands documented at cakebuild.net/api/Cake.Common.Build.TFBuild/TFBuildCommands

0
votes

TFBuild has renamed to AzurePipelines so the update code is ,

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"); 

 });