Search code examples
asp.netazure-devopsazure-pipelines-yaml

Azure DevOps build pipeline is publishing an old/cached build artifact


I have a strange situation going on with an Azure DevOps build pipeline where it seems to be repeatedly publishing an old/cached build artifact. The interesting thing I've noticed is that this only happens when I am attempting to add nuget package caching as part of the build.

The pipeline builds a solution made up of 4 different .NET7 projects. One ASP.NET web app, one console app, one library, and one xunit test project. The solution file is in the root, with each project in subfolders. The pipeline creates a zip of the webapp and publishes it as a build artifact that is later picked up by a release pipeline for deployment. This is in a Microsoft Azure/hosted agent.

Relevant pipeline steps:

- task: NuGetToolInstaller@1
  displayName: 'NuGet tool installer'

- task: NuGetAuthenticate@1

- task: Cache@2
  displayName: 'NuGet Cache'
  inputs:
    key: 'nuget | "$(Agent.OS)" | **/packages.lock.json,!**/bin/**,!**/obj/**'
    restoreKeys: |
       nuget | "$(Agent.OS)"
       nuget
    path: '${{ parameters.nugetPackages }}'
    cacheHitVar: 'CACHE_RESTORED'

- task: NuGetCommand@2
  displayName: 'NuGet restore'
  condition: ne(variables.CACHE_RESTORED, true)
  inputs:
    command: 'restore'
    restoreSolution: '${{ parameters.solution }}'
    nugetConfigPath: 'nuget.config'
    feedRestore: '..team project../..custom feed..'

- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactStagingDirectory)"'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

- task: PublishBuildArtifacts@1
  inputs:
    pathtoPublish: '$(build.artifactStagingDirectory)' # string. Required. Path to publish. Default: $(Build.ArtifactStagingDirectory).
    artifactName: 'drop' # string. Required. Artifact name. Default: drop.
    publishLocation: 'Container' # 'Container' | 'FilePath'. Alias: ArtifactType. Required. Artifact publish location. Default: Container.

My Release pipeline simply pulls the latest artifact from a specific branch, but I know the Release pipeline isn't even a factor because I have downloaded the build artifact and extracted it locally. I can clearly see it's an old version, yet inspecting the publish step logs show everything publishing successfully. The paths and filenames all match up just fine.

The way I can force it to actually publish the current artifact is to comment out the Cache@2 step so as not to cache the nuget packages, OR if I change the cache key as to invalidate the cache. Then everything magically works as expected.

I cannot for the life of me figure out what nuget package caching has to do with publishing the build artifact, but there's obviously a connection I'm missing.

Any ideas would be greatly appreciated.

Thanks-


Solution

  • Although I am embarrassed to admit this one, I felt compelled to answer my own question here. It turns out there was another Cache@2 step in a referenced pipeline template for caching npm packages that I was completely glossing over. The problem with it was that the required path parameter was not set. I don't fully understand exactly what this caused, but by correctly specifying the path to cache, the problem went away. Maybe it was caching everything or an output folder or something? I'm not sure.. but hopefully this helps someone else.