Skip to content

Commit

Permalink
Handle build info more accurately (#588)
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 authored Jun 29, 2022
1 parent 7844f9f commit 8ef2fd9
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 31 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
35 changes: 21 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,47 @@ SHELL := /bin/bash -o pipefail
# the output is consistent across environments.
VERSION ?= $(shell git describe --tags 2>/dev/null || git rev-parse --short HEAD)

# allows for passing additional args to the build e.g.
# make build GO_ARGS='--ldflags "-s -w"'
GO_ARGS ?= ""

# Enables support for tools such as https://github.com/rakyll/gotest
TEST_COMMAND ?= go test

# The compute tests can sometimes exceed the default 10m limit.
TESTARGS ?= -timeout 15m ./{cmd,pkg}/...
TEST_ARGS ?= -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.
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.Environment=${CLI_ENV}' \
"
GOHOSTOS ?= $(shell go env GOHOSTOS || echo unknown)
GOHOSTARCH ?= $(shell go env GOHOSTARCH || echo unknown)

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:
@go build -gcflags="all=-N -l" $(LDFLAGS) -o "fastly" ./cmd/fastly
@go build -gcflags="all=-N -l" $(GO_ARGS) -o "fastly" ./cmd/fastly

.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/[email protected]

.PHONY: tidy
tidy:
Expand Down Expand Up @@ -67,15 +74,15 @@ staticcheck:

.PHONY: test
test: config
@$(TEST_COMMAND) -race $(TESTARGS)
@$(TEST_COMMAND) -race $(TEST_ARGS)

.PHONY: build
build: config
go build $(LDFLAGS) ./cmd/fastly
go build $(GO_ARGS) ./cmd/fastly

.PHONY: install
install: config
go install $(LDFLAGS) ./cmd/fastly
go install $(GO_ARGS) ./cmd/fastly

.PHONY: changelog
changelog:
Expand Down
6 changes: 3 additions & 3 deletions TESTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ Note that by default the tests are run using `go test` with the following config
To run a specific test use the `-run` flag (exposed by `go test`) and also provide the path to the directory where the test files reside (replace `...` and `<path>` with appropriate values):

```sh
make test TESTARGS="-run <...> <path>"
make test TEST_ARGS="-run <...> <path>"
```

**Example**:

```sh
make test TESTARGS="-run TestBackendCreate ./pkg/backend/..."
make test TEST_ARGS="-run TestBackendCreate ./pkg/backend/..."
```

Some integration tests aren't run outside of the CI environment, to enable these tests locally you'll need to set a specific environment variable relevant to the test.
Expand All @@ -38,7 +38,7 @@ The available environment variables are:
**Example**:

```sh
TEST_COMPUTE_BUILD_RUST=1 make test TESTARGS="-run TestBuildRust/fastly_crate_prerelease ./pkg/compute/..."
TEST_COMPUTE_BUILD_RUST=1 make test TEST_ARGS="-run TestBuildRust/fastly_crate_prerelease ./pkg/compute/..."
```

When running the tests locally, if you don't have the relevant language ecosystems set-up properly then the tests will fail to run and you'll need to review the code to see what the remediation steps are, as that output doesn't get shown when running the test suite.
Expand Down
41 changes: 33 additions & 8 deletions pkg/revision/revision.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
// Package revision defines variables that will be populated with values from
// the Makefile at build time via LDFLAGS.
// Package revision defines variables that will be populated with values
// specified at build time via LDFLAGS. goreleaser will prompt for missing env
// variables.
// For more details on LDFLAGS:
// https://github.com/golang/go/wiki/GcToolchainTricks#including-build-information-in-the-executable
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 +48,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 accessible 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 8ef2fd9

Please sign in to comment.