From 5112a78b75fe76021ab152bff9f4a339d27edf46 Mon Sep 17 00:00:00 2001 From: Jason Hall Date: Wed, 22 Dec 2021 09:13:43 -0500 Subject: [PATCH] Share the Go build cache when building in Dockerfiles On my machine this makes image rebuilds go from ~5m to 1.5s. This also required setting DOCKER_BUILDKIT=1 in integration test image builds. I also took the opportunity to bump the Go version used in those tests to Go 1.17 to match the image build processes, and tidied up the workflow files a bit too (renaming for consistency, typos, whitespace, etc.) --- .github/workflows/integration-k8s-tests.yaml | 29 +++++++++++++ .../workflows/integration-layers-tests.yaml | 20 ++++----- .github/workflows/integration-misc.yaml | 42 +++++++++---------- .github/workflows/integration-run-tests.yaml | 31 +++++++------- .github/workflows/integration_k8_tests.yaml | 33 --------------- .github/workflows/unit-tests.yaml | 28 ++++--------- Makefile | 2 +- deploy/Dockerfile | 5 ++- deploy/Dockerfile_debug | 7 +++- deploy/Dockerfile_slim | 5 ++- deploy/Dockerfile_warmer | 5 ++- integration/integration_test.go | 1 + scripts/integration-test.sh | 1 - 13 files changed, 98 insertions(+), 111 deletions(-) create mode 100644 .github/workflows/integration-k8s-tests.yaml delete mode 100644 .github/workflows/integration_k8_tests.yaml diff --git a/.github/workflows/integration-k8s-tests.yaml b/.github/workflows/integration-k8s-tests.yaml new file mode 100644 index 0000000000..ee491b8fa9 --- /dev/null +++ b/.github/workflows/integration-k8s-tests.yaml @@ -0,0 +1,29 @@ +name: Integration tests (K8s) + +on: + pull_request: + branches: ['master'] + +concurrency: + group: int-test-k8s-${{ github.head_ref }} + cancel-in-progress: true + +jobs: + tests: + env: + IMAGE_REPO: 'localhost:5000' + REGISTRY: 'localhost:5000' + runs-on: ubuntu-latest + steps: + - uses: actions/setup-go@v2 + with: + go-version: 1.17 + - uses: actions/checkout@v2 + - uses: docker/setup-buildx-action@v1 + + - name: Run build-image-and-k8s-integration-test + run: | + make travis-setup + make minikube-setup + make k8s-executor-build-push + make integration-test-k8s diff --git a/.github/workflows/integration-layers-tests.yaml b/.github/workflows/integration-layers-tests.yaml index df705d8f8c..39b3c0634b 100644 --- a/.github/workflows/integration-layers-tests.yaml +++ b/.github/workflows/integration-layers-tests.yaml @@ -1,29 +1,25 @@ name: Integration tests (Layers) -# Triggers the workflow on push or pull request events -on: [pull_request] +on: + pull_request: + branches: ['master'] concurrency: group: int-test-layers-${{ github.head_ref }} cancel-in-progress: true jobs: - build-executor: + tests: env: IMAGE_REPO: 'localhost:5000' REGISTRY: 'localhost:5000' runs-on: ubuntu-latest steps: - - name: Set up Go - uses: actions/setup-go@v2 + - uses: actions/setup-go@v2 with: - go-version: 1.14 - id: go - - - name: Check out code into the Go module directory - uses: actions/checkout@v2 - with: - fetch-depth: 0 + go-version: 1.17 + - uses: actions/checkout@v2 + - uses: docker/setup-buildx-action@v1 - name: Run integration-test-layers run: | diff --git a/.github/workflows/integration-misc.yaml b/.github/workflows/integration-misc.yaml index 0e928202c7..9441206946 100644 --- a/.github/workflows/integration-misc.yaml +++ b/.github/workflows/integration-misc.yaml @@ -1,14 +1,15 @@ name: Integration Tests Misc -# Triggers the workflow on push or pull request events -on: [pull_request] +on: + pull_request: + branches: ['master'] concurrency: group: int-test-mis-${{ github.head_ref }} cancel-in-progress: true jobs: - build-executor: + tests: env: IMAGE_REPO: 'localhost:5000' REGISTRY: 'localhost:5000' @@ -17,25 +18,20 @@ jobs: TRAVIS_PULL_REQUEST: ${{ github.event.number }} runs-on: ubuntu-latest steps: - - name: Set up Go - uses: actions/setup-go@v2 - with: - go-version: 1.14 - id: go + - uses: actions/setup-go@v2 + with: + go-version: 1.17 + - uses: actions/checkout@v2 + - uses: docker/setup-buildx-action@v1 - - name: Check out code into the Go module directory - uses: actions/checkout@v2 - with: - fetch-depth: 0 + - name: Print Travis ENV vars + run: | + echo "TRAVIS_BRANCH: ${TRAVIS_BRANCH}" + echo "TRAVIS_PULL_REQUEST: ${TRAVIS_PULL_REQUEST}" + echo "TRAVIS_REPO_SLUG: ${TRAVIS_REPO_SLUG}" - - name: Print Travis ENV vars - run: | - echo "TRAVIS_BRANCH: ${TRAVIS_BRANCH}" - echo "TRAVIS_PULL_REQUEST: ${TRAVIS_PULL_REQUEST}" - echo "TRAVIS_REPO_SLUG: ${TRAVIS_REPO_SLUG}" - - - name: Run integration-test-misc - run : | - make travis-setup - make minikube-setup - make integration-test-misc \ No newline at end of file + - name: Run integration-test-misc + run : | + make travis-setup + make minikube-setup + make integration-test-misc diff --git a/.github/workflows/integration-run-tests.yaml b/.github/workflows/integration-run-tests.yaml index 62921e8cb6..f04b076a7d 100644 --- a/.github/workflows/integration-run-tests.yaml +++ b/.github/workflows/integration-run-tests.yaml @@ -1,31 +1,28 @@ name: Integration tests (Run) -# Triggers the workflow on push or pull request events -on: [pull_request] +on: + pull_request: + branches: ['master'] concurrency: group: int-test-run-${{ github.head_ref }} cancel-in-progress: true jobs: - build-executor: + tests: env: IMAGE_REPO: 'localhost:5000' REGISTRY: 'localhost:5000' runs-on: ubuntu-latest steps: - - name: Set up Go - uses: actions/setup-go@v2 - with: - go-version: 1.14 - id: go + - uses: actions/setup-go@v2 + with: + go-version: 1.17 + - uses: actions/checkout@v2 + - uses: docker/setup-buildx-action@v1 - - name: Check out code into the Go module directory - uses: actions/checkout@v2 - with: - fetch-depth: 0 - - name: Run integration-test-run - run: | - make travis-setup - make minikube-setup - make integration-test-run \ No newline at end of file + - name: Run integration-test-run + run: | + make travis-setup + make minikube-setup + make integration-test-run diff --git a/.github/workflows/integration_k8_tests.yaml b/.github/workflows/integration_k8_tests.yaml deleted file mode 100644 index d21c8a248d..0000000000 --- a/.github/workflows/integration_k8_tests.yaml +++ /dev/null @@ -1,33 +0,0 @@ -name: Integration tests (K8s) - -# Triggers the workflow on pull request events -on: [pull_request] - -concurrency: - group: int-test-k8s${{ github.head_ref }} - cancel-in-progress: true - -jobs: - build-executor: - env: - IMAGE_REPO: 'localhost:5000' - REGISTRY: 'localhost:5000' - runs-on: ubuntu-latest - steps: - - name: Set up Go - uses: actions/setup-go@v2 - with: - go-version: 1.14 - id: go - - - name: Check out code into the Go module directory - uses: actions/checkout@v2 - with: - fetch-depth: 0 - - - name: Run build-image-and-k8s-integration-test - run: | - make travis-setup - make minikube-setup - make k8s-executor-build-push - make integration-test-k8s \ No newline at end of file diff --git a/.github/workflows/unit-tests.yaml b/.github/workflows/unit-tests.yaml index c21776ce56..e11903b001 100644 --- a/.github/workflows/unit-tests.yaml +++ b/.github/workflows/unit-tests.yaml @@ -1,30 +1,20 @@ name: Unit tests -# Triggers the workflow on push or pull request events -on: [pull_request] +on: + pull_request: + branches: ['master'] concurrency: group: unit-tests-${{ github.head_ref }} cancel-in-progress: true jobs: - build-executor: - env: - IMAGE_REPO: 'localhost:5000' - REGISTRY: 'localhost:5000' + tests: runs-on: ubuntu-latest steps: - - name: Set up Go - uses: actions/setup-go@v2 - with: - go-version: 1.14 - id: go + - uses: actions/setup-go@v2 + with: + go-version: 1.17 + - uses: actions/checkout@v2 - - name: Check out code into the Go module directory - uses: actions/checkout@v2 - with: - fetch-depth: 0 - - - name: Run unit-test - run: - make test \ No newline at end of file + - run: make test diff --git a/Makefile b/Makefile index 6164c1e6e3..bfdbffc2e8 100644 --- a/Makefile +++ b/Makefile @@ -88,7 +88,7 @@ integration-test-misc: .PHONY: k8s-executor-build-push k8s-executor-build-push: - docker build ${BUILD_ARG} --build-arg=GOARCH=$(GOARCH) -t $(REGISTRY)/executor:latest -f deploy/Dockerfile . + DOCKER_BUILDKIT=1 docker build ${BUILD_ARG} --build-arg=GOARCH=$(GOARCH) -t $(REGISTRY)/executor:latest -f deploy/Dockerfile . docker push $(REGISTRY)/executor:latest .PHONY: images diff --git a/deploy/Dockerfile b/deploy/Dockerfile index 9a92ad6dae..ac78dbee68 100644 --- a/deploy/Dockerfile +++ b/deploy/Dockerfile @@ -35,7 +35,10 @@ RUN go install github.com/chrismellard/docker-credential-acr-env@09e2b5a8ac86c3e RUN mkdir -p /kaniko/.docker COPY . . -RUN make GOARCH=$TARGETARCH +RUN \ + --mount=type=cache,target=/root/.cache/go-build \ + --mount=type=cache,target=/go/pkg \ + make GOARCH=$TARGETARCH # Generate latest ca-certificates diff --git a/deploy/Dockerfile_debug b/deploy/Dockerfile_debug index 6c9ffe3be0..63390f4015 100644 --- a/deploy/Dockerfile_debug +++ b/deploy/Dockerfile_debug @@ -35,8 +35,11 @@ RUN go install github.com/chrismellard/docker-credential-acr-env@09e2b5a8ac86c3e RUN mkdir -p /kaniko/.docker COPY . . -RUN make GOARCH=$TARGETARCH -RUN make GOARCH=$TARGETARCH out/warmer +RUN \ + --mount=type=cache,target=/root/.cache/go-build \ + --mount=type=cache,target=/go/pkg \ + make GOARCH=$TARGETARCH && \ + make GOARCH=$TARGETARCH out/warmer # Generate latest ca-certificates diff --git a/deploy/Dockerfile_slim b/deploy/Dockerfile_slim index 07ece82ee7..e5bc222d3f 100644 --- a/deploy/Dockerfile_slim +++ b/deploy/Dockerfile_slim @@ -21,7 +21,10 @@ ARG TARGETARCH WORKDIR /src COPY . . -RUN make GOARCH=$TARGETARCH +RUN \ + --mount=type=cache,target=/root/.cache/go-build \ + --mount=type=cache,target=/go/pkg \ + make GOARCH=$TARGETARCH # Generate latest ca-certificates diff --git a/deploy/Dockerfile_warmer b/deploy/Dockerfile_warmer index 6e8c0ee1bc..34fb1115db 100644 --- a/deploy/Dockerfile_warmer +++ b/deploy/Dockerfile_warmer @@ -35,7 +35,10 @@ RUN go install github.com/chrismellard/docker-credential-acr-env@09e2b5a8ac86c3e RUN mkdir -p /kaniko/.docker COPY . . -RUN make GOARCH=$TARGETARCH out/warmer +RUN \ + --mount=type=cache,target=/root/.cache/go-build \ + --mount=type=cache,target=/go/pkg \ + make GOARCH=$TARGETARCH out/warmer # Generate latest ca-certificates diff --git a/integration/integration_test.go b/integration/integration_test.go index cdfe7a72d3..9093020c99 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -164,6 +164,7 @@ func buildRequiredImages() error { for _, setupCmd := range setupCommands { fmt.Println(setupCmd.name) cmd := exec.Command(setupCmd.command[0], setupCmd.command[1:]...) + cmd.Env = append(os.Environ(), "DOCKER_BUILDKIT=1") // Build with buildkit enabled. if out, err := RunCommandWithoutTest(cmd); err != nil { return errors.Wrap(err, fmt.Sprintf("%s failed: %s", setupCmd.name, string(out))) } diff --git a/scripts/integration-test.sh b/scripts/integration-test.sh index caf1ab1127..0cbcd2277e 100755 --- a/scripts/integration-test.sh +++ b/scripts/integration-test.sh @@ -20,7 +20,6 @@ IMAGE_REPO="${IMAGE_REPO:-gcr.io/kaniko-test}" docker version -# Sets up a kokoro (Google internal integration testing tool) environment echo "Running integration tests..." make out/executor make out/warmer