简体   繁体   English

npm 发布不适用于 AWS CodeArtifact

[英]npm publish not working on AWS CodeArtifact

I am trying to use npm publish to publish a javascript library to a private NPM repository hosted on AWS Codeartifact.我正在尝试使用npm publish将 javascript 库发布到 AWS Codeartifact 上托管的私有 NPM 存储库。

When I try running npm publish on my local machine, it works without any issues.当我尝试在本地机器上运行npm publish时,它可以正常工作。 However, when I try to run it on Jenkins, it looks like the package is trying to be published to NPM's package repository instead of AWS codeartifact and I get the following error:但是,当我尝试在 Jenkins 上运行它时,看起来 package 正试图发布到 NPM 的 packageact 存储库,而不是 AWS codeartif:

 > @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

If I understand the AWS codeartifact documentation , it should set the right settings in .npmrc when we run aws codeartifact login command.如果我了解AWS codeartifact 文档,当我们运行aws codeartifact login命令时,它应该在.npmrc中设置正确的设置。 I still don't understand why it is trying to push to the npm registry instead of AWS.我仍然不明白为什么它试图推送到 npm 注册表而不是 AWS。

Here is my package.json:这是我的 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": {...}
}

Update更新

I added the link to my CodeArtificat using我使用添加到我的 CodeArtificat 的链接

npm set registry <LINK_TO_CODE_ARTIFACT>

When I execute the publish script locally, I get this error on the script's first run:当我在本地执行发布脚本时,脚本第一次运行时出现此错误:

Unable to authenticate, need: Bearer realm="company/package", Basic realm="company/package"

That error disappears on subsequent runs.该错误在随后的运行中消失。 However, when I try the same on my Jenkins pipeline, I keep getting this error.但是,当我在 Jenkins 管道上尝试相同的操作时,我不断收到此错误。 Not sure what this means.不知道这意味着什么。 I saw a thread related to this error on Azure, but am unable to find anything on AWS CodeArtifact.我在 Azure 上看到了与此错误相关的线程,但在 AWS CodeArtifact 上找不到任何内容。

The problem was that I was using two different package managers, yarn and npm to do several tasks in this project.问题是我在这个项目中使用了两个不同的 package 管理器yarnnpm来完成几个任务。 The code was built using yarn but the publish was happening over npm .代码是使用yarn构建的,但发布是在npm上进行的。 In order to be uniform, ie use yarn across all our tasks, I tried yarn publish and it magically worked.为了统一,即在我们所有的任务中使用yarn ,我尝试了yarn publish ,它神奇地工作。

Moral of the story : Be consistent with your package managers.故事的寓意:与您的 package 经理保持一致。

I had a very similar issue to this one, using CodeBuild and CodeArtifact to publish npm packages.我有一个非常相似的问题,使用 CodeBuild 和 CodeArtifact 发布 npm 包。

The recommended approach of using a prepare step with a aws codeartifact login command didn't work for me.使用aws codeartifact login命令的prepare步骤的推荐方法对我不起作用。 I think that might be down to some issue with Ubuntu and npm not allowing actual access to the .npmrc file that the login command writes to.我认为这可能与Ubuntunpm不允许实际访问login命令写入的.npmrc文件有关。

In the end I added the following extra shell commands to my script before npm publish using advice from https://docs.aws.amazon.com/codeartifact/latest/ug/npm-auth.html : In the end I added the following extra shell commands to my script before npm publish using advice from 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

Note, the endpoint name and domain name will depend on your own set up.请注意,端点名称和域名将取决于您自己的设置。

Note also, I've used jq to extract the auth token - this comes as standard on a CodeBuild server.另请注意,我使用jq来提取身份验证令牌 - 这是 CodeBuild 服务器上的标准配置。

Note also this example is in eu-west-2 but could be in any region if you change that.另请注意,此示例位于eu-west-2中,但如果您更改它,则可能位于任何区域。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM