func buildFn()

in cmd/go/build/main.go [51:106]


func buildFn(ctx *gcp.Context) error {
	// Keep GOCACHE in Devmode for faster rebuilds.
	cl, err := ctx.Layer("gocache", gcp.BuildLayer, gcp.LaunchLayerIfDevMode)
	if err != nil {
		return fmt.Errorf("creating layer: %w", err)
	}
	if devmode.Enabled(ctx) {
		cl.LaunchEnvironment.Override("GOCACHE", cl.Path)
	}

	// Create a layer for the compiled binary.  Add it to PATH in case
	// users wish to invoke the binary manually.
	bl, err := ctx.Layer("bin", gcp.LaunchLayer)
	if err != nil {
		return fmt.Errorf("creating layer: %w", err)
	}
	bl.LaunchEnvironment.Prepend("PATH", string(os.PathListSeparator), bl.Path)
	outBin := filepath.Join(bl.Path, golang.OutBin)

	buildable, err := goBuildable(ctx)
	if err != nil {
		return fmt.Errorf("unable to find a valid buildable: %w", err)
	}

	// Build the application.
	bld := []string{"go", "build"}
	bld = append(bld, goBuildFlags()...)
	bld = append(bld, "-o", outBin)
	bld = append(bld, buildable)
	// BuildDirEnv should only be set by App Engine buildpacks.
	workdir := os.Getenv(golang.BuildDirEnv)
	if workdir == "" {
		workdir = ctx.ApplicationRoot()
	}
	if _, err := ctx.Exec(bld, gcp.WithEnv("GOCACHE="+cl.Path), gcp.WithWorkDir(workdir), gcp.WithMessageProducer(printTipsAndKeepStderrTail(ctx)), gcp.WithUserAttribution); err != nil {
		return err
	}

	// Configure the entrypoint for production. Use the full path to save `skaffold debug`
	// from fetching the remote container image (tens to hundreds of megabytes), which is slow.
	if !devmode.Enabled(ctx) {
		ctx.AddWebProcess([]string{outBin})
		return nil
	}

	// Configure the entrypoint and metadata for dev mode.
	if err := devmode.AddFileWatcherProcess(ctx, devmode.Config{
		BuildCmd: bld,
		RunCmd:   []string{outBin},
		Ext:      devmode.GoWatchedExtensions,
	}); err != nil {
		return fmt.Errorf("adding devmode file watcher: %w", err)
	}

	return nil
}