From 2da05bcd8a9cd5e2ff2c109fbee79d7b19c64258 Mon Sep 17 00:00:00 2001 From: Takeshi Yoneda Date: Tue, 26 Nov 2024 09:31:07 -0800 Subject: [PATCH] Initial CI setup Signed-off-by: Takeshi Yoneda --- .github/workflows/commit.yaml | 54 +++++++++++++++++++++++++++++++++ .gitignore | 3 ++ Makefile | 42 +++++++++++++++++++++++++ Makefile.tools.mk | 44 +++++++++++++++++++++++++++ go.mod | 3 ++ internal/cmd/controller/main.go | 5 +++ 6 files changed, 151 insertions(+) create mode 100644 .github/workflows/commit.yaml create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 Makefile.tools.mk create mode 100644 go.mod create mode 100644 internal/cmd/controller/main.go diff --git a/.github/workflows/commit.yaml b/.github/workflows/commit.yaml new file mode 100644 index 00000000..ee51470f --- /dev/null +++ b/.github/workflows/commit.yaml @@ -0,0 +1,54 @@ +name: Commit +on: + pull_request: + branches: + - main + push: + branches: + - main + +concurrency: + # https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#example-using-concurrency-to-cancel-any-in-progress-job-or-run + group: ${{ github.ref }}-${{ github.workflow }}-${{ github.actor }} + cancel-in-progress: true + +jobs: + style: + # This verifies the code is formatted correctly via `make precommit`. + name: Code Style Check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-go@v5 + with: + cache: false + go-version-file: go.mod + - uses: actions/cache@v4 + with: + path: | + ~/.cache/go-build + ~/.cache/golangci-lint + ~/go/pkg/mod + ~/go/bin + key: code-style-check-${{ hashFiles('**/go.mod', '**/go.sum', '**/Makefile') }} + - name: Run code style check + run: make check + + unittest: + name: Unit Test + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-go@v5 + with: + cache: false + go-version-file: go.mod + - uses: actions/cache@v4 + with: + path: | + ~/.cache/go-build + ~/go/pkg/mod + ~/go/bin + key: unittest-${{ hashFiles('**/go.mod', '**/go.sum', '**/Makefile') }} + - name: Run unit tests + run: make test diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..8ba5370c --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.kube +.bin +out/ diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..10c17312 --- /dev/null +++ b/Makefile @@ -0,0 +1,42 @@ +# The Go-based tools are defined in Makefile.tools.mk. +include Makefile.tools.mk + +.PHONY: lint +lint: golangci-lint + @echo "lint => ./..." + @$(GOLANGCI_LINT) run --build-tags=$(LINT_BUILD_TAGS) ./... + +.PHONY: format +format: goimports gofumpt + @echo "format => *.go" + @find . -type f -name '*.go' | xargs gofmt -s -w + @find . -type f -name '*.go' | xargs $(GO_FUMPT) -l -w + @echo "goimports => *.go" + @for f in `find . -name '*.go'`; do \ + awk '/^import \($$/,/^\)$$/{if($$0=="")next}{print}' $$f > /tmp/fmt; \ + mv /tmp/fmt $$f; \ + done + @$(GO_IMPORTS) -w -local github.com/envoyproxy/ai-gateway `find . -name '*.go'` + +.PHONY: tidy +tidy: ## Runs go mod tidy on every module + @find . -name "go.mod" \ + | grep go.mod \ + | xargs -I {} bash -c 'dirname {}' \ + | xargs -I {} bash -c 'echo "tidy => {}"; cd {}; go mod tidy -v; ' + +.PHONY: precommit +precommit: format tidy lint + +.PHONY: check +check: + @$(MAKE) precommit + @if [ ! -z "`git status -s`" ]; then \ + echo "The following differences will fail CI until committed:"; \ + git diff --exit-code; \ + fi + +.PHONY: test +test: + @echo "test => ./..." + @go test -v $(shell go list ./... | grep -v e2e) diff --git a/Makefile.tools.mk b/Makefile.tools.mk new file mode 100644 index 00000000..75a77986 --- /dev/null +++ b/Makefile.tools.mk @@ -0,0 +1,44 @@ +LOCALBIN ?= $(shell pwd)/.bin +$(LOCALBIN): + mkdir -p $(LOCALBIN) + +## Tool binary names. +GOLANGCI_LINT = $(LOCALBIN)/golangci-lint +GO_FUMPT = $(LOCALBIN)/gofumpt +GO_IMPORTS = $(LOCALBIN)/goimports + +## Tool versions. +GOLANGCI_LINT_VERSION ?= v1.60.1 +GO_FUMPT_VERSION ?= v0.6.0 +GO_IMPORTS_VERSION ?= v0.21.0 + +.PHONY: golangci-lint +golangci-lint: $(GOLANGCI_LINT) +$(GOLANGCI_LINT): $(LOCALBIN) + $(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION)) + +.PHONY: gofumpt +gofumpt: $(GO_FUMPT) +$(GO_FUMPT): $(LOCALBIN) + $(call go-install-tool,$(GO_FUMPT),mvdan.cc/gofumpt,$(GO_FUMPT_VERSION)) + +.PHONY: goimports +goimports: $(GO_IMPORTS) +$(GO_IMPORTS): $(LOCALBIN) + $(call go-install-tool,$(GO_IMPORTS),golang.org/x/tools/cmd/goimports,$(GO_IMPORTS_VERSION)) + +# go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist +# $1 - target path with name of binary +# $2 - package url which can be installed +# $3 - specific version of package +define go-install-tool +@[ -f "$(1)-$(3)" ] || { \ +set -e; \ +package=$(2)@$(3) ;\ +echo "Downloading $${package}" ;\ +rm -f $(1) || true ;\ +GOBIN=$(LOCALBIN) go install $${package} ;\ +mv $(1) $(1)-$(3) ;\ +} ;\ +ln -sf $(1)-$(3) $(1) +endef diff --git a/go.mod b/go.mod new file mode 100644 index 00000000..2ce93f2b --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/envoyproxy/ai-gateway + +go 1.23.2 diff --git a/internal/cmd/controller/main.go b/internal/cmd/controller/main.go new file mode 100644 index 00000000..4a739874 --- /dev/null +++ b/internal/cmd/controller/main.go @@ -0,0 +1,5 @@ +package main + +func main() { + println("hello") +}