Skip to content

Commit

Permalink
Handle build info more accurately
Browse files Browse the repository at this point in the history
`runtime.Version()` will always give an accurate build host version

For the host's GOOS and host's GOARCH they're not available so they must
still be passed in via ldflags

The Makefile has been modified to use goreleaser for the fastly item
  • Loading branch information
06kellyjac committed Jun 29, 2022
1 parent 7844f9f commit 8bf5dfc
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 16 deletions.
12 changes: 7 additions & 5 deletions .github/workflows/tag_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ jobs:
uses: actions/setup-go@v2
with:
go-version: '1.18.x'
- name: "Set GOVERSION"
id: set_goversion
run: echo "GOVERSION=$(go version)" >> $GITHUB_ENV
- name: "Set GOHOSTOS and GOHOSTARCH"
run: echo "GOHOSTOS=$(go env GOHOSTOS)" >> $GITHUB_ENV && echo "GOHOSTARCH=$(go env GOHOSTARCH)" >> $GITHUB_ENV
- name: "Download latest app config"
run: |
make config
Expand All @@ -41,11 +40,14 @@ jobs:
- name: "Run GoReleaser"
uses: goreleaser/goreleaser-action@v2
with:
version: v1.5.0 # goreleaser version (NOT goreleaser-action version)
# goreleaser version (NOT goreleaser-action version)
# update inline with the Makefile
version: v1.9.2
args: release --rm-dist
env:
AUR_KEY: '${{ github.workspace }}/aur_key'
GOVERSION: ${{ env.GOVERSION }}
GOHOSTOS: ${{ env.GOHOSTOS }}
GOHOSTARCH: ${{ env.GOHOSTARCH }}
GITHUB_TOKEN: ${{ secrets.RELEASE_GITHUB_TOKEN }}
- name: "Generate release commits"
id: generate-commits
Expand Down
3 changes: 2 additions & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ builds:
ldflags:
- -s -w -X "github.com/fastly/cli/pkg/revision.AppVersion=v{{ .Version }}"
- -X "github.com/fastly/cli/pkg/revision.GitCommit={{ .ShortCommit }}"
- -X "github.com/fastly/cli/pkg/revision.GoVersion={{ .Env.GOVERSION }}"
- -X "github.com/fastly/cli/pkg/revision.GoHostOS={{ .Env.GOHOSTOS }}"
- -X "github.com/fastly/cli/pkg/revision.GoHostArch={{ .Env.GOHOSTARCH }}"
- -X "github.com/fastly/cli/pkg/revision.Environment=release"
id: macos
goos: [darwin]
Expand Down
19 changes: 15 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,28 @@ TESTARGS ?= -timeout 15m ./{cmd,pkg}/...

CLI_ENV ?= "development"

# TODO: This is duplicated in .goreleaser and we should figure out how to clean
# this up so we have one source of truth.
GOHOSTOS ?= $(shell go env GOHOSTOS || echo unknown)
GOHOSTARCH ?= $(shell go env GOHOSTARCH || echo unknown)

LDFLAGS = -ldflags "\
-X 'github.com/fastly/cli/pkg/revision.AppVersion=${VERSION}' \
-X 'github.com/fastly/cli/pkg/revision.GitCommit=$(shell git rev-parse --short HEAD || echo unknown)' \
-X 'github.com/fastly/cli/pkg/revision.GoVersion=$(shell go version)' \
-X 'github.com/fastly/cli/pkg/revision.GoHostOS=${GOHOSTOS}' \
-X 'github.com/fastly/cli/pkg/revision.GoHostArch=${GOHOSTARCH}' \
-X 'github.com/fastly/cli/pkg/revision.Environment=${CLI_ENV}' \
"

GO_FILES = $(shell find cmd pkg -type f -name '*.go')

# You can pass flags to goreleaser via GORELEASER_ARGS
# --skip-validate will skip the checks
# --rm-dist will save you deleting the dist dir
# --single-target will be quicker and only build for your os & architecture
# e.g.
# make fastly GORELEASER_ARGS="--skip-validate --rm-dist"
fastly: $(GO_FILES)
@go build -trimpath $(LDFLAGS) -o "$@" ./cmd/fastly
@GOHOSTOS="${GOHOSTOS}" GOHOSTARCH="${GOHOSTARCH}" goreleaser build ${GORELEASER_ARGS}


# useful for attaching a debugger such as https://github.com/go-delve/delve
debug:
Expand All @@ -34,11 +43,13 @@ debug:
.PHONY: all
all: dependencies config tidy fmt vet staticcheck gosec test build install

# update goreleaser inline with the release GHA workflow
.PHONY: dependencies
dependencies:
go install github.com/securego/gosec/v2/cmd/gosec@latest
go install honnef.co/go/tools/cmd/staticcheck@latest
go install github.com/mgechev/revive@latest
go install github.com/goreleaser/goreleaser@v1.9.2

.PHONY: tidy
tidy:
Expand Down
34 changes: 28 additions & 6 deletions pkg/revision/revision.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,36 @@
// the Makefile at build time via LDFLAGS.
package revision

import "strings"
import (
"fmt"
"runtime"
"strings"
)

var (
// AppVersion is the semver for this version of the client, or
// "v0.0.0-unknown". Set by `make release`.
// "v0.0.0-unknown". Handled by goreleaser.
AppVersion string

// GitCommit is the short git SHA associated with this build, or
// "unknown". Set by `make release`.
// "unknown". Handled by goreleaser.
GitCommit string

// GoVersion is the output of `go version` associated with this build, or
// "go version unknown". Set by `make release`.
// GoVersion - Prefer letting the code handle this and set GoHostOS and
// GoHostArc instead. It can be set to the build host's `go version` output.
GoVersion string

// GoHostOS is the output of `go env GOHOSTOS` Passed to goreleaser by
// `make fastly` or the GHA workflow.
GoHostOS string

// GoHostArch is the output of `go env GOHOSTARCH` Passed to goreleaser by
// `make fastly` or the GHA workflow.
GoHostArch string

// Environment is set to either "development" (when working locally) or
// "release" when the code being executed is from a published release.
// Handled by goreleaser.
Environment string
)

Expand All @@ -32,8 +45,17 @@ func init() {
if GitCommit == "" {
GitCommit = "unknown"
}
if GoHostOS == "" {
GoHostOS = "unknown"
}
if GoHostArch == "" {
GoHostArch = "unknown"
}
if GoVersion == "" {
GoVersion = "go version unknown"
// runtime.Version() provides the Go tree's version string at build time
// the other values like OS and Arch aren't accessable and are passed in
// separately
GoVersion = fmt.Sprintf("go version %s %s/%s", runtime.Version(), GoHostOS, GoHostArch)
}
if Environment == "" {
Environment = "development"
Expand Down

0 comments on commit 8bf5dfc

Please sign in to comment.