英文:
Argo Events: Use data filter in sensor to identify modified/added/removed path in mono-repo
问题
我正在使用Argo Events和Argo Workflow来构建我的CI/CD链,效果非常好。但是我在为我的单体仓库设置GitHub Webhook负载的数据过滤器时遇到了一些问题。
我想让传感器只在特定子路径下的文件发生更改时触发定义的工作流程。负载包含三个字段:added、removed和modified。其中列出了在此提交中更改的文件(webhook-events-and-payloads#push)。
我要搜索的路径是 service/jobs/*
和 service/common*/*
。
我定义的过滤器如下:
- path: "[commits.#.modified.#(\"*service*\")#,commits.#.added.#(\"*service*\")#,commits.#.removed.#(\"*service*\")#]"
type: string
value:
- "(\bservice/jobs\b)|(\bservice/common*)"
我在一个小的Go脚本中验证了我的过滤器,因为Argo Events使用gjson来应用数据过滤器。
package main
import (
"github.com/tidwall/gjson"
"regexp"
)
const json = `{
"commits": [
{
"added": [
],
"removed": [
],
"modified": [
"service/job-manager/README.md"
]
},
{
"added": [
],
"removed": [
"service/joby/something.md"
],
"modified": [
"service/job-manager/something.md"
]
},
{
"added": [
],
"removed": [
"service/joby/something.md"
],
"modified": [
"service/joby/someother.md"
]
}
],
"head_commit": {
"added": [
"service/job-manager/something.md"
],
"removed": [
"service/joby/something.md"
],
"modified": [
"service/job-manager/README.md"
]
}
}`
func main() {
value := gjson.Get(json, "[commits.#.modified.#(\"*service*\")#,commits.#.added.#(\"*service*\")#,commits.#.removed.#(\"*service*\")#]")
println(value.String())
matched, _ := regexp.MatchString(`(\bservice/job-manager\b)|(\bservice/common*)`, value.String())
println(matched) // 字符串是否包含?
}
这个脚本给出了我期望的结果。但是当将数据过滤器添加到传感器时,对于相同的Webhook负载,工作流程没有触发。
有人有什么想法吗?
更新:
感谢提示,包括路径中的 body.
。
我最终设置了以下过滤器:
- path: "[body.commits.#.modified.#()#,body.commits.#.added.#()#,body.commits.#.removed.#()#]"
type: string
value:
- ".*service/jobs.*"
- ".*service/common.*"
英文:
I'm using Argo Events and Argo Workflow for my CI/CD chain, which works pretty neat. But I'm having some troubles setting up the data filter for the GitHub webhook payloads of my mono repo.
I'm trying to let the sensor only trigger the defined workflow if files were changed in a certain subpath. The payload contains three fields added, removed, modified. There the files are listed which were changed in this commit (webhook-events-and-payloads#push).
The paths I'm searching for is service/jobs/*
and service/common*/*
.
The filter I defined is:
- path: "[commits.#.modified.#(%\"*service*\")#,commits.#.added.#(%\"*service*\")#,commits.#.removed.#(%\"*service*\")#]"
type: string
value:
- "(\bservice/jobs\b)|(\bservice/common*)"
I valididated my filter in a tiny Go script, as gjson is used by Argo Events to apply the data filter.
package main
import (
"github.com/tidwall/gjson"
"regexp"
)
const json = `{
"commits": [
{
"added": [
],
"removed": [
],
"modified": [
"service/job-manager/README.md"
]
},
{
"added": [
],
"removed": [
"service/joby/something.md"
],
"modified": [
"service/job-manager/something.md"
]
},
{
"added": [
],
"removed": [
"service/joby/something.md"
],
"modified": [
"service/joby/someother.md"
]
}
],
"head_commit": {
"added": [
"service/job-manager/something.md"
],
"removed": [
"service/joby/something.md"
],
"modified": [
"service/job-manager/README.md"
]
}
}`
func main() {
value := gjson.Get(json, "[commits.#.modified.#(%\"*service*\")#,commits.#.added.#(%\"*service*\")#,commits.#.removed.#(%\"*service*\")#]")
println(value.String())
matched, _ := regexp.MatchString(`(\bservice/job-manager\b)|(\bservice/common*)`, value.String())
println(matched) // string is contained?
}
The script gives me the results I expect. But for the same webhook payload the workflow is not triggered, when adding the data filter to the sensor.
Someone any ideas?
UPDATED:
Thanks for the hint incl. body.
in the paths.
I ended up setting the filters:
- path: "[body.commits.#.modified.#()#,body.commits.#.added.#()#,body.commits.#.removed.#()#]"
type: string
value:
- ".*service/jobs.*"
- ".*service/common.*"
答案1
得分: 2
- 路径应该以
body.
开头。 - 值应该使用
\\
转义特殊字符。
因此,数据过滤器应该是这样的:
- path: "body.commits.#.modified.#(%\"*service*\")#,body.commits.#.added.#(%\"*service*\")#,body.commits.#.removed.#(%\"*service*\")#"
type: string
value:
- "(\\bservice/jobs\\b)|(\\bservice/common*)""
英文:
- Path shoud start with
body.
- Value should add escape special character with
\\
So the data filter should be
- path: "[body.commits.#.modified.#(%\"*service*\")#,body.commits.#.added.#(%\"*service*\")#,body.commits.#.removed.#(%\"*service*\")#]"
type: string
value:
- "(\\bservice/jobs\\b)|(\\bservice/common*)"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论