简体   繁体   中英

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/npm-install@v1.1.0
        # 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?

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

You need to add secret to your Github repository. Go to "Settings" tab click 'New secret' and put there content of the file master.key and save with name RAILS_MASTER_KEY. Then you can use it in your CI .yml file like secrets.RAILS_MASTER_KEY 在此处输入图像描述

@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 }} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM