grafana / cloudcost-exporter

Prometheus Exporter for Cloud Provider agnostic cost metrics
Apache License 2.0
66 stars 4 forks source link

ci(build): Update build to use make #160

Closed Pokom closed 6 months ago

Pokom commented 6 months ago

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)