'Publishing build artifacts failed with an error: Not found PathtoPublish: D:\a\1\s\$(buildStagingDirectory)

Building CI pipeline for .Net core APIs in VSTS. But while building getting the below error.

Publishing build artifacts failed with an error: Not found PathtoPublish: D:\a\1\s\$(buildStagingDirectory)

This is my build definition looks like

enter image description here

I have mentioned PathToPublish as $(buildStagingDirectory)

How do I get rid of this error??



Solution 1:[1]

I just encounted the exact same error.

Cause

After setting the system.debug variable to true, it revealed that the publish task actually performs a zip of the output folder (which by default is $(build.artifactstagingdirectory)) and places this 1 level higher in the directory structure. It then proceeds to delete the actual folder itself! I'm not sure if this is intended at all or a bug.

Workaround

After observing the above, I simply had the output of the publish task write to $(build.artifactstagingdirectory)\artifact and the resulting Publish Artifact task was then happy to pick up the zip file as it was still pointing to $(build.artifactstagingdirectory)

Default Publish Task output that fails

2018-06-07T02:24:17.8506434Z ##[debug]Zip Source: D:\a\1\a
2018-06-07T02:24:17.8508216Z ##[debug]Zip arguments: Source: D:\a\1\a , target: D:\a\1\a.zip
2018-06-07T02:24:18.0627499Z ##[debug]Successfully created archive D:\a\1\a.zip
2018-06-07T02:24:18.0628200Z ##[debug]rm -rf D:\a\1\a
2018-06-07T02:24:18.0629858Z ##[debug]removing directory
...
...
2018-06-07T02:24:18.3052522Z ##[error]Publishing build artifacts failed with an error: Not found PathtoPublish: D:\a\1\a

Modified output after adding extra directory

2018-06-07T02:38:59.8138062Z ##[debug]Zip Source: D:\a\1\a\artifact
2018-06-07T02:38:59.8139294Z ##[debug]Zip arguments: Source: D:\a\1\a\artifact , target: D:\a\1\a\artifact.zip
2018-06-07T02:39:00.0331460Z ##[debug]Successfully created archive D:\a\1\a\artifact.zip
2018-06-07T02:39:00.0334435Z ##[debug]rm -rf D:\a\1\a\artifact
2018-06-07T02:39:00.0336336Z ##[debug]removing directory
...
...
2018-06-07T02:39:00.4157615Z Uploading 1 files
2018-06-07T02:39:01.9425586Z ##[debug]File: 'D:\a\1\a\artifact.zip' took 1504 milliseconds to finish upload

Solution 2:[2]

The way I normally tackle this issue is, first, to use the PublishPipelineArtifact@0 task, instead of the deprecated PublishBuildArtifacts@1. So, in YAML, I would replace:

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

for:

- task: PublishPipelineArtifact@0
  displayName: 'Publish pipeline artifact'
  inputs:
    artifactName: 'drop'
    targetPath: '$(Build.ArtifactStagingDirectory)'

If I would continue having this error, then I would set the pipeline variable system.debug to true, trigger the pipeline again, and observe the logs from the tasks that produce the artifacts I want to publish. The path should be there somewhere in those logs

Solution 3:[3]

There is no built-in variable with that name, are you looking for:

$(Build.ArtifactStagingDirectory)

See: https://docs.microsoft.com/en-us/vsts/pipelines/build/variables?view=vsts&tabs=batch

Solution 4:[4]

this worked for me

- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: $(System.DefaultWorkingDirectory)/bin/Any CPU/Release/netcoreapp3.1
    ArtifactName: 'drop'
    publishLocation: 'Container'

Solution 5:[5]

As has already been pointed out, you probably meant $(build.StagingDirectory) so with the dot. But I'm regarding that as a simple typo, as I've encountered the same problem.

The answer is that when PUBLISHING the BUILD variables don't seem to be available (despite it being shown as the example in the tool-tip). What you probably want is $(System.ArtifactsDirectory). That worked for me.

Solution 6:[6]

After you have the build folder you need to copy the file contents into Build.ArtifactStagingDirectory.

- task: CopyFiles@2
  inputs:
    contents: '/home/vsts/work/1/s/api-project/bin/Debug/net6.0/publish/**'
    targetFolder: $(Build.ArtifactStagingDirectory)

You can then publish the artifacts

- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)'
    ArtifactName: 'drop'
    publishLocation: 'Container'

You can then use the artifact in your release pipeline. I hope this helps I was struggling with this for a while I found help with this issue on this Link https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/publish-build-artifacts?view=azure-devops

Here is the source code for my whole YAML file for reference this is a pipeline for a Monorepo.

# ASP.NET Core
# Build and test ASP.NET Core projects targeting .NET Core.
# Add steps that run tests, create a NuGet package, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/dotnet-core

trigger:
  branches:
    include:
      - main
  paths:
    include: 
      - api-project/*

pool:
  vmImage: ubuntu-latest

variables:
  buildConfiguration: 'Release'

steps:
- script: |
    dotnet build --configuration $(buildConfiguration)
    dotnet publish
  displayName: 'dotnet build and publish'
  workingDirectory: './api-project'

- task: CopyFiles@2
  inputs:
    contents: '/home/vsts/work/1/s/api-project/bin/Debug/net6.0/publish/**'
    targetFolder: $(Build.ArtifactStagingDirectory)

- script: |
    ls
  displayName: check location

- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)'
    ArtifactName: 'drop'
    publishLocation: 'Container'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Wah Yuen
Solution 2 ccoutinho
Solution 3 jessehouwing
Solution 4 Mosè Bottacini
Solution 5 Brian Cryer
Solution 6 Daniel Albert