使用gitlab令牌进行克隆而无需身份验证

Muk*_*uky 111 gitlab

我想通过使用我的gitlab帐户中的私有令牌来克隆gitlab存储库而不提示我的自动化脚本.

有人能为我提供样品吗?

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

git clone https://" + user + ":" + password + "@" + gitlaburl;
Run Code Online (Sandbox Code Playgroud)

我知道用ssh键是可能的

但是,这两种选择都不够

Ros*_*tam 144

我知道这是旧的,但这是你如何做到的:

git clone https://oauth2:ACCESS_TOKEN@somegitlab.com/vendor/package.git

  • 如何在`ssh`上使用它? (7认同)
  • 有用!我想知道为什么在gitlab.com上关于项目细节他们没有提供完整的命令语法: - (( (4认同)
  • `oauth2` 部分可能是任意的。我改为输入访问令牌的名称。 (4认同)
  • 即使是“read_api”和“write_api”权限也是不够的。你必须有“api”。 (4认同)
  • 这对我来说在GitLab 8.5.7企业版上有用. (3认同)

Tim*_*hes 36

你可以这样做:

git clone https://gitlab-ci-token:<private token>@git.example.com/myuser/myrepo.git
Run Code Online (Sandbox Code Playgroud)

  • <private token>需要替换为CI runner的令牌,而不是帐户的私有令牌. (4认同)
  • 这似乎是正确的,但它始终无法验证我:( (2认同)
  • 我认为你也应该能够使用你的个人令牌@tim (2认同)

xua*_*eng 28

gitlab有很多令牌:

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

我仅使用GitLab Community Edition 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
Run Code Online (Sandbox Code Playgroud)

或使用用户名和密码:

git clone https://${username}:${password}@gitlab.com/username/myrepo.git
Run Code Online (Sandbox Code Playgroud)

或输入您的密码:

git clone https://${username}@gitlab.com/username/myrepo.git
Run Code Online (Sandbox Code Playgroud)

私人令牌似乎无法奏效.

  • 请注意,在GitLab 10.2中删除了私人令牌以支持个人访问令牌:https://about.gitlab.com/2017/09/22/gitlab-10-0-released/#private-tokens (3认同)
  • `gitlab-ci-token`、`oauth2` 和 `x-access-token` 有什么区别?这三个都对我有用。 (3认同)

Zby*_*ler 20

使用令牌而不是密码(令牌需要具有允许克隆的"api"范围):

git clone https://username:token@gitlab.com/user/repo.git
Run Code Online (Sandbox Code Playgroud)

测试11.0.0-ee.

  • 对于通过 Google 搜索的人:如果在 gitlab.com 上通过 HTTPS 使用个人访问令牌,这就是您想要的。 (2认同)
  • 我们必须在这里找到它,而不是在有关个人访问令牌的 GitLab 文档页面上,这不是很奇怪吗? (2认同)

小智 14

您可以使用GitLab存储库的CI/CD管道的运行程序标记.

git clone https://gitlab-ci-token:<runners token>@git.example.com/myuser/myrepo.git
Run Code Online (Sandbox Code Playgroud)

哪里<runners token>可以从:

git.example.com/myuser/myrepo/pipelines/settings
Run Code Online (Sandbox Code Playgroud)

或者单击Settings icon -> CI/CD Pipeline并在页面上查找Runners Token

跑者令牌位置的屏幕截图: 跑步者令牌位置的屏幕截图

  • 注意:现在已弃用runners令牌. (5认同)
  • @miq见[here](https://docs.gitlab.com/ce/user/project/new_ci_build_permissions_model.html)(截至> = 8.12) (2认同)
  • 无法在 GitLab CI 管道内部工作。但这行代码有效: `git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.com/...` (2认同)

Rog*_*eto 10

如果您已经有一个存储库,并且只是更改了对MFA进行身份验证的方式,则可以更改remote originHTTP URI以使用新的api令牌,如下所示:

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

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

  • `git clone https:// oauth2:TOKEN @ ANY_GIT_PROVIDER_DOMAIN / YOUR_PROJECT / YOUR_REPO.git`也为我工作,谢谢!!我将使用正确的解决方案来回答该线程。 (3认同)

Yan*_*oto 9

由于8.12使用克隆HTTPS如前所述+令牌不再被支持,在这里:

在8.12中,我们改进了构建权限.能够使用跑步者令牌克隆项目从现在开始不支持它(它实际上是巧合而且从来不是一个完全成熟的功能,所以我们在8.12中改变了它).您应该使用构建令牌.

这在此广泛记录 - https://docs.gitlab.com/ce/user/project/new_ci_build_permissions_model.html.

  • *自动化脚本*意味着整个过程不在本地运行。可能其他人也可以访问的某个地方。 (2认同)

Mou*_*Ash 8

这些天(2020 年 10 月)您可以使用以下内容

git clone $CI_REPOSITORY_URL
Run Code Online (Sandbox Code Playgroud)

这将扩展到类似:

git clone https://gitlab-ci-token:[MASKED]@gitlab.com/gitlab-examples/ci-debug-trace.git
Run Code Online (Sandbox Code Playgroud)

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


Sla*_*awa 7

在GitLab CI管道中,CI_JOB_TOKEN环境变量对我有用:

git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.com/...
Run Code Online (Sandbox Code Playgroud)

资料来源:Gitlab文件

顺便说一句,在中设置此变量.gitlab-ci.yml有助于调试错误。

variables:
    CI_DEBUG_TRACE: "true"
Run Code Online (Sandbox Code Playgroud)


sha*_*ite 6

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

git clone https://<username>:<deploy_token>@gitlab.example.com/tanuki/awesome_project.git 
Run Code Online (Sandbox Code Playgroud)

如上面链接中所述。


mik*_*ike 6

上面的许多答案都很接近,但是他们得到usernamedeploy不正确的令牌语法。还有其他类型的令牌,但这deploy token是 gitlab 提供的(至少大约 2020 年以上)每个 repo以允许自定义访问,包括只读。

repository(或group),找到settings--> repository--> deploy tokens。创建一个新的。Ausernametoken字段已创建。在username默认情况下不固定值; 它是这个令牌的独特之处。

git clone https://<your_deploy_token_username>:<the_token>@gitlab.com/your/repo/path.git

在 gitlab.com 公共、免费帐户上测试。


Mar*_*oma 5

为了让我的未来快乐:RTFM - 根本不使用 gitlab-ci-token,而是使用文件.netrc

有几个要点:

  1. echo -e "machine gitlab.com\nlogin gitlab-ci-token\npassword ${CI_JOB_TOKEN}" > ~/.netrc
  2. 不要忘记用您的 URL 替换“gitlab.com”!
  3. 不要试图聪明地直接创建 .netrc 文件 - gitlab 不会替换$CI_JOB_TOKEN文件中的!
  4. 使用https://gitlab.com/whatever/foobar.com-不ssh://git@foobar,不git+ssh://,不git+https://。您也不需要 URL 中包含任何 CI-TOKEN 内容。
  5. 确保你可以git clone [url from step 4]

背景:我得到了

fatal: could not read Username for 'https://gitlab.mycompany.com': No such device or address
Run Code Online (Sandbox Code Playgroud)

当我试图让 Ansible + Gitlab + Docker 按照我的想象工作时。现在可以了。