极狐GitLab 是一个一体化的 DevOps 平台,用户可以私有化部署安装之后进行使用。为了方便用户使用极狐GitLab,官方开发了一个命令行工具——glab
来对极狐GitLab 进行操作,此命令能够简化极狐GitLab 的某些操作,比如 issue、MR 的处理等。下面演示 glab
的安装和使用。
glab
的安装
不同的操作系统有不同的安装方式,比如在 Ubuntu 22.04 上面可以使用如下命令进行安装:
curl -sSL "https://raw.githubusercontent.com/upciti/wakemeops/main/assets/install_repository" | sudo bash
apt install glab
安装成功,可以用 version
命令进行查看:
glab version
glab version 1.37.0
用 help
来查看 glab
的所有用法:
glab help
GLab is an open source GitLab CLI tool that brings GitLab to your command line.
USAGE
glab <command> <subcommand> [flags]
CORE COMMANDS
alias: Create, list and delete aliases
api: Make an authenticated request to GitLab API
ask: Generate terminal commands from natural language. (Experimental.)
auth: Manage glab's authentication state
changelog: Interact with the changelog API
check-update: Check for latest glab releases
ci: Work with GitLab CI/CD pipelines and jobs
cluster: Manage GitLab Agents for Kubernetes and their clusters
completion: Generate shell completion scripts
config: Set and get glab settings
help: Help about any command
incident: Work with GitLab incidents
issue: Work with GitLab issues
label: Manage labels on remote
mr: Create, view and manage merge requests
release: Manage GitLab releases
repo: Work with GitLab repositories and projects
schedule: Work with GitLab CI schedules
snippet: Create, view and manage snippets
ssh-key: Manage SSH keys registered with your GitLab account.
user: Interact with user
variable: Manage GitLab Project and Group Variables
version: Show glab version information
FLAGS
--help Show help for command
-v, --version show glab version information
ENVIRONMENT VARIABLES
GITLAB_TOKEN: An authentication token for API requests. Set this variable to
avoid prompts to authenticate. Overrides any previously-stored credentials.
Can be set in the config with 'glab config set token xxxxxx'.
GITLAB_HOST or GL_HOST: Specify the URL of the GitLab server if self-managed.
(Example: https://gitlab.example.com) Defaults to https://gitlab.com.
REMOTE_ALIAS or GIT_REMOTE_URL_VAR: A 'git remote' variable or alias that contains
the GitLab URL. Can be set in the config with 'glab config set remote_alias origin'.
VISUAL, EDITOR (in order of precedence): The editor tool to use for authoring text.
Can be set in the config with 'glab config set editor vim'.
BROWSER: The web browser to use for opening links.
Can be set in the config with 'glab config set browser mybrowser'.
GLAMOUR_STYLE: The environment variable to set your desired Markdown renderer style.
Available options: dark, light, notty. To set a custom style, read
https://github.com/charmbracelet/glamour#styles
NO_PROMPT: Set to 1 (true) or 0 (false) to disable or enable prompts.
NO_COLOR: Set to any value to avoid printing ANSI escape sequences for color output.
FORCE_HYPERLINKS: Set to 1 to force hyperlinks in output, even when not outputting to a TTY.
GLAB_CONFIG_DIR: Set to a directory path to override the global configuration location.
LEARN MORE
Use 'glab <command> <subcommand> --help' for more information about a command.
FEEDBACK
Encountered a bug or want to suggest a feature?
Open an issue using 'glab issue create -R gitlab-org/cli'
glab
在使用之前,需要先认证,使用 glab auth login
登录验证:
# glab auth login
? What GitLab instance do you want to log into? GitLab Self-hosted Instance
? GitLab hostname: jihulab.com # 输入实例地址
? API hostname: jihulab.com
- Logging into jihulab.com
? How would you like to login? Token
Tip: you can generate a Personal Access Token here https://jihulab.com/-/profile/personal_access_tokens?scopes=api,write_repository
The minimum required scopes are 'api' and 'write_repository'.
? Paste your authentication token: ******************** # 输入个人访问令牌,这是重点
? Choose default git protocol SSH
? Choose host API protocol HTTPS
- glab config set -h jihulab.com git_protocol ssh
✓ Configured git protocol
- glab config set -h jihulab.com api_protocol https
✓ Configured API protocol
✓ Logged in as majinghe
登录之后,使用 glab auth status
查看认证状态:
$ glab auth status
jihulab.com
✓ Logged in to jihulab.com as majinghe (/root/.config/glab-cli/config.yml)
✓ Git operations for jihulab.com configured to use ssh protocol.
✓ API calls for jihulab.com are made over https protocol
✓ REST API Endpoint: https://jihulab.com/api/v4/
✓ GraphQL Endpoint: https://jihulab.com/api/graphql/
✓ Token: **************************
接着就可以执行其他命令了。
glab repo
glab repo
可以对代码仓库进行管理操作。比如 clone
一个代码仓库,可以执行如下命令:
$ glab repo clone jh-xiaomage-devops/terraform
Cloning into 'terraform'...
remote: Enumerating objects: 58, done.
remote: Counting objects: 100% (30/30), done.
remote: Compressing objects: 100% (30/30), done.
remote: Total 58 (delta 16), reused 0 (delta 0), pack-reused 28
Receiving objects: 100% (58/58), 9.51 KiB | 2.38 MiB/s, done.
用下面的命令创建一个新的极狐GitLab 仓库:
$ glab repo create jh-xiaomage-devops/glab-demo
✓ Created repository jh-xiaomage-devops / glab-demo on GitLab: https://jihulab.com/jh-xiaomage-devops/glab-demo
? Create a local project directory for jh-xiaomage-devops / glab-demo? Yes
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint: git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint: git branch -m <name>
Initialized empty Git repository in /home/xiaomage/glab/terraform/glab-demo/.git/
✓ Initialized repository in './glab-demo/'
在极狐GitLab 实例页面上就可以看到创建好的项目了:
使用如下命令删除仓库:
$ glab repo delete jh-xiaomage-devops/glab-demo
This action will permanently delete jh-xiaomage-devops/glab-demo immediately, including its repositories and all content: issues, merge requests.
? Are you ABSOLUTELY SURE you wish to delete jh-xiaomage-devops/glab-demo Yes
- Deleting project jh-xiaomage-devops/glab-demo
glab issue
glab issue
主要用来处理极狐GitLab issue。比如选定一个极狐GitLab 项目。使用如下命令,查看该项目上的 issue:
$ glab issue list -R jh-xiaomage-devops/terraform
GET https://gitlab.com/api/v4/projects/jh-xiaomage-devops/terraform/issues: 404 {message: 404 Project Not Found}
结果显示 404
也就是为找到项目。原因是 glab
如果不在某个具体的仓库目录下执行,就会默认去 gitlab.com 上找对应的项目,而演示的代码仓库是在极狐GitLab SaaS jihulab.com 上的。所以,这时候需要做一个设置,让 glab
不要读取默认 host 的信息,而是使用 jihulab.com:
glab config set -g host jihulab.com
再次执行上次的命令就可以了:
glab issue list -R jh-xiaomage-devops/terraform
Showing 1 open issue in jh-xiaomage-devops/terraform that match your search (Page 1)
#1 jh-xiaomage-devops/terraform#1 钉钉群添加极狐GitLab机器人测试 about 8 days ago
可以看到,列举出来了该项目上的一个 issue。当然,还可以创建新的 issue。
glab issue create -a majinghe -c -l bug
? Choose a template Open a blank Issue
? Title jihu gitlab cli demo show
? Description <Received>
? What's next? Add metadata
? Which metadata types to add?
? What's next? Submit
- Creating issue in jh-xiaomage-devops/terraform
#2 jihu gitlab cli demo show (less than a minute ago)
https://jihulab.com/jh-xiaomage-devops/terraform/-/issues/2
根据每一步输出的信息,填写对应的信息,诸如议题主题、描述、标签等,即可完成创建。这些步骤和在 UI 上创建 issue 是一样的。
创建完毕即可在 UI 界面上看到对应的 issue 了:
当然,还可以使用 glab issue view
命令查看某个 issue 的详情:
$ glab issue view 1
open • opened by majinghe about 9 days ago
钉钉群添加极狐GitLab机器人测试 #1
test。
0 upvotes • 0 downvotes • 0 comments
Assignees: majinghe
View this issue on GitLab: https://jihulab.com/jh-xiaomage-devops/terraform/-/issues/1
glab mr
glab mr
用于对 MR 进行管理(创建、删除等)。
使用下面的命令列举当初当前的 MR:
$ glab mr list
No open merge requests available on jh-xiaomage-devops/terraform
使用下面的命令创建一个 MR:
$ glab mr create -a majinghe -t "change disk size in terraform file"
? Choose a template Open a merge request with commit messages
? Description <Received>
? What's next? Add metadata
? Which metadata types to add?
? What's next? Submit
Creating merge request for glab-demo into main in jh-xiaomage-devops/terraform
!1 change disk size in terraform file (glab-demo)
https://jihulab.com/jh-xiaomage-devops/terraform/-/merge_requests/1
在用 glab mr view
查看此 MR 的详情:
$ glab mr view 1
open • opened by majinghe less than a minute ago
change disk size in terraform file !1
• chang disk size
0 upvotes • 0 downvotes • 0 comments
Assignees: majinghe
Approvals Status:
✓ This merge request has changes
x This branch has conflicts that must be resolved
View this merge request on GitLab: https://jihulab.com/jh-xiaomage-devops/terraform/-/merge_requests/1
使用如下的命令关闭 MR:
$ glab mr close 1
- Closing Merge request...
✓ Closed Merge request !1
!1 change disk size in terraform file (glab-demo)
https://jihulab.com/jh-xiaomage-devops/terraform/-/merge_requests/1
glab
还有其他很多用法,可以参考官方文档进行学习使用。