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

有人能给我一个样品吗?

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

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

我知道这是可能的ssh密钥

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


当前回答

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

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

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

其他回答

现在(2020年10月),你可以只使用以下内容

git clone $CI_REPOSITORY_URL

它将扩展为如下内容:

git clone https://gitlab-ci-token:[MASKED]@gitlab.com/gitlab-examples/ci-debug-trace.git

其中“令牌”密码是临时令牌(它将在构建完成后自动撤销)。

一种可能的方法是使用部署令牌 (https://docs.gitlab.com/ee/user/project/deploy_tokens)。创建令牌后,使用:

git clone https://<username>:<deploy_token>@gitlab.example.com/tanuki/awesome_project.git 

如上面的链接所述。

你可以这样做:

git clone https://gitlab-ci-token:<private token>@git.example.com/myuser/myrepo.git

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

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

在本例中,我只提供了令牌而不是密码(第二个输入字段)。

我第一次从命令行推送本地回购。

从头开始,这些是我输入的命令(记得先移动到回购文件夹内)。

$ git init

$ git status

$ git add .

$ git status

$ git commit -m 'Shinra Tensei.'

$ git push --set-upstream https://gitlab.com/userName/my-repo.git master

然后,您可以在图片中看到的弹出消息出现。提供用户名和令牌。