Skip to content

Instantly share code, notes, and snippets.

@semick-dev
Last active July 10, 2024 18:40
Show Gist options
  • Save semick-dev/2f17a36c5f39735d6a63983df9278f8b to your computer and use it in GitHub Desktop.
Save semick-dev/2f17a36c5f39735d6a63983df9278f8b to your computer and use it in GitHub Desktop.
Connect to NPM Azure DevOps Feed

How to add a devops feed to local configuration for an npm package

  • Within your package folder (folder containing package.json for your package), locate where the .npmrc for the package is located. Usually it's right alongside the package.json. Creating an .npmrc with the feed is covered in a step below.

    • For openapi-alps this is located under common/config/rush/.npmrc
  • Ensure your project .npmrc has a reference to the feed containing your packages.

    • To get detailed instructions from devops, go to the feed and click Connect to Feed (top right button), then click NPM, and change from Windows tab to Other. The Other tab is excellent to use for linux/mac connections.
  • Update your project .npmrc with the following

    • # "registry" is a url, don't make it multiline!
      registry=https://devdiv.pkgs.visualstudio.com/_packaging/openapi-platform/npm/registry/ 
      always-auth=true
  • Generate a PAT from devops

    • Ensure that it has Packaging: Read permissions.
    • Ensure it is for the devdiv organization.
      • Going to user settings from the devdiv org seems to work consistently for me.
  • Base64 the PAT. Invoke this:

    # bash
    node -e "require('readline') .createInterface({input:process.stdin,output:process.stdout,historySize:0}) .question('PAT> ',p => { b64=Buffer.from(p.trim()).toString('base64');console.log(b64);process.exit(); })"
    # enter the PAT and press enter
    // in browser console
    btoa("<pat>")
    • As long as you have node installed locally this should work. I use Node 16 as my daily driver.
  • Create a user .npmrc.

    • In powershell, cd ~/ && code .npmrc
    • Populate NPMRC with:
      //devdiv.pkgs.visualstudio.com/_packaging/openapi-platform/npm/registry/:username=devdiv
      //devdiv.pkgs.visualstudio.com/_packaging/openapi-platform/npm/registry/:_password=[BASE64_ENCODED_PERSONAL_ACCESS_TOKEN]
      //devdiv.pkgs.visualstudio.com/_packaging/openapi-platform/npm/registry/:email=npm requires email to be set but doesn't use the value
      //devdiv.pkgs.visualstudio.com/_packaging/openapi-platform/npm/:username=devdiv
      //devdiv.pkgs.visualstudio.com/_packaging/openapi-platform/npm/:_password=[BASE64_ENCODED_PERSONAL_ACCESS_TOKEN]
      //devdiv.pkgs.visualstudio.com/_packaging/openapi-platform/npm/:email=npm requires email to be set but doesn't use the value
      
  • That's it! The combination of package .npmrc pointing out which feed to reference and your user .npmrc providing the auth for that feed URL is what makes all this work!

@chidozieononiwu
Copy link

Thanks. The node logic for Base64 PAT did not work for me. But I was able to do btoa(PAT) in browser console.

@semick-dev
Copy link
Author

  1. btoa? Excellent workaround!
  2. Can you link the failure to me so that I can fix it for the next person? Hopefully I can ascertain what went wrong just based off the invocation error from your CLI session.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment