英文:
How to publish a scoped npm package to gitlab's package registry in a group namespace via a CI/CD pipeline
问题
我在GitLab上有一个私有的虚拟项目,我想要将其发布到GitLab的包注册表。我的虚拟项目包含四个文件:
package.json
{
"name": "@<my-group>/<my-project>",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://gitlab.com/<my-group>/<my-project>.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://gitlab.com/<my-group>/<my-project>/issues"
},
"homepage": "https://gitlab.com/<my-group>/<my-project>#readme"
}
.gitlab-ci.yml
image: node:latest
stages:
- deploy
deploy:
stage: deploy
script:
- echo "@<my-group>:registry=https://${CI_SERVER_HOST}/api/v4/projects/${CI_PROJECT_ID}/packages/npm/".npmrc
- echo "//${CI_SERVER_HOST}/api/v4/projects/${CI_PROJECT_ID}/packages/npm/:_authToken=${CI_JOB_TOKEN}" >> .npmrc
- npm publish
index.js
console.log('success');
README.md
<my-project>
当我提交我的项目到GitLab时,作业失败,输出如下:
npm notice package: @<my-group>/<my-project>@1.0.0
npm notice === Tarball Contents ===
npm notice 8B README.md
npm notice 20B index.js
npm notice 612B package.json
npm notice === Tarball Details ===
npm notice name: @<my-group>/<my-project>
npm notice version: 1.0.0
npm notice filename: <my-group>-<my-project>-1.0.0.tgz
npm notice package size: 475 B
npm notice unpacked size: 640 B
npm notice shasum: 7b3db...
npm notice integrity: sha512-xDv0dl9A86...
npm notice total files: 3
npm notice
npm notice Publishing to https://gitlab.com/api/v4/projects/<my-project-id>/packages/npm/ with tag latest and default access
npm ERR! code E403
npm ERR! 403 403 Forbidden - PUT https://gitlab.com/api/v4/projects/<my-project-id>/packages/npm/@<my-group>%2f<my-project> - insufficient_scope
npm ERR! 403 In most cases, you or one of your dependencies are requesting
npm ERR! 403 a package version that is forbidden by your security policy, or
npm ERR! 403 on a server you do not have access to.
注意:
我已在上面的代码部分中使用<my-group>
,<my-project>
和<my-project-id>
替代了实际的组名、项目名和项目ID。我遵循了GitLab官方文档上的设置步骤(参见https://docs.gitlab.com/ee/user/packages/npm_registry/),并认为可以安全排除以下可能性:
- 我确保项目设置中启用了
Package registry
。 - 我按照文档中描述的命名约定进行命名。
- 我使用的是
CI_JOB_TOKEN
,它应该始终有效并具有适当的权限。 - 我确保给定范围内没有同名或同版本的包。
- 我确保了有作用域的包的URL包含尾随斜杠(请参考上面的gitlab-ci.yml)。
- 我确认了
<my-group>
命名空间的路径,查询https://gitlab.com/api/v4/groups(只是为了确保根命名空间是正确的)。 - 我使用
npm init --scope=@<my-group> --yes
进行初始化。
仓库的URL确实是:https://gitlab.com/<my-group>/<my-project>/
对于让这个工作的任何帮助都将不胜感激。
英文:
I have a private dummy project in gitlab which I want to publish to gitlab's package registry. My dummy project contains four files:
package.json
{
"name": "@<my-group>/<my-project>",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://gitlab.com/<my-group>/<my-project>.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://gitlab.com/<my-group>/<my-project>/issues"
},
"homepage": "https://gitlab.com/<my-group>/<my-project>#readme"
}
.gitlab-ci.yml
image: node:latest
stages:
- deploy
deploy:
stage: deploy
script:
- echo "@<my-group>:registry=https://${CI_SERVER_HOST}/api/v4/projects/${CI_PROJECT_ID}/packages/npm/">.npmrc
- echo "//${CI_SERVER_HOST}/api/v4/projects/${CI_PROJECT_ID}/packages/npm/:_authToken=${CI_JOB_TOKEN}">>.npmrc
- npm publish
index.js
console.log('success');
README.md
<my-project>
When I commit my project to gitlab, the job fails with the following output
npm notice package: @<my-group>/<my-project>@1.0.0
npm notice === Tarball Contents ===
npm notice 8B README.md
npm notice 20B index.js
npm notice 612B package.json
npm notice === Tarball Details ===
npm notice name: @<my-group>/<my-project>
npm notice version: 1.0.0
npm notice filename: <my-group>-<my-project>-1.0.0.tgz
npm notice package size: 475 B
npm notice unpacked size: 640 B
npm notice shasum: 7b3db...
npm notice integrity: sha512-xDv0dl9A86...
npm notice total files: 3
npm notice
npm notice Publishing to https://gitlab.com/api/v4/projects/<my-project-id>/packages/npm/ with tag latest and default access
npm ERR! code E403
npm ERR! 403 403 Forbidden - PUT https://gitlab.com/api/v4/projects/<my-project-id>/packages/npm/@<my-group>%2f<my-project> - insufficient_scope
npm ERR! 403 In most cases, you or one of your dependencies are requesting
npm ERR! 403 a package version that is forbidden by your security policy, or
npm ERR! 403 on a server you do not have access to.
NOTE:
I have replaced the actual group name, project name and project id with <my-group>
, <my-project>
, and <my-project-id>
in the code sections above.
I have followed gitlab's official documentation on setting this up (see https://docs.gitlab.com/ee/user/packages/npm_registry/) and believe that I can safely rule out the following:
- I have made sure that
Package registry
is enabled in the project setup - I followed naming convention as described in the documentation
- I am using a
CI_JOB_TOKEN
which should always be valid and should have appropriate permissions. - I made sure that there is no other package with the same name or version within the given scope.
- I have made sure that the scoped package's URL includes a trailing slash (see gitlab-ci.yml above)
- I have confirmed the path of the
<my-group>
namespace querying https://gitlab.com/api/v4/groups (just to make sure that the root namespace is correct) - I have used
npm init --scope=@<my-group> --yes
for initialization
The url of the repository is indeed: https://gitlab.com/<my-group>/<my-project>/
Any help on getting this to work would be much appreciated.
答案1
得分: 1
更改从作业令牌${CI_JOB_TOKEN}
到部署令牌${CI_DEPLOY_PASSWORD}
后,我能够发布到注册表。
有关预定义变量的更多信息,请参阅预定义变量参考。
Gitlab部署令牌可以在设置 > 仓库 > 部署令牌下为项目创建,具有以下范围:read_repository、read_package_registry、write_package_registry
英文:
After changing from a job token ${CI_JOB_TOKEN}
to a deploy token ${CI_DEPLOY_PASSWORD}
, I was able to publish to the registry.
See Predefined variables reference for more info on predefined variables.
Gitlab deploy tokens can be created for projects under Settings > Repository > Deploy tokens with the following scopes: read_repository, read_package_registry, write_package_registry
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论