【发布时间】: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?
-
好收获,干得好。