Skip to content

k1LoW/github-script-ruby

Use this GitHub action with your project
Add this Action to an existing workflow or create a new one
View on Marketplace

Repository files navigation

github-script-ruby Test Coverage Code to Test Ratio Test Execution Time

This action makes it easy to write Ruby scripts in the workflow, just like actions/github-script.

In order to use this action, a script input is provided. The value of that input should be the body of an function call. The following arguments will be provided:

Examples

Print the available attributes of context

ref: actions/github-script example

- name: View context attributes
  uses: k1LoW/github-script-ruby@v2
  with:
    script: pp context

Comment on an issue

ref: actions/github-script example

on:
  issues:
    types: [opened]

jobs:
  comment:
    runs-on: ubuntu-latest
    steps:
      - uses: k1LoW/github-script-ruby@v2
        with:
          script: |
            repo = "#{context.repo.owner}/#{context.repo.repo}"
            number = context.issue.number
            comment = '👋 Thanks for reporting!'
            github.add_comment(repo, number, comment)

Use Gem packages

It is possible to change the Gemfile to use.

If you want to use octokit.rb, don't forget to add it.

- name: 'Post message to Slack #general channel'
  uses: k1LoW/github-script-ruby@v2
  with:
    script: |
      require 'slack-ruby-client'
      Slack.configure do |config|
        config.token = ENV['SLACK_API_TOKEN']
      end
      client = Slack::Web::Client.new
      client.chat_postMessage(channel: '#general', text: 'Hello, Slack bot!')
    gemfile: |
      source 'https://rubygems.org'
      gem 'octokit', '~> 4.0'
      gem 'slack-ruby-client'
  env:
    SLACK_API_TOKEN: ${{ secrets.SLACK_API_TOKEN }}

Pre-install packages for building native extentions.

- name: 'List users'
  uses: k1LoW/github-script-ruby@v2
  with:
    script: |
      require 'mysql2'
      client = Mysql2::Client.new(:host => "localhost", :username => "root")
      client.query('SELECT * FROM users').each do |row|
        puts row['name']
      end
    pre-command: |
      apt-get update
      apt-get install -y libmysqld-dev
    gemfile: |
      source 'https://rubygems.org'
      gem 'mysql2'

Change Ruby version of use

- name: 'Hello Ruby version'
  uses: k1LoW/github-script-ruby@v2
  with:
    script: |
      repo = "#{context.repo.owner}/#{context.repo.repo}"
      number = context.issue.number
      comment = "Hello using Ruby v#{RUBY_VERSION}"
      github.add_comment(repo, number, comment)
    ruby-version: 2.7.5

The ruby-version: feature is realized using the prebuilt Ruby releases are generated by ruby-builder

Execute Ruby script file

- name: 'Capistrano deploy'
  uses: k1LoW/github-script-ruby@v2
  with:
    gemfile-path: path/to/Gemfile
    command: bundle exec cap deploy --gemfile=path/to/Gemfile
    ruby-version: 2.7.5

References