Skip to content

Add bundler cache feature #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions features/bundler-cache/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Bundler cache

Creates a volume for persisting the installed gems across different containers.

## Example Usage

```json
"features": {
"ghcr.io/rails/devcontainer/features/bundler-cache:1": {}
}
```

## Options

## Customizations

## OS Support

`bash` is required to execute the `install.sh` script.
17 changes: 17 additions & 0 deletions features/bundler-cache/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"id": "bundler-cache",
"version": "1.0.0",
"name": "Bundler cache",
"description": "Creates a volume for persisting the installed gems across different containers",
"containerEnv": {
"BUNDLE_PATH": "/bundle/vendor"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that once you're in the container, if you run rails c (or any other executable in your Gemfile) you'll get a "command not found" error.

to solve this, we can add these env vars:

Suggested change
"BUNDLE_PATH": "/bundle/vendor"
"BUNDLE_PATH": "/bundle/vendor",
"BUNDLE_BIN": "/bundle/bin",
"PATH": "/bundle/bin:$PATH"

BUNDLE_BIN tells bundler to install the bins in the specified dir, and then we'll prepend this dir in the PATH.

By default, the BUNDLE_BIN would be something like /bundle/vendor/ruby/COMPATIBILITY_VERSION/bin. And compatibility version is not always the same ruby version you're running (for example i'm testing with 3.3.4 and the compat version is 3.3.0 - This is not trivial to get, so changing the BUNDLE_BIN makes sense..

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should add the bundle bin to the path like that. Instead people should do bundle exec rails c or bin/rails c. I expect global gems to be in the path but not bundled gems.

},
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps also add this?

Suggested change
},
},
"installsAfter": [
"ghcr.io/rails/devcontainer/features/ruby"
],

I'm not sure if it makes sense though

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's necessary. The ruby feature should not install any gems.

"mounts": [
{
"source": "bundler-data",
"target": "/bundle",
"type": "volume"
}
],
"postCreateCommand": "/usr/local/share/bundler-data-permissions.sh"
}
16 changes: 16 additions & 0 deletions features/bundler-cache/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/sh

set -e

USERNAME="${USERNAME:-"${_REMOTE_USER:-"automatic"}"}"

POST_CREATE_COMMAND_SCRIPT_PATH="/usr/local/share/bundler-data-permissions.sh"

tee "$POST_CREATE_COMMAND_SCRIPT_PATH" > /dev/null \
<< EOF
#!/bin/sh
set -e
sudo chown -R ${USERNAME} /bundle
EOF

chmod 755 "$POST_CREATE_COMMAND_SCRIPT_PATH"