{{< details >}}

  1. Tier: 基础版, 专业版, 旗舰版
  2. Offering: JihuLab.com, 私有化部署

{{< /details >}}

虽然极狐GitLab 提供了内置的部署解决方案,但您可能更喜欢使用外部部署工具,例如 Heroku 或 ArgoCD。极狐GitLab 可以接收来自这些外部工具的部署事件,并允许您在极狐GitLab 中跟踪部署。例如,通过设置跟踪,可以使用以下功能:

{{< alert type=”note” >}}

某些功能不可用,因为极狐GitLab 无法授权和利用这些外部部署,包括受保护环境部署批准部署安全性部署回滚

{{< /alert >}}

如何设置部署跟踪

外部部署工具通常提供一个 webhook,在部署状态更改时执行额外的 API 请求。您可以配置您的工具向极狐GitLab 部署 API 发出请求。以下是事件和 API 请求流程的概述:

  1. 当部署开始运行时,创建一个状态为 running 的部署
  2. 当部署成功时,将部署状态更新为 success
  3. 当部署失败时,将部署状态更新为 failed

{{< alert type=”note” >}}

您可以为极狐GitLab API 身份验证创建一个项目访问令牌

{{< /alert >}}

示例:跟踪 ArgoCD 的部署

您可以使用 ArgoCD webhook 向极狐GitLab 部署 API 发送部署事件。以下是一个示例设置,当 ArgoCD 成功部署新版本时,在极狐GitLab 中创建一个 success 部署记录:

  1. 创建一个新的 webhook。您可以保存以下清单文件,并通过 kubectl apply -n argocd -f <manifest-file-path> 应用它:

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: argocd-notifications-cm
    data:
      trigger.on-deployed: |
        - description: Application is synced and healthy. Triggered once per commit.
          oncePer: app.status.sync.revision
          send:
          - gitlab-deployment-status
          when: app.status.operationState.phase in ['Succeeded'] and app.status.health.status == 'Healthy'
      template.gitlab-deployment-status: |
        webhook:
          gitlab:
            method: POST
            path: /projects/<your-project-id>/deployments
            body: |
              {
                "status": "success",
                "environment": "production",
                "sha": "{{.app.status.operationState.operation.sync.revision}}",
                "ref": "main",
                "tag": "false"
              }
      service.webhook.gitlab: |
        url: https://gitlab.com/api/v4
        headers:
        - name: PRIVATE-TOKEN
          value: <your-access-token>
        - name: Content-type
          value: application/json
    
  2. 在您的应用程序中创建一个新的订阅:

    kubectl patch app <your-app-name> -n argocd -p '{"metadata": {"annotations": {"notifications.argoproj.io/subscribe.on-deployed.gitlab":""}}}' --type merge
    

{{< alert type=”note” >}}

如果未按预期创建部署,您可以使用argocd-notifications 工具进行故障排除。例如,argocd-notifications template notify gitlab-deployment-status <your-app-name> --recipient gitlab:argocd-notifications 会立即触发 API 请求,并在极狐GitLab API 服务器出现错误时呈现错误信息。

{{< /alert >}}