Skip to content

Commit

Permalink
ci(build): Update build to use make (#160)
Browse files Browse the repository at this point in the history
Previously we relied upon `GO_LDFLAGS` to be passed in to get information such as the branch, revision, and version information. Another pattern we can follow is to use the existing `make build-binary` command which as all of the scripting defined for the necessary variables to build up the `GO_LDFLAGS`. In order to support this, we need `git` and `make` installed, which thankfully come bundled with the regular golang image.

Updates `VERSION` to be `git describe --tags --dirty --always` which will use the the tag information from git directly. The neat this about this command is that if you're on a branch that isn't tagged, it'll return the latest tag and append an abbreviation of the current commit which will guarantee uniqueness between branches. This will support us making "dirty" releases on main, and then when we create a new tags, we'll have an image created with that specific tag. 

To test out, I've built locally with a tag `poko-test-local`:

```
make build-image                                                                                                                                                                                                                           ~/dev/grafana/cloudcost-exporter
docker build --build-arg GO_LDFLAGS="-X github.com/prometheus/common/version.Branch=ci/update-build-binary-to-use-make -X github.com/prometheus/common/version.Version=dev-poko-test-local -X github.com/prometheus/common/version.Revision=a7db1f2 -X github.com/prometheus/common/version.BuildUser=poko@roberto -X github.com/prometheus/common/version.BuildDate=2024-05-01T17:54:56Z" -t grafana/cloudcost-exporter -t grafana/cloudcost-exporter:dev-poko-test-local .
[+] Building 22.3s (17/17) FINISHED                                                                                                                                                                                                                                                                 docker:default
 => [internal] load build definition from Dockerfile                                                                                                                                                                                                                                                          0.0s
 => => transferring dockerfile: 427B                                                                                                                                                                                                                                                                          0.0s
 => [internal] load metadata for docker.io/library/alpine:latest                                                                                                                                                                                                                                              0.0s
 => [internal] load metadata for docker.io/library/golang:1.22.1                                                                                                                                                                                                                                              0.4s
 => [internal] load .dockerignore                                                                                                                                                                                                                                                                             0.0s
 => => transferring context: 2B                                                                                                                                                                                                                                                                               0.0s
 => [build 1/6] FROM docker.io/library/golang:1.22.1@sha256:0b55ab82ac2a54a6f8f85ec8b943b9e470c39e32c109b766bbc1b801f3fa8d3b                                                                                                                                                                                  0.0s
 => FROM docker.io/library/alpine:latest                                                                                                                                                                                                                                                                      0.0s
 => [internal] load build context                                                                                                                                                                                                                                                                             0.1s
 => => transferring context: 348.99kB                                                                                                                                                                                                                                                                         0.1s
 => CACHED [build 2/6] WORKDIR /app                                                                                                                                                                                                                                                                           0.0s
 => CACHED [build 3/6] COPY [go.mod, go.sum, ./]                                                                                                                                                                                                                                                              0.0s
 => CACHED [build 4/6] RUN go mod download                                                                                                                                                                                                                                                                    0.0s
 => [build 5/6] COPY . .                                                                                                                                                                                                                                                                                      0.2s
 => [build 6/6] RUN make build-binary                                                                                                                                                                                                                                                                        20.5s
 => CACHED [stage-1 1/4] COPY --from=alpine:latest /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/                                                                                                                                                                                                         0.0s 
 => CACHED [stage-1 2/4] COPY --from=build /etc/passwd /etc/passwd                                                                                                                                                                                                                                            0.0s 
 => CACHED [stage-1 3/4] WORKDIR /root                                                                                                                                                                                                                                                                        0.0s 
 => [stage-1 4/4] COPY --from=build /app/cloudcost-exporter ./                                                                                                                                                                                                                                                0.1s 
 => exporting to image                                                                                                                                                                                                                                                                                        0.2s 
 => => exporting layers                                                                                                                                                                                                                                                                                       0.2s 
 => => writing image sha256:098a4862eaf8574964646dc1038bc832a70e61ff10a438354cd2673b4fa1c413                                                                                                                                                                                                                  0.0s
 => => naming to docker.io/grafana/cloudcost-exporter                                                                                                                                                                                                                                                         0.0s
 => => naming to docker.io/grafana/cloudcost-exporter:dev-poko-test-local 
```

When running the image locally, you'll note the version gets propogated:

```
docker run docker.io/grafana/cloudcost-exporter:dev-poko-test-local                                                                                                                                                                        ~/dev/grafana/cloudcost-exporter
2024/05/01 17:57:33 Version (version=dev-poko-test-local, branch=ci/update-build-binary-to-use-make, revision=a7db1f2)
```

- refs #5
  • Loading branch information
Pokom authored May 2, 2024
1 parent ea1d557 commit 89e6843
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 11 deletions.
5 changes: 2 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# Build Go Binary
FROM golang:1.22.1-alpine AS build
ARG GO_LDFLAGS
FROM golang:1.22.1 AS build

WORKDIR /app
COPY ["go.mod", "go.sum", "./"]
RUN go mod download

COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -a -ldflags "${GO_LDFLAGS} -extldflags '-static'" -o cloudcost-exporter ./cmd/exporter
RUN make build-binary

# Build Image
FROM scratch
Expand Down
11 changes: 3 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
.PHONY: build-image build-binary build test push push-dev

current_makefile_dir := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
VERSION=$(shell git describe --tags --dirty --always)

# deployment_tools/docker/image-tag.sh
VERSION=dev-$(shell date +%Y-%m-%d)-$(shell git rev-parse --short HEAD)

# deployment_tools/docker/common.inc
IMAGE_PREFIX=us.gcr.io/kubernetes-dev
IMAGE_PREFIX=grafana

IMAGE_NAME=cloudcost-exporter
IMAGE_NAME_LATEST=${IMAGE_PREFIX}/${IMAGE_NAME}:latest
IMAGE_NAME_VERSION=$(IMAGE_PREFIX)/$(IMAGE_NAME):$(VERSION)

WORKFLOW_TEMPLATE=cloudcost-exporter
WORKFLOW_NAMESPACE=capacity-cd

PROM_VERSION_PKG ?= github.com/prometheus/common/version
BUILD_USER ?= $(shell whoami)@$(shell hostname)
Expand All @@ -26,7 +21,7 @@ build-image:
docker build --build-arg GO_LDFLAGS="$(GO_LDFLAGS)" -t $(IMAGE_PREFIX)/$(IMAGE_NAME) -t $(IMAGE_NAME_VERSION) .

build-binary:
go build -v -ldflags "$(GO_LDFLAGS)" -o cloudcost-exporter ./cmd/exporter
CGO_ENABLED=0 go build -v -ldflags "$(GO_LDFLAGS)" -o cloudcost-exporter ./cmd/exporter

build: build-binary build-image

Expand Down

0 comments on commit 89e6843

Please sign in to comment.