我想克隆GitLab仓库没有提示我的自动化脚本,通过使用我的私人令牌从我的GitLab帐户。

有人能给我一个样品吗?

我知道我可以这样做的用户和密码:

git clone https://" + user + ":" + password + "@" + gitlaburl;

我知道这是可能的ssh密钥

但是,这两种选择都不够。


当前回答

不需要定制URL。只需为gitlab令牌使用git配置,例如

git config --global gitlab.accesstoken {TOKEN_VALUE}

此处扩展描述

其他回答

使用令牌而不是密码(令牌需要有“api”范围才能允许克隆):

git clone https://username:token@gitlab.com/user/repo.git

在11.0.0-ee上测试。

如果你已经有一个存储库,只是改变了你做MFA认证的方式,你可以改变你的远程原始HTTP URI来使用你的新api令牌,如下所示:

git remote set-url origin https://oauth2:TOKEN@ANY_GIT_PROVIDER_DOMAIN/YOUR_PROJECT/YOUR_REPO.git

而且您根本不需要重新克隆存储库。

在GitLab CI管道中,CI_JOB_TOKEN环境变量为我工作:

git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.com/...

来源:Gitlab Docs

顺便说一下,在.gitlab-ci中设置这个变量。Yml有助于调试错误。

variables:
    CI_DEBUG_TRACE: "true"

gitlab有很多令牌:

私人的令牌 个人访问令牌 CI/CD运行令牌

我只测试了个人访问令牌使用GitLab社区版10.1.2,示例:

git clone https://gitlab-ci-token:${Personal Access Tokens}@gitlab.com/username/myrepo.git


git clone https://oauth2:${Personal Access Tokens}@gitlab.com/username/myrepo.git

或使用用户名和密码:

git clone https://${username}:${password}@gitlab.com/username/myrepo.git

或输入您的密码:

git clone https://${username}@gitlab.com/username/myrepo.git

但是私有令牌似乎不能工作。

您可以更改您的:

user:oauth2
password : <youraccesstoken>

例子:

git clone https://oauth2:<token>@hahahehe.com/yourname/yourproject.git