Skip to content

Commit

Permalink
move build cache into .go to skip indexing
Browse files Browse the repository at this point in the history
Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com>
  • Loading branch information
charlie0129 committed Feb 13, 2023
1 parent 6471ee1 commit de9bd6e
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 40 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/go-checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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-
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release-binary.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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-
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/release-image.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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-
Expand Down Expand Up @@ -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-
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/upload-test-binary.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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-
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ cscope.*

# Build output
bin
.go

# etcd
default.etcd
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
4 changes: 3 additions & 1 deletion makefiles/consts.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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))
85 changes: 57 additions & 28 deletions makefiles/targets.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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-<os>_<arch> to build for a specific platform
Expand Down Expand Up @@ -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.
Expand All @@ -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:
Expand Down

0 comments on commit de9bd6e

Please sign in to comment.