From c5bcb48ef99693ead1b73b11a2df2842daf73676 Mon Sep 17 00:00:00 2001 From: Charlie Chiang Date: Mon, 13 Feb 2023 12:26:49 +0800 Subject: [PATCH 1/4] fix release Signed-off-by: Charlie Chiang --- .github/workflows/release-binary.yaml | 2 +- makefiles/targets.mk | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/release-binary.yaml b/.github/workflows/release-binary.yaml index ce5ff18..9d99b01 100644 --- a/.github/workflows/release-binary.yaml +++ b/.github/workflows/release-binary.yaml @@ -46,7 +46,7 @@ jobs: echo ::set-output name=VERSION::${VERSION} - name: Build Binaries for All Platforms - run: make trigger-all-build + run: make trigger-all-package - name: Attach Binaries to Release uses: softprops/action-gh-release@v1 diff --git a/makefiles/targets.mk b/makefiles/targets.mk index d6797ad..831f4da 100644 --- a/makefiles/targets.mk +++ b/makefiles/targets.mk @@ -186,7 +186,7 @@ help: # @HELP print this message help: variables echo "TARGETS:" grep -E '^.*: *# *@HELP' $(MAKEFILE_LIST) \ - | sed --expression='s_.*.mk:__g' \ + | sed 's_.*.mk:__g' \ | awk ' \ BEGIN {FS = ": *# *@HELP"}; \ { printf " %-25s %s\n", $$1, $$2 }; \ From 6471ee153ffaa4b7205c89ce6017480936b6004b Mon Sep 17 00:00:00 2001 From: Charlie Chiang Date: Mon, 13 Feb 2023 12:28:40 +0800 Subject: [PATCH 2/4] sed macOS fix Signed-off-by: Charlie Chiang --- makefiles/targets.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/makefiles/targets.mk b/makefiles/targets.mk index 831f4da..aeb2fdb 100644 --- a/makefiles/targets.mk +++ b/makefiles/targets.mk @@ -186,7 +186,7 @@ help: # @HELP print this message help: variables echo "TARGETS:" grep -E '^.*: *# *@HELP' $(MAKEFILE_LIST) \ - | sed 's_.*.mk:__g' \ + | sed -E 's_.*.mk:__g' \ | awk ' \ BEGIN {FS = ": *# *@HELP"}; \ { printf " %-25s %s\n", $$1, $$2 }; \ From 1679e806fa889122d8a52e432c7e4a9998a01c7b Mon Sep 17 00:00:00 2001 From: Charlie Chiang Date: Mon, 13 Feb 2023 22:28:14 +0800 Subject: [PATCH 3/4] move build cache into .go to skip indexing Signed-off-by: Charlie Chiang --- .github/workflows/go-checks.yaml | 4 +- .github/workflows/release-binary.yaml | 4 +- .github/workflows/release-image.yaml | 8 +-- .github/workflows/upload-test-binary.yaml | 4 +- .gitignore | 1 + Makefile | 9 ++- makefiles/consts.mk | 4 +- makefiles/targets.mk | 85 +++++++++++++++-------- 8 files changed, 77 insertions(+), 42 deletions(-) diff --git a/.github/workflows/go-checks.yaml b/.github/workflows/go-checks.yaml index 4c81ce8..ff3b083 100644 --- a/.github/workflows/go-checks.yaml +++ b/.github/workflows/go-checks.yaml @@ -65,8 +65,8 @@ jobs: uses: actions/cache@v3 with: path: | - bin/gomodcache - bin/gocache + .go/gomodcache + .go/gocache key: ${{ runner.os }}-gobuildcontainer-${{ hashFiles('**/go.sum') }} restore-keys: | ${{ runner.os }}-gobuildcontainer- diff --git a/.github/workflows/release-binary.yaml b/.github/workflows/release-binary.yaml index 9d99b01..2948119 100644 --- a/.github/workflows/release-binary.yaml +++ b/.github/workflows/release-binary.yaml @@ -30,8 +30,8 @@ jobs: uses: actions/cache@v3 with: path: | - bin/gomodcache - bin/gocache + .go/gomodcache + .go/gocache key: ${{ runner.os }}-gobuildcontainer-${{ hashFiles('**/go.sum') }} restore-keys: | ${{ runner.os }}-gobuildcontainer- diff --git a/.github/workflows/release-image.yaml b/.github/workflows/release-image.yaml index 8e4a06a..4bf97eb 100644 --- a/.github/workflows/release-image.yaml +++ b/.github/workflows/release-image.yaml @@ -20,8 +20,8 @@ jobs: uses: actions/cache@v3 with: path: | - bin/gomodcache - bin/gocache + .go/gomodcache + .go/gocache key: ${{ runner.os }}-gobuildcontainer-${{ hashFiles('**/go.sum') }} restore-keys: | ${{ runner.os }}-gobuildcontainer- @@ -69,8 +69,8 @@ jobs: uses: actions/cache@v3 with: path: | - bin/gomodcache - bin/gocache + .go/gomodcache + .go/gocache key: ${{ runner.os }}-gobuildcontainer-${{ hashFiles('**/go.sum') }} restore-keys: | ${{ runner.os }}-gobuildcontainer- diff --git a/.github/workflows/upload-test-binary.yaml b/.github/workflows/upload-test-binary.yaml index b1de6fc..0deb601 100644 --- a/.github/workflows/upload-test-binary.yaml +++ b/.github/workflows/upload-test-binary.yaml @@ -64,8 +64,8 @@ jobs: uses: actions/cache@v3 with: path: | - bin/gomodcache - bin/gocache + .go/gomodcache + .go/gocache key: ${{ runner.os }}-gobuildcontainer-${{ hashFiles('**/go.sum') }} restore-keys: | ${{ runner.os }}-gobuildcontainer- diff --git a/.gitignore b/.gitignore index 215a7a3..4704ae5 100644 --- a/.gitignore +++ b/.gitignore @@ -56,6 +56,7 @@ cscope.* # Build output bin +.go # etcd default.etcd diff --git a/Makefile b/Makefile index 409a1fd..e51030d 100644 --- a/Makefile +++ b/Makefile @@ -33,7 +33,7 @@ $(foreach p,$(SUBPROJS),$(eval \ $(p)-%: mk-%.$(p); \ )) -# Common targets for subprojects +# Common targets for subprojects, will be executed on all subprojects TARGETS := build \ all-build \ package \ @@ -42,18 +42,21 @@ TARGETS := build \ container-push \ all-container-build-push \ clean \ - cleanall \ + all-clean \ version \ imageversion \ binaryname \ variables \ help -# Targets to run on all subprojects +# Run common targets on all subprojects $(foreach t,$(TARGETS),$(eval \ $(t): $(addprefix mk-$(t).,$(SUBPROJS)); \ )) +# `shell' only needs to be executed once, not on every subproject +shell: $(addprefix mk-shell.,$(word 1,$(SUBPROJS))); + mk-%: $(MAKE) -f $(lastword $(subst ., ,$*)).mk $(firstword $(subst ., ,$*)) diff --git a/makefiles/consts.mk b/makefiles/consts.mk index d651c92..133fb41 100644 --- a/makefiles/consts.mk +++ b/makefiles/consts.mk @@ -68,8 +68,10 @@ ifeq ($(OS), windows) PKG_FULLNAME := $(subst .exe,,$(if $(FULL_NAME),$(BIN_VERBOSE_BASE),$(BIN_BASENAME))).zip endif -# This holds build output, cache, and helper tools +# This holds build output and helper tools DIST := bin +# This holds build cache +GOCACHE := .go BIN_VERBOSE_DIR := $(DIST)/$(BIN)-$(VERSION) # Full output path OUTPUT := $(if $(FULL_NAME),$(BIN_VERBOSE_DIR)/$(BIN_FULLNAME),$(DIST)/$(BIN_FULLNAME)) diff --git a/makefiles/targets.mk b/makefiles/targets.mk index aeb2fdb..85fa1ff 100644 --- a/makefiles/targets.mk +++ b/makefiles/targets.mk @@ -14,34 +14,34 @@ all: build -# Build cache of the build container -BUILDCACHE ?= $$(pwd)/$(DIST) - # ===== BUILD ===== +build-dirs: + mkdir -p "$(GOCACHE)/gocache" \ + "$(GOCACHE)/gomodcache" \ + "$(DIST)" + build: # @HELP build binary for current platform -build: gen-dockerignore - mkdir -p "$(BUILDCACHE)/gocache" "$(BUILDCACHE)/gomodcache" && \ - docker run \ - -i \ - --rm \ - -u $$(id -u):$$(id -g) \ - -v $$(pwd):/src \ - -w /src \ - -v $(BUILDCACHE)/gocache:/gocache \ - -v $(BUILDCACHE)/gomodcache:/gomodcache \ - --env GOCACHE="/gocache" \ - --env GOMODCACHE="/gomodcache" \ - --env ARCH="$(ARCH)" \ - --env OS="$(OS)" \ - --env VERSION="$(VERSION)" \ - --env DEBUG="$(DEBUG)" \ - --env OUTPUT="$(OUTPUT)" \ - --env GOFLAGS="$(GOFLAGS)" \ - --env GOPROXY="$(GOPROXY)" \ - --env HTTP_PROXY="$(HTTP_PROXY)" \ - --env HTTPS_PROXY="$(HTTPS_PROXY)" \ - $(BUILD_IMAGE) \ +build: gen-dockerignore build-dirs + docker run \ + -i \ + --rm \ + -u $$(id -u):$$(id -g) \ + -v $$(pwd):/src \ + -w /src \ + -v $$(pwd)/$(GOCACHE):/cache \ + --env GOCACHE="/cache/gocache" \ + --env GOMODCACHE="/cache/gomodcache" \ + --env ARCH="$(ARCH)" \ + --env OS="$(OS)" \ + --env VERSION="$(VERSION)" \ + --env DEBUG="$(DEBUG)" \ + --env OUTPUT="$(OUTPUT)" \ + --env GOFLAGS="$(GOFLAGS)" \ + --env GOPROXY="$(GOPROXY)" \ + --env HTTP_PROXY="$(HTTP_PROXY)" \ + --env HTTPS_PROXY="$(HTTPS_PROXY)" \ + $(BUILD_IMAGE) \ ./build/build.sh $(ENTRY) # INTERNAL: build-_ to build for a specific platform @@ -138,6 +138,34 @@ all-container-build-push: $(addprefix build-, $(subst /,_, $(IMAGE_PLATFORMS))) # ===== MISC ===== +# Optional variable to pass arguments to sh +# Example: make shell CMD="-c 'date'" +CMD ?= + +shell: # @HELP launches a shell in the containerized build environment +shell: build-dirs + echo "# launching a shell in the containerized build environment" + docker run \ + -it \ + --rm \ + -u $$(id -u):$$(id -g) \ + -v $$(pwd):/src \ + -w /src \ + -v $$(pwd)/$(GOCACHE):/cache \ + --env GOCACHE="/cache/gocache" \ + --env GOMODCACHE="/cache/gomodcache" \ + --env ARCH="$(ARCH)" \ + --env OS="$(OS)" \ + --env VERSION="$(VERSION)" \ + --env DEBUG="$(DEBUG)" \ + --env OUTPUT="$(OUTPUT)" \ + --env GOFLAGS="$(GOFLAGS)" \ + --env GOPROXY="$(GOPROXY)" \ + --env HTTP_PROXY="$(HTTP_PROXY)" \ + --env HTTPS_PROXY="$(HTTPS_PROXY)" \ + $(BUILD_IMAGE) \ + /bin/sh $(CMD) + # Generate a dockerignore file to ignore everything except # current build output directory. This is useful because # when building a container, we only need the final binary. @@ -150,9 +178,10 @@ clean: # @HELP clean built binaries clean: rm -rf $(DIST)/$(BIN)* -cleanall: # @HELP clean built binaries, build cache, and helper tools -cleanall: clean - rm -rf $(DIST) +all-clean: # @HELP clean built binaries, build cache, and helper tools +all-clean: clean + test -d $(GOCACHE) && chmod -R u+w $(GOCACHE) || true + rm -rf $(GOCACHE) $(DIST) version: # @HELP output the version string version: From 4d81c90c4d6c7eb4651ff2a5beb914a3c9b1b76b Mon Sep 17 00:00:00 2001 From: Charlie Chiang Date: Tue, 14 Feb 2023 01:15:48 +0800 Subject: [PATCH 4/4] make container target os always linux Signed-off-by: Charlie Chiang --- makefiles/targets.mk | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/makefiles/targets.mk b/makefiles/targets.mk index 85fa1ff..8d01b88 100644 --- a/makefiles/targets.mk +++ b/makefiles/targets.mk @@ -92,27 +92,22 @@ all-package: $(addprefix package-, $(subst /,_, $(BIN_PLATFORMS))) # ===== CONTAINERS ===== container-build: # @HELP build container image for current platform -container-build: build-$(OS)_$(ARCH) - printf "# CONTAINER repotags: %s\ttarget: %s/%s\tbinaryversion: %s\n" "$(IMAGE_REPO_TAGS)" "$(OS)" "$(ARCH)" "$(VERSION)" +container-build: build-linux_$(ARCH) + printf "# CONTAINER repotags: %s\ttarget: %s/%s\tbinaryversion: %s\n" "$(IMAGE_REPO_TAGS)" "linux" "$(ARCH)" "$(VERSION)" + if [ "$(OS)" != "linux" ]; then \ + echo "# CONTAINER warning: you have set target os to $(OS), but container target os will always be linux"; \ + fi; \ TMPFILE=Dockerfile && \ sed 's/$${BIN}/$(BIN)/g' Dockerfile.in > $${TMPFILE} && \ DOCKER_BUILDKIT=1 \ docker build \ -f $${TMPFILE} \ --build-arg "ARCH=$(ARCH)" \ - --build-arg "OS=$(OS)" \ + --build-arg "OS=linux" \ --build-arg "VERSION=$(VERSION)" \ --build-arg "BASE_IMAGE=$(BASE_IMAGE)" \ $(addprefix -t ,$(IMAGE_REPO_TAGS)) . -# INTERNAL: container-build-_ to build container image for a specific platform -container-build-%: - $(MAKE) -f $(firstword $(MAKEFILE_LIST)) \ - docker-build \ - --no-print-directory \ - GOOS=$(firstword $(subst _, ,$*)) \ - GOARCH=$(lastword $(subst _, ,$*)) - container-push: # @HELP push built container image to all repos container-push: $(addprefix container-push-, $(subst :,=, $(subst /,_, $(IMAGE_REPO_TAGS))))