如何在ArgoCD通知Webhook模板中移除仓库URL末尾的.git
移除Argo CD Webhook配置中RepoURL末尾的.git
你的问题出在使用strings.ReplaceAll替换了所有"git"子串,而非仅末尾的.git后缀,错误的替换逻辑还导致了重复输出。以下是两种解决方案:
方法一:使用TrimSuffix精准移除末尾后缀(推荐)
Argo CD的模板函数提供了.strings.TrimSuffix,专门用于移除字符串末尾的指定后缀,完全匹配你的需求。修改后的模板配置如下:
--- apiVersion: v1 kind: ConfigMap metadata: name: argocd-notifications-cm data: service.webhook.velocity: | url: https://velocity.codeclimate.com/deploys headers: - name: Content-Type value: application/x-www-form-urlencoded template.velocity-update: | webhook: velocity: method: POST body: repository_url={{.app.spec.source.repoURL | call .repo.RepoURLToHTTPS | call .strings.TrimSuffix ".git"}} trigger.sync-operation-change: | - when: app.status.operationState.phase in ['Succeeded'] oncePer: app.status.sync.revision send: [velocity-update]
这个方法只会移除URL末尾的.git,不会影响URL中其他位置的"git"字符串(比如github中的git),结果完全符合预期。
方法二:使用正则替换(备选)
如果一定要用替换函数,可以借助正则表达式精准匹配末尾的.git,需要确保你的Argo CD版本支持.regexp.ReplaceAllString函数:
body: repository_url={{.app.spec.source.repoURL | call .repo.RepoURLToHTTPS | call .regexp.ReplaceAllString "\.git$" ""}}
正则\.git$会精准匹配字符串末尾的.git,仅替换目标后缀,不会干扰URL其他部分。
内容的提问来源于stack exchange,提问作者Marcus
相关产品推荐
相关产品推荐

