Testing FluxCD ImagePolicy Regex with Go
FluxCD ImagePolicy uses Go regex to filter image tags. Test your regex patterns locally before deploying.
Setup
go mod init atoss-regex
touch main.go
Code
// main.go
package main
import (
"fmt"
"regexp"
)
func main() {
// pattern := `^(?P<version>[0-9]+\.[0-9]+\.[0-9]+)$`
pattern := `^(?P<version>[0-9]+\\.[0-9]+\\.[0-9]+)$`
// pattern := `^([0-9]+\\.[0-9]+\\.[0-9]+)$`
tags := []string{
"v1.0.0",
"0.0.51",
"0.0.16",
"2.1.0",
"1.2.3",
"v0.9.8",
"1.0",
"invalid",
"1.0.0-beta",
}
re, err := regexp.Compile(pattern)
if err != nil {
fmt.Println("Error compiling regex:", err)
return
}
for _, tag := range tags {
if re.MatchString(tag) {
fmt.Printf("Tag '%s' matches the pattern.\n", tag)
} else {
fmt.Printf("Tag '%s' does not match the pattern.\n", tag)
}
}
}
Note the double backslash escaping (\\.) - this is required for FluxCD YAML configuration.
Run
## run directly
go run main.go
## or build and run
go build -o app
./app