从GitLab仓库中在GitLab流水线运行时下载单个文件。

huangapple go评论49阅读模式
英文:

Curl single file from gitlab repo during gitlab pipeline run

问题

我在仓库A中有一个流水线,正在尝试通过curl从另一个仓库(仓库B)检索文件,这两个仓库都位于我的自托管Gitlab实例上。

仓库A中的流水线只是尝试运行此命令:

curl -H "CI_JOB_TOKEN:$CI_JOB_TOKEN" "https://gitlab.myinstance.com/api/v4/projects/111/repository/files/credentials%2Fcredentials%2Etpl/raw?ref=main"

在流水线中运行时,我收到了错误消息 - { "message": "404 Project Not Found" }

使用私有令牌运行相同的命令却没有问题。

curl -H "PRIVATE-TOKEN:glpat-xxxxxxxx" "https://gitlab.myinstance.com/api/v4/projects/111/repository/files/credentials%2Fcredentials%2Etpl/raw?ref=main"

在仓库B中,我已经为仓库A授予了CI_JOB_TOKEN访问权限,但我仍然收到相同的错误消息。

我是否漏掉了什么?

英文:

I have a pipeline in repo A that is trying to retrieve a file from another repo (repo B) via curl. Both repos are on my self-hosted Gitlab instance.

The pipeline in repo A is simply trying to run this command:

curl -H "CI_JOB_TOKEN:$CI_JOB_TOKEN" "https://gitlab.myinstance.com/api/v4/projects/111/repository/files/credentials%2Fcredentials%2Etpl/raw?ref=main"

When ran in the pipeline I get the error - {"message":"404 Project Not Found"}

Running the same command but using a private token works without an issue.

curl -H "PRIVATE-TOKEN:glpat-xxxxxxxx" "https://gitlab.myinstance.com/api/v4/projects/111/repository/files/credentials%2Fcredentials%2Etpl/raw?ref=main"

In the repo B I have given repo A CI_JOB_TOKEN access but I still get the same error.

Is there something I am missing here?

答案1

得分: 1

内置作业令牌只能访问certain API endpoints。不幸的是,您无法使用此令牌访问存储库文件API。

一个解决方法是从包含您想要下载的文件的项目中生成项目访问令牌,然后像使用个人访问令牌一样使用它。或者,您可以配置项目以允许使用作业令牌从需要访问的特定项目中允许项目API访问

另一种无需令牌或项目设置更改的解决方法是克隆项目(默认情况下,CI_JOB_TOKEN允许您这样做),但显然您必须至少以深度为1克隆项目,而不仅仅是检索所需的单个文件。

英文:

Builtin job tokens only have access to certain API endpoints. Unfortunately, you cannot access the repository files API with this token.

A workaround is to generate a project access token from the project containing the file you want to download and use that just as you would use a personal access token. Alternatively, you can configure the project to allow project API access with a job token from the specific project(s) that need access.

Another workaround that doesn't require a token or project setting changes is to clone the project instead (which the CI_JOB_TOKEN does let you do by default), but obviously you will have to clone the project with a depth of at least 1 rather than only retrieving the single file needed.

答案2

得分: 0

这是一个已知问题:https://gitlab.com/gitlab-org/gitlab/-/issues/17511

英文:

It turns out this is a known issue:
https://gitlab.com/gitlab-org/gitlab/-/issues/17511

huangapple
  • 本文由 发表于 2023年5月24日 21:19:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76324004.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定