Chore: Add Dockerfile CI check for new modules (#92239)

This commit is contained in:
Todd Treece 2024-08-21 16:25:25 -04:00 committed by GitHub
parent a86ded2438
commit e4953b6ffd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 51 additions and 2 deletions

View File

@ -31,3 +31,5 @@ jobs:
echo "If there is a change in enterprise dependencies, please update pkg/extensions/main.go."
exit 1
fi
- name: Ensure Dockerfile contains submodule COPY commands
run: ./scripts/go-workspace/validate-dockerfile.sh

View File

@ -19,6 +19,8 @@ func main() {
switch os.Args[1] {
case "list-submodules":
err = listSubmodules()
case "validate-dockerfile":
err = validateDockerfile()
default:
printUsage()
}
@ -40,7 +42,9 @@ func listSubmodules() error {
delimiter := fs.String("delimiter", "\n", "Delimiter to use when printing paths")
skip := fs.String("skip", "", "Skip submodules with this comment tag")
help := fs.Bool("help", false, "Print help message")
fs.Parse(os.Args[2:])
if err := fs.Parse(os.Args[2:]); err != nil {
return err
}
if *help {
fs.Usage()
@ -60,6 +64,41 @@ func listSubmodules() error {
return nil
}
func validateDockerfile() error {
fs := flag.NewFlagSet("validate-dockerfile", flag.ExitOnError)
workPath := fs.String("path", "go.work", "Path to go.work")
dockerfilePath := fs.String("dockerfile-path", "Dockerfile", "Path to Dockerfile")
skip := fs.String("skip", "", "Skip submodules with this comment tag")
if err := fs.Parse(os.Args[2:]); err != nil {
return err
}
dockerFileRaw, err := os.ReadFile(*dockerfilePath)
if err != nil {
return err
}
dockerFile := string(dockerFileRaw)
workfile, err := parseGoWork(*workPath)
if err != nil {
return err
}
paths := getSubmodulePaths(workfile, *skip)
for _, p := range paths {
path := strings.TrimPrefix(p, "./")
if path == "" || path == "." {
continue
}
if !strings.Contains(dockerFile, path) {
return fmt.Errorf("the Dockerfile is missing `COPY %s/go.* %s` for the related module. Please add it and commit the change.", path, path)
}
}
fmt.Println("All submodules are included in the Dockerfile.")
return nil
}
func getSubmodulePaths(wf *modfile.WorkFile, skip string) []string {
var paths []string
for _, d := range wf.Use {

View File

@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
REPO_ROOT=$(dirname "${BASH_SOURCE[0]}")/../..
go run scripts/go-workspace/main.go validate-dockerfile --path "${REPO_ROOT}/go.work" --dockerfile-path "${REPO_ROOT}/Dockerfile"