'Azure pipeline - publish artifacts failing due to empty directory but its not

I have an azure build pipeline which I am trying to get to publish it's artifacts so I can then build a release pipeline.

I have built a master pipeline which consumes stages as defined in other Yaml pipeline templates.

For example the mast pipeline looks like this:

trigger: 
  - main
pr: 
  - main

pool:
  vmImage: ubuntu-latest

# Templates lives in the parent repository: Common
resources:
   repositories:
     - repository: Common
       type: github
       name: Common
       ref: refs/tags/v1
       endpoint: test-org

stages:
- template: /ADO_Templates/Pipeline Templates/unit-test-code-analysis-stage.yml@Common

- template: /ADO_Templates/Pipeline Templates/build-stage.yml@Common
  parameters:
    buildStageDependsOn: 'Unit_Tests_And_Static_Code_Analysis'

- template: /ADO_Templates/Pipeline Templates/publish-build-artifacts-stage.yml@Common
  parameters:
    publishArtifactsStageDependsOn: 'Build_Stage'

The pipeline builds as expected and in the build stage I see the files copied to staging with this step:

- task: CopyFiles@2
  displayName: 'Copy Files to: $(build.artifactstagingdirectory)'
  inputs:
    SourceFolder: '$(system.defaultworkingdirectory)/target'
    Contents: '**'
    TargetFolder: '$(build.artifactstagingdirectory)'

In the next step I have this stage:

#Publish Build Artifact
- task: PublishBuildArtifacts@1
  displayName: Publish Build Artifacts
  inputs:
    PathtoPublish: '$(build.artifactstagingdirectory)'

but I get the following error:

##[warning]Directory '/home/vsts/work/1/a' is empty. Nothing will be added to build artifact 'drop'.

I don't know why - but I'm guessing it's because they copy & publish are in different steps?

Anyone able to explain why/whats happening?



Sources

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

Source: Stack Overflow

Solution Source