A Rails and PostgreSQL setup for GitHub actions (CI)
Tags: rails, github, ci, actions
Published: 2020-01-10 10:16:52

---
title: A Rails and PostgreSQL setup for GitHub actions (CI)
published: true
description: 
tags: rails, github, ci, actions
cover_image: https://thepracticaldev.s3.amazonaws.com/i/790hc6m8ptzlx7akqzmd.png
---

Since the release of [GitHub actions](https://github.com/features/actions) I always wanted to setup a continuous integration using GitHub actions [as described in their documentation](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/about-continuous-integration).

Why even use GitHub actions? 👉 The less tools I have to use to do the job (coding), the better. So if GitHub is now providing a way to not have to use external tools like [Travis CI](https://travis-ci.com/) or [CircleCI](https://circleci.com/) then I am happy with it. **And later on** if GitHub also starts providing cloud services, and they are good, I'll just use that too.

_Table of contents:_
- [Requirements](#requirements)
- [Full workflow example](#full-workflow-example)
- [Optional step: secrets as environment variables](#optional-step-secrets-as-environment-variables)
- [Opinions and current issues with GitHub actions](#opinions-and-current-issues-with-github-actions)

# Requirements

I currently work on a [Rails 6 application](https://dev.to/vvo/a-rails-6-setup-guide-for-2019-and-2020-hf5) using PostgreSQL so **I needed a workflow that would allow me to build and test a Rails 6 application using GitHub actions**. Since actions are a recent feature ([released publicly](https://github.blog/2019-08-08-github-actions-now-supports-ci-cd/) on August 8, 2019), it took me a bit more time than usual to have it working completely.

**I like reproducible environments** and so I needed a workflow that would:
1. Install the ruby version specified in `.ruby-version`, along with [Bundler](https://bundler.io/)
2. Install the Node.js version (for webpacker) specified in `.nvmrc` along with [Yarn](https://yarnpkg.com/lang/en/)
3. Easily allow me to have a PostgreSQL database
4. Cache steps as much as possible (node_modules, bundler) so that I have a fast build

# Full workflow example

GitHub workflows are a set of GitHub actions and are stored in your code, inside the `.github/workflows` directory. GitHub actions can either be from the official [GitHub actions](https://github.com/actions) or from their [actions marketplace](https://github.com/marketplace?type=actions).

The first step to testing your Rails application with GitHub actions is to create a file at `.github/workflows/test.yml` and paste in it the content of [this gist](https://gist.github.com/vvo/5f7b8a1d34d5efc2df18140fb1d3ed20):

**.github/workflows/test.yml:**
{% gist https://gist.github.com/vvo/5f7b8a1d34d5efc2df18140fb1d3ed20 %}

Then commit and push, here's the result:

![GitHub workflow example running](https://thepracticaldev.s3.amazonaws.com/i/ahvh8fef794ai3xtyzww.gif)

# Optional step: secrets as environment variables

If you are using [Rails credentials](https://guides.rubyonrails.org/security.html#custom-credentials) then you need the content of the file `master.key` as a GitHub secret named `RAILS_MASTER_KEY`. To create secrets in GitHub, just go in your repository settings in the `secret` tab.

Then if you look at the previous gist, you'll see this:

```yaml
      - name: Rails test
        env: # Or as an environment variable
          RAILS_MASTER_KEY: ${{ secrets.RAILS_MASTER_KEY }}
```

Which will add the `RAILS_MASTER_KEY` environment variable for your Rails application to decrypt Rails credentials.

![GitHub secrets example](https://thepracticaldev.s3.amazonaws.com/i/8kowedq9rzqr96jlquqx.png)

# Opinions and current issues with GitHub actions

While I enjoy the fact of having a continuous integration system that is builtin into GitHub, since GitHub actions are so new, they lack the attention to detail and experience of other tools for now. I will list the main ones from my point of view and I advise you to follow their associated GitHub issues if any, if you want to follow their progress:

- **no support for [package.json's engines field](https://docs.npmjs.com/files/package.json#engines) or [`.nvmrc`](https://github.com/nvm-sh/nvm#nvmrc)** (to specify which version of Node.js to install) in the default `setup-node` action from GitHub.

{% github github.com/actions/setup-node/issues/94 %}
{% github github.com/actions/setup-node/issues/32 %}

- **no support for [`.ruby-version`](https://github.com/rbenv/rbenv#choosing-the-ruby-version)** files (to specify which version of Ruby to install) in the official [action/setup-ruby](https://github.com/actions/setup-ruby) action GitHub.

{% github github.com/actions/setup-ruby/issues/31 %}

- For some libraries, like [ged/ruby-pg](https://github.com/ged/ruby-pg) you have to apt-get install the `libpq-dev` package while this should be a default.

{% github https://github.com/actions/virtual-environments/issues/12 %}

- Actions are versionned but it would be great if there was a way to be notified of new versions easily. Or even better, to have GitHub bots opening pull requests to upgrade the versions of the actions. So that you always have the latest features available
- no way to [manually trigger workflows](github.community/t5/GitHub-Actions/GitHub-Actions-Manual-Trigger-Approvals/m-p/31517#M813)

---

🔚 That's it!

🙏 Thank you for reading.

_Thanks to [Benoit Daloze](https://github.com/eregon/) and [Masatoshi Iwasaki](https://github.com/masa-iwasaki) for their help on Ruby and Bundler setup in GitHub workflows._

Did you enjoy this post? Any advice as for GitHub actions and Rails configuration? Add your thank you note or advice in the comments and I'll reply :)

Click below to share this post:

{% twitter 1215578842045259778 %}

![Cat says: ACTION!](https://thepracticaldev.s3.amazonaws.com/i/3u6qutlm5kekez8ovbsf.gif)