This document covers the core types and functions available in gobake.
gobake.Engine: The central controller for defining tasks and loading project metadata.gobake.Context: A helper passed to each task, providing execution and utility methods.gobake.Task: Represents a single build step.gobake.RecipeInfo: Struct mapping torecipe.piml.
Registers a new task.
- name: The name of the task (e.g., "build", "test"). Must be unique.
- description: A short description shown in
gobake help. - action: The function to execute. Returns
error.
bake.Task("clean", "Cleans the build artifacts", func(ctx *gobake.Context) error {
return ctx.Remove("bin/")
})func (e *Engine) TaskWithDeps(name, description string, deps []string, action func(ctx *Context) error)
Registers a task that depends on other tasks. Dependencies run before the main action.
- deps: A slice of task names (e.g.,
[]string{"test", "lint"}).
bake.TaskWithDeps("build", "Build app", []string{"clean"}, func(ctx *gobake.Context) error {
return ctx.BakeBinary("linux", "amd64", "bin/app")
})Loads project metadata from recipe.piml into e.Info.
- path: Usually
"recipe.piml".
if err := bake.LoadRecipeInfo("recipe.piml"); err != nil {
return err
}
fmt.Println("Project Name:", bake.Info.Name)Saves the current e.Info back to recipe.piml. Useful for custom scripts that modify version or metadata.
Executes a shell command. It inherits stdout/stderr and environment variables.
- name: The command (e.g., "go", "npm", "docker").
- args: Command arguments.
ctx.Run("go", "test", "./...")Like Run, but executes the command in the given working directory. An empty dir falls back to the current working directory.
ctx.RunIn("./service", "go", "build", "./...")Executes a command and returns its captured stdout (with trailing newlines trimmed). Stderr is still streamed to the terminal so failures stay visible.
sha, err := ctx.RunOutput("git", "rev-parse", "HEAD")
if err != nil {
return err
}
ctx.Log("commit: %s", sha)RunOutput with an explicit working directory.
branch, _ := ctx.RunInOutput("./vendor/lib", "git", "rev-parse", "--abbrev-ref", "HEAD")A helper for cross-compiling Go binaries. Sets GOOS and GOARCH automatically.
- osName: Target OS (e.g., "linux", "windows").
- arch: Target Architecture (e.g., "amd64", "arm64").
- output: Output file path.
- flags: Additional
go buildflags (e.g.,"-ldflags", "-s -w").
ctx.BakeBinary("windows", "amd64", "bin/app.exe", "-v")Installs all Go tools listed in recipe.piml -> (tools).
bake.Task("setup", "Install dev tools", func(ctx *gobake.Context) error {
return ctx.InstallTools()
})Creates a directory and any necessary parents (like mkdir -p).
Removes a file or directory recursively (like rm -rf).
Copies a single file from src to dst.
Prints a formatted message to stdout, prefixed with [gobake].
Sets an environment variable for subsequent Run or BakeBinary calls within the same task context.
ctx.SetEnv("CGO_ENABLED", "0")
ctx.BakeBinary("linux", "amd64", "bin/app")