From de9bd6eef82490589e210f595af0b18e7aa074d2 Mon Sep 17 00:00:00 2001 From: Charlie Chiang Date: Mon, 13 Feb 2023 22:18:59 +0800 Subject: [PATCH] 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 | 3 +- makefiles/consts.mk | 4 +- makefiles/targets.mk | 85 +++++++++++++++-------- 8 files changed, 73 insertions(+), 40 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..0d3accb 100644 --- a/Makefile +++ b/Makefile @@ -42,12 +42,13 @@ TARGETS := build \ container-push \ all-container-build-push \ clean \ - cleanall \ + all-clean \ version \ imageversion \ binaryname \ variables \ help + # `shell' is excluded since it only need to be run once # Targets to run on all subprojects $(foreach t,$(TARGETS),$(eval \ 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: