diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..9da8389 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,14 @@ +# Ignore files +*.md +*.sh +*.yml +*.yaml +.gitignore + +# Ignore folders +.github/ +.git/ +contrib/ + +# Don't include a previously built binary +unbound_exporter diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index d7c3822..aad52c2 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -23,7 +23,7 @@ jobs: persist-credentials: false - name: build binary - run: go build + run: go build -ldflags "-X github.com/letsencrypt/unbound_exporter/util.BuildID=$(git rev-parse --short HEAD) -X github.com/letsencrypt/unbound_exporter/util.BuildBranch=$(git rev-parse --abbrev-ref HEAD) -X github.com/letsencrypt/unbound_exporter/util.BuildTime=$(date +%F-%T -u)" - name: install nfpm run: go install github.com/goreleaser/nfpm/v2/cmd/nfpm@v2.15.1 diff --git a/Dockerfile b/Dockerfile index 3977962..f33c42e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,20 +1,9 @@ FROM --platform=$BUILDPLATFORM docker.io/library/golang:1.21.4-bookworm AS build - WORKDIR /go/src/app - -COPY go.mod . -COPY go.sum . - +COPY . . RUN go mod download - -COPY *.go . - -ENV CGO_ENABLED=0 - -RUN GOOS=$TARGETOS GOARCH=$TARGETPLATFORM go build -v -o /go/bin/unbound_exporter ./... +RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETPLATFORM go build -v -o /go/bin/unbound_exporter FROM gcr.io/distroless/static-debian12 - COPY --from=build /go/bin/unbound_exporter / - ENTRYPOINT ["/unbound_exporter"] diff --git a/build/vars.go b/build/vars.go new file mode 100644 index 0000000..0591395 --- /dev/null +++ b/build/vars.go @@ -0,0 +1,28 @@ +package build + +const Unspecified = "Unspecified" + +// BuildID is set by the compiler (using -ldflags "-X util.BuildID $(git rev-parse --short HEAD)") +// and is used by GetID +var BuildID string + +// BuildBranch is set by the compiler and is used by GetBranch +var BuildBranch string + +// GetBuildID identifies what build is running. +func GetID() (BuildID string) { + if BuildID != "" { + return BuildID + } + + return Unspecified +} + +// GetBranch identifies the building host +func GetBranch() (BuildBranch string) { + if BuildBranch != "" { + return BuildBranch + } + + return Unspecified +} diff --git a/unbound_exporter.go b/unbound_exporter.go index 291bb00..cc0aacb 100644 --- a/unbound_exporter.go +++ b/unbound_exporter.go @@ -25,12 +25,14 @@ import ( "net/url" "os" "regexp" + "runtime" "strconv" "strings" "sort" "github.com/go-kit/log/level" + "github.com/letsencrypt/unbound_exporter/build" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/common/promlog" @@ -548,7 +550,10 @@ func main() { ) flag.Parse() - _ = level.Info(log).Log("Starting unbound_exporter") + _ = level.Info(log).Log( + "msg", "Starting unbound_exporter", + "version", fmt.Sprintf("(version=%s, branch=%s, revision=%s)", runtime.Version(), build.GetBranch(), build.GetID()), + ) exporter, err := NewUnboundExporter(*unboundHost, *unboundCa, *unboundCert, *unboundKey) if err != nil { panic(err)