Medium,  on  CI/CD Submissions: 2/12

In the previous challenge, you learned how to create a Dagger module with some dummy functions in it. Now you will need to create a more useful module - one that can build a Go project.

The sample Go project is located in ~/my-project. Explore it and try to build it with make build first. After that, in the project's root directory, bootstrap a Dagger module. The module should have only one function, which performs the same go build command that the Makefile uses. The function should take in the project's root directory and return the resulting binary file.

The solution checker will use the following command to verify the implementation:

# Assuming the PWD is the project's root directory

dagger call build --src . export --path ./server

Good luck!

Hint 1 💡

Don't know where to start? Make sure you solved the previous challenge first!

Hint 2 💡

The function should conform to the following signature:

type MyProject struct{}

func (m *MyProject) Build(
  ctx context.Context,
  src *Directory,
) *File {
  // Your implementation goes here...
}

The above is Go code, but you're free to use any of the supported languages.

Hint 3 💡

Even though Dagger functions are written in full-blown programming languages, they often look like a bunch of shell commands glued together with Go (or Python, or TypeScript, or whatever). Something still has to execute that go build command.

Hint 4 💡

Tried executing the go build command directly from the build command and it failed? To be able to run the go build command, you need a proper Go development environment, and the function's runtime is definitely not suitable for that.

Dagger uses additional containers to provision required environments for every command execution. Here is what you can do:

func (m *MyProject) Build(
  ctx context.Context,
  src *Directory,
) *File {
  buildContainer := dag.Container().
    From("golang:1").
      WithDirectory("/src", src).
      WithExec([]string{"go", "build", "..."})

  ...
}
Categories: CI/CD
Discussion:  Discord

Level up your server-side game — Join 6,700 engineers who receive insightful learning materials straight to their inbox