使用 GitLab CLI (glab) 配合 GitLab Connector

本指南演示如何使用 GitLab CLI (glab) 配合 GitLab Connector,执行高级 GitLab 操作,而无需手动配置凭据。

Overview

GitLab CLI (glab) 是 GitLab 官方的命令行工具,允许您通过 GitLab API 管理合并请求、问题、流水线等。GitLab Connector 提供了 glab 的自动配置,使其在容器化环境中使用更加便捷,无需手动处理凭据。

Requirements for GitLab Servers and GitLab CLI (glab)

详情请参阅 Requirements for GitLab Servers and GitLab CLI (glab)

前提条件

  • 配置了具有 api 范围的 Private Access Token 的 GitLab Connector
  • 对 GitLab CLI 命令有基本了解
  • 拥有对 Kubernetes 集群的 kubectl 访问权限

您将学到

  • 如何使用 connector 挂载 GitLab CLI 配置
  • 如何使用 glab 命令执行常见的 GitLab 操作
  • 如何在同一 Pod 中结合使用 Git 和 glab 操作

第 1 步:创建 GitLab Connector

首先,确保您拥有具有适当权限的 GitLab Connector:

cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Secret
metadata:
  name: gitlab-secret
  namespace: gitlab-connector-demo
type: connectors.cpaas.io/gitlab-pat-auth
stringData:
  token: glpat-xxxxxxxxxxxxxxxxxxxx  # 令牌必须具有 'api' 范围
---
apiVersion: connectors.alauda.io/v1alpha1
kind: Connector
metadata:
  name: gitlab-connector
  namespace: gitlab-connector-demo
spec:
  connectorClassName: gitlab
  address: https://gitlab.com
  auth:
    name: patAuth
    secretRef:
      name: gitlab-secret
EOF

替换说明:

  • glpat-xxxxxxxxxxxxxxxxxxxx:替换为您实际的 GitLab Private Access Token(必须具有 api 范围)
  • https://gitlab.com:替换为您的 GitLab 服务器地址(GitLab.com 使用 https://gitlab.com,自托管 GitLab 使用您的服务器 URL)

确认 connector 处于 “Ready” 状态:

kubectl get connector gitlab-connector -n gitlab-connector-demo

第 2 步:使用 glab 执行 API 操作

创建一个使用 glab 与 GitLab API 交互的 Pod:

cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
  name: glab-api-demo
  namespace: gitlab-connector-demo
spec:
  restartPolicy: Never
  containers:
  - name: gitlab-cli
    image: gitlab/glab:v1.74.0
    command:
    - "sh"
    - "-c"
    - |
      set -ex
      
      # 设置 glab 配置
      mkdir -p ~/.config/glab-cli
      cp /opt/gitlab/config.yml ~/.config/glab-cli/
      chmod 600 ~/.config/glab-cli/config.yml
      
      # 列出项目
      echo "Listing projects..."
      glab api projects
      
      # 获取当前用户信息
      echo "Getting user info..."
      glab api user
      
      echo "glab api operations completed"
    volumeMounts:
    - name: gitlab-config
      mountPath: /opt/gitlab
  volumes:
  - name: gitlab-config
    csi:
      driver: connectors-csi
      readOnly: true
      volumeAttributes:
        connector.name: "gitlab-connector"
        configuration.names: "gitlabconfig"
EOF

查看日志:

kubectl logs -f glab-api-demo -n gitlab-connector-demo

您将看到项目列表和用户信息的输出。

第 3 步:使用 glab 克隆仓库

使用 glab 克隆仓库:

cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
  name: glab-clone-demo
  namespace: gitlab-connector-demo
spec:
  restartPolicy: Never
  containers:
  - name: gitlab-cli
    image: gitlab/glab:v1.74.0
    command:
    - "sh"
    - "-c"
    - |
      set -ex
      
      # 设置 glab 和 git 配置
      mkdir -p ~/.config/glab-cli
      cp /opt/gitlab/config.yml ~/.config/glab-cli/
      chmod 600 ~/.config/glab-cli/config.yml
      
      cp /opt/gitlab/.gitconfig ~/
      chmod 644 ~/.gitconfig
      
      # 使用 glab 克隆仓库
      glab repo clone <your-group>/<your-repo> /tmp/repo
      echo "Repository cloned successfully"
      
      # 列出仓库内容
      ls -la /tmp/repo
    volumeMounts:
    - name: gitlab-config
      mountPath: /opt/gitlab
  volumes:
  - name: gitlab-config
    csi:
      driver: connectors-csi
      readOnly: true
      volumeAttributes:
        connector.name: "gitlab-connector"
        configuration.names: "gitlabconfig,gitconfig"
EOF

替换说明:

  • <your-group>/<your-repo>:替换为您实际的 GitLab 仓库路径(例如 gitlab-org/gitlab

重要提示: 使用 glab 克隆仓库时,必须挂载 gitlabconfiggitconfig 两个配置。

查看日志:

kubectl logs -f glab-clone-demo -n gitlab-connector-demo

您将看到仓库成功克隆的输出:

+ glab repo clone <your-group>/<your-repo> /tmp/repo
Cloning into '/tmp/repo'...
remote: Enumerating objects: 123, done.
remote: Counting objects: 100% (123/123), done.
remote: Compressing objects: 100% (89/89), done.
remote: Total 123 (delta 34), reused 123 (delta 34), pack-reused 0
Receiving objects: 100% (123/123), 45.67 KiB | 1.23 MiB/s, done.
Resolving deltas: 100% (34/34), done.
+ echo 'Repository cloned successfully'
Repository cloned successfully

第 4 步:管理合并请求

使用 glab 管理合并请求:

cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
  name: glab-mr-demo
  namespace: gitlab-connector-demo
spec:
  restartPolicy: Never
  containers:
  - name: gitlab-cli
    image: gitlab/glab:v1.74.0
    command:
    - "sh"
    - "-c"
    - |
      set -ex
      
      # 设置配置
      mkdir -p ~/.config/glab-cli
      cp /opt/gitlab/config.yml ~/.config/glab-cli/
      chmod 600 ~/.config/glab-cli/config.yml
      
      cp /opt/gitlab/.gitconfig ~/
      chmod 644 ~/.gitconfig
      
      # 克隆仓库
      glab repo clone <your-group>/<your-repo> /tmp/repo
      cd /tmp/repo
      
      # 列出合并请求
      echo "Listing merge requests..."
      glab mr list
      
      # 查看指定合并请求
      echo "Viewing merge request #1..."
      glab mr view <mr-number>
    volumeMounts:
    - name: gitlab-config
      mountPath: /opt/gitlab
  volumes:
  - name: gitlab-config
    csi:
      driver: connectors-csi
      readOnly: true
      volumeAttributes:
        connector.name: "gitlab-connector"
        configuration.names: "gitlabconfig,gitconfig"
EOF

替换说明:

  • <your-group>/<your-repo>:替换为您实际的 GitLab 仓库路径(例如 gitlab-org/gitlab
  • <mr-number>:替换为您想查看或关闭的合并请求编号(例如 142

重要提示: 使用需要与仓库交互的 glab 命令(如在仓库目录下执行 glab mr list)时,必须挂载 gitlabconfiggitconfig 两个配置。

查看日志:

kubectl logs -f glab-mr-demo -n gitlab-connector-demo

您将看到合并请求列表和合并请求详情的输出:

+ glab mr list
Listing merge requests...
Showing 8 open merge requests on <your-group>/<your-repo>. (Page 1)

!8     <your-group>/<your-repo>!14    change at: 1764221453   (master) ← (dev-1764221453)
...

Viewing merge request #1...
+ echo 'Viewing merge request #1...'
+ glab mr view 8
title:  Feat/support build xiaokk
state:  open
author: kkxiao
labels:
assignees:
reviewers:
comments:       0
number: 8
url:    https://<your-gitlab-server>/<your-group>/<your-repo>/-/merge_requests/8
--

后续步骤