diff --git a/.github/workflow_calls/common.yml b/.github/workflow_calls/common.yml new file mode 100644 index 00000000..dce498b6 --- /dev/null +++ b/.github/workflow_calls/common.yml @@ -0,0 +1,90 @@ +name: common +on: + workflow_call: + inputs: + debug: + type: boolean + description: "set tmate on failure" + required: true + +jobs: + build_cmd: + strategy: + matrix: + platform: [ubuntu-latest, macos-latest, windows-latest] + runs-on: ${{ matrix.platform }} + steps: + - name: "checkout repo" + uses: actions/checkout@v3 + - name: "setup golang" + uses: actions/setup-go@v3 + with: + go-version: 1.18 + - name: "try to use build cache" + uses: actions/cache@v3 + with: + path: | + ~/.cache/go-build + ~/go/pkg/mod + ~/Library/Caches/go-build + ~\AppData\Local\go-build + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-go- + - name: "build cmd" + run: make + - name: "set up tmate session if necessary" + if: ${{ failure() && inputs.debug }} + uses: mxschmitt/action-tmate@v3 + + build: + runs-on: ubuntu-latest + steps: + - name: "checkout repo" + uses: actions/checkout@v3 + - name: "setup golang" + uses: actions/setup-go@v3 + with: + go-version: 1.18 + - name: "try to use build cache" + uses: actions/cache@v3 + with: + path: | + ~/.cache/go-build + ~/go/pkg/mod + ~/Library/Caches/go-build + ~\AppData\Local\go-build + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-go- + - name: "build all" + run: make build + - name: "set up tmate session if necessary" + if: ${{ failure() && inputs.debug }} + uses: mxschmitt/action-tmate@v3 + + test: + runs-on: ubuntu-latest + steps: + - name: "checkout repo" + uses: actions/checkout@v3 + - name: "setup golang" + uses: actions/setup-go@v3 + with: + go-version: 1.18 + - name: "try to use build cache" + uses: actions/cache@v3 + with: + path: | + ~/.cache/go-build + ~/go/pkg/mod + ~/Library/Caches/go-build + ~\AppData\Local\go-build + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-go- + - name: "test all" + run: make test + - name: "set up tmate session if necessary" + if: ${{ failure() && inputs.debug }} + uses: mxschmitt/action-tmate@v3 diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 00000000..63b5604a --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,29 @@ +name: build +on: + workflow_dispatch: + inputs: + debug: + type: boolean + description: "Run the build with tmate debugging enabled" + required: false + default: false + pull_request: + branches: + - master + - release-* + +concurrency: + group: ${{ github.ref }}-${{ github.workflow }} + cancel-in-progress: true + +jobs: + build_cmd: + uses: ./.github/workflow_calls/common.yml@test + with: + debug: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.debug }} + + build: + needs: build_cmd + uses: ./.github/workflow_calls/common.yml@test + with: + debug: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.debug }} diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 00000000..d7de766f --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,26 @@ +name: test +on: + workflow_dispatch: + inputs: + debug: + type: boolean + description: "Run the build with tmate debugging enabled" + required: false + default: false + workflow_run: + workflows: ["build"] + branches: + - master + - release-* + types: + - completed + +concurrency: + group: ${{ github.ref }}-${{ github.workflow }} + cancel-in-progress: true + +jobs: + test: + uses: ./.github/workflow_calls/common.yml@test + with: + debug: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.debug }} diff --git a/.gitignore b/.gitignore index 559e62fb..5d1773ba 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ bin .DS_Store vendor .vscode/ +.coverage* diff --git a/Makefile b/Makefile index 9d0a49c7..f901cf24 100644 --- a/Makefile +++ b/Makefile @@ -14,41 +14,29 @@ # limitations under the License. PROJECTNAME = $(shell basename "$(PWD)") -TOOL_BIN_PATH := $(shell pwd)/.tools/bin -GOBASE = $(shell pwd) +GOBIN := $(shell pwd)/bin BUILD_TAGS ?= LDFLAGS ?= -export GOBIN := $(TOOL_BIN_PATH) -export PATH := $(TOOL_BIN_PATH):$(PATH) - -default: weirproxy weirctl - -weirproxy: +GOFLAGS ?= -gcflags '$(GCFLAGS)' -ldflags '$(LDFLAGS)' -tags '${BUILD_TAGS}' ifeq ("$(WITH_RACE)", "1") - go build -race -gcflags '$(GCFLAGS)' -ldflags '$(LDFLAGS)' -tags '${BUILD_TAGS}' -o bin/weirproxy cmd/weirproxy/main.go -else - go build -gcflags '$(GCFLAGS)' -ldflags '$(LDFLAGS)' -tags '${BUILD_TAGS}' -o bin/weirproxy cmd/weirproxy/main.go + GOFLAGS = $(GOFLAGS) -race endif +EXECUTABLE_TARGETS := $(patsubst cmd/%,cmd_%,$(wildcard cmd/*)) -weirctl: -ifeq ("$(WITH_RACE)", "1") - go build -race -gcflags '$(GCFLAGS)' -ldflags '$(LDFLAGS)' -tags '${BUILD_TAGS}' -o bin/weirctl cmd/weirctl/main.go -else - go build -gcflags '$(GCFLAGS)' -ldflags '$(LDFLAGS)' -tags '${BUILD_TAGS}' -o bin/weirctl cmd/weirctl/main.go -endif +default: cmd + +cmd: $(EXECUTABLE_TARGETS) -go-test: +build: cmd + go build $(GOFLAGS) ./... + +cmd_%: OUTPUT=$(patsubst cmd_%,bin/%,$@) +cmd_%: SOURCE=$(patsubst cmd_%,cmd/%/main.go,$@) +cmd_%: + go build $(GOFLAGS) -o $(OUTPUT) $(SOURCE) + +test: go test -coverprofile=.coverage.out ./... go tool cover -func=.coverage.out -o .coverage.func tail -1 .coverage.func go tool cover -html=.coverage.out -o .coverage.html - -go-lint-check: install-tools - golangci-lint run - -go-lint-fix: install-tools - golangci-lint run --fix - -install-tools: - @mkdir -p $(TOOL_BIN_PATH) - @test -e $(TOOL_BIN_PATH)/golangci-lint >/dev/null 2>&1 || curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(TOOL_BIN_PATH) v1.30.0