Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add build flags #76

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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)"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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)"
run: go build -ldflags "-X github.com/letsencrypt/unbound_exporter/build.Revision=$(git rev-parse --short HEAD) -X github.com/letsencrypt/unbound_exporter/build.Branch=$(git rev-parse --abbrev-ref HEAD)"


- name: install nfpm
run: go install github.com/goreleaser/nfpm/v2/cmd/[email protected]
Expand Down
15 changes: 2 additions & 13 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
28 changes: 28 additions & 0 deletions build/vars.go
Original file line number Diff line number Diff line change
@@ -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)")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// BuildID is set by the compiler (using -ldflags "-X util.BuildID $(git rev-parse --short HEAD)")
// Revision is set by the compiler (using -ldflags "-X build.Revision $(git rev-parse --short HEAD)")

// and is used by GetID
var BuildID string
Comment on lines +5 to +7

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// BuildID is set by the compiler (using -ldflags "-X util.BuildID $(git rev-parse --short HEAD)")
// and is used by GetID
var BuildID string
// Revision is set by the compiler (using -ldflags "-X build.Revision $(git rev-parse --short HEAD)")
// and is used by GetRevision
var Revision string


// BuildBranch is set by the compiler and is used by GetBranch

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// BuildBranch is set by the compiler and is used by GetBranch
// BuildBranch is set by the compiler (using -ldflags "-X build.Branch $git rev-parse --abbrev-rev HEAD)")
// and is used by GetBranch

var BuildBranch string
Comment on lines +9 to +10

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// BuildBranch is set by the compiler and is used by GetBranch
var BuildBranch string
// Branch is set by the compiler and is used by GetBranch
var Branch string


// GetBuildID identifies what build is running.
func GetID() (BuildID string) {
if BuildID != "" {
return BuildID
Comment on lines +12 to +15

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// GetBuildID identifies what build is running.
func GetID() (BuildID string) {
if BuildID != "" {
return BuildID
// GetRevision identifies what commit this binary was built from.
func GetRevision() string {
if Revision != "" {
return Revision

}

return Unspecified
}

// GetBranch identifies the building host
func GetBranch() (BuildBranch string) {
if BuildBranch != "" {
return BuildBranch
Comment on lines +21 to +24

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// GetBranch identifies the building host
func GetBranch() (BuildBranch string) {
if BuildBranch != "" {
return BuildBranch
// GetBranch identifies what git branch this binary was built from.
func GetBranch() string {
if Branch != "" {
return Branch

}

return Unspecified
}
7 changes: 6 additions & 1 deletion unbound_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
Loading