Skip to content

Commit

Permalink
chore: automate go version upgrade
Browse files Browse the repository at this point in the history
We will use the latest version of Golang for executing the tests.
We change the structure of the Makefile and the run-test GitHub action
to bump the Golang version automatically in the Makefile, go.mod
and GitHub action files.

[#186180915](https://www.pivotaltracker.com/story/show/186180915)
  • Loading branch information
zucchinidev committed Nov 13, 2023
1 parent 13faaf9 commit af87d20
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
9 changes: 3 additions & 6 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@ on:
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
version: [ '1.20', '1.21' ]
name: Go ${{ matrix.version }}
name: Go test
outputs:
pr_number: ${{ github.event.number }}
steps:
- uses: actions/setup-go@v2
with:
go-version: ${{ matrix.version }}
- uses: actions/checkout@v2
go-version: '1.21.4'
- uses: actions/checkout@v4
- run: make test
call-dependabot-pr-workflow:
needs: test
Expand Down
49 changes: 39 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,62 @@

.DEFAULT_GOAL = help

GO-VERSION = 1.21.4
GO-VER = go$(GO-VERSION)

GO_OK := $(or $(USE_GO_CONTAINERS), $(shell which go 1>/dev/null 2>/dev/null; echo $$?))
DOCKER_OK := $(shell which docker 1>/dev/null 2>/dev/null; echo $$?)

ifeq ($(GO_OK), 0) # use local go binary

PAK_PATH=$(PWD)
GO=go
GOFMT=gofmt

else ifeq ($(DOCKER_OK), 0)

PAK_PATH=/brokerapi
GO_DOCKER_OPTS=--rm -v $(PWD):$(PAK_PATH) -w $(PAK_PATH) --network=host
GO=docker run $(GO_DOCKER_OPTS) golang:latest go
GOFMT=docker run $(GO_DOCKER_OPTS) golang:latest gofmt

else
$(error either Go or Docker must be installed)
endif

.PHONY: help

help: ## list Makefile targets
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

###### Targets ################################################################

test: version download fmt vet ginkgo ## Runs all build, static analysis, and test steps
test: deps-go-binary download fmt vet ginkgo ## Runs all build, static analysis, and test steps

download: ## Download dependencies
go mod download
${GO} mod download

vet: ## Run static code analysis
go vet ./...
go run honnef.co/go/tools/cmd/staticcheck ./...
${GO} vet ./...
${GO} run honnef.co/go/tools/cmd/staticcheck ./...

ginkgo: ## Run tests using Ginkgo
go run github.com/onsi/ginkgo/v2/ginkgo -r
${GO} run github.com/onsi/ginkgo/v2/ginkgo -r

fmt: ## Checks that the code is formatted correctly
@@if [ -n "$$(gofmt -s -e -l -d .)" ]; then \
@@if [ -n "$$(${GOFMT} -s -e -l -d .)" ]; then \
echo "gofmt check failed: run 'gofmt -d -e -l -w .'"; \
exit 1; \
fi

generate: ## Generates the fakes using counterfeiter
go generate ./...

version: ## Display the version of Go
@@go version
${GO} generate ./...

.PHONY: deps-go-binary
deps-go-binary:
ifeq ($(SKIP_GO_VERSION_CHECK),)
@@if [ "$$($(GO) version | awk '{print $$3}')" != "${GO-VER}" ]; then \
echo "Go version does not match: expected: ${GO-VER}, got $$($(GO) version | awk '{print $$3}')"; \
exit 1; \
fi
endif

0 comments on commit af87d20

Please sign in to comment.