You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Pipelines中如何实现Azure Artifacts的NPM认证?

Azure Pipelines访问Azure Artifacts私有NPM仓库401认证错误问题

我们拥有一个存储私有构件的Azure Artifacts仓库:
https://pkgs.dev.azure.com/bridgette/letspretend/_packaging/thisistherealaddress/npm/registry/

我使用发布Node.js Web应用的默认Pipeline,仅做少量修改,配置如下:

# Node.js Express Web App to Linux on Azure
# Build a Node.js Express app and deploy it to Azure as a Linux web app.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/javascript

trigger:
  - main

variables:
  - group: azure-artifacts
  # Azure Resource Manager connection created during pipeline creation
  - name: azureSubscription
    value: "waap-waap-waap-waaaaaaa"

  # Web app name
  - name: webAppName
    value: "MYAPP-Dev"

  # Environment name
  - name: environmentName
    value: "MYAPP-Dev"

  # Agent VM image name
  - name: vmImageName
    value: "windows-latest"

stages:
  - stage: Build
    displayName: Build stage
    jobs:
      - job: Build
        displayName: Build
        pool:
          vmImage: $(vmImageName)

        steps:
          - task: NodeTool@0
            inputs:
              versionSpec: "20.x"
            displayName: "Install Node.js"

          - task: PowerShell@2
            displayName: "Install vsts-npm-auth"
            inputs:
              targetType: inline
              script: |
                try {
                  echo "Install vsts-npm-auth"
                  npm install -g vsts-npm-auth
                  echo "vsts-npm-auth Installed"
                }
                catch {
                  echo "vsts-npm-auth failed to install"
                  exit 1
                }

          - task: PowerShell@2
            displayName: "Running vsts-npm-config"
            inputs:
              targetType: inline
              script: |
                try {
                  Write-Host "Running vsts-npm-config"
                  vsts-npm-auth -C .npmrc -F -N
                }
                catch {
                  Write-Host "Debug: Outputting .npmrc"
                  Get-Content $env:USERPROFILE\.npmrc
                  exit 1
                }

            continueOnError: false
            condition: succeeded()
            env:
              NPM_AUTH_TOKEN: $(NPM_AUTH_TOKEN)

          - task: PowerShell@2
            displayName: "npm install"
            inputs:
              targetType: inline
              script: |
                npm install
            continueOnError: false
            condition: succeeded()

          - task: PowerShell@2
            displayName: "Outputting npm logs on failure."
            inputs:
              targetType: inline
              script: |
                Write-Host "Debug: Outputting npm log contents due to npm install failure"
                Get-ChildItem -Path "/home/vsts/.npm/_logs/" -File | ForEach-Object {
                    Write-Host "==============================================="
                    Write-Host "Contents of $($_.FullName):"
                    Write-Host "==============================================="
                    Get-Content $_.FullName
                    Write-Host ""
                }
            continueOnError: false
            condition: failed()

          - task: PowerShell@2
            displayName: "npm build"
            inputs:
              targetType: inline
              script: |
                npm run build --if-present
            continueOnError: false
            condition: succeeded()

          - task: ArchiveFiles@2
            displayName: "Archive files"
            inputs:
              rootFolderOrFile: "$(System.DefaultWorkingDirectory)"
              includeRootFolder: false
              archiveType: zip
              archiveFile: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
              replaceExistingArchive: true
            condition: succeeded()

          - upload: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
            artifact: drop
            condition: succeeded()

  - stage: Deploy
    displayName: Deploy stage
    dependsOn: Build
    condition: succeeded()
    jobs:
      - deployment: Deploy
        displayName: Deploy
        environment: $(environmentName)
        pool:
          vmImage: $(vmImageName)
        strategy:
          runOnce:
            deploy:
              steps:
                - task: AzureWebApp@1
                  displayName: "Azure Web App Deploy: MYAPP-Dev"
                  inputs:
                    azureSubscription: $(azureSubscription)
                    appType: webAppLinux
                    appName: $(webAppName)
                    runtimeStack: "NODE|10.10"
                    package: $(Pipeline.Workspace)/drop/$(Build.BuildId).zip
                    startUpCommand: "npm run start"

我尝试了多种方法均未解决问题:

方法1:使用ubuntu-latest代理镜像

修改项目本地.npmrc文件:

@evo-uikit:registry=https://pkgs.dev.azure.com/bridgette/letspretend/_packaging/thisistherealaddress/npm/registry
//pkgs.dev.azure.com/bridgette/letspretend/_packaging/thisistherealaddress/npm/registry:_authToken={$NPM_AUTH_TOKEN}
registry=https://registry.npmjs.org
always-auth=false

设置NPM_AUTH_TOKEN环境变量(个人PAT或System.AccessToken的Base64值),但始终收到401错误:

npm error code E401
npm error Unable to authenticate, your authentication token seems to be invalid.

日志详情:

751 verbose stack HttpErrorAuthUnknown: Unable to authenticate, need: Bearer authorization_uri=https://login.windows.net/***, Basic realm="https://pkgsprodeau1.pkgs.visualstudio.com/", TFS-Federated
751 verbose stack     at /opt/hostedtoolcache/node/20.16.0/x64/lib/node_modules/npm/node_modules/npm-registry-fetch/lib/check-response.js:80:17
751 verbose stack     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
752 verbose statusCode 401
753 verbose pkgid @blah-blah/text/@https://pkgs.dev.azure.com/bridgette/letspretend/_packaging/thisistherealaddress/npm/registry/@blah-blah/text/-/text-0.1.2.tgz
754 error code E401
755 error Unable to authenticate, your authentication token seems to be invalid.

方法2:使用windows-latest镜像并尝试vsts-npm-auth工具

执行vsts-npm-auth -C .npmrc -F -N命令,但无法获取认证token:

vsts-npm-auth v0.43.0.0 
-----------------------
Creating npmrc file. Path: C:\Users\VssAdministrator\.npmrc
Getting new credentials for source:https://pkgs.dev.azure.com/bridgette/letspretend/_packaging/thisistherealaddress/npm/registry/, scope:vso.packaging_write vso.drop_write
Couldn't get an authentication token for https://pkgs.dev.azure.com/bridgette/letspretend/_packaging/thisistherealaddress/npm/registry/.
Couldn't get an authentication token for https://pkgs.dev.azure.com/bridgette/letspretend/_packaging/thisistherealaddress/npm/registry/.

需求

我需要实现Azure托管Pipeline无需人工干预即可访问Azure Artifacts仓库(支持拉取和发布),且需兼容rushjs单仓库的rush install和rush publish命令。此前在其他Pipeline中使用NuGetAuthenticate@1任务结合Npm@1任务可实现发布,但此场景需要在脚本中解决认证问题,恳请解决401认证错误。

内容的提问来源于stack exchange,提问作者Bridgette Ryan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 23:05:06