Help us learn about your current experience with the documentation. Take the survey.
跟踪外部部署工具的部署
- Tier: Free, Premium, Ultimate
- Offering: GitLab.com, GitLab Self-Managed, GitLab Dedicated
虽然 GitLab 提供了内置部署方案,但您可能更喜欢使用外部部署工具,例如 Heroku 或 ArgoCD。 GitLab 可以接收来自这些外部工具的部署事件,并允许您在 GitLab 中跟踪部署。 例如,通过设置跟踪,可以使用以下功能:
如何设置部署跟踪
外部部署工具通常提供 webhook,在部署状态更改时执行额外的 API 请求。 您可以将工具配置为向 GitLab 部署 API 发送请求。以下是事件和 API 请求流程的概述:
- 当部署开始运行时,创建状态为
running的部署。 - 当部署成功时,将部署状态更新为
success。 - 当部署失败时,将部署状态更新为
failed。
您可以为 GitLab API 认证创建一个项目访问令牌。
示例:跟踪 ArgoCD 的部署
您可以使用 ArgoCD webhook 将部署事件发送到 GitLab 部署 API。
以下是一个示例设置,当 ArgoCD 成功部署新版本时,在 GitLab 中创建一个 success 部署记录:
-
创建一个新的 webhook。您可以保存以下清单文件,并通过
kubectl apply -n argocd -f <manifiest-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 -
在您的应用程序中创建一个新的订阅:
kubectl patch app <your-app-name> -n argocd -p '{"metadata": {"annotations": {"notifications.argoproj.io/subscribe.on-deployed.gitlab":""}}}' --type merge
如果部署未按预期创建,您可以使用 argocd-notifications 工具 进行故障排除。
例如,argocd-notifications template notify gitlab-deployment-status <your-app-name> --recipient gitlab:argocd-notifications
会立即触发 API 请求,并在有任何错误时显示来自 GitLab API 服务器的错误消息。