Skip to content

Commit

Permalink
Merge pull request #12 from circleci/ONPREM-1263/use-goreleaser
Browse files Browse the repository at this point in the history
Build binaries using GoReleaser
  • Loading branch information
christian-stephen authored Jun 12, 2024
2 parents 3ea76c1 + 4dc7eb8 commit 0731ed3
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 25 deletions.
57 changes: 40 additions & 17 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -42,9 +45,7 @@ workflows:

jobs:
lint:
docker:
- image: *go_image
resource_class: circleci-runner/rum-large
executor: go
steps:
- setup
- run:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
27 changes: 27 additions & 0 deletions .goreleaser/binaries/builds.yaml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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:

Expand All @@ -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.
20 changes: 20 additions & 0 deletions cmd/orchestrator/main.go
Original file line number Diff line number Diff line change
@@ -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
}
7 changes: 7 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package cmd

// Variables to be set by the linker
var (
Version = "dev"
Date = ""
)
35 changes: 30 additions & 5 deletions do
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/circleci/circleci-fake-agent
module github.com/circleci/runner-init

go 1.22.0

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion runner-init/fake-agent.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
1 change: 1 addition & 0 deletions version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.0.0

0 comments on commit 0731ed3

Please sign in to comment.