enigma.b17
enigma.b17

Reputation: 135

Azure YAML Pipelines - Jobs/Tasks package issues

So I've been doing some learning into Azure DevOps Yaml Pipelines and I've come across an issue I can't seem to figure out what would be the cause.

I was building my first pipeline for a small class library solution, the idea being to restore, build, test, pack & publish it when changes are committed into master.

I split the different parts of the deployment into stages/jobs (which may not be the correct way to use these), but when I do so, the "Nuget pack" step can never find any of the built files.

This YAML does not work, and errors out on the "NuGet (Pack)" Step because it cannot find the "projects.assets.json" file, which I have confirmed that the build step does produce.

trigger:
- master

pool:
  vmImage: 'windows-latest'

name: 'Set dynamically'

variables:
  buildConfiguration: 'Release'
  version.Major: 1
  version.Minor: $[counter(variables['version.Major'], 0)]
  version.Patch: 0
  version.Revision: $[counter(variables['version.Minor'], 0)]
  version.Number: '$(version.Major).$(version.Minor).$(version.Patch).$(version.Revision)'

stages:
- stage: Prepare
  jobs:
    - job: Prepare_Sources
      steps:
      - checkout: self
        clean: true

    - job: Prepare_BuildAndVersionNumbers
      steps:
      - task: PowerShell@2
        displayName: Set the name of the build
        inputs:
          targetType: 'inline'
          script: |
            [string] $dateTime = (Get-Date -Format 'yyyyMMdd')
            [string] $buildName = "$(Build.DefinitionName)_$(Build.SourceBranchName)_$($dateTime)_$(version.Number)"
            Write-Host "Setting the name of the build to '$buildName'."
            Write-Host "##vso[build.updatebuildnumber]$buildName"

- stage: Build
  jobs:
    - job: BuildRestore
      steps:
      - task: NuGetCommand@2
        displayName: 'Restore (NuGet)'
        inputs:
          command: restore
          restoreSolution: '**\*.sln'fs
          feedsToUse: select
          includeNuGetOrg: true
          vstsFeed: 'internalfeed1'
          arguments: '--configuration $(buildConfiguration) /p:Version=$(version.Number)'

      - task: DotNetCoreCLI@2
        displayName: 'Restore (.NET Core)'
        inputs:
          command: restore
          includeNuGetOrg: true
          nobuild: true
          vstsFeed: 'internalfeed1'
          nuGetFeedType: internal
          projects: '**/*.csproj'
          arguments: '--configuration $(buildConfiguration) /p:Version=$(version.Number)'

      - task: DotNetCoreCLI@2
        displayName: 'Build all projects in solution'
        inputs:
          command: build
          projects: '**/*.csproj'
          arguments: '--configuration $(buildConfiguration) /p:Version=$(version.Number)'

- stage: Test
  jobs:
    - job: Test_UnitTests
      steps:
      - task: DotNetCoreCLI@2
        displayName: 'Run & Analyse UnitTests'
        inputs:
          command: test
          projects: '**/*Tests/*UnitTests.csproj'
          arguments: '--configuration $(buildConfiguration) --collect "Code coverage"'

- stage: Package
  jobs:
    - job: Package_Nuget
      steps:
      - task: NuGetAuthenticate@0
        displayName: "Nuget (Authenticate)"
      
      - task: DotNetCoreCLI@2
        displayName: 'NuGet (Package)'
        inputs:
          nobuild: true
          command: pack
          packagesToPack: '**/*.csproj'
          versioningScheme: byBuildNumber
          arguments: '--configuration $(buildConfiguration)'

      - task: DotNetCoreCLI@2
        displayName: 'NuGet (Publish)'
        inputs:
          command: push
          searchPatternPush: '$(Build.ArtifactStagingDirectory)/*.nupkg;'
          feedPublish: 'internalfeed1'
          

If I simplify it all down into one single job without the stages/jobs the deployment all works ok (as below)

trigger:
- master

pool:
  vmImage: 'windows-latest'

name: 'Set dynamically'

variables:
  buildConfiguration: 'Release'
  version.Major: 1
  version.Minor: $[counter(variables['version.Major'], 0)]
  version.Patch: 0
  version.Revision: $[counter(variables['version.Minor'], 0)]
  version.Number: '$(version.Major).$(version.Minor).$(version.Patch).$(version.Revision)'

steps:
      - checkout: self
        clean: true

    - task: PowerShell@2
        displayName: Set the name of the build
        inputs:
          targetType: 'inline'
          script: |
            [string] $dateTime = (Get-Date -Format 'yyyyMMdd')
            [string] $buildName = "$(Build.DefinitionName)_$(Build.SourceBranchName)_$($dateTime)_$(version.Number)"
            Write-Host "Setting the name of the build to '$buildName'."
            Write-Host "##vso[build.updatebuildnumber]$buildName"

    - task: NuGetCommand@2
        displayName: 'Restore (NuGet)'
        inputs:
          command: restore
          restoreSolution: '**\*.sln'fs
          feedsToUse: select
          includeNuGetOrg: true
          vstsFeed: 'internalfeed1'
          arguments: '--configuration $(buildConfiguration) /p:Version=$(version.Number)'

    - task: DotNetCoreCLI@2
        displayName: 'Restore (.NET Core)'
        inputs:
          command: restore
          includeNuGetOrg: true
          nobuild: true
          vstsFeed: 'internalfeed1'
          nuGetFeedType: internal
          projects: '**/*.csproj'
          arguments: '--configuration $(buildConfiguration) /p:Version=$(version.Number)'

    - task: DotNetCoreCLI@2
        displayName: 'Build all projects in solution'
        inputs:
          command: build
          projects: '**/*.csproj'
          arguments: '--configuration $(buildConfiguration) /p:Version=$(version.Number)'

      - task: DotNetCoreCLI@2
        displayName: 'Run & Analyse UnitTests'
        inputs:
          command: test
          projects: '**/*Tests/*UnitTests.csproj'
          arguments: '--configuration $(buildConfiguration) --collect "Code coverage"'

      - task: DotNetCoreCLI@2
        displayName: 'NuGet (Package)'
        inputs:
          nobuild: true
          command: pack
          packagesToPack: '**/*.csproj'
          versioningScheme: byBuildNumber
          arguments: '--configuration $(buildConfiguration)'

      - task: DotNetCoreCLI@2
        displayName: 'NuGet (Publish)'
        inputs:
          command: push
          searchPatternPush: '$(Build.ArtifactStagingDirectory)/*.nupkg;'
          feedPublish: 'internalfeed1'
          

Can't find an answer on the documentation for these pieces of the puzzle that would explain why it wouldn't work when split up into stages/jobs, does anyone know what the reasoning is? Are Stages/Jobs not supposed to interact with each other in this way?

Thanks

Upvotes: 1

Views: 835

Answers (1)

Krzysztof Madej
Krzysztof Madej

Reputation: 40603

This is becuase each job runs on different agent

A stage contains one or more jobs. Each job runs on an agent. A job represents an execution boundary of a set of steps. All of the steps run together on the same agent. For example, you might build two configurations - x86 and x64. In this case, you have one build stage and two jobs.

And this since jon is a boundary for set of steps source code is not shared amongs them. SO if you need keep this as seprate jobs and stages you should repeat in each job checkout step

      - checkout: self
        clean: true

Please read this basics about pipeline, they will give you high level picture how it works.

And if you want to share some artifact between jobs please take a look here.

And if you need share some variables between stages I wrote an article about this.

Upvotes: 1

Related Questions