Skip to content

Commit

Permalink
Chore: Add Dockerfile CI check for new modules (grafana#92239)
Browse files Browse the repository at this point in the history
  • Loading branch information
toddtreece authored Aug 21, 2024
1 parent a86ded2 commit e4953b6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/pr-go-workspace-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ jobs:
echo "Please run 'make update-workspace' and commit the changes."
echo "If there is a change in enterprise dependencies, please update pkg/extensions/main.go."
exit 1
fi
fi
- name: Ensure Dockerfile contains submodule COPY commands
run: ./scripts/go-workspace/validate-dockerfile.sh
41 changes: 40 additions & 1 deletion scripts/go-workspace/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ func main() {
switch os.Args[1] {
case "list-submodules":
err = listSubmodules()
case "validate-dockerfile":
err = validateDockerfile()
default:
printUsage()
}
Expand All @@ -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()
Expand All @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions scripts/go-workspace/validate-dockerfile.sh
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit e4953b6

Please sign in to comment.