Thursday, February 2, 2023

Azure DevOps Pipelines: Install npm Packages from an External Private Package Registry

In this post let's see how we can Install npm Packages from an external private package registry in an Azure DevOps Pipeline.

The first step is adding a Service Connection for Connection Type: npm. You can do it by going into your Project Settings -> Service Connections and then New service connection and choosing npm.

New npm service connection
Click on Next.
New npm service connection
Here you can put the details, give the Service Connection a name and save it (note down the name as we are going to need it in a future step). In my case, I want to consume npm packages from private package repository and I have only the Username and the Password. So I have filled those up.

Next, we need to add/update the .npmrc file and specify the private package registry URL.
# Some other Azure Artifact Packages

# Premium Packages

And now we can modify the DevOps pipeline to install the private packages.
      - main
      - feature/*
      - version/*

  vmImage: 'ubuntu-latest'

name: $(Build.SourceBranchName).$(Build.BuildId)

  - task: npmAuthenticate@0
    displayName: "Authenticate for FormIo Premium Packages"
      workingFile: .npmrc
      customEndpoint: FormIo-Packages # Important: Name of the npm Service Connection created

  - task: NodeTool@0
    displayName: 'Install Node.js'
      versionSpec: '16.x'
      checkLatest: true

  - task: Npm@1
    displayName: 'Install Formio Premium Packages'
      command: custom
      workingDir: $(RootPath)
      verbose: false
      customCommand: 'install @formio/premium --registry --force'
Here the important step is using npmAuthenticate@0 task to provide npm credentials to the .npmrc file for the scope of the build. So it will get used when we are doing the npm install.

And that should do.

Hope this helps.

Happy Coding.


No comments:

Post a Comment