Naming resulting ZIP archive in Azure Pipelines build?

Viewed 4755

Still trying to get a full grasp on the Azure Pipelines and what I can do with those...

I've managed to finally get my little .NET Core command line utility built with Azure Pipelines - now trying to publish it as a build artifact.

With this YAML, I can build and package the tool:

- task: DotNetCoreCLI@2
  displayName: 'dotnet build'
  inputs:
    command: 'build'
    projects: '$(solution)'

- task: DotNetCoreCLI@2
  displayName: 'dotnet publish'
  inputs:
    command: publish
    publishWebProjects: False
    arguments: '--configuration $(BuildConfiguration) --output $(Build.ArtifactStagingDirectory)'
    zipAfterPublish: True

- task: PublishBuildArtifacts@1
  inputs:
    pathtoPublish: '$(Build.ArtifactStagingDirectory)' 
    artifactName: 'MyTool_LatestBuild'

But in the published build artifacts, I only get a file a.zip - is there any way I can influence the name of that ZIP archive?? I'd really love to have something like MyTool_2020_Sep_08.zip or MyTool_v1.5.7.zip something.... but how can I do that?? Can't seem to find anything very useful anywhere on the interwebs ... any inputs?

Thanks!

3 Answers

1.Agree with Vernou. You can customize the name by specifying the folder name that contains your binaries. So we need to modify the --output argument of dotnet publish step and PathtoPublish argument of PublishBuildArtifacts step.

2.But to get Date time, you can use Build.BuildNumber which is predefined variables.

My working sample:

name: $(Date:yyyyMMdd)$(Rev:.r)

steps:
- task: DotNetCoreCLI@2
  inputs:
    command: 'build'
    projects: '**/*.csproj'

- task: DotNetCoreCLI@2
  displayName: 'dotnet publish'
  inputs:
    command: publish
    publishWebProjects: False
    arguments: '--configuration $(BuildConfiguration) --output $(Build.ArtifactStagingDirectory)/MyTool_$(Build.BuildNumber)'
    zipAfterPublish: True

- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)/MyTool_$(Build.BuildNumber)'
    ArtifactName: 'MyTool_LatestBuild'
    publishLocation: 'Container'

More details about using BuildNumber to get date, you can check Configure run or build numbers.

The result:

enter image description here

We can control the format via modifying how we define the Build.BuildNumber(In Yaml, it's name). If we define name: $(Date:yyyyMMdd), then the output zip would be MyTool_20200908.zip.

EDIT: Loking at vernou's answer - i was wrong, i'll leave this here because some may prefer to have different steps and we all love copying from SO.

The Task you're using does not contain that power, but you can (like i have) opt to zip it yourself instead:

- task: DotNetCoreCLI@2
  inputs:
    command: 'publish'
    publishWebProjects: false
    projects: |
      **/*Client.csproj
      **/*WorkerService.csproj
      **/*Server.csproj
    arguments: '-c $(BuildConfiguration) -o $(Build.StagingDirectory)/ci-build --no-build --self-contained -r $(runtime)'
    zipAfterPublish: false

# Archive the /staging/ci-build folder to /staging/RemoteData.<BuildNumber>
- task: ArchiveFiles@2
  inputs:
    rootFolderOrFile: '$(Build.StagingDirectory)/ci-build'
    includeRootFolder: false
    archiveType: 'zip'
    archiveFile: '$(Build.ArtifactStagingDirectory)/RemoteData.$(Build.BuildNumber).zip'
    replaceExistingArchive: true

# Publish the zipfile as artifact
- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)/RemoteData.$(Build.BuildNumber).zip'
    ArtifactName: 'RemoteData.$(Build.BuildNumber)'
    publishLocation: 'Container'

This has the added benefit of being able to more closely manage your jobs:

enter image description here

As you can see each step is laid out separately here.

dotnet publish task copy file in the output and zip this folder. You can output to folder with a suffix like :

- task: DotNetCoreCLI@2
  displayName: 'dotnet build'
  inputs:
    command: 'build'
    projects: '$(solution)'

- task: DotNetCoreCLI@2
  displayName: 'dotnet publish'
  inputs:
    command: publish
    publishWebProjects: False
    arguments: '--configuration $(BuildConfiguration) --output $(Build.ArtifactStagingDirectory)/app_v1.5.7'
    zipAfterPublish: True

- task: PublishBuildArtifacts@1
  inputs:
    pathtoPublish: '$(Build.ArtifactStagingDirectory)' 
    artifactName: 'MyTool_LatestBuild'

You can define a build number and replace the suffix with : https://docs.microsoft.com/en-us/azure/devops/pipelines/process/run-number?view=azure-devops&tabs=yaml

...
- task: DotNetCoreCLI@2
  displayName: 'dotnet publish'
  inputs:
    command: publish
    publishWebProjects: False
    arguments: '--configuration $(BuildConfiguration) --output $(Build.ArtifactStagingDirectory)/app_$(Build.BuildNumber)'
    zipAfterPublish: True
...
Related