Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Github actions and RAILS_MASTER_KEY

I've been trying to set up GitHub-actions with Rails, however I'm facing an issue with the RAILS_MASTER_KEY (my assumption).

This is the error-message I get in the last step # Build and run tests:

rails aborted!
ActiveSupport::MessageEncryptor::InvalidMessage: ActiveSupport::MessageEncryptor::InvalidMessage
/home/runner/work/my-project/my-project/config/environment.rb:5:in `<main>'
/home/runner/work/my-project/my-project/bin/rails:9:in `<top (required)>'
/home/runner/work/my-project/my-project/bin/spring:15:in `require'
/home/runner/work/my-project/my-project/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'

Caused by:
ArgumentError: key must be 16 bytes

.github/workflows/main.yml:

name: CI
on: [push, pull_request]
jobs:
  test:
    runs-on:  ubuntu-latest
    services:
      db:
        image: postgres:11
        env:
          POSTGRES_PASSWORD: xxxxxx
        ports: ['5432:5432']
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

    steps:
      # Download a copy of the code in the repository before running CI tests
      - name: Checkout repository
        uses: actions/checkout@v2

      # Setup Ruby
      - name: Setup Ruby
        uses: actions/setup-ruby@v1
        with: 
          ruby-version: 2.6.6

      # NPM install
      - name: NPM install with caching
        uses: bahmutov/[email protected]
        # with:
         # cmd: install

      # Build and run tests
      - name: Build and run tests
        env:
          DATABASE_URL: postgres://postgres:@localhost:5432/test
          RAILS_ENV: test
          RAILS_MASTER_KEY: ${{ secrets.RAILS_MASTER_KEY }}
        run: |
          sudo apt-get -yqq install libpq-dev
          gem install bundler
          bundle install --jobs 4 --retry 3
          bundle exec rails db:prepare
          bundle exec rails test

I am inferring that this has to do with my RAILS_MASTER_KEY-variable.

I checked the key which I stored in my project's ENV-variables and it happens to be 32 bytes. I am unsure about how to best proceed and I am hesitant to delete the key, fearing I'll break other parts of the application. Setting an option for the key to be legal with 32 bytes would be much preferable in my book - it's just that I wouldn't know how and where...

Any thoughts on how to solve this issue?

like image 868
Dominik Picke Avatar asked Apr 26 '20 16:04

Dominik Picke


People also ask

What are GitHub Actions and workflows?

GitHub Actions is a continuous integration and continuous delivery (CI/CD) platform that allows you to automate your build, test, and deployment pipeline. You can create workflows that build and test every pull request to your repository, or deploy merged pull requests to production.

What is the difference between GitHub and GitHub Actions?

GitHub Actions helps you automate your software development workflows from within GitHub. You can deploy workflows in the same place where you store code and collaborate on pull requests and issues. In GitHub Actions, a workflow is an automated process that you set up in your GitHub repository.

What are GitHub Actions and packages?

About GitHub Packages with GitHub ActionsGitHub Actions help you automate your software development workflows in the same place you store code and collaborate on pull requests and issues. You can write individual tasks, called actions, and combine them to create a custom workflow.

Why are GitHub Actions better?

GitHub Actions can be triggered by any event that happens within your repository. This makes it possible to do anything from automating unit tests to adding labels to pull requests. Workflows and jobs can be referenced from each other, allowing you to spin up new tests that leverage old ones quickly.


2 Answers

@Ruslan Vaeev gives the first part of the answer - add the master key value to a GitHub repo Secret. Next use that secret in your workflow yml file by using ${{ secrets.RAILS_MASTER_KEY }} such as:

env:
  RAILS_MASTER_KEY: ${{ secrets.RAILS_MASTER_KEY }} 
like image 94
cdmo Avatar answered Oct 11 '22 04:10

cdmo


I faced the same problem and I found a solution by creating credentials for my test environment.

EDITOR=vim rails credentials:edit --environment --test

You might need to upload your test key file to your repo so credentials file can be decrypted. Just be careful to maintain only test info.

I found the explanation here: https://blog.saeloun.com/2019/10/10/rails-6-adds-support-for-multi-environment-credentials.html

And I uploaded my configs here: https://tello.io/ruby-rails-github-actions-mysql

like image 37
Daniel Tello Avatar answered Oct 11 '22 02:10

Daniel Tello