GitHub Actions enables you to create custom workflows for your software development life cycle and automate them within your GitHub repository. In this article, we'll walk through how to create a GitHub action in Ruby to automate your continuous integration pipeline.
Note: Before you start, you'll need a basic understanding of GitHub Actions. If you're not familiar with GitHub Actions, first read the GitHub guide to understanding GitHub Actions, which explains essential concepts and terminology.
GitHub Actions is becoming increasingly popular with organisations who are looking to automate the workflows in their CI/CD pipeline. GitHub Actions is free to use in public and private repositories, with limited free minutes and storage per month. It's also very easy to configure GitHub Actions, as the workflow is available in the repository, with no need for any additional configuration. There is a substantial library of built-in actions available for free, which makes it easy to get started. You can also compose your workflow by using multiple actions.
Much has been documented about how to create a GitHub action in JavaScript. The GitHub guide to creating a JavaScript action walks through the steps. However, in Ruby, it's not as straightforward.
You have two options:
To create a GitHub action in Ruby using a Dockerfile, you'll need to:
Create a minimal Ruby project that contains a Ruby file, for example run.rb
, in the root directory and a Gemfile to manage dependencies in your project.
Create a Dockerfile in the root of your project. Add run.rb
as the entry point.
FROM ruby:2.7.0
RUN gem install bundler
RUN mkdir -p /runner/action
WORKDIR /runner/action
COPY Gemfile* ./
COPY run.rb ./
RUN bundle install --retry 3
ENV BUNDLE_GEMFILE /runner/action/Gemfile
RUN chmod +x /runner/action/run.rb
ENTRYPOINT ["ruby", "/runner/action/run.rb"]
Create an action.yml
metadata file that defines the input/output and the information required to publish your action. You can customise your action by using the metadata syntax for GitHub Actions, including customising how your action will look. Here is an example:
name: 'Unique name of your action'
author: '[email protected]'
description: 'description of what action does'
branding:
icon: code
color: red
runs:
using: 'docker'
image: 'Dockerfile'
Push your changes to your default branch (master/main).
Here is an example of a Simply Business public GitHub action - the deploy status action. The deploy status action uses the Octokit library to call the GitHub API and create statuses that can be used to block and unblock deploys in the pipeline.
To use the action you created in your project:
.github/workflows/my-workflow.yml
in your repository, which will be triggered by an event such as when a pull request is opened or changes are pushed to the repository.
name: 'my workflow'
on:
pull_request:
types: [opened, synchronize]
permissions:
contents: read
statuses: write # change according to the permissions your workflow needs. For more info, visit: https://docs.github.com/en/actions/learn-github-actions/workflow-syntax-for-github-actions#permissions
jobs:
build:
runs-on: ubuntu-20.04
steps:
- uses: org-name/[email protected]
env:
ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }} # provided by GitHub. You can use this to access the GitHub API.
actions
tab of your repository. If not, check the workflow to ensure there are no typos or syntax errors.
Docker container actions are great, but they don't take advantage of the caching action that GitHub provides. Therefore, each run can take a long time. We can save a lot of time (over half) by caching bundler dependencies after the first run. It not only saves time but reduces the number of minutes taken to run the action, thereby saving money if you're using a private repository.
Here are the steps for creating an action using the setup-ruby action:
run.rb
file, .ruby-version
to specify ruby at the root directory and a Gemfile to manage dependencies in your project.action.yml
metadata file. Note here we are not using a Dockerfile in the run section, instead using a composite action to run the Ruby code. You can customise your action by using the metadata syntax for GitHub Actions, including customising how your action will look.Here is an example:
name: 'Unique name of your action'
description: 'Description of what your action does'
author: '[email protected]'
branding:
icon: filter
color: red
runs:
using: 'composite'
steps:
- run: |
bundle exec ruby run.rb
shell: bash
Here is an example of a Simply Business public GitHub action - dobby. Dobby uses the Octokit library to call the GitHub API to update the gem version based on a comment added to a pull request.
To use the action in your project, you'll need to add some additional steps in your workflow:
.github/workflows/my-workflow.yml
, for example:
name: 'my workflow'
on:
pull_request:
types: [opened, synchronize]
permissions:
contents: read
statuses: write # change according to the permissions your workflow needs. For more info, visit: https://docs.github.com/en/actions/learn-github-actions/workflow-syntax-for-github-actions#permissions
jobs:
build:
runs-on: ubuntu-20.04
steps:
- uses: actions/[email protected]
with:
repository: 'org-name/[email protected]'
ref: 'v1.0.0'
- uses: ruby/setup-ruby@v1
with:
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
- uses: org-name/[email protected]
env:
ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }} # provided by GitHub. You can use this to access the GitHub API.
actions
tab of your repository. If not, check the workflow to ensure there are no typos or syntax errors.It's not easy to debug a GitHub action locally, primarily because an action provides a lot of tools and environments (ENV) that are not available locally. That said, there are several things that you can try:
I hope this article helps you to get started with GitHub actions in Ruby. Enjoy!
Want to know more about what it's like to work in tech at Simply Business? Read about our approach to tech, then check out our current vacancies.
Find out moreWe create this content for general information purposes and it should not be taken as advice. Always take professional advice. Read our full disclaimer
Keep up to date with Simply Business. Subscribe to our monthly newsletter and follow us on social media.
Subscribe to our newsletter6th Floor99 Gresham StreetLondonEC2V 7NG
Sol House29 St Katherine's StreetNorthamptonNN1 2QZ
© Copyright 2023 Simply Business. All Rights Reserved. Simply Business is a trading name of Xbridge Limited which is authorised and regulated by the Financial Conduct Authority (Financial Services Registration No: 313348). Xbridge Limited (No: 3967717) has its registered office at 6th Floor, 99 Gresham Street, London, EC2V 7NG.