Skip to content

Commit ae31bf0

Browse files
dblockclaude
andcommitted
Ignore Ruby setup output in the release checkout
ruby/setup-ruby's bundler-cache writes vendor/bundle, .bundle/config and, when none is committed, the lockfile into the checkout, and the capture step, which also runs in dry runs, fails on untracked files. Consumers had to add these to their .gitignore or every release and dry run failed. Right after Ruby setup, the reusable workflow's preflight and release jobs and the composite action now add exactly these paths to the checkout's .git/info/exclude, mirroring setup-ruby's Gemfile and lockfile detection. The lockfile is only excluded when Git doesn't track it, so a changed committed lockfile, and any other untracked file, still fails the clean-checkout check. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011Gr21htyJfr8XZGHBcPwkW
1 parent 4389652 commit ae31bf0

5 files changed

Lines changed: 141 additions & 5 deletions

File tree

‎.github/workflows/release.yml‎

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ on:
1010
description: Optional gemspec path; defaults to the only root gemspec.
1111
type: string
1212
gemfile:
13-
description: Gemfile for Bundler, relative to the repository root; its lockfile must be committed or ignored.
13+
description: Gemfile for Bundler, relative to the repository root.
1414
type: string
1515
default: Gemfile
1616
rubygems-owner:
@@ -101,6 +101,29 @@ jobs:
101101
with:
102102
ruby-version: ${{ inputs.ruby-version }}
103103
bundler-cache: true
104+
- name: Ignore Ruby Setup Output
105+
id: ignore_ruby_setup_output
106+
shell: bash
107+
run: |
108+
# ruby/setup-ruby's bundler-cache writes these into the checkout; ignore them
109+
# locally so the clean-checkout check only fails on the project's own files.
110+
exclude="$(git rev-parse --git-path info/exclude)"
111+
mkdir -p "$(dirname "$exclude")"
112+
gemfile="${BUNDLE_GEMFILE:-Gemfile}"
113+
lockfile="${BUNDLE_LOCKFILE:-}"
114+
if [[ -z "$lockfile" ]]; then
115+
if [[ -z "${BUNDLE_GEMFILE:-}" && ! -f Gemfile && -f gems.rb ]]; then
116+
lockfile=gems.locked
117+
else
118+
lockfile="$gemfile.lock"
119+
fi
120+
fi
121+
lockfile="${lockfile#"$PWD/"}"
122+
paths=(/vendor/bundle/ /.bundle/)
123+
if ! git ls-files --error-unmatch -- "$lockfile" >/dev/null 2>&1; then
124+
paths+=("/$(printf '%s' "$lockfile" | sed 's/[][*?\\!#]/\\&/g')")
125+
fi
126+
printf '%s\n' "${paths[@]}" >> "$exclude"
104127
- name: Capture Release Context
105128
id: capture_release_context
106129
env:
@@ -135,6 +158,29 @@ jobs:
135158
with:
136159
ruby-version: ${{ inputs.ruby-version }}
137160
bundler-cache: true
161+
- name: Ignore Ruby Setup Output
162+
id: ignore_ruby_setup_output
163+
shell: bash
164+
run: |
165+
# ruby/setup-ruby's bundler-cache writes these into the checkout; ignore them
166+
# locally so the clean-checkout check only fails on the project's own files.
167+
exclude="$(git rev-parse --git-path info/exclude)"
168+
mkdir -p "$(dirname "$exclude")"
169+
gemfile="${BUNDLE_GEMFILE:-Gemfile}"
170+
lockfile="${BUNDLE_LOCKFILE:-}"
171+
if [[ -z "$lockfile" ]]; then
172+
if [[ -z "${BUNDLE_GEMFILE:-}" && ! -f Gemfile && -f gems.rb ]]; then
173+
lockfile=gems.locked
174+
else
175+
lockfile="$gemfile.lock"
176+
fi
177+
fi
178+
lockfile="${lockfile#"$PWD/"}"
179+
paths=(/vendor/bundle/ /.bundle/)
180+
if ! git ls-files --error-unmatch -- "$lockfile" >/dev/null 2>&1; then
181+
paths+=("/$(printf '%s' "$lockfile" | sed 's/[][*?\\!#]/\\&/g')")
182+
fi
183+
printf '%s\n' "${paths[@]}" >> "$exclude"
138184
- name: Prepare Input Artifacts
139185
id: prepare_input_artifacts
140186
if: ${{ inputs.prepare-artifacts != '' }}

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- [#92](https://github.com/dblock/ruby-gem-release/pull/92): Document manual recovery from the preserved bundle when the default branch advances during publication - [@dblock](https://github.com/dblock).
88
- [#93](https://github.com/dblock/ruby-gem-release/pull/93): Document disabling the local Bundler `release` tasks so the workflow is the only release path - [@dblock](https://github.com/dblock).
99
- [#95](https://github.com/dblock/ruby-gem-release/pull/95): Rewrite the documentation in plain language, consolidating repeated rules so each lives in one place - [@dblock](https://github.com/dblock).
10+
- [#97](https://github.com/dblock/ruby-gem-release/pull/97): Ignore the `vendor/bundle`, `.bundle/config` and uncommitted lockfile that Ruby setup writes into the checkout, which failed the clean-checkout check in every release and dry run unless the project's `.gitignore` covered them - [@dblock](https://github.com/dblock).
1011
- [#98](https://github.com/dblock/ruby-gem-release/pull/98): Document installing release tasks from a separate release Gemfile with `Gem.loaded_specs` instead of `rescue LoadError` - [@dblock](https://github.com/dblock).
1112
- Your contribution here.
1213

‎action.yml‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ inputs:
1313
description: Optional gemspec path; defaults to the only root gemspec.
1414
default: ''
1515
gemfile:
16-
description: Optional Gemfile for Bundler, relative to the repository root; its lockfile must be committed or ignored. Empty keeps the job's BUNDLE_GEMFILE or Gemfile.
16+
description: Optional Gemfile for Bundler, relative to the repository root. Empty keeps the job's BUNDLE_GEMFILE or Gemfile.
1717
default: ''
1818
rubygems-owner:
1919
description: Optional RubyGems account ID for first publication with a pending trusted publisher.
@@ -79,6 +79,29 @@ runs:
7979
with:
8080
ruby-version: ${{ inputs.ruby-version }}
8181
bundler-cache: true
82+
- name: Ignore Ruby Setup Output
83+
id: ignore_ruby_setup_output
84+
shell: bash
85+
run: |
86+
# ruby/setup-ruby's bundler-cache writes these into the checkout; ignore them
87+
# locally so the clean-checkout check only fails on the project's own files.
88+
exclude="$(git rev-parse --git-path info/exclude)"
89+
mkdir -p "$(dirname "$exclude")"
90+
gemfile="${BUNDLE_GEMFILE:-Gemfile}"
91+
lockfile="${BUNDLE_LOCKFILE:-}"
92+
if [[ -z "$lockfile" ]]; then
93+
if [[ -z "${BUNDLE_GEMFILE:-}" && ! -f Gemfile && -f gems.rb ]]; then
94+
lockfile=gems.locked
95+
else
96+
lockfile="$gemfile.lock"
97+
fi
98+
fi
99+
lockfile="${lockfile#"$PWD/"}"
100+
paths=(/vendor/bundle/ /.bundle/)
101+
if ! git ls-files --error-unmatch -- "$lockfile" >/dev/null 2>&1; then
102+
paths+=("/$(printf '%s' "$lockfile" | sed 's/[][*?\\!#]/\\&/g')")
103+
fi
104+
printf '%s\n' "${paths[@]}" >> "$exclude"
82105
- name: Prepare Input Artifacts
83106
id: prepare_input_artifacts
84107
if: ${{ inputs.prepare-artifacts != '' }}

‎docs/actions/release.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ gem 'commonmarker', '~> 2.10', require: false
9292
gem 'ruby-gem-release', '0.9.0', require: false
9393
```
9494

95-
The workflow sets `BUNDLE_GEMFILE` for the whole job, so every step and default command, including the full `test` suite, uses this bundle. Commit or ignore the lockfile Bundler writes next to it; an untracked lockfile fails the clean-checkout check. In your Rakefile, install the release tasks only when the gem is in the active bundle, so the main bundle works without it:
95+
The workflow sets `BUNDLE_GEMFILE` for the whole job, so every step and default command, including the full `test` suite, uses this bundle. If you don't commit its lockfile, the workflow ignores the one Bundler writes next to it. In your Rakefile, install the release tasks only when the gem is in the active bundle, so the main bundle works without it:
9696

9797
```ruby
9898
if Gem.loaded_specs.key?('ruby-gem-release')
@@ -138,7 +138,7 @@ The workflow sets `GH_TOKEN` during publication. Branch rules must allow fast-fo
138138

139139
The job runs on `ubuntu-latest` in the `release` environment with a full checkout. Releases share one concurrency group per repository, so a new release waits rather than canceling a running one.
140140

141-
1. **Capture:** after Ruby setup, the checkout must be clean. Changed, staged, or untracked files fail with their paths. Ignored Bundler output is fine; tracked dependency files are not.
141+
1. **Capture:** after Ruby setup, the checkout must be clean, in a dry run too. Changed, staged, or untracked files fail with their paths. Ruby setup writes `vendor/bundle`, `.bundle/config` and, when none is committed, the lockfile into the checkout; the workflow adds exactly these to the checkout's `.git/info/exclude`, so you don't need to ignore them. Other ignored files are fine; tracked files are never exempt, so a changed committed lockfile still fails.
142142
2. **Validate, prepare, test:** files changed by `prepare` are staged before tests. Publication rejects any later change instead of committing it. Custom bump callbacks wrap edits in `git.stage_changes!`, as in [Detailed Usage](../usage.md#custom-callbacks).
143143
3. **Checkpoint:** the workflow commits, builds, and tags locally, then uploads [recovery artifacts](#recovery-artifacts). This must succeed before `publish` and `bump`.
144144
4. **Publish, bump, notify:** any failure stops later steps.

‎spec/workflow_interfaces_spec.rb‎

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,24 @@
2121
end
2222
end
2323

24+
it 'ignores Ruby setup output immediately after Ruby setup' do
25+
setup = steps.index { |step| step['id'] == 'setup_ruby' }
26+
ignore = steps.find { |step| step['id'] == 'ignore_ruby_setup_output' }
27+
expect(steps.index(ignore)).to eq(setup + 1)
28+
expect(ignore).to include('shell' => 'bash')
29+
expect(ignore).not_to have_key('if')
30+
end
31+
32+
it 'leaves only the project files for the clean-checkout check after Ruby setup', :aggregate_failures do
33+
expect(setup_output_status(files: %w[Gemfile])).to eq([])
34+
expect(setup_output_status(files: %w[Gemfile Gemfile.lock])).to eq([' M Gemfile.lock'])
35+
expect(setup_output_status(files: %w[Gemfile], untracked: %w[notes.txt])).to eq(['?? notes.txt'])
36+
expect(setup_output_status(files: %w[Gemfile gemfiles/release.gemfile], untracked: %w[Gemfile.lock], env: { 'BUNDLE_GEMFILE' => 'gemfiles/release.gemfile' })).to eq(['?? Gemfile.lock'])
37+
expect(setup_output_status(files: %w[gemfiles/release.gemfile], env: ->(root) { { 'BUNDLE_GEMFILE' => File.join(root, 'gemfiles/release.gemfile') } })).to eq([])
38+
expect(setup_output_status(files: %w[gems.rb], lockfile: 'gems.locked')).to eq([])
39+
expect(setup_output_status(files: %w[Gemfile], untracked: %w[Gemfile.lock], lockfile: 'locks/[ci]*.lock', env: { 'BUNDLE_LOCKFILE' => 'locks/[ci]*.lock' })).to eq(['?? Gemfile.lock'])
40+
end
41+
2442
it 'checks release prerequisites after capture, before validation, edits, or publication' do
2543
preflight = steps.find { |step| step['id'] == 'check_release_prerequisites' }
2644
expect(preflight.fetch('run')).to eq('bundle exec rake release:workflow:preflight')
@@ -108,6 +126,52 @@
108126
end
109127
end
110128

129+
# Commits the project files to a fresh repository, adds the untracked ones,
130+
# runs the ignore step, simulates what `ruby/setup-ruby` writes, and returns
131+
# what `git status` reports.
132+
def setup_output_status(files:, untracked: [], env: {}, lockfile: nil)
133+
Dir.mktmpdir do |root|
134+
root = File.realpath(root)
135+
create_repository(root, files, untracked)
136+
values = ENV.to_h.slice('PATH', 'HOME', 'TMPDIR').merge(env.respond_to?(:call) ? env.call(root) : env)
137+
run_ignore_step(root, values)
138+
write_files(root, setup_output(root, values, lockfile))
139+
git(root, 'status', '--porcelain', '--untracked-files=all').lines.map(&:chomp)
140+
end
141+
end
142+
143+
def create_repository(root, files, untracked)
144+
git(root, 'init', '-q')
145+
write_files(root, files + untracked)
146+
git(root, 'add', '--', *files)
147+
git(root, '-c', 'user.name=spec', '-c', 'user.email=spec@example.com', 'commit', '-q', '-m', 'initial')
148+
end
149+
150+
def setup_output(root, values, lockfile)
151+
gemfile = values.fetch('BUNDLE_GEMFILE', 'Gemfile').delete_prefix("#{root}/")
152+
['vendor/bundle/ruby/3.3.0/gems/rake/rake.rb', '.bundle/config', lockfile || "#{gemfile}.lock"]
153+
end
154+
155+
def run_ignore_step(root, values)
156+
script = steps.find { |step| step['id'] == 'ignore_ruby_setup_output' }.fetch('run')
157+
_output, error, status = Open3.capture3(values, 'bash', '-euo', 'pipefail', '-c', script, chdir: root, unsetenv_others: true)
158+
raise error unless status.success?
159+
end
160+
161+
def write_files(root, files)
162+
files.each do |file|
163+
FileUtils.mkdir_p(File.dirname(File.join(root, file)))
164+
File.write(File.join(root, file), "# #{file}\n", mode: 'a')
165+
end
166+
end
167+
168+
def git(root, *)
169+
output, error, status = Open3.capture3('git', '-C', root, *)
170+
raise error unless status.success?
171+
172+
output
173+
end
174+
111175
def run_step(id, directory:, command: '')
112176
values = ENV.to_h.slice('PATH', 'HOME', 'TMPDIR').merge(
113177
'RUNNER_TEMP' => directory, 'RELEASE_COMMAND' => command,
@@ -143,7 +207,9 @@ def run_step(id, directory:, command: '')
143207
preflight = workflow.dig('jobs', 'preflight')
144208
expect(preflight).to include('if' => '${{ inputs.dry-run }}', 'environment' => 'release')
145209
expect(preflight.dig('env', 'RUBYGEMS_OWNER_ID')).to eq('${{ inputs.rubygems-owner }}')
146-
expect(preflight.fetch('steps').map { |step| step['run'] }.compact).to eq(['bundle exec rake release:workflow:capture', 'bundle exec rake release:workflow:preflight'])
210+
expect(preflight.fetch('steps').map { |step| step['id'] }).to eq(%w[checkout setup_ruby ignore_ruby_setup_output capture_release_context check_release_prerequisites])
211+
expect(preflight.fetch('steps').find { |step| step['id'] == 'ignore_ruby_setup_output' }).to eq(steps.find { |step| step['id'] == 'ignore_ruby_setup_output' })
212+
expect(preflight.fetch('steps').map { |step| step['run'] }.compact.drop(1)).to eq(['bundle exec rake release:workflow:capture', 'bundle exec rake release:workflow:preflight'])
147213
expect(preflight.fetch('env')).to eq(workflow.dig('jobs', 'release', 'env'))
148214
expect(preflight.fetch('steps').last).to eq(steps.find { |step| step['id'] == 'check_release_prerequisites' })
149215
end

0 commit comments

Comments
 (0)