Skip to content

Commit 4dc7eb8

Browse files
committedJun 12, 2024
Build binaries using GoReleaser
1 parent 3ea76c1 commit 4dc7eb8

File tree

10 files changed

+129
-25
lines changed

10 files changed

+129
-25
lines changed
 

‎.circleci/config.yml

+40-17
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,27 @@ x-data:
1212
linux_machine_image: &linux_machine_image ubuntu-2204:2024.02.7
1313
go_image: &go_image cimg/go:1.22
1414

15+
executors:
16+
go:
17+
docker:
18+
- image: *go_image
19+
resource_class: circleci-runner/rum-large
20+
1521
workflows:
1622
main-workflow:
1723
jobs:
1824
- lint
19-
- prepare-agents:
20-
context: org-global
25+
- build
2126
- build-and-publish-images:
2227
name: build-and-publish-image-amd64
2328
context: org-global
24-
requires:
25-
- prepare-agents
29+
requires: [ lint, build ]
2630
- build-and-publish-images:
2731
name: build-and-publish-image-arm64
2832
resource: arm.medium
2933
arch: arm64
3034
context: org-global
31-
requires:
32-
- prepare-agents
35+
requires: [ lint, build ]
3336
- publish-manifest:
3437
context: org-global
3538
filters:
@@ -42,9 +45,7 @@ workflows:
4245

4346
jobs:
4447
lint:
45-
docker:
46-
- image: *go_image
47-
resource_class: circleci-runner/rum-large
48+
executor: go
4849
steps:
4950
- setup
5051
- run:
@@ -77,17 +78,19 @@ jobs:
7778
- store_results
7879
- notify_failing_main
7980

80-
prepare-agents:
81-
docker:
82-
- image: *go_image
83-
resource_class: circleci-runner/rum-large
81+
build:
82+
executor: go
8483
steps:
85-
- checkout
86-
- run: ./do build-fake-agents
84+
- goreleaser_setup
85+
- run: go mod download
86+
- run:
87+
name: Build binaries
88+
command: |
89+
BUILD_VERSION="$(<version.txt)-<< pipeline.number >>-$(git rev-parse --short HEAD 2>/dev/null || echo latest)" \
90+
./do build
8791
- persist_to_workspace:
8892
root: .
89-
paths:
90-
- "./bin/circleci-*"
93+
paths: [ target ]
9194
- notify_failing_main
9295

9396
build-and-publish-images:
@@ -145,6 +148,26 @@ commands:
145148
- store_test_results:
146149
path: test-reports
147150

151+
goreleaser_setup:
152+
steps:
153+
- checkout
154+
- attach_workspace:
155+
at: .
156+
- run:
157+
name: Install GoReleaser
158+
command: |
159+
if [[ "$(command -v apk)" ]]; then
160+
# Alpine doesn't come with the coreutils' sha256sum, which the GoReleaser Bash script expects...
161+
apk upgrade && apk add coreutils
162+
fi
163+
164+
curl -sfL https://goreleaser.com/static/run -o ./bin/goreleaser --create-dirs && chmod +x ./bin/goreleaser
165+
166+
if [[ $(uname -m) == aarch64 ]]; then
167+
# A little hack to get the GoReleaser Bash script working on ARM64...
168+
sed -i 's/$(uname -m)/arm64/g' ./bin/goreleaser
169+
fi
170+
148171
docker_login:
149172
steps:
150173
- run:

‎.goreleaser/binaries/builds.yaml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
dist: ./target
2+
3+
builds:
4+
- id: orchestrator
5+
main: ./cmd/orchestrator
6+
binary: ./bin/{{.Arch}}/orchestrator
7+
ldflags: &ldflags
8+
- -s -w
9+
- -X github.com/circleci/runner-init/cmd.Version={{.Env.BUILD_VERSION}}
10+
- -X github.com/circleci/runner-init/cmd.Date={{.Date}}
11+
env: [CGO_ENABLED=0]
12+
goos: [linux]
13+
goarch: [amd64, arm64]
14+
no_unique_dist_dir: true
15+
16+
- id: fake-task-agent
17+
main: ./internal/faketaskagent
18+
binary: ./bin/{{.Arch}}/fake-task-agent
19+
ldflags:
20+
- -s -w
21+
env: [CGO_ENABLED=0]
22+
goos: [linux]
23+
goarch: [amd64, arm64]
24+
no_unique_dist_dir: true
25+
26+
release:
27+
disable: true

‎CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# CircleCI Init Changelog
1+
# CircleCI Runner Init Changelog
22

33
This document serves to keep track of all changes made to the Runner init images and agents. Please follow the guidelines below while adding an entry:
44

@@ -10,5 +10,6 @@ By following these guidelines, we can easily determine which changes should be i
1010

1111
## Edge
1212

13+
- [#12](https://github.com/circleci/runner-init/pull/12) [INTERNAL] Use [GoReleaser](https://goreleaser.com/) For building the binaries.
1314
- [#11](https://github.com/circleci/runner-init/pull/11) [INTERNAL] Download task agent binaries directly via the Dockerfile.
1415
- [#10](https://github.com/circleci/runner-init/pull/10) [INTERNAL] Set up linting tools, initiated a changelog, and performed other configurations in preparation for the orchestration agent.

‎cmd/orchestrator/main.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import (
4+
"log" //nolint:depguard // a non-O11y log is allowed for a top-level fatal exit
5+
6+
"github.com/circleci/runner-init/cmd"
7+
)
8+
9+
func main() {
10+
if err := run(cmd.Version, cmd.Date); err != nil {
11+
log.Fatal(err)
12+
}
13+
}
14+
15+
//nolint:unparam // TODO
16+
func run(version, date string) (err error) {
17+
println(version, date)
18+
19+
return err
20+
}

‎cmd/version.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package cmd
2+
3+
// Variables to be set by the linker
4+
var (
5+
Version = "dev"
6+
Date = ""
7+
)

‎do

+30-5
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,37 @@ GORELEASER_VERSION="v1.26.2"
77

88
# This variable is used, but shellcheck can't tell.
99
# shellcheck disable=SC2034
10-
help_build_fake_agents="Build the fake agent go binaries"
11-
build-fake-agents() {
12-
export CGO_ENABLED=0
10+
help_build="Build the Go binaries and archives"
11+
build() {
12+
set -x
13+
14+
[[ -f ./bin/goreleaser ]] || install-go-bin "github.com/goreleaser/goreleaser@latest"
15+
16+
VERSION="${GORELEASER_VERSION}" \
17+
BUILD_VERSION="${BUILD_VERSION:?'required'}" ./bin/goreleaser \
18+
--clean \
19+
--config "${BUILD_CONFIG:-./.goreleaser/binaries/builds.yaml}" \
20+
--skip=validate \
21+
--snapshot "$@"
22+
23+
echo "${BUILD_VERSION}" | tee ./target/version.txt
24+
}
25+
26+
# This variable is used, but shellcheck can't tell.
27+
# shellcheck disable=SC2034
28+
help_dev_binary="Build single target Go binaries for local dev"
29+
dev-build() {
30+
set -x
31+
32+
[[ -f ./bin/goreleaser ]] || install-go-bin "github.com/goreleaser/goreleaser@latest"
1333

14-
GOOS=linux GOARCH=amd64 go build -o ./bin/circleci-fake-agent-amd64 ./internal/fake-agent
15-
GOOS=linux GOARCH=arm64 go build -o ./bin/circleci-fake-agent-arm64 ./internal/fake-agent
34+
VERSION="${GORELEASER_VERSION}" \
35+
BUILD_VERSION="${BUILD_VERSION:-dev}" ./bin/goreleaser build \
36+
--clean \
37+
--config ./.goreleaser/binaries/builds.yaml \
38+
--single-target \
39+
--skip=validate \
40+
--snapshot "$@"
1641
}
1742

1843
# This variable is used, but shellcheck can't tell.

‎go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module github.com/circleci/circleci-fake-agent
1+
module github.com/circleci/runner-init
22

33
go 1.22.0
44

File renamed without changes.

‎runner-init/fake-agent.Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ FROM busybox:stable-musl as build
22

33
ARG ARCH=amd64
44

5-
COPY ./bin/circleci-fake-agent-${ARCH} /opt/circleci/bin/circleci-agent
5+
COPY ./target/bin/${ARCH}/fake-task-agent /opt/circleci/bin/circleci-agent
66
COPY ./runner-init/init.sh /init.sh
77

88
ENTRYPOINT ["/init.sh"]

‎version.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.0.0

0 commit comments

Comments
 (0)
Failed to load comments.