Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- [#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).
- [#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).
- [#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).
- [#97](https://github.com/dblock/ruby-gem-release/pull/97): Name the `.gitignore` lines to add when Ruby setup output such as `vendor/bundle` fails the clean-checkout check, including in dry runs, and document them - [@dblock](https://github.com/dblock).
- [#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).
- Your contribution here.

Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ With these defaults, a release:

If your Rakefile also installs the Bundler gem tasks, disable the local Bundler `release` tasks so nobody can publish around the workflow. See [Bundler Release Tasks](docs/usage.md#bundler-release-tasks).

Every release starts by checking that the checkout is clean, and Ruby setup installs gems inside it. Add what it writes to `.gitignore`, leaving out the lockfile if you commit it:

```gitignore
/vendor/bundle/
/.bundle/
/Gemfile.lock
```

With a [release Gemfile](docs/actions/release.md#release-gemfile), ignore its lockfile instead, such as `/gemfiles/release.gemfile.lock`. A [dry run](docs/actions/release.md#dry-run) fails and lists any lines that are missing.

### 2. Add the Release Workflow

Copy the [sample workflow](examples/.github/workflows/release.yml) to `.github/workflows/release.yml` and set your Ruby version:
Expand Down
4 changes: 2 additions & 2 deletions docs/actions/release.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ The check cannot confirm the publisher belongs to this gem, or that RubyGems wil

### Dry Run

Set `dry-run: true` to run only the [prerequisite checks](#release-prerequisites). A separate `preflight` job replaces the `release` job, in the same `release` environment, so required reviewers still approve it. It never edits, commits, tags, pushes, builds, or publishes. To dispatch dry runs, add a boolean input:
Set `dry-run: true` to run only the [prerequisite checks](#release-prerequisites). A separate `preflight` job replaces the `release` job, in the same `release` environment, so required reviewers still approve it. It never edits, commits, tags, pushes, builds, or publishes. It also checks that the checkout is clean after Ruby setup, so a dry run catches setup output such as `vendor/bundle` that `.gitignore` is missing. To dispatch dry runs, add a boolean input:

```yaml
on:
Expand Down Expand Up @@ -138,7 +138,7 @@ The workflow sets `GH_TOKEN` during publication. Branch rules must allow fast-fo

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.

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.
1. **Capture:** after Ruby setup, the checkout must be clean. Changed, staged, or untracked files fail with their paths. Ruby setup installs gems into `vendor/bundle`, writes `.bundle/config`, and creates a lockfile when none is committed; [ignore them](../../README.md#1-configure-your-gem). When they are not ignored, the error lists the `.gitignore` lines to add. Tracked dependency files are not allowed.
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).
3. **Checkpoint:** the workflow commits, builds, and tags locally, then uploads [recovery artifacts](#recovery-artifacts). This must succeed before `publish` and `bump`.
4. **Publish, bump, notify:** any failure stops later steps.
Expand Down
44 changes: 43 additions & 1 deletion lib/ruby-gem-release/source_control/git/preparation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ module RubyGemRelease
module SourceControl
class Git
class Preparation
# Directories ruby/setup-ruby's bundler-cache writes into the checkout.
SETUP_DIRECTORIES = %w[vendor/bundle/ .bundle/].freeze
# The lockfile Bundler writes next to a Gemfile when none is committed.
SETUP_LOCKFILE = %r{(?:\A|/)(?:Gemfile\.lock|gems\.locked|[^/]+\.gemfile\.lock)\z}

def initialize(directory:, &command)
@path = File.join(directory, 'prepared-tree')
@command = command
Expand All @@ -15,7 +20,11 @@ def check_clean!
entries = changes
return if entries.empty?

raise unexpected_changes('Release requires a clean checkout after dependency installation', entries)
message = unexpected_changes('Release requires a clean checkout after dependency installation', entries)
ignores = setup_ignores(entries)
raise message if ignores.empty?

raise "#{message}\n#{setup_hint(ignores)}"
end

def stage_changes!
Expand Down Expand Up @@ -66,7 +75,40 @@ def changes
entries
end

# Ignore lines for untracked Ruby setup output, in a stable order.
def setup_ignores(entries)
untracked = entries.filter_map { |status, path| path if status == '??' }
directories = SETUP_DIRECTORIES.select { |directory| untracked.any? { |path| setup_directory(path) == directory } }
lockfiles = untracked.select { |path| setup_lockfile?(path) }
(directories + lockfiles).map { |path| "/#{path}" }
end

def setup_lockfile?(path)
!setup_directory(path) && SETUP_LOCKFILE.match?(path)
end

def setup_directory(path)
SETUP_DIRECTORIES.find { |directory| path.start_with?(directory) }
end

def setup_hint(ignores)
lockfile = ' (or commit the lockfile)' if ignores.any? { |line| SETUP_LOCKFILE.match?(line) }
"Ruby setup wrote these into the checkout; add them to .gitignore#{lockfile}:\n#{ignores.map { |line| " #{line}" }.join("\n")}"
end

# Folds untracked files under setup directories into one line per directory.
def folded(entries)
entries.each_with_object([]) do |entry, result|
status, path = entry
directory = setup_directory(path) if status == '??'
next result << entry unless directory

result << ['??', directory] unless result.include?(['??', directory])
end
end

def unexpected_changes(message, entries)
entries = folded(entries)
details = entries.first(20).map { |status, path, source| " #{status} #{path.inspect}#{" (from #{source.inspect})" if source}" }
details << " ... #{entries.size - 20} more paths" if entries.size > 20
"#{message} (#{entries.size} paths):\n#{details.join("\n")}\n" \
Expand Down
24 changes: 24 additions & 0 deletions spec/ruby_gem_release/source_control/git_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,30 @@ def prepare_tag(ctx = release)
expect(git.head).to eq(initial)
end

it 'names the gitignore lines for unignored Ruby setup output and folds its directories', :aggregate_failures do
FileUtils.mkdir_p(['.bundle', 'vendor/bundle/ruby/4.0.0/gems/rake-13.3.0', 'gemfiles'])
File.write('.bundle/config', "BUNDLE_PATH: vendor/bundle\n")
30.times { |index| File.write("vendor/bundle/ruby/4.0.0/gems/rake-13.3.0/file#{index}.rb", '') }
File.write('Gemfile.lock', 'lock')
File.write('gemfiles/release.gemfile.lock', 'lock')
expect { git.check_clean! }.to raise_error(RuntimeError) { |error|
expect(error.message).to include('(4 paths):', ' ?? ".bundle/"', ' ?? "vendor/bundle/"', ' ?? "Gemfile.lock"')
expect(error.message).not_to include('rake-13.3.0')
expect(error.message).to end_with(
"Ruby setup wrote these into the checkout; add them to .gitignore (or commit the lockfile):\n /vendor/bundle/\n /.bundle/\n /Gemfile.lock\n /gemfiles/release.gemfile.lock"
)
}
end

it 'omits the lockfile advice without an untracked lockfile and the hint for other files', :aggregate_failures do
FileUtils.mkdir_p('vendor/bundle')
File.write('vendor/bundle/generated.rb', 'dependency')
expect { git.check_clean! }.to raise_error(%r{add them to \.gitignore:\n /vendor/bundle/\z})
FileUtils.rm_rf('vendor')
File.write('notes.txt', 'unexpected')
expect { git.check_clean! }.to raise_error(RuntimeError) { |error| expect(error.message).not_to include('Ruby setup') }
end

it 'permits ignored dependency output without staging it in release preparation' do
File.write('.gitignore', ".bundle/\nvendor/bundle/\n")
command('add', '.gitignore')
Expand Down
Loading