【发布时间】:2013-11-04 07:09:44
【问题描述】:
我们的办公室有一个gitlab 设置,我们每周要在那里创建大约 100-150 个项目,而管理员希望保持对创建 repos 和分配团队的控制权,这似乎相当任何人每周创建这么多回购的任务。
有没有办法create repo on Gitlab using CLI - 我不介意我必须使用ssh。
【问题讨论】:
我们的办公室有一个gitlab 设置,我们每周要在那里创建大约 100-150 个项目,而管理员希望保持对创建 repos 和分配团队的控制权,这似乎相当任何人每周创建这么多回购的任务。
有没有办法create repo on Gitlab using CLI - 我不介意我必须使用ssh。
【问题讨论】:
与ChillarAnand 和eigenfield 之前的答案相比,虽然这个答案也使用了curl 的REST API,但它也:
curl 在出现错误时以非零代码退出(通过-f)path 参数代替name 参数,从而避免使用不同路径的风险首先,obtain a token 可以访问 api 范围。
REPO_NAME=foo1
GITLAB_TOKEN=xxxxxxxxxxxxxxxxxxxx # Enter your own.
curl -f -X POST \
-H "PRIVATE-TOKEN: ${GITLAB_TOKEN}" -H "Content-Type:application/json" \
"https://gitlab.com/api/v4/projects" -d "{\"path\": \"${REPO}\", \"visibility\": \"private\"}"
此答案仅与将存储库创建为 user 相关。将存储库创建为 admin 的请求不同。
顺便说一句,显式创建 repo 是可选的,因为已知 GitLab 能够创建 repo on first push。 (图片来源:Elan R.)
【讨论】:
现在 gitlab 支持通过提供 URL 来创建新的 repo。如果您的 gitlab 用户名是 shahidcodes,那么您只需执行以下步骤 -
git init # init a repo if you don't have already
git remote add origin https://gitlab.com./<your_username>/<new_repo_name>
git push -u origin master
你会看到以下来自 git 输出的消息
remote: The private project shahidcodes/new_repo_name was successfully created.
remote:
remote: To configure the remote, run:
remote: git remote add origin https://gitlab.com/shahidcodes/new_repo_name.git
remote:
remote: To view the project, visit:
remote: https://gitlab.com/shahidcodes/new_repo_name
remote:
remote:
remote:
To https://gitlab.com/shahidcodes/new_repo_name
* [new branch] master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
默认情况下,GitLab 将创建一个私有仓库。而且似乎没有办法将其配置为创建公共。
【讨论】:
由于您只是想创建一个存储库,因此不需要第三方应用程序。您可以直接向 gitlab API 发送 post 请求,该 API 将创建 repo。
转到您的个人资料中的account tab,您将找到一个私人令牌。复制那个。
现在打开终端并使用私有令牌(例如 foo)和您的存储库名称(例如 bar)运行此命令。
curl -H "Content-Type:application/json" https://gitlab.com/api/v4/projects?private_token=foo -d "{ \"name\": \"bar\" }"
为方便起见,如果您不想每次都运行此命令,可以创建一个 shell 脚本。
#!/bin/sh
curl -H "Content-Type:application/json" https://gitlab.com/api/v4/projects?private_token=foo -d "{ \"name\": \"$1\" }"
将此保存到文件gcr.sh 并使用chmod +x gcr.sh 使其可执行。
现在创建一个 repo 名称bar,运行
$ ./gcr.sh bar
【讨论】:
易于使用:
此答案与 2019 年 8 月相关。将来,其数据可能会过时。
lab — CLI 工具,使 GitLab 存储库的一些操作变得简单。 lab 相当于 Gitlab 的 hub GitHub 扩展。
首次运行实验室后,将提供输入令牌。 Create a personal access token 具有所需范围 api → 将其粘贴到终端 → Enter。
然后运行lab project create:
lab project create -n KiraLab --public -d "Kira lab demo project"
选项可用:
--public — 将存储库设为公开,而不是私有-d, --description — 创建描述用于 GitLab 存储库操作的跨平台 Go 编写的命令行实用程序。
Create your GitLab personal access token → gitlab-cli login YOUR_TOKEN → 运行gitlab-cli project create 命令:
gitlab-cli project create KiraGitLabCLI
请不要混淆这个 Go 项目和来自 @thameera answer 的 Ruby gitlab-cli 工具。
【讨论】:
这是我的 ~/.bashrc 中的内容
gitlify() {
[ $# -eq 0 ] && return 1
repo_name=$1
username=smeagol
token=01234567890
curl -H "Content-Type:application/json" https://gitlab.com/api/v4/projects?private_token=$token -d "{\"name\": \"$repo_name\"}"
if [ $? -eq 0 ];then
git init
git add .
git commit -m "first blood"
git remote add origin git@gitlab.com:$username/$repo_name.git
git push -u origin master
else
echo "error create gitlab repo $repo_name"
fi
}
您必须首先为您的用户名设置一个令牌。放置此 bash 函数后,您可以通过以下方式使用它:
mkdir /tmp/firstblood
echo '#hello world' > /tmp/firstblood/README.md
cd /tmp/firstblood
gitlify fallenangel
这个 sn-p 仅适用于 gitlab.com。我还有一个我为 github.com 命名的 gitify。
【讨论】:
curl 真的会自动失败并返回非零退出代码吗?我不相信它会。我是说即使请求失败,它的退出代码也是 dfault 0。除非你使用-f。
gitlab-cli 不再维护,作者引用了Gitlab 模块来代替 - 它还包括一个 CLI 工具。
对于您的特定要求 - 即在命令行上创建项目,请使用以下命令:
gitlab create_project "YOUR_PROJECT_NAME" "{namespace_id: 'YOUR_NUMERIC_GROUP_ID'}"
请务必使用选项namespace_id 而不是group_id!如果你不确定你的GROUP_ID 是什么,你可以使用
gitlab groups | grep YOUR_GROUP_NAME
找出答案。
可以从API documentation 推断出每个命令的参数。任何非标量值参数都必须以内联 YAML 语法编码(如上)。
【讨论】:
gitlab create_project "{visibility_level: 10}" $GIT_REPO_NAME 这样的 visibility_level。
您可以使用 gitlab-cli 并使用 shell 脚本自动执行该过程。我在 gitlab 5.x 中使用过它,但根据网站的说法,它可能不适用于 gitlab 6。
【讨论】: