2

I have a Ruby script that I want to run when a pull-request is created. This pull-request validates a series of conditions to be sure the pull-request can be merged. It's a very simple script with no external gems, just standard Ruby.

I'm trying to run this script on a job on a run step. The problem is, I'm not sure the path where the file should be saved.

The script is called: validator.rb. From my local computer I can run the script using:

ruby -r ./validator.rb -e "Validator.new.validate_something 'One parameter'"

This works fine locally but when I push this to GitHub it is failing. I saved my script as .github/workflows/ruby-scripts so my job looks like this:

jobs:
  title:
    name: "Title"
    runs-on: ubuntu-latest
    steps:
      - run: ruby -r ./ruby-scripts/validator.rb -e "Validator.new.validate_something '${{ github.event.pull_request.title}}'"

And I get:

Run ruby -r ./ruby-scripts/validator.rb -e "Validator.new.validate 'Create README.md'"
/usr/lib/ruby/vendor_ruby/rubygems/defaults/operating_system.rb:10: warning: constant Gem::ConfigMap is deprecated
/usr/lib/ruby/vendor_ruby/rubygems/defaults/operating_system.rb:10: warning: constant Gem::ConfigMap is deprecated
/usr/lib/ruby/vendor_ruby/rubygems/defaults/operating_system.rb:29: warning: constant Gem::ConfigMap is deprecated
/usr/lib/ruby/vendor_ruby/rubygems/defaults/operating_system.rb:30: warning: constant Gem::ConfigMap is deprecated
/usr/lib/ruby/vendor_ruby/rubygems/defaults/operating_system.rb:10: warning: constant Gem::ConfigMap is deprecated
/usr/local/lib/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:92:in `require': cannot load such file -- ./ruby-scripts/validator.rb (LoadError)
  from /usr/local/lib/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:92:in `require'
##[error]Process completed with exit code 1.

I tried with all the possible combination of paths and it fails each time.

Running pwd and ls returned:

 - run: pwd => /home/runner/work/repo-name/repo-name
 - run: ls => shell: /bin/bash -e {0}

What is the right way to do this?

5
  • - run: ruby -r ruby-scripts/validator.rb -e also didn't work? Commented Mar 26, 2020 at 18:17
  • Hi @AlexGolubenko, thanks for helping. It didn't work either. I think I tested all the possible combinations and nothing. Commented Mar 26, 2020 at 18:31
  • Can you get the script to print a ls and find out what you’ve got in the directory? Permissions might also be relevant, Ruby is sometimes bad at reporting errors. Commented Mar 26, 2020 at 18:58
  • @simonwo edited the question with the results of running that and I also run a pwd Commented Mar 26, 2020 at 19:11
  • 1
    It's because you didn't check out your repo. By default, your repository won't be checked out unless you use the Checkout GitHub Action to check out your code. Commented Mar 29, 2020 at 8:51

2 Answers 2

4

As I've mentioned in the comments, the reason why your workflow isn't working is that you forgot the crucial step that checks-out your repository. By default, the workspace is empty unless you check out the repository with the Checkout GitHub Action.

From the GitHub Action's README:

This action checks-out your repository under $GITHUB_WORKSPACE, so your workflow can access it.

Here's an example:

- name: Checkout Repo
  uses: actions/checkout@v2

(That's it, really)

(Note: You don't have to specify a name for the step.)

Sign up to request clarification or add additional context in comments.

Comments

0

For me, the problem was also the location of the file. The ruby script was running in the root of the repository, not in the same path as the workflow YAML (as I was expecting).

I was running run: ruby -r ./my-file.rb, where my-file.rb was next to the workflow yaml.

I realized this by adding this step:

- run: ruby -e 'p `ls`.split("\n")'

Which printed the output of ls in a ruby array.

I fixed using: run: ruby ./.github/workflows/my_file.rb

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.