diff --git a/.circleci/config.yml b/.circleci/config.yml index fdc0b52b..9eb34059 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -23,14 +23,24 @@ jobs: - run: git diff --exit-code build: - executor: golang + machine: + image: ubuntu-1604:201903-01 working_directory: /home/circleci/.go_workspace/src/github.com/prometheus/promu steps: - - setup_remote_docker - checkout - - run: make promu - - run: promu crossbuild -v + - run: go install . + - restore_cache: + keys: + - v01-gocache-{{ checksum "go.mod" }} + - v01-gocache- + - run: mkdir -p /home/circleci/.cache/go-build + - run: promu crossbuild -v --gocache-path /home/circleci/.cache/go-build + - run: du -sh /home/circleci/.cache/go-build + - save_cache: + key: v01-gocache-{{ checksum "go.mod" }} + paths: + - /home/circleci/.cache/go-build - persist_to_workspace: root: . paths: diff --git a/.promu.yml b/.promu.yml index 9e6c9f8c..45d18272 100644 --- a/.promu.yml +++ b/.promu.yml @@ -5,7 +5,7 @@ go: repository: path: github.com/prometheus/promu build: - flags: -mod=vendor -a -tags 'netgo static_build' + flags: -mod=vendor -tags 'netgo static_build' ldflags: | -s -X github.com/prometheus/common/version.Version={{.Version}} @@ -17,4 +17,3 @@ tarball: files: - LICENSE - NOTICE - diff --git a/cmd/crossbuild.go b/cmd/crossbuild.go index 37f99481..d1647baa 100644 --- a/cmd/crossbuild.go +++ b/cmd/crossbuild.go @@ -78,14 +78,15 @@ var ( platformsFlagSet = true return nil }).Strings() + goCachePath = crossbuildcmd.Flag("gocache-path", "Path to a Go cache directory on the local machine").String() // kingpin doesn't currently support using the crossbuild command and the // crossbuild tarball subcommand at the same time, so we treat the - // tarball subcommand as an optional arg + // tarball subcommand as an optional arg. tarballsSubcommand = crossbuildcmd.Arg("tarballs", "Optionally pass the string \"tarballs\" from cross-built binaries").String() ) func runCrossbuild() { - //Check required configuration + // Check required configuration. if len(strings.TrimSpace(config.Repository.Path)) == 0 { log.Fatalf("missing required '%s' configuration", "repository.path") } @@ -149,7 +150,7 @@ func runCrossbuild() { } if !cgo { - // In non-CGO, use the base image without any crossbuild toolchain + // In non-CGO, use the base image without any crossbuild toolchain. var allPlatforms []string allPlatforms = append(allPlatforms, mainPlatforms[:]...) allPlatforms = append(allPlatforms, armPlatforms[:]...) @@ -158,7 +159,7 @@ func runCrossbuild() { allPlatforms = append(allPlatforms, s390xPlatforms[:]...) pg := &platformGroup{"base", dockerBaseBuilderImage, allPlatforms} - if err := pg.Build(repoPath); err != nil { + if err := pg.Build(repoPath, *goCachePath); err != nil { fatal(errors.Wrapf(err, "The %s builder docker image exited unexpectedly", pg.Name)) } } else { @@ -169,7 +170,7 @@ func runCrossbuild() { {"MIPS", dockerMIPSBuilderImage, mipsPlatforms}, {"s390x", dockerS390XBuilderImage, s390xPlatforms}, } { - if err := pg.Build(repoPath); err != nil { + if err := pg.Build(repoPath, *goCachePath); err != nil { fatal(errors.Wrapf(err, "The %s builder docker image exited unexpectedly", pg.Name)) } } @@ -182,7 +183,9 @@ type platformGroup struct { Platforms []string } -func (pg platformGroup) Build(repoPath string) error { +const containerGoCacheDir = "/root/.cache/go-build" + +func (pg platformGroup) Build(repoPath string, goCachePath string) error { platformsParam := strings.Join(pg.Platforms[:], " ") if len(platformsParam) == 0 { return nil @@ -196,11 +199,22 @@ func (pg platformGroup) Build(repoPath string) error { } ctrName := "promu-crossbuild-" + pg.Name + strconv.FormatInt(time.Now().Unix(), 10) - err = sh.RunCommand("docker", "create", "-t", + args := []string{ + "create", "-t", "--name", ctrName, + } + if goCachePath != "" { + args = append(args, + "--env", fmt.Sprintf("GOCACHE=%s", containerGoCacheDir), + "-v", fmt.Sprintf("%s:%s", goCachePath, containerGoCacheDir), + ) + } + args = append(args, pg.DockerImage, "-i", repoPath, - "-p", platformsParam) + "-p", platformsParam, + ) + err = sh.RunCommand("docker", args...) if err != nil { return err }