0

I'm new to go and am trying to figure out how to execute the build step in my circleci project.

I noticed in the CircleCI Go orb, that there was no use of go build, which was confusing to me because in the CircleCI Go Language Guide, they specifically use go build via a makefile.

So I am not sure if using the Go orb alone is sufficient (though that seems odd to me). I vaguely understand what go mod download does, and what go build does, but I've seen examples where they are used together:

RUN go mod download

COPY . .

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build

Currently, according to the go orb usage doc, I have:

  build:
    executor: go
    steps:
      - checkout
      - go/load-cache:
          key: go-mod-v1-{{ checksum "go.sum" }}
      - go/mod-download
      - go/save-cache:
          key: go-mod-v1-{{ checksum "go.sum" }}
      - slack/notify:
          event: fail
          template: basic_fail_1   

What am I missing or unintentionally including by using go mod download instead of go build or go build with go mod download?

TheRealFakeNews
  • 7,512
  • 16
  • 73
  • 114
  • 1
    Did you test it? How was the output not conforming to your expectations? – Tyler Kropp Apr 15 '21 at 04:12
  • 2
    Doing a `go mod download` in a build CI/CD setting has the advantage that if `go mod download` fails you have an individual step that fails and it is clear(er) that your build pipeline has a problem _finding_ or _accessing_ a dependency and not in compiling your code (or that of a dependency). This can help identifying problems (e.g. with private repos) faster. – Volker Apr 15 '21 at 04:48

2 Answers2

2

go mod download downloads source code for all dependency modules, and verifies checksums for newly-downloaded modules. (Note that in Go 1.18 we plan to change the behavior of go mod download so that it can instead download only the modules needed to build packages and tests in in main module.)

go build builds packages. It automatically downloads (and verifies) module source code as needed to build those packages. In many cases, that is a much smaller set of modules than what is downloaded by go mod download.

bcmills
  • 4,391
  • 24
  • 34
  • I am not sure why CircleCI would use `go mod download` instead of `go build`. The `go` command has a separate cache for built object files, so if the goal is to speed up builds then it seems like you would get a strictly larger speedup by running an initial `go build` and then saving the resulting contents of *both* the module cache and the build cache. – bcmills Jun 29 '21 at 02:51
0

Documentation of go mod download details that all dependencies will be downloaded as no argument is provided.

With no arguments, download applies to all dependencies of the main module.

The lines in the question seem to be a subset of the complete instructions of the CI job. The example from Circle CI documentation suggests a build step which is mandatory to get an executable.

Any build command allows to choose the behavior regarding dependencies. As suggested, having a separate go mod download depends on the constraints of the environment.