今回はGitLabが提供する glab
というCLIツールを紹介します。
背景
glab
はコマンドラインからGitLabの各種APIにアクセスすることで、Webブラウザを開くことなくGitLabの情報を確認する手段を提供します。例えばIssueを確認してタスクを確認したり、git push後にパイプラインが実行されたかチェックしたり、といった作業を実現できます。
glabはGitLab SaaS版 / Self-managed版のどちらでも利用可能です。また複数のGitLabインスタンスへの認証もサポートしています。
glabでは幅広いコマンドを利用できますが、こちらのドキュメントでCore commandとしているのは以下の18個です。
glab alias
glab api
glab auth
glab check-update
glab ci
glab completion
glab config
glab incident
glab issue
glab label
glab mr
glab release
glab repo
glab schedule
glab snippet
glab ssh-key
glab user
glab variable
検証
ここから検証ですが、glabを操作する場合、GitLabにログインするためPersonal Access Tokenの発行が必要です。
今回は glab-test
というProjectを検証用に用意しました。また事前にIssueの登録と .gitlab-ci.yml
の配置を完了しています。
インストール
まずは glab をインストールします。インストール方法は複数用意されていますが、ここではバイナリを取得する方法にしました。
$ wget https://gitlab.com/gitlab-org/cli/-/releases/v1.36.0/downloads/glab_1.36.0_Linux_x86_64.tar.gz $ tar -zxvf glab_1.36.0_Linux_x86_64.tar.gz LICENSE README.md bin/glab $ sudo cp -p bin/glab /usr/local/bin/glab $ glab version glab version 1.36.0
インストールを完了したので、glabからGitLabにログインします。 glab auth login
コマンドを実行すると、対話形式でログイン処理を進められます。
#対話形式でのログイン #GitLab SaaSかSelf-managedか選択 $ glab auth login ? What GitLab instance do you want to log into? [Use arrows to move, type to filter] > gitlab.com GitLab Self-hosted Instance #ログインをTokenかWebか選択 $ glab auth login ? What GitLab instance do you want to log into? gitlab.com - Logging into gitlab.com ? How would you like to login? [Use arrows to move, type to filter] > Token Web #Tokenの場合はここで入力 $ glab auth login ? What GitLab instance do you want to log into? gitlab.com - Logging into gitlab.com ? How would you like to login? Token Tip: you can generate a Personal Access Token here https://gitlab.com/-/profile/personal_access_tokens?scopes=api,write_repository The minimum required scopes are 'api' and 'write_repository'. ? Paste your authentication token: #Gitプロトコルを選択 $ glab auth login ? What GitLab instance do you want to log into? gitlab.com - Logging into gitlab.com ? How would you like to login? Token Tip: you can generate a Personal Access Token here https://gitlab.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 [Use arrows to move, type to filter] SSH > HTTPS HTTP #GitLab credentialでのGit認証を許可 $ glab auth login ? What GitLab instance do you want to log into? gitlab.com - Logging into gitlab.com ? How would you like to login? Token Tip: you can generate a Personal Access Token here https://gitlab.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 HTTPS ? Authenticate Git with your GitLab credentials? (Y/n) #ログイン完了 $ glab auth login ? What GitLab instance do you want to log into? gitlab.com - Logging into gitlab.com ? How would you like to login? Token Tip: you can generate a Personal Access Token here https://gitlab.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 HTTPS ? Authenticate Git with your GitLab credentials? Yes - glab config set -h gitlab.com git_protocol https ✓ Configured git protocol - glab config set -h gitlab.com api_protocol https ✓ Configured API protocol ✓ Logged in as fy0323
GitLabにログインしたので、試しに glab repo list
コマンドを実行し、リポジトリの一覧を出力してみます。
$ glab repo list Showing 24 of 24 projects (Page 1 of 1) fy0323/glab-test git@gitlab.com:fy0323/glab-test.git (以降割愛)
glab repo view
コマンドを実行すると、各リポジトリのREADME.mdの内容が確認できます。
$ glab repo view glab-test Futa Yamaji / glab-test (No description provided) glab-test ## Getting started To make it easy for you to get started with GitLab, here's a list of recommended next steps. ~~~ View this project on GitLab: https://gitlab.com/fy0323/glab-test
Issueの操作
続いてIssueのチェックをしてみます。作成済みのIssueの一覧を確認するには glab issue list
コマンドを実行します。対象のIssueの詳細を見るには glab issue view
コマンドを使用します。
$ glab issue list -R fy0323/glab-test Showing 1 open issue in fy0323/glab-test that match your search (Page 1) #1 fy0323/glab-test#1 test issue about 6 minutes ago $ glab issue view 1 -R fy0323/glab-test open • opened by fy0323 about 6 minutes ago test issue #1 テスト用の文章 0 upvotes • 0 downvotes • 0 comments View this issue on GitLab: https://gitlab.com/fy0323/glab-test/-/issues/1
次に新しいIssueの作成を行います。 glab issue create
コマンドを実行すると、対話形式でIssueの作成を実施できます。
#templateがある場合は選択できるようですが、ここでは空のIssueのみ選択できます $ glab issue create ? Choose a template [Use arrows to move, type to filter] > Open a blank Issue #Issueのタイトルを入力 $ glab issue create ? Choose a template Open a blank Issue ? Title #Descriptionを追加 $ glab issue create ? Choose a template Open a blank Issue ? Title test from glab ? Description [(e) or Enter to launch nano, (s) or Esc to skip] #そのまま提出するか、ブラウザに移動して操作を続けるか、などを選択 $ glab issue create ? Choose a template Open a blank Issue ? Title test from glab ? Description <Received> ? What’s next? [Use arrows to move, type to filter] > Submit Continue in browser Add metadata Cancel #作成完了 $ glab issue create ? Choose a template Open a blank Issue ? Title test from glab ? Description <Received> ? What’s next? Submit - Creating issue in fy0323/glab-test #2 test from glab (less than a minute ago) https://gitlab.com/fy0323/glab-test/-/issues/2 #Issueの確認 $ glab issue list Showing 2 open issues in fy0323/glab-test that match your search (Page 1) #2 fy0323/glab-test#2 test from glab less than a minute ago #1 fy0323/glab-test#1 test issue about 39 minutes ago
Pipelineの操作
次にPipelineの操作を試します。
glabには glab ci lint
というコマンドがあり、これを使うと .gitlab-ci.yml
に対して構文チェックを実行できます。
$ glab ci lint Validating... ✓ CI/CD YAML is valid! #適当に修正してエラーを発生させる $ glab ci lint Validating... .gitlab-ci.yml is invalid 1 jobs:lint-test-job configcontains unknown keys: stages
glabはPipelineの実行も可能です。 glab ci run
コマンドでPipelineを起動後、 glab ci status
コマンドで進行状況をリアルタイムにチェックすることもできます。
$ glab ci run Created pipeline (id: 1163246103 ), status: created , ref: main , weburl: https://gitlab.com/fy0323/glab-test/-/pipelines/1163246103 )
$ glab ci status --live (created) • not started deploy deploy-job (created) • not started test lint-test-job (created) • not started test unit-test-job (running) • 00m 16s build build-job https://gitlab.com/fy0323/glab-test/-/pipelines/1163246103 SHA: 82784a89350e52a26c2458ffae998a04b69db4e7 Pipeline State: running #build Jobが成功 $ glab ci status --live (created) • not started deploy deploy-job (running) • 00m 09s test lint-test-job (running) • 00m 09s test unit-test-job (success) • 00m 26s build build-job https://gitlab.com/fy0323/glab-test/-/pipelines/1163246103 SHA: 82784a89350e52a26c2458ffae998a04b69db4e7 Pipeline State: running #全てのJobが成功し、Pipelineが終了 $ glab ci status --live (success) • 00m 27s deploy deploy-job (success) • 00m 37s test lint-test-job (success) • 01m 27s test unit-test-job (success) • 00m 27s deploy deploy-job (success) • 00m 37s test lint-test-job (success) • 01m 27s test unit-test-job (success) • 00m 26s build build-job https://gitlab.com/fy0323/glab-test/-/pipelines/1163246103 SHA: 82784a89350e52a26c2458ffae998a04b69db4e7 Pipeline State: success > Exit
終了後のPipeline情報も確認できます。
$ glab ci get # Pipeline: id: 1163246103 status: success source: api ref: main sha: 82784a89350e52a26c2458ffae998a04b69db4e7 tag: false yaml Errors: user: fy0323 created: 2024-02-03 09:04:58.737 +0000 UTC started: 2024-02-03 09:04:59.275 +0000 UTC updated: 2024-02-03 09:07:20.748 +0000 UTC # Jobs: deploy-job: success lint-test-job: success unit-test-job: success build-job: success
また glab ci view
コマンドを使うと、CUIのような形式でPipeline/Jobの実行結果、実行時間、Jobのログなどを確認できます。
$ glab ci view
╔════════════════════════════════════════════════ Pipeline #1163246103 triggered about 9 minutes ago by Futa Yamaji ═════════════════════════════════════════════════╗
║ ╔════✔ build-job═════╗ ┌──✔ unit-test-job───┐ ┌────✔ deploy-job────┐ ║
║ ║ ║ │ │ │ │ ║
║ ║ 00m 26s║════════════════════════════╦══│ 01m 27s│══╦════════════════════════════│ 00m 27s│ ║
║ ╚════════════════════╝ ║ └────────────────────┘ ║ └────────────────────┘ ║
║ ║ ║ ║
║ ║ ┌──✔ lint-test-job───┐ ║ ║
║ ║ │ │ ║ ║
║ ╚══│ 00m 37s│══╝ ║
║ └────────────────────┘ ║
║ ║
║ ║
║ ║
║ ║
╚════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝
╔════════════════════════════════════════════════ Pipeline #1163246103 triggered about 9 minutes ago by Futa Yamaji ═════════════════════════════════════════════════╗ ║ ║ ║ ╔══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╗ ║ ║ ║ Using docker image sha256:5cd1ffe2802975362ac267390dbf21810460e86b2e7ce5cb4b4870db9f579e6c for ruby:3.1 with digest ruby@sha256:e921f98b3211fee6789927e55f6c ║ ║ ║ ║ cfcba583bb26565dbf3efb9c58ad072bc3a6 ... ║ ║ ║ ║ $ echo "Compiling the code..." ║ ║ ║ ║ Compiling the code... ║ ║ ║ ║ $ echo "Compile complete." ║ ║ ║ ║ Compile complete. ║ ║ ║ ║ Cleaning up project directory and file based variables ║ ║ ║ ║ Job succeeded ║ ║ ║ ║ ║ ║ ║ ╚══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝ ║ ║ ║ ╚════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝
番外編: GitLab Duoとの連携
glabはGitLab Duoとも連携しており、 glab ask
コマンドで自然言語によるサジェストを受けることができるようです。私はGitLab Duoを使える環境になかったため以下のようにエラーとなりましたが、良ければ試してみてください。
$ glab ask git list last 10 commit titles x Error: API is unreachable error=POST https://gitlab.com/api/v4/ai/llm/git_command: 400 {message: 400 Bad request - AI features are not enabled or resource is not permitted to be sent.}