Rails 5.1 with Webpacker and Yarn

rails
postgresql
yarn

#1

I have a new application using Rails 5.1.1, Webpacker and Yarn.

The example 5.1 application does not use webpacker and do therefore not need the same configurations. There is a PR adding a new configuration, which I have tried but couldn’t get to work either.

My config looks like this:

version: 2
jobs:
  build:
    working_directory: ~/inspections
    docker:
      - image: circleci/ruby:2.4.1-node
        environment:
          PGHOST: 127.0.0.1
          PGUSER: myapp-test
          RAILS_ENV: test
      - image: circleci/postgres:9.6.3-alpine
        environtment:
          POSTGRES_USER: myapp
          POSTGRES_DB: myapp-test
          POSTGRES_PASSWORD: ""
    steps:
      - checkout

      - type: cache-restore
        name: Restore bundle cache
        key: myapp-bundle-{{ checksum "Gemfile.lock" }}

      - type: cache-restore
        name: Restore yarn cache
        key: myapp-yarn-{{ checksum "yarn.lock" }}

      - run:
          name: Bundle Install
          command: bin/bundle install --path vendor/bundle

      - run:
          name: Yarn Install
          command: yarn install

      - type: cache-save
        name: Store bundle cache
        key: myapp-bundle-{{ checksum "Gemfile.lock" }}
        paths:
          - vendor/bundle

      - type: cache-save
        name: Store yarn cache
        key: myapp-yarn-{{ checksum "yarn.lock" }}
        paths:
          - ~/.yarn-cache

      - run:
          name: Wait for DB
          command: dockerize -wait tcp://localhost:5432 -timeout 1m

      - run:
          name: Database setup
          command: bin/rails db:schema:load --trace

      - run:
          name: Run tests
          command: bin/rails test

But my build fails with the error:
PG::ConnectionBad: FATAL: role "myapp-test" does not exist.

I have tried different versions of postgres, using both PGHOST vs PG_HOST, without getting any build to work.

What do I need to change?


#2

You defined PGUSER: myapp-test and are therefor seeing an error because the role myapp-test does not exist. You can change that to myapp and it will work.


#3

Thank you for swift response! :slight_smile:

I updated to use PGUSER: myapp, but the build failed again with error:
PG::ConnectionBad: FATAL: role "myapp" does not exist.


#4

Oh, you spelled environment with an extra t - environtment


#5

Of course, thank you very much - I have been staring to blindly at that config…!


Any clue why the bundle cache always fails:
No cache is found for key: myapp-bundle-...

but the Yarn cache works great:

Found a cache from build 5 at myapp-yarn-...
Size: 32 B
Cached paths:
  * /home/circleci/.yarn-cache

Downloading cache archive...
Unarchiving cache...

?


#6

I’d use package.json and Gemfile instead of their *.lock files.

Also that yarn cache is only caching 32 B :frowning: You might need to change your key name and/or include the node_modules directory.


#7

@rohara

I’m getting the same thing with the yarn cache only being 32B. I tried caching the node_modules directories (I’m using ReactOnRails so I have two), but then when I restored, I get crazy permission denied errors:

``` Found a cache from build 9702 at v5-yarn-cache-pL0WW3m8MYztDvEE5_eyo0XQNh+GlemS+xjG4L3civw=-TssxfepXC7_lnSAZv6bKHCXkQzAK6hXblP9fEWYGZNg= Size: 56 MB Cached paths: * /root/.yarn-cache * /root/friendsandguests/node_modules * /root/friendsandguests/client/node_modules

Downloading cache archive…
Validating cache…

Unarchiving cache…
tar: root/friendsandguests/node_modules: Cannot mkdir: Permission denied
tar: root/friendsandguests/node_modules/.bin: Cannot mkdir: Permission denied
tar: root/friendsandguests/node_modules/.bin/css-beautify: Cannot open: Permission denied
tar: root/friendsandguests/node_modules/.bin/editorconfig: Cannot open: Permission denied
tar: root/friendsandguests/node_modules/.bin/har-validator: Cannot open: Permission denied
tar: root/friendsandguests/node_modules/.bin/he: Cannot open: Permission denied
tar: root/friendsandguests/node_modules/.bin/html-beautify: Cannot open: Permission denied
tar: root/friendsandguests/node_modules/.bin/html-minifier: Cannot open: Permission denied
tar: root/friendsandguests/node_modules/.bin/image-size: Cannot open: Permission denied
tar: root/friendsandguests/node_modules/.bin/js-beautify: Cannot open: Permission denied
tar: root/friendsandguests/node_modules/.bin/juice: Cannot open: Permission denied
tar: root/friendsandguests/node_modules/.bin/loose-envify: Cannot open: Permission denied
tar: root/friendsandguests/node_modules/.bin/mimer: Cannot open: Permission denied
tar: root/friendsandguests/node_modules/.bin/mjml: Cannot open: Permission denied

</details>

(Note: this happens for every directory and file in the node_modules folders, not just the `.bin` files, it's just that those came alphabetically and I figured you got the point).

#8

Perhaps it’s because I’m using a different container for building the node modules, and the root user is different than the circle container on which I’m running the actual tests?


#9

That’s correct. You changed the cache path without realizing. Change your cache key to revert to an empty, fresh cache.


#10

How does your config look to take the users into account? :slight_smile:


#11

@david.wesmn It was too much of yak-shave, so I found a way to just use the CircleCI-provided container. If you want to use this locally for dev, you can make a dockerfile that uses FROM and then change to USER root, chown circlei everything in your volumes, and then USER circleci back


#12

By following the Circle caching guide I managed to get my system to work.

My configuration is found in this gist.

And I made a PR to the rails CI example repo.

Thank you for your help @rohara.


#13

Thanks for your PR @david.wesmn, I will try to get to this soon!


#14

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.