0

I have an Azure DevOps pipeline for one of my .Net Core projects. I would like to encapsulate as much of the build process as possible so I can run builds on my local machine, so I have the steps of restore, compile, test, publish, etc. encapsulated in a build script which I call from my Azure DevOps pipeline.

The .Net Core CLI task automatically handles authentication to Azure Artifacts feeds, which is great when you want to script out your build in your pipeline, but I really want to add a task which only does the authentication part and passes off responsibility for everything else to my build script. Is there some way to authenticate without doing a restore?

Here's the relevant portion of my pipeline:

  - task: DotNetCoreCLI@2
    displayName: 'NuGet Authentiation and Restore'
    inputs:
      command: 'restore'
      projects: 'src/*.sln'
      feedsToUse: 'select'
      vstsFeed: '5483129a-4405-40c1-8ccb-a688120b3137'
      includeNuGetOrg: false 

  - task: Npm@1
    displayName: 'Build release distribution'
    condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
    inputs:
      command: custom
      verbose: false
      customCommand: 'run build'

The npm run build step will call dotnet restore which succeeds because the packages are already restored, but I'm really just using the DotnetCoreCLI task for it's authentication ability. I don't really want it to restore. I mean, it works, but it just feels messy.

Derek Greer
  • 15,454
  • 5
  • 45
  • 52

1 Answers1

0

Is there some way to authenticate without doing a restore?

Since you are using the npm feed, you could try to use the npm authenticate task to authenticate the azure artifacts feed.

Here are the steps:

Step1: Create .npmrc file in the same directory as your package.json.

For example:

enter image description here

Step2: Navigate to Azure Artifacts -> Connect to feed -> Npm to get the .npmrc file content.

For example:

registry=https://pkgs.dev.azure.com/org/project/_packaging/456/npm/registry/ 
                        
always-auth=true

Step3: Add the npm authenticate task.

- task: npmAuthenticate@0
  displayName: 'npm Authenticate package/.npmrc'
  inputs:
    workingFile: package/.npmrc
  enabled: false

This allows subsequent build steps to use the credentials in the .npmrc.

For more detailed information, you could refer to this doc about npm authentication.

Update:

For Nuget feed, you could use the NuGet Authenticate task.

For example:

- task: NuGetAuthenticate@0
  displayName: 'NuGet Authenticate'
  inputs:
    nuGetServiceConnections: 'test nuget feed'

If you do not set the service connection, it will authenticate the default feed.

If you want to specify the feed, you need to create a nuget service connection.

enter image description here

Feed URL: Azure Artifacts -> Connect to feed -> Nuget

User name and Password: you could use current account or PAT.

Kevin Lu-MSFT
  • 20,786
  • 3
  • 19
  • 28
  • I apologize if my question was unclear, but I'm only using NPM as a build tool. The feed that needs to be authenticated is a NuGet feed, not an npm feed. – Derek Greer Aug 05 '20 at 14:30
  • Hi @DerekGreer. Thanks for your clarification. I have Updated the answer. Please check it. – Kevin Lu-MSFT Aug 06 '20 at 01:04
  • Hi @DerekGreer. Is there any update about this ticket? Feel free to let me know if the answer could give you some help. – Kevin Lu-MSFT Aug 07 '20 at 09:15