AWS CodeArtifact上npm publish无法正常工作

3
我正在尝试使用npm publish将javascript库发布到托管在AWS Codeartifact上的私有NPM存储库。当我在本地计算机上运行npm publish时,它可以正常工作。然而,当我在Jenkins上运行时,似乎该软件包尝试发布到NPM的软件包存储库,而不是AWS Codeartifact,并且我会收到以下错误:
 > @company/package@0.2.2 prepare .
 > npm run co:login
 
 
 > @company/package@0.2.2 co:login /usr/src/components
 > aws codeartifact login --tool npm --domain <DOMAIN> --repository <REPOSITORY>
 
 Successfully configured npm to use AWS CodeArtifact repository <AWS_CODEARTIFACT_URL>
 Login expires in 12 hours at 2020-12-08 11:15:37+00:00
 npm notice 
 npm notice package: @company/package@0.2.2
 npm notice === Tarball Contents === 
 npm notice 2.9kB package.json   
 npm notice 1.6kB README.md      
 npm notice 268B  dist/index.d.ts
 npm notice === Tarball Details === 
 npm notice name:          @company/package              
 npm notice version:       0.2.2                                   
 npm notice package size:  2.1 kB                                  
 npm notice unpacked size: 4.8 kB                                  
 npm notice shasum:        <SHASUM>
 npm notice integrity:     <INTEGRITY>
 npm notice total files:   3                                       
 npm notice 
 npm ERR! code E404
 npm ERR! 404 Not Found - PUT https://registry.npmjs.org/@company%2fpackage - Not found
 npm ERR! 404 
 npm ERR! 404  '@company/package@0.2.3' is not in the npm registry.
 npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
 npm ERR! 404 
 npm ERR! 404 Note that you can also install from a
 npm ERR! 404 tarball, folder, http url, or git url.
 
 npm ERR! A complete log of this run can be found in:
 npm ERR!     /root/.npm/_logs/2020-12-08T00_20_19_949Z-debug.log

如果我理解AWS codeartifact文档正确的话,当我们运行aws codeartifact login命令时,它应该在.npmrc中设置正确的设置。我仍然不明白为什么它试图将内容推送到npm注册表而不是AWS。

以下是我的package.json文件:

{
  "name": "@company/package",
  "version": <VERSION>,
  "main": "dist/index.js",
  "module": "dist/index.js",
  "types": "dist/index.d.ts",
  "files": [
    "dist"
  ],
  "dependencies": {...},
  "scripts": {
    "co:login": "aws codeartifact login --tool npm --domain <DOMAIN> --repository <REPOSITORY>",
    "prepare": "npm run co:login"
  },
  "eslintConfig": {...},
  "browserslist": {...},
  "peerDependencies": {...},
  "devDependencies": {...}
}

更新

我添加了使用 CodeArtificat 的链接。

npm set registry <LINK_TO_CODE_ARTIFACT>

当我在本地执行发布脚本时,在脚本第一次运行时出现了以下错误:
Unable to authenticate, need: Bearer realm="company/package", Basic realm="company/package"

那个错误在后续运行中消失了。然而,当我在我的 Jenkins 管道上尝试相同的操作时,我不断地遇到这个错误。不确定这是什么意思。我看到了一个与 Azure 相关的讨论线程,但无法在 AWS CodeArtifact 上找到任何信息。

2个回答

1

我遇到了一个非常类似的问题,使用CodeBuild和CodeArtifact发布npm包。

使用aws codeartifact login命令的prepare步骤推荐方法对我不起作用。我认为这可能是由于Ubuntunpm不允许实际访问login命令写入的.npmrc文件的某些问题。

最后,在我的脚本中添加了以下额外的shell命令,在npm publish之前使用https://docs.aws.amazon.com/codeartifact/latest/ug/npm-auth.html的建议:

  npm set registry https://<endpoint name>.d.codeartifact.eu-west-2.amazonaws.com/npm/<repo name>/
  npm config set //<endpoint name>.d.codeartifact.eu-west-2.amazonaws.com/npm/<repo name>/:always-auth=true
  npm config set //<endpoint name>.d.codeartifact.eu-west-2.amazonaws.com/npm/<repo name>/:_authToken=$(aws codeartifact get-authorization-token --domain <domain> | jq -r .authorizationToken)
  npm publish --unsafe-perm


请注意,端点名称和域名将取决于您自己的设置。
另外请注意,我使用了 jq 来提取认证令牌 - 这是CodeBuild服务器的标准功能。
此外,请注意,此示例位于eu-west-2,但如果更改该区域,则可以在任何区域中使用。

0
问题在于我在这个项目中使用了两个不同的包管理器yarnnpm来执行几个任务。代码是使用yarn构建的,但发布是通过npm进行的。为了保持一致,即在所有任务中都使用yarn,我尝试使用yarn publish,结果神奇地成功了。
故事的寓意:要始终如一地使用您的包管理器。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接