azure-pipelinesmultistage-pipeline

How to share files between jobs


Is it possible for two jobs to share files in multi-stage pipeline builds? Publish stage has dotnet publish job (single task) and publish artifacts job (single task). However, the output from dotnet publish doesn't seem to be available to publish artifacts.


Solution

  • If the each of the two jobs have a single task and the second task consumes the output of the first task. So why not run them under the same job.

    You can refer to below yaml

      stages: 
      - stage: Publish
        displayName: 'Publish stage'
        jobs:
        - job: dotnetpublishartifacts
          pool:
            vmImage: 'windows-latest'
          steps:
          - task: DotNetCoreCLI@2
            displayName: 'dotnet publish'
            inputs:
              command: publish
              projects: '**/*.csproj'
              arguments: '-o $(build.artifactstagingdirectory)'
              publishWebProjects: false 
    
          - task: PublishBuildArtifacts@1
            displayName: 'Publish Artifact: drop'
            inputs:
              PathtoPublish: '$(build.artifactstagingdirectory)'
    

    If the two tasks have to be in separate jobs. And the jobs run on the same agent. Then you can try making the dotnet publish task output to an folder which will not be cleared by the next job(As the agent job will clear build.artifactstagingdirectory of the previous job),

    In below example dotnet publish task output to $(Agent.BuildDirectory)\firtjobpublish, this folder will not be cleared by the following job execution.

    You may need to click on the 3dots on the right to corner of your yaml pipeline edit page, click triggers, go to YAML, and in the Get sources section set Clean to false. enter image description here

    Below yaml is for example:

     jobs:
        - job: dotnetpublishartifacts
          pool: Default
          steps:
          - task: DotNetCoreCLI@2
            displayName: 'dotnet publish'
            inputs: 
              command: publish
              projects: '**/*.csproj'
              arguments: '-o $(Agent.BuildDirectory)\firtjobpublish'
              publishWebProjects: false 
    
        - job: publishartifacts
          dependsOn: dotnetpublishartifacts
          pool: Default  
          steps:
            - task: PublishBuildArtifacts@1
              displayName: 'Publish Artifact: drop'
              inputs:
                PathtoPublish: '$(Agent.BuildDirectory)\firtjobpublish'
    

    Addtion: For sharing files between jobs, if jobs run on different agents, you can try adding a publish artifacts task in the first job to publish the artifacts to azure server. And in the following job add a download artifacts task to download the artifacts which is published by the previous job. This method is apparently not for your case.

    Hope you find above helpful!