【问题标题】:remote: HTTP Basic: Access denied downloading local repo in gitlab远程:HTTP 基本:访问被拒绝在 gitlab 中下载本地存储库
【发布时间】:2022-01-23 14:34:49
【问题描述】:

关于这个话题有很多讨论,我读了很多,但我不知道我做错了什么。

Gitlab 版本 14.5.2

Gitlab 运行器版本:14.5.1 并作为 shell 运行

2FA 已启用,我已创建访问令牌;我正在尝试编译一个使用我的 gitlab 存储库中的库的 Golang 程序。这是我的 yml 文件

variables:
  REPOSITORY: $CI_REGISTRY/acme/test/master

before_script:
  - export PATH=$PATH:/usr/local/go/bin
  - docker login -u $CI_REGISTRY_USER -p $CI_JOB_TOKEN $CI_REGISTRY
  - go env -w GOOS=linux
  - go env -w GOARCH=amd64
  - go env -w GOPRIVATE=gitlab.acme.com

build_image:
  script: 
    - ssh-keyscan -t rsa gitlab.acme.com >> ~/.ssh/known_hosts
    - echo -e "machine gitlab.acme.com\nlogin gitlab-ci-token\npassword ${CI_JOB_TOKEN}" > ~/.netrc
    - git config --global url."https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.acme.com/".insteadOf git://gitlab.acme.com/
    - go mod download
    - go build
    - docker build -f Dockerfile -t $REPOSITORY:latest .
    - docker push $REPOSITORY:latest
    - docker rmi $(docker images $REPOSITORY -a -q)
    - rm $HOME/.netrc

结果是这样的:

go mod download: gitlab.acme.com/datamanent/go-commons@v0.0.0-20211221151250-f0220d428461: invalid version: git fetch -f origin refs/heads/*:refs/heads/* refs/tags/*:refs/tags/* in /home/gitlab-runner/go/pkg/mod/cache/vcs/c9ecbc2c20382f733e0a04c852c63cb9a78c5166f9ae2d25864a2d7728490ddb: exit status 128:
    remote: HTTP Basic: Access denied
    fatal: Authentication failed for 'https://gitlab.acme.com/test/go-commons.git/'
Cleaning up project directory and file based variables

如果我不使用内部库,编译就可以了,并且在 gitlab 注册表中推送也可以。 如果我尝试克隆 repo 而不是 go mod download,请执行以下操作:

- git clone git@gitlab.acme.com:test/go-commons.git

当然不行,我收到了这条消息:

cloning into 'go-commons'...
Permission denied, please try again.
Permission denied, please try again.
git@gitlab.acme.com: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Cleaning up project directory and file based variables

--------------- 更新 ---------------

感谢@VonC,我将 git 指令更改为

git config --global url."https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.acme.com/".insteadOf \
                    git@gitlab.acme.com:

不幸的是这仍然不够,而且真的很奇怪,所以我添加到管道cat $HOME/.gitconfig 我想看看它是否正确添加了指令。我看到的是有很多条目,很可能每次我尝试管道时,愚蠢的我,我以为每次运行 CI 时文件都会消失(但我在 shell 中而不是在 docker 中),所以我删除它现在可以工作了。

【问题讨论】:

  • 您是否考虑过使用基于 SSH 密钥的身份验证?或者这不适用于 gitlab 的 2fa?
  • 好收获,干得好。

标签: git go gitlab gitlab-ci


【解决方案1】:

在您的测试中,您尝试使用 SSH URL git@gitlab.acme.com:... 进行克隆,但没有成功。
将其替换为带有凭据(包括令牌,以通过 2FA)的 HTTPS 将是有意义的。

但是在您的 git config 中,您替换了 Git URL git://gitlab.acme.com/不是 SSH URL)。

首先尝试显示 $REPOSITORY,以仔细检查它是 SSH 还是 Git URL。
因为如果它是 SSH 的,您将需要一个 InsteadOf 指令,例如:

git config --global url."https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.acme.com/".insteadOf \
                    git@gitlab.acme.com:

【讨论】:

  • 感谢@VonC 我按照您的建议更改了指令,并尝试简单地克隆存储库。我得到的是:致命:无法从重定向更新 url 库....重定向:gitlab.acme.com/users/sign_in 在我看来这是一个身份验证问题,它似乎不喜欢令牌?或者我错过了 gitlab 端的一些配置
  • 我刚刚在管道中尝试过它并且它有效: git clone gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.acme.com/test/go-commons 所以看来我做错了什么git 重定向指令
  • @NiBE 所以你需要调整你的替代指令来模拟有效的 URL
猜你喜欢
  • 2019-01-28
  • 2022-07-15
  • 2012-10-17
  • 2023-01-31
  • 2020-12-13
  • 1970-01-01
  • 1970-01-01
  • 2018-03-11
  • 1970-01-01
相关资源
最近更新 更多