diff --git a/.github/workflows/gem_test.yml b/.github/workflows/gem_test.yml index e1b7fbcf7..381baa8eb 100644 --- a/.github/workflows/gem_test.yml +++ b/.github/workflows/gem_test.yml @@ -39,11 +39,58 @@ jobs: - id: set run: echo "include=$(jq -c . ${{ inputs.gem }}/test-matrix.json)" >> "$GITHUB_OUTPUT" + # Builds one Bundler download cache (gems, compiled extensions, git checkouts, + # index) covering every cell of the gem, so test jobs stop re-downloading the + # same gems. Runs only when the gem's lockfiles change; test jobs don't wait + # for it and fall back to the previous cache until it's saved. + gem-cache: + runs-on: ubuntu-latest + timeout-minutes: 60 + env: + CC: gcc-13 + CXX: g++-13 + BUNDLE_GLOBAL_GEM_CACHE: "true" + steps: + - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 + + - name: Check for an existing gem cache + id: lookup + uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: ~/.bundle/cache + key: bundle-v1-${{ runner.os }}-${{ inputs.gem }}-${{ hashFiles(format('{0}/gemfiles/*.lock', inputs.gem)) }} + lookup-only: true + + - name: Install apt packages + if: steps.lookup.outputs.cache-hit != 'true' && inputs.sqlite + run: | + sudo apt-get update + sudo apt-get install --no-install-recommends libsqlite3-dev + + - name: Set up Ruby + if: steps.lookup.outputs.cache-hit != 'true' + uses: ./.github/actions/setup-mise + + - name: Install every cell's gems + if: steps.lookup.outputs.cache-hit != 'true' + run: | + for cell in $(ruby bin/test --gem "${{ inputs.gem }}" -l); do + ruby bin/test --cell "$cell" --install-only + done + + - name: Save gem cache + if: steps.lookup.outputs.cache-hit != 'true' + uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: ~/.bundle/cache + key: ${{ steps.lookup.outputs.cache-primary-key }} + test: needs: matrix env: CC: gcc-13 CXX: g++-13 + BUNDLE_GLOBAL_GEM_CACHE: "true" defaults: run: working-directory: ${{ inputs.gem }} @@ -96,6 +143,13 @@ jobs: - name: Set up Ruby uses: ./.github/actions/setup-mise + - name: Restore gem cache + uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: ~/.bundle/cache + key: bundle-v1-${{ runner.os }}-${{ inputs.gem }}-${{ hashFiles(format('{0}/gemfiles/*.lock', inputs.gem)) }} + restore-keys: bundle-v1-${{ runner.os }}-${{ inputs.gem }}- + - name: Run specs env: MATRIX_ENTRY: ${{ toJson(matrix) }} diff --git a/bin/test b/bin/test index 66473de5a..b295cb423 100755 --- a/bin/test +++ b/bin/test @@ -20,6 +20,7 @@ # bin/test --cell spec/sentry/client_spec.rb # forward args to rspec # bin/test --cell -- --tag foo # everything after -- goes to rspec # bin/test --cell --rake # run `bundle exec rake` like CI +# bin/test --cell --install-only # install the cell's gems, run nothing # # The Ruby must already be installed (`mise --env ci install`). # @@ -70,6 +71,7 @@ opts = { entry: nil, gem: nil, rake: false, + install_only: false, list: false } @@ -79,6 +81,7 @@ parser = OptionParser.new do |o| o.on("--entry JSON", "Cell to run, from a test-matrix.json entry (pairs with --gem/cwd).") { |v| opts[:entry] = v } o.on("--gem NAME", "Gem to run (auto-picks newest installed Ruby cell); scopes -l.") { |v| opts[:gem] = v } o.on("--rake", "Run `bundle exec rake` (full CI task) instead of rspec.") { opts[:rake] = true } + o.on("--install-only", "Install the cell's gems and exit without running anything.") { opts[:install_only] = true } o.on("-l", "--list", "List every cell's wrapper path, one per line; do nothing.") { opts[:list] = true } o.on("-h", "--help") { puts o; exit 0 } end @@ -131,7 +134,14 @@ env["JRUBY_OPTS"] = "--debug" if cell.ruby.include?("jruby") # for more accurate # `git` or tab-completion resolves too — both forms name the same file. rspec_args = rspec_args.map { |a| a.sub(%r{\A(?:\./)?#{Regexp.escape(gem)}/}, "") } -test_cmd = opts[:rake] ? ["rake"] : ["rspec", *rspec_args] +test_cmd = + if opts[:install_only] + [] + elsif opts[:rake] + ["rake"] + else + ["rspec", *rspec_args] + end # Ensure the cell's gems are present before running. The wrapper is committed, # but write it if absent so a freshly added matrix row still works. Args reach @@ -142,11 +152,11 @@ RUN = <<~SH mkdir -p "$(dirname "$BUNDLE_GEMFILE")" [ -f "$BUNDLE_GEMFILE" ] || echo 'eval_gemfile "../Gemfile"' > "$BUNDLE_GEMFILE" bundle check >/dev/null 2>&1 || bundle install - exec bundle exec "$@" + [ "$#" -eq 0 ] || exec bundle exec "$@" SH puts "#{color('→', CYAN)} #{color(cell.label, BOLD)} #{color("(ruby #{cell.ruby})", DIM)}" -puts " #{color(test_cmd.join(' '), DIM)}" +puts " #{color(test_cmd.empty? ? "bundle install" : test_cmd.join(' '), DIM)}" # bash -c (not -lc): inherit the env mise just set; a login shell would reset # Ruby to the host default. $0 is a label; "$@" is the test command.