使用命令行在Gitlab中创建仓库

29
我们在办公室安装了 Gitlab,并且每周大约有100-150个项目需要在那里创建,尽管管理员希望保持对创建仓库和分配团队的控制权,但对于任何人来说每周创建如此之多的仓库似乎是一个相当耗时的任务。
有没有一种使用 CLI 在 Gitlab 上创建仓库的方法?如果必须要使用 ssh 我也不介意。

1
只需将代码推送到存储库,它就会自动创建。 - Zaz
9个回答

19

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 文档 中推断出。任何非标量值参数都必须以内联 YAML 语法(如上所示)进行编码。


创建公共仓库时,您不需要namespace_id,但需要像这样的visibility_level gitlab create_project "{visibility_level: 10}" $GIT_REPO_NAME - MoOx

16

如果您只想创建一个repo,不需要第三方应用程序。您可以直接向gitlab API发送POST请求来创建repo。

进入帐户选项卡,在个人资料中找到私有标记。复制它。

现在打开终端并运行此命令,使用私有标记(比如foo)和您的repo名称(比如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 使其可执行。

现在要创建一个名为 bar 的仓库,请运行

$ ./gcr.sh bar

有没有办法使其在Gitlab的默认HTTP上正常工作? - Gerrat
@Gerrat 我只收到一个422 HTTP错误。 - Marco Roth

7
ChillarAnandeigenfield之前的答案相比,尽管这个答案也使用了带有curl的REST API,但它还具有以下特点:
  1. 通过在标头中提供令牌而不是在URL中进行授权
  2. 在出现错误时使curl以非零代码退出(通过-f
  3. 使用path参数而不是name参数,从而避免使用不同路径的风险
首先,获取一个具有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身份创建代码仓库的请求是不同的。

顺便提一下,明确地创建代码仓库是可选的,因为众所周知 GitLab 能够在第一次推送时创建代码仓库。 (致谢:Elan R.


5

现在 GitLab 支持通过 URL 创建新存储库。 如果您的 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会创建一个私有仓库,似乎没有办法进行配置以创建公共仓库。

1
这是我在 ~/.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

这段代码仅适用于gitlab.com。我还有另一个名为gitify的代码,适用于github.com。

1
自从GitLab 10.5以来,您可以省略curl命令,因为项目会在第一次推送时自动创建。 - glen
curl 真的会在仓库未创建时自动失败并返回非零退出码吗?我不相信它会这样做。我想说,即使请求失败,它的退出码默认也是0。除非你使用 -f 参数。 - Asclepius

1
你可以使用 gitlab-cli并使用shell脚本自动化该过程。我已在gitlab 5.x中使用过这个,但根据网站的说法,它可能无法与gitlab 6一起使用。

1
gitlab-cli已不再得到积极维护-请参阅作者的说明:“这可能无法与GitLab 6.x+兼容。此项目已不再得到维护。要使用完全功能且受支持的CLI工具,请查看http://narkoz.github.io/gitlab/”。 - Michael Lihs

1

1. 概述

易于使用:


2. 免责声明

本答案适用于2019年8月。将来,其数据可能已过时。


3. 实验室(推荐)

3.1. 关于

实验室是一个CLI工具,可以简化与GitLab存储库的一些操作。实验室是GitHub的hub扩展的Gitlab等价物。

3.2. 用法

第一次运行实验室后,它将提示您输入令牌。创建个人访问令牌并选择所需的范围api → 将其粘贴到终端中 → Enter

然后运行lab project create

lab project create -n KiraLab --public -d "Kira lab demo project"

3.3. 结果

lab

3.4. 为什么推荐

有以下选项可用:

  1. --public — 将存储库设为公共而非私有
  2. -d, --description — 创建描述

lab description


4. gitlab-cli

4.1. 关于

跨平台的Go编写的命令行实用程序,用于GitLab存储库操作。

4.2. 用法

创建您的GitLab个人访问令牌gitlab-cli login YOUR_TOKEN → 运行gitlab-cli project create 命令:

gitlab-cli project create KiraGitLabCLI

4.3. 结果

gitlab-cli

4.4. 注意

请不要将这个Go项目和Ruby gitlab-cli工具以及@thameera answer混淆。


5. 外部链接

  1. GitLab CLI客户端
  2. 如何从命令行创建GitHub存储库的说明(俄语)

0

请参考 glab 和命令 glab repo create

引用文档:

# create a repository under your account using the current directory name
$ glab repo create

# create a repository with a specific name
$ glab repo create my-project

0
export GITLAB_API_TOKEN=______ 添加到您的 .bashrc 或相关文件中,然后运行:
curl --request POST -H "PRIVATE-TOKEN: ${GITLAB_API_TOKEN}" \
  -H "Content-Type:application/json" \
  "https://gitlab.com/api/v4/projects" \
  -d "{ \"name\":\"test\", \"description\":\"Description.\" }"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接