ruby / setup-ruby

An action to download a prebuilt Ruby and add it to the PATH in 5 seconds

Home Page:https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unable to pick 3.2.2 version when only git diff files are in the scope

mpurusottamc opened this issue · comments

Ensure the following before filing this issue

  • I verified it reproduces with the latest version with - uses: ruby/setup-ruby@v1 (see Versioning policy)

  • I tried to reproduce the issue locally by following the workflow steps (including all commands done by ruby/setup-ruby, except for Downloading Ruby & Extracting Ruby),
    and it did not reproduce locally (if it does reproduce locally, it's not a ruby/setup-ruby issue)

Are you running on a GitHub-hosted runner or a self-hosted runner?

GitHub-hosted runner

Link to the failed workflow job (must be a public workflow job, so the necessary information is available)

NA

Any other notes?

I am trying to run rubocop only on the changed files. For that, i am checking out the PR branch. But, when i try to install ruby 3.2.2, it throws an error.

Here's the error (with debug logs enabled):

##[debug]Evaluating condition for step: 'Install Dependencies'
##[debug]Evaluating: success()
##[debug]Evaluating success:
##[debug]=> true
##[debug]Result: true
##[debug]Starting: Install Dependencies
##[debug]Loading inputs
##[debug]Loading env
Run bundle install --jobs 4 --retry 3
##[debug]/usr/bin/bash -e /home/runner/work/_temp/383fcb74-9a47-4dc0-88a6-5c[2](https://github.com/<org>/<repo_name>/actions/runs/8440722176/job/23118618377?pr=1463#step:4:2)9a5b1c67e.sh
Your Ruby version is [3](https://github.com/<org>/<repo_name>/actions/runs/8440722176/job/23118618377?pr=1463#step:4:3).2.2, but your Gemfile specified 
Error: Process completed with exit code 1[8](https://github.com/<org>/<repo_name>/actions/runs/8440722176/job/23118618377?pr=1463#step:4:8).
##[debug]Finishing: Install Dependencies

Screenshot:
Screenshot 2024-03-26 at 11 10 47 AM

Here's my entire yml file:

name: CI

on: [ pull_request_target ]

jobs:
  rubocop:
    name: RuboCop Auto-fix
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Code
        uses: actions/checkout@v4
        with:
          ref: ${{ github.event.pull_request.head.sha }}

      - name: Set up Ruby
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: '3.2.2'

      - name: Install Dependencies
        run: bundle install --jobs 4 --retry 3

      - name: Run RuboCop Auto-correct
        run: |
          # Run RuboCop auto-correct on the changed files
          bundle exec rubocop --auto-correct $(git diff --name-only ${{ github.event.before }}...${{ github.event.after }} | grep '\.rb$')

      - name: Commit Changes
        run: |
          git config --global user.name "GitHub Actions"
          git config --global user.email "actions@users.noreply.github.com"
          git add .
          if git diff --quiet; then
            echo "No changes to commit"
          else
            git commit -m "Auto-fix RuboCop offenses"
            git push
          fi

Any help is much appreciated.

That should repro locally by checking out that branch + running bundle install on Ruby 3.2.2.
It seems a mistmatch between the version specified in the Gemfile and the version passed to setup-ruby.
In any case, it seems pretty clear this is not a setup-ruby issue.

@eregon on the local machine, I am able to checkout and run bundle install with 3.2.2.

In this case though, since the CI job is checking out only the files in the PR, it does not have access to Gemfile or .ruby-version file. Is there a way to skip the check for Gemfile or .ruby-version and install the version specific in the ruby-version parameter?

setup-ruby respects the version you gave it, so it does install Ruby 3.2.2.

The issue if you don't checkout the Gemfile is there is no way to bundle install, so that's a bug of your workflow.

According to the workflow you posted it checks out all files, no?

      - name: Checkout Code
        uses: actions/checkout@v4
        with:
          ref: ${{ github.event.pull_request.head.sha }}

But anyway, the command that fails is run: bundle install --jobs 4 --retry 3, this has nothing to do with setup-ruby as far as I can see, it fails after it.

Thanks for your prompt response @eregon. Yes, I misunderstood :(. checkout step checks out all files.

Adding ruby version to Gemfile to resolve dependency installation issue.