13

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??

Community
  • 1
  • 1
Kgn-web
  • 7,047
  • 24
  • 95
  • 161
  • Please unlink the Path to publish option, and then speacify Path to publish as `$(Build.ArtifactStagingDirectory)`. And can you show the build logd here after updating your build definition? – Marina Liu Jun 07 '18 at 08:23
  • u got the solution for this? – Sunil Garg Mar 29 '19 at 17:19

7 Answers7

12

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

ccoutinho
  • 3,308
  • 5
  • 39
  • 47
  • Doing this worked for me, however I have another existing pipeline using the apparently deprecated Publish Build Artifacts and it works. I am wondering if it has more to do with the fact that I tried adding another folder onto the pathToPublish, like I had $(Build.ArtifactStagingDirectory)\$(ProjectName) and when I used the newer task, I just left it as it was defaulted using the root directory of Pipeline.Workspace – Kelly Nov 10 '20 at 23:32
  • @ccoutinho, you said it in reverse! PublishPipelineArtifact@0 is deprecated, and PublishPipelineArtifact@1 is the new one. – Kamran Bigdely Jul 25 '23 at 00:02
  • At least I mentioned the two in my sentence :D – ccoutinho Jul 25 '23 at 08:27
  • is PublishBuildArtifacts@1 currently deprecated? I don't see that anywhere anymore. – Urasquirrel Aug 01 '23 at 15:29
8

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
Wah Yuen
  • 1,569
  • 1
  • 9
  • 13
3

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

$(Build.ArtifactStagingDirectory)

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

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
2

this worked for me

- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: $(System.DefaultWorkingDirectory)/bin/Any CPU/Release/netcoreapp3.1
    ArtifactName: 'drop'
    publishLocation: 'Container'
Mosè Bottacini
  • 4,016
  • 2
  • 24
  • 28
1

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://learn.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://learn.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'
0

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.

Brian Cryer
  • 2,126
  • 18
  • 18
0

I faced the same issue while creating a CI pipeline. Finally found that the files were created in $(System.DefaultWorkingDirectory). So we need to copyfiles first to $(build.artifactstagingdirectory) before the publish build artifact task. So I added Copy Files task with Source folder as $(System.DefaultWorkingDirectory) and Target folder as $(Build.ArtifactStagingDirectory) and that resolved it.

akarahman
  • 235
  • 2
  • 15