使用 azure 工件通过 azure devops 组织共享 NPM 脚本/应用程序

问题描述

我有一个 git 存储库,其中包含一个 node.js“应用程序”(只有 1 个文件)及其 package.json。

我需要使用来自其他组织的 azure devops 管道的“应用程序”,所以我在那个 nodejs 项目中创建了一个管道,在基于 NPM 的 azure 工件提要上制作该脚本的版本控制+发布。

在另一个组织中,在我想使用该应用程序的管道中,我使用了一个“npm”任务,配置如下:

steps:
- task: Npm@1
  displayName: 'npm install MyPackage'
  inputs:
    command: custom
    verbose: false
    customCommand: 'install MyPackage'
    customregistry: useFeed
    customFeed: '...some-guid....'

安装似乎没问题(我在第二个组织中创建了一个提要,其中第一个组织的提要作为上游源,如文档中所示)。

现在,我应该如何在下一个管道任务中执行该脚本? 我试图连接到代理,但是当我运行“MyPackage”命令时,我得到“找不到命令”。 脚本安装在哪里?我应该如何执行它? 有什么东西可以放在 PATH 中吗? 我应该使用“-g”选项吗?

感谢您的帮助:)

使用请求的日志更新:

任务日志:

Starting: npm custom
==============================================================================
Task         : npm
Description  : Install and publish npm packages,or run an npm command. Supports npmjs.com and authenticated registries like Azure Artifacts.
Version      : 1.182.0
Author       : Microsoft Corporation
Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/package/npm
==============================================================================
/usr/local/bin/npm --version
6.13.7
/usr/local/bin/npm config list
; cli configs
metrics-registry = "https://pkgs.dev.azure.com/org/_packaging/....some-guid..../npm/registry/"
scope = ""
user-agent = "npm/6.13.7 node/v13.11.0 darwin x64"

; environment configs
userconfig = "/Users/.../AGENTS/vsts-agent-osx-x64-2.179.0-AZURE-01/_work/10/npm/33366.npmrc"

; userconfig /Users/.../AGENTS/vsts-agent-osx-x64-2.179.0-AZURE-01/_work/10/npm/33366.npmrc
registry = "https://pkgs.dev.azure.com/org/_packaging/....some-guid..../npm/registry/"

; builtin config undefined
prefix = "/usr/local"

; node bin location = /usr/local/Cellar/node/13.11.0/bin/node
; cwd = /Users/.../AGENTS/vsts-agent-osx-x64-2.179.0-AZURE-01/_work/10/s
; HOME = /Users/...
; "npm config ls -l" to show all defaults.

/usr/local/bin/npm install -g MyPackage
+ MyPackage@1.0.20210216-1
added 198 packages from 99 contributors in 18.319s
Finishing: npm custom

这是第二个定义:

steps:
- script: |
   echo "1"
   node MyPackage
   
   echo "2"
   MyPackage
   
  displayName: 'Command Line Script'

以及相关的执行日志:

Starting: Command Line Script
==============================================================================
Task         : Command line
Description  : Run a command line script using Bash on Linux and macOS and cmd.exe on Windows
Version      : 2.182.0
Author       : Microsoft Corporation
Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/command-line
==============================================================================
Generating script.
========================== Starting Command Output ===========================
/bin/bash --noprofile --norc /Users/.../AGENTS/vsts-agent-osx-x64-2.179.0-AZURE-01/_work/_temp/7fe7833a-ce7d-48af-ab8f-1fcf2e740c36.sh
1
internal/modules/cjs/loader.js:979
  throw err;
  ^

Error: Cannot find module '/Users/.../AGENTS/vsts-agent-osx-x64-2.179.0-AZURE-01/_work/10/s/MyPackage'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:976:15)
    at Function.Module._load (internal/modules/cjs/loader.js:859:27)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47 {
  code: 'MODULE_NOT_FOUND',requireStack: []
}
2
/Users/.../AGENTS/vsts-agent-osx-x64-2.179.0-AZURE-01/_work/_temp/7fe7833a-ce7d-48af-ab8f-1fcf2e740c36.sh: line 5: MyPackage: command not found
##[error]Bash exited with code '127'.
Finishing: Command Line Script

解决方法

我们可以add an Azure Artifacts feed in a different organization within your Azure AD tenant as an upstream source

比如你在第一个组织中创建了一个feed,并且在feed中添加了npm包,现在你想在第二个组织中使用这个包,我们需要在第二个组织中设置上游源,比如azure-feed://myOrg/myProject/myFeed@local,然后我们可以在第二个组织管道中安装和使用该包。

您也可以查看此 blog 了解更多详情。

更新 1

我需要找到它的安装路径

我们可以添加任务 Command line 并运行 npm list 以查看您当前位置安装的非全局库。查看此 ticketblog 了解更多详情。

安装路径是$(Build.SourcesDirectory)/node_modules,我们可以添加task bash并运行ls '$(Build.SourcesDirectory)/node_modules'来查看。

,

终于找到问题了:

  • 首先,我必须使用 -g 选项全局安装软件包
  • 第二,我不得不使用 npx 运行它。
  • 第 3 次,我必须用 :
  • 更新 package.json
  "bin": {
    "NAME_OF_THE_BINARY": "./FILE_TO_EXECUTE.js"
  },