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 Form.io 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
registry=https://pkgs.dev.azure.com/{some-organization}/{some-project}/_packaging/{some-feed}/npm/registry/

# Form.io Premium Packages
@formio:registry=https://pkg.form.io/


always-auth=true
And now we can modify the DevOps pipeline to install the private packages.
trigger:
  branches:
    include:
      - main
      - feature/*
      - version/*

pool:
  vmImage: 'ubuntu-latest'

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

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

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

  - task: Npm@1
    displayName: 'Install Formio Premium Packages'
    inputs:
      command: custom
      workingDir: $(RootPath)
      verbose: false
      customCommand: 'install @formio/premium --registry https://pkg.form.io --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.

Regards,
Jaliya

No comments:

Post a Comment