我想克隆GitLab仓库没有提示我的自动化脚本,通过使用我的私人令牌从我的GitLab帐户。
有人能给我一个样品吗?
我知道我可以这样做的用户和密码:
git clone https://" + user + ":" + password + "@" + gitlaburl;
我知道这是可能的ssh密钥
但是,这两种选择都不够。
我想克隆GitLab仓库没有提示我的自动化脚本,通过使用我的私人令牌从我的GitLab帐户。
有人能给我一个样品吗?
我知道我可以这样做的用户和密码:
git clone https://" + user + ":" + password + "@" + gitlaburl;
我知道这是可能的ssh密钥
但是,这两种选择都不够。
当前回答
你可以这样做:
git clone https://gitlab-ci-token:<private token>@git.example.com/myuser/myrepo.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"
使用令牌而不是密码(令牌需要有“api”范围才能允许克隆):
git clone https://username:token@gitlab.com/user/repo.git
在11.0.0-ee上测试。
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
但是私有令牌似乎不能工作。
上面的许多答案都很接近,但是部署令牌的用户名语法不正确。还有其他类型的令牌,但部署令牌是gitlab为每个回购提供的(至少在2020年左右),以允许自定义访问,包括只读。
从存储库(或组)中找到设置——>存储库——>部署令牌。创建一个新的。创建用户名和令牌字段。默认情况下,用户名不是固定值;它对这个令牌来说是唯一的。
git clone https://<your_deploy_token_username>:<the_token>@gitlab.com/your/repo/path.git
在gitlab.com公开测试,免费账号。
为了让我的未来我快乐:RTFM -不要使用gitlab-ci-令牌,而是使用.netrc文件。
这里有几个要点:
echo -e "machine gitlab.com\nlogin gitlab-ci-token\npassword ${CI_JOB_TOKEN}" > ~/.netrc . 不要忘记用你的URL替换“gitlab.com”! 不要试图聪明,直接创建.netrc文件- gitlab不会替换文件中的$CI_JOB_TOKEN ! 使用https://gitlab.com/whatever/foobar.com -不是ssh://git@foobar,不是git+ssh://,不是git+https://.URL中也不需要任何CI-TOKEN。 确保你可以git clone [url from step 4]
背景:我有
fatal: could not read Username for 'https://gitlab.mycompany.com': No such device or address
当我试着让Ansible + Gitlab + Docker像我想象的那样工作时。现在它起作用了。