|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "os" |
| 9 | + "os/exec" |
| 10 | + "path/filepath" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "github.com/docker/mcp-registry/pkg/github" |
| 14 | + "github.com/docker/mcp-registry/pkg/servers" |
| 15 | +) |
| 16 | + |
| 17 | +func main() { |
| 18 | + flag.Parse() |
| 19 | + args := flag.Args() |
| 20 | + |
| 21 | + if len(args) != 1 { |
| 22 | + fmt.Println("Usage: task build -- <server>") |
| 23 | + os.Exit(1) |
| 24 | + } |
| 25 | + |
| 26 | + if err := run(context.Background(), args[0]); err != nil { |
| 27 | + log.Fatal(err) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +func run(ctx context.Context, name string) error { |
| 32 | + server, err := servers.Read(filepath.Join("servers", name, "server.yaml")) |
| 33 | + if err != nil { |
| 34 | + if os.IsNotExist(err) { |
| 35 | + return fmt.Errorf("server %s not found (did you already create it with `task create`?)", name) |
| 36 | + } |
| 37 | + return err |
| 38 | + } |
| 39 | + |
| 40 | + if !strings.HasPrefix(server.Image, "mcp/") { |
| 41 | + return fmt.Errorf("server is not docker built (ie, in the 'mcp/' namespace), you must either build it yourself or pull it with `docker pull %s` if you want to use it", server.Image) |
| 42 | + } |
| 43 | + |
| 44 | + projectURL := server.Source.Project |
| 45 | + branch := server.Source.Branch |
| 46 | + directory := server.Source.Directory |
| 47 | + |
| 48 | + client := github.New() |
| 49 | + |
| 50 | + repository, err := client.GetProjectRepository(ctx, projectURL) |
| 51 | + if err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + |
| 55 | + if branch == "" { |
| 56 | + branch = repository.GetDefaultBranch() |
| 57 | + } |
| 58 | + |
| 59 | + sha, err := client.GetCommitSHA1(ctx, projectURL, branch) |
| 60 | + if err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + |
| 64 | + gitURL := projectURL + ".git#" |
| 65 | + if branch != "" { |
| 66 | + gitURL += branch |
| 67 | + } |
| 68 | + if directory != "" && directory != "." { |
| 69 | + gitURL += ":" + directory |
| 70 | + } |
| 71 | + |
| 72 | + var cmd *exec.Cmd |
| 73 | + token := os.Getenv("GITHUB_TOKEN") |
| 74 | + |
| 75 | + if token != "" { |
| 76 | + cmd = exec.CommandContext(ctx, "docker", "buildx", "build", "--secret", "id=GIT_AUTH_TOKEN", "-t", "check", "-t", server.Image, "--label", "org.opencontainers.image.revision="+sha, gitURL) |
| 77 | + cmd.Env = []string{"GIT_AUTH_TOKEN=" + token, "PATH=" + os.Getenv("PATH")} |
| 78 | + } else { |
| 79 | + cmd = exec.CommandContext(ctx, "docker", "buildx", "build", "-t", "check", "-t", server.Image, "--label", "org.opencontainers.image.revision="+sha, gitURL) |
| 80 | + cmd.Env = []string{"PATH=" + os.Getenv("PATH")} |
| 81 | + } |
| 82 | + |
| 83 | + cmd.Dir = os.TempDir() |
| 84 | + cmd.Stdout = os.Stdout |
| 85 | + cmd.Stderr = os.Stderr |
| 86 | + |
| 87 | + if err := cmd.Run(); err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + |
| 91 | + fmt.Println("✅ Image built as", server.Image) |
| 92 | + |
| 93 | + return nil |
| 94 | +} |
0 commit comments