1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-08 03:31:59 +02:00
goreleaser/cmd
Kazuki Matsumaru b495c905d5
fix: set parallelism to match Linux container CPU (#3901)
<!--

Hi, thanks for contributing!

Please make sure you read our CONTRIBUTING guide.

Also, add tests and the respective documentation changes as well.

-->

Currently Goreleaser uses `runtime.NumCPU()` as the default value if
`--parallelism` is not set.
However, this will get the number of CPUs on the host even when
Goreleaser is run in a container with a limit on the maximum number of
CPUs that can be used (typically in a Kubernetes pod).
Actually, `docker run --cpus=1 goreleaser/goreleaser --debug` shows
`parallelism: 4` on my machine.
This behavior causes CPU throttling, which increases execution time and,
in the worst case, terminates with an error.
I ran into this problem with Jenkins where the agent runs on pod
([Kubernetes plugin for
Jenkins](https://plugins.jenkins.io/kubernetes/)).

This commit introduces
[automaxprocs](https://github.com/uber-go/automaxprocs) to fix this
issue.
This library sets `GOMAXPROCS` to match Linux container CPU quota.
I have also looked for a library that can get CPU quota more directly,
but this seems to be the best I could find.
The reason it is set in a different notation from the automaxprocs
README is to prevent logs from being displayed
([comment](https://github.com/uber-go/automaxprocs/issues/18#issuecomment-511330567)).

I would have liked to write a test, but this change is dependent on the
number of CPUs in the execution environment, so I could not.
Instead, I wrote a Dockerfile for testing

```Dockerfile
FROM golang:1.20.2

WORKDIR /go/app
RUN sh -c "$(curl --location https://taskfile.dev/install.sh)" -- -d -b /usr/local/bin
COPY . .
RUN task build
```

and confirmed built binary shows expected parallelism by following
commands:

```sh
docker build --file Dockerfile.test . -t test-goreleaser
docker run --cpus=1 test-goreleaser ./goreleaser build --snapshot --debug # parallelism: 1
docker run test-goreleaser ./goreleaser build --snapshot --debug # parallelism: 4
```

I also ran the built binary on my Macbook and it was fine.
2023-04-02 17:16:41 -03:00
..
testdata feat: add healthcheck cmd (#3826) 2023-03-03 09:50:15 -03:00
build_test.go test: testctx pkg (#3807) 2023-03-02 00:01:11 -03:00
build.go fix: set parallelism to match Linux container CPU (#3901) 2023-04-02 17:16:41 -03:00
check_test.go test: use ErrorIs 2023-03-03 10:36:45 -03:00
check.go feat: replacing the log library (#3139) 2022-06-21 21:11:15 -03:00
config_test.go feat: allow to load config from stdin (#3175) 2022-06-22 22:03:00 -03:00
config.go fix: ruleguard and semgrep scans and fixes (#3364) 2022-09-11 15:32:23 -03:00
docs.go feat: migrate back to cobra 2022-05-06 20:38:50 -03:00
error.go feat: refactor main.go to spf13/cobra (#1472) 2020-04-27 21:42:44 -03:00
healthcheck_test.go test: use ErrorIs 2023-03-03 10:36:45 -03:00
healthcheck.go docs: updates 2023-03-03 12:57:54 -03:00
init_test.go fix: gitignore patching needs leading newline (#2350) 2021-07-19 17:31:29 -03:00
init.go feat: deprecate replacements (#3589) 2022-11-25 15:26:14 -03:00
man.go feat: migrate back to cobra 2022-05-06 20:38:50 -03:00
release_test.go test: testctx pkg (#3807) 2023-03-02 00:01:11 -03:00
release.go fix: set parallelism to match Linux container CPU (#3901) 2023-04-02 17:16:41 -03:00
root_test.go chore: gofumpt & lint (#2190) 2021-04-25 14:20:49 -03:00
root.go feat: add healthcheck cmd (#3826) 2023-03-03 09:50:15 -03:00
schema_test.go test: fix test 2022-08-04 15:37:19 -03:00
schema.go fix: annotate flags so they complete better (#3444) 2022-10-06 11:24:13 +02:00
util_test.go build: use go1.20 (#3757) 2023-02-05 13:39:39 -03:00