From 4dc7eb8dac3008757eeac9825bab0af63157dcf2 Mon Sep 17 00:00:00 2001 From: Chris Stephen <chris.stephen@circleci.com> Date: Wed, 12 Jun 2024 12:17:18 -0300 Subject: [PATCH] Build binaries using GoReleaser --- .circleci/config.yml | 57 +++++++++++++------ .goreleaser/binaries/builds.yaml | 27 +++++++++ CHANGELOG.md | 3 +- cmd/orchestrator/main.go | 20 +++++++ cmd/version.go | 7 +++ do | 35 ++++++++++-- go.mod | 2 +- .../{fake-agent => faketaskagent}/main.go | 0 runner-init/fake-agent.Dockerfile | 2 +- version.txt | 1 + 10 files changed, 129 insertions(+), 25 deletions(-) create mode 100644 .goreleaser/binaries/builds.yaml create mode 100644 cmd/orchestrator/main.go create mode 100644 cmd/version.go rename internal/{fake-agent => faketaskagent}/main.go (100%) create mode 100644 version.txt diff --git a/.circleci/config.yml b/.circleci/config.yml index 981de36..b24963c 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -12,24 +12,27 @@ x-data: linux_machine_image: &linux_machine_image ubuntu-2204:2024.02.7 go_image: &go_image cimg/go:1.22 +executors: + go: + docker: + - image: *go_image + resource_class: circleci-runner/rum-large + workflows: main-workflow: jobs: - lint - - prepare-agents: - context: org-global + - build - build-and-publish-images: name: build-and-publish-image-amd64 context: org-global - requires: - - prepare-agents + requires: [ lint, build ] - build-and-publish-images: name: build-and-publish-image-arm64 resource: arm.medium arch: arm64 context: org-global - requires: - - prepare-agents + requires: [ lint, build ] - publish-manifest: context: org-global filters: @@ -42,9 +45,7 @@ workflows: jobs: lint: - docker: - - image: *go_image - resource_class: circleci-runner/rum-large + executor: go steps: - setup - run: @@ -77,17 +78,19 @@ jobs: - store_results - notify_failing_main - prepare-agents: - docker: - - image: *go_image - resource_class: circleci-runner/rum-large + build: + executor: go steps: - - checkout - - run: ./do build-fake-agents + - goreleaser_setup + - run: go mod download + - run: + name: Build binaries + command: | + BUILD_VERSION="$(<version.txt)-<< pipeline.number >>-$(git rev-parse --short HEAD 2>/dev/null || echo latest)" \ + ./do build - persist_to_workspace: root: . - paths: - - "./bin/circleci-*" + paths: [ target ] - notify_failing_main build-and-publish-images: @@ -145,6 +148,26 @@ commands: - store_test_results: path: test-reports + goreleaser_setup: + steps: + - checkout + - attach_workspace: + at: . + - run: + name: Install GoReleaser + command: | + if [[ "$(command -v apk)" ]]; then + # Alpine doesn't come with the coreutils' sha256sum, which the GoReleaser Bash script expects... + apk upgrade && apk add coreutils + fi + + curl -sfL https://goreleaser.com/static/run -o ./bin/goreleaser --create-dirs && chmod +x ./bin/goreleaser + + if [[ $(uname -m) == aarch64 ]]; then + # A little hack to get the GoReleaser Bash script working on ARM64... + sed -i 's/$(uname -m)/arm64/g' ./bin/goreleaser + fi + docker_login: steps: - run: diff --git a/.goreleaser/binaries/builds.yaml b/.goreleaser/binaries/builds.yaml new file mode 100644 index 0000000..7371f24 --- /dev/null +++ b/.goreleaser/binaries/builds.yaml @@ -0,0 +1,27 @@ +dist: ./target + +builds: + - id: orchestrator + main: ./cmd/orchestrator + binary: ./bin/{{.Arch}}/orchestrator + ldflags: &ldflags + - -s -w + - -X github.com/circleci/runner-init/cmd.Version={{.Env.BUILD_VERSION}} + - -X github.com/circleci/runner-init/cmd.Date={{.Date}} + env: [CGO_ENABLED=0] + goos: [linux] + goarch: [amd64, arm64] + no_unique_dist_dir: true + + - id: fake-task-agent + main: ./internal/faketaskagent + binary: ./bin/{{.Arch}}/fake-task-agent + ldflags: + - -s -w + env: [CGO_ENABLED=0] + goos: [linux] + goarch: [amd64, arm64] + no_unique_dist_dir: true + +release: + disable: true diff --git a/CHANGELOG.md b/CHANGELOG.md index e1076f8..94865bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -# CircleCI Init Changelog +# CircleCI Runner Init Changelog 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: @@ -10,5 +10,6 @@ By following these guidelines, we can easily determine which changes should be i ## Edge +- [#12](https://github.com/circleci/runner-init/pull/12) [INTERNAL] Use [GoReleaser](https://goreleaser.com/) For building the binaries. - [#11](https://github.com/circleci/runner-init/pull/11) [INTERNAL] Download task agent binaries directly via the Dockerfile. - [#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. diff --git a/cmd/orchestrator/main.go b/cmd/orchestrator/main.go new file mode 100644 index 0000000..9035da7 --- /dev/null +++ b/cmd/orchestrator/main.go @@ -0,0 +1,20 @@ +package main + +import ( + "log" //nolint:depguard // a non-O11y log is allowed for a top-level fatal exit + + "github.com/circleci/runner-init/cmd" +) + +func main() { + if err := run(cmd.Version, cmd.Date); err != nil { + log.Fatal(err) + } +} + +//nolint:unparam // TODO +func run(version, date string) (err error) { + println(version, date) + + return err +} diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 0000000..627534c --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,7 @@ +package cmd + +// Variables to be set by the linker +var ( + Version = "dev" + Date = "" +) diff --git a/do b/do index 816c247..cb5b637 100755 --- a/do +++ b/do @@ -7,12 +7,37 @@ GORELEASER_VERSION="v1.26.2" # This variable is used, but shellcheck can't tell. # shellcheck disable=SC2034 -help_build_fake_agents="Build the fake agent go binaries" -build-fake-agents() { - export CGO_ENABLED=0 +help_build="Build the Go binaries and archives" +build() { + set -x + + [[ -f ./bin/goreleaser ]] || install-go-bin "github.com/goreleaser/goreleaser@latest" + + VERSION="${GORELEASER_VERSION}" \ + BUILD_VERSION="${BUILD_VERSION:?'required'}" ./bin/goreleaser \ + --clean \ + --config "${BUILD_CONFIG:-./.goreleaser/binaries/builds.yaml}" \ + --skip=validate \ + --snapshot "$@" + + echo "${BUILD_VERSION}" | tee ./target/version.txt +} + +# This variable is used, but shellcheck can't tell. +# shellcheck disable=SC2034 +help_dev_binary="Build single target Go binaries for local dev" +dev-build() { + set -x + + [[ -f ./bin/goreleaser ]] || install-go-bin "github.com/goreleaser/goreleaser@latest" - GOOS=linux GOARCH=amd64 go build -o ./bin/circleci-fake-agent-amd64 ./internal/fake-agent - GOOS=linux GOARCH=arm64 go build -o ./bin/circleci-fake-agent-arm64 ./internal/fake-agent + VERSION="${GORELEASER_VERSION}" \ + BUILD_VERSION="${BUILD_VERSION:-dev}" ./bin/goreleaser build \ + --clean \ + --config ./.goreleaser/binaries/builds.yaml \ + --single-target \ + --skip=validate \ + --snapshot "$@" } # This variable is used, but shellcheck can't tell. diff --git a/go.mod b/go.mod index d2e23ff..2c05e64 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/circleci/circleci-fake-agent +module github.com/circleci/runner-init go 1.22.0 diff --git a/internal/fake-agent/main.go b/internal/faketaskagent/main.go similarity index 100% rename from internal/fake-agent/main.go rename to internal/faketaskagent/main.go diff --git a/runner-init/fake-agent.Dockerfile b/runner-init/fake-agent.Dockerfile index a765736..d958d9b 100644 --- a/runner-init/fake-agent.Dockerfile +++ b/runner-init/fake-agent.Dockerfile @@ -2,7 +2,7 @@ FROM busybox:stable-musl as build ARG ARCH=amd64 -COPY ./bin/circleci-fake-agent-${ARCH} /opt/circleci/bin/circleci-agent +COPY ./target/bin/${ARCH}/fake-task-agent /opt/circleci/bin/circleci-agent COPY ./runner-init/init.sh /init.sh ENTRYPOINT ["/init.sh"] diff --git a/version.txt b/version.txt new file mode 100644 index 0000000..77d6f4c --- /dev/null +++ b/version.txt @@ -0,0 +1 @@ +0.0.0