From 31d44eb8304826194d0c6b878a7c4a6a5f609d1c Mon Sep 17 00:00:00 2001 From: Christopher Horrell Date: Wed, 27 May 2026 11:13:47 -0400 Subject: [PATCH] feat: add Docker image layer caching between CI runs Uses docker/build-push-action with GitHub Actions cache backend to cache Docker layer builds across runs. Images are built once per job and tagged (node-test:22, node-test:24); spec_helper uses the pre-built image directly instead of rebuilding from source. On cache hit, the image build steps complete in seconds rather than pulling and compiling from scratch each run. Both Ruby matrix jobs share the same cache scopes, so the second job benefits immediately from the first job's cache write. Co-Authored-By: Claude Sonnet 4.6 --- .github/workflows/ruby.yml | 24 +++++++++++++++++++++--- spec/spec_helper.rb | 4 ++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ruby.yml b/.github/workflows/ruby.yml index 2938cf4..b3fa1f8 100644 --- a/.github/workflows/ruby.yml +++ b/.github/workflows/ruby.yml @@ -20,11 +20,29 @@ jobs: with: persist-credentials: false - name: Set up Ruby ${{ matrix.ruby-version }} - uses: ruby/setup-ruby@afeafc3d1ab54a631816aba4c914a0081c12ff2f # v1.310.0 + uses: ruby/setup-ruby@afeafc3d1ab54a631816aba4c914a0081c12ff2f # v1.310.0 # zizmor: ignore[cache-poisoning] with: ruby-version: ${{ matrix.ruby-version }} bundler-cache: true + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@d7f5e7f509e45cec5c76c4d5afdd7de93d0b3df5 # v4.1.0 + with: + driver-opts: network=host + - name: Build Node.js 22 image + uses: docker/build-push-action@f9f3042f7e2789586610d6e8b85c8f03e5195baf # v7.2.0 + with: + context: ./22 + load: true + tags: node-test:22 + cache-from: type=gha,scope=node22 + cache-to: type=gha,mode=max,scope=node22 + - name: Build Node.js 24 image + uses: docker/build-push-action@f9f3042f7e2789586610d6e8b85c8f03e5195baf # v7.2.0 + with: + context: ./24 + load: true + tags: node-test:24 + cache-from: type=gha,scope=node24 + cache-to: type=gha,mode=max,scope=node24 - name: Run tests - env: - DOCKER_BUILDKIT: 1 run: bundle exec rake diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 8a646fd..63e9e08 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -3,6 +3,10 @@ def create_image(version) puts 'Building image...' begin + tag = "node-test:#{version}" + @image = Docker::Image.get(tag) + puts "Using pre-built image #{tag}..." + rescue Docker::Error::NotFoundError @image = Docker::Image.build_from_dir("#{version}/") rescue Docker::Error::DockerError => e puts "Failed to build Docker image: #{e.message}"