Skip to content

Commit

Permalink
Add support for using kamal inside of the generated devcontainer
Browse files Browse the repository at this point in the history
Kamal requires docker. The docker-in-docker devcontainer feature allow
running docker inside the container.
  • Loading branch information
JoeDupuis committed Oct 16, 2024
1 parent aeb0828 commit bd52ba1
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 0 deletions.
6 changes: 6 additions & 0 deletions railties/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
* Add Kamal support for devcontainers

Previously generated devcontainer could not use docker and therefore Kamal.

*Joé Dupuis*

## Rails 8.0.0.beta1 (September 26, 2024) ##

* Exit `rails g` with code 1 if generator could not be found.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def devcontainer_options
redis: !!((defined?(ActionCable) && !defined?(SolidCable)) || (defined?(ActiveJob) && !defined?(SolidQueue))),
system_test: File.exist?("test/application_system_test_case.rb"),
node: File.exist?(".node-version"),
kamal: File.exist?("config/deploy.yml"),
}
end

Expand Down
1 change: 1 addition & 0 deletions railties/lib/rails/generators/rails/app/app_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ def devcontainer
devcontainer_options = {
database: options[:database],
redis: options[:skip_solid] && !(options[:skip_action_cable] && options[:skip_active_job]),
kamal: !options[:skip_kamal],
system_test: depends_on_system_test?,
active_storage: !options[:skip_active_storage],
dev: options[:dev],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class DevcontainerGenerator < Base # :nodoc:
class_option :dev, type: :boolean, default: false,
desc: "For applications pointing to a local Rails checkout"

class_option :kamal, type: :boolean, default: true,
desc: "Include configuration for Kamal"


source_paths << File.expand_path(File.join(base_name, "app", "templates"), base_root)

def create_devcontainer
Expand Down Expand Up @@ -80,6 +84,7 @@ def container_env
@container_env["CAPYBARA_SERVER_PORT"] = "45678" if options[:system_test]
@container_env["SELENIUM_HOST"] = "selenium" if options[:system_test]
@container_env["REDIS_URL"] = "redis://redis:6379/1" if options[:redis]
@container_env["KAMAL_REGISTRY_PASSWORD"] = "$KAMAL_REGISTRY_PASSWORD" if options[:kamal]
@container_env["DB_HOST"] = database.name if database.service

@container_env
Expand All @@ -105,6 +110,7 @@ def features

@features["ghcr.io/rails/devcontainer/features/activestorage"] = {} if options[:active_storage]
@features["ghcr.io/devcontainers/features/node:1"] = {} if options[:node]
@features["ghcr.io/devcontainers/features/docker-in-docker:2"] = {} if options[:kamal]

@features.merge!(database.feature) if database.feature

Expand Down
1 change: 1 addition & 0 deletions railties/test/commands/devcontainer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Rails::Command::DevcontainerTest < ActiveSupport::TestCase
assert_match "redis: false", output
assert_match "system_test: true", output
assert_match "node: false", output
assert_match "kamal: false", output
end

test "generates dev container for without solid gems" do
Expand Down
11 changes: 11 additions & 0 deletions railties/test/generators/app_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1255,10 +1255,12 @@ def test_devcontainer
assert_devcontainer_json_file do |content|
assert_equal "my_app", content["name"]
assert_equal "45678", content["containerEnv"]["CAPYBARA_SERVER_PORT"]
assert_equal "$KAMAL_REGISTRY_PASSWORD", content["containerEnv"]["KAMAL_REGISTRY_PASSWORD"]
assert_equal "selenium", content["containerEnv"]["SELENIUM_HOST"]
assert_includes content["features"].keys, "ghcr.io/rails/devcontainer/features/activestorage"
assert_includes content["features"].keys, "ghcr.io/devcontainers/features/github-cli:1"
assert_includes content["features"].keys, "ghcr.io/rails/devcontainer/features/sqlite3"
assert_includes content["features"].keys, "ghcr.io/devcontainers/features/docker-in-docker:2"
assert_includes(content["forwardPorts"], 3000)
end
assert_file(".devcontainer/Dockerfile") do |content|
Expand Down Expand Up @@ -1294,6 +1296,15 @@ def test_devcontainer
end
end

def test_devcontainer_skip_kamal
run_generator [destination_root, "--devcontainer", "--name=my-app", "--skip-kamal"]

assert_devcontainer_json_file do |devcontainer_json|
assert_not_includes devcontainer_json["features"].keys, "ghcr.io/devcontainers/features/docker-in-docker:2"
assert_not_includes devcontainer_json["containerEnv"].keys, "KAMAL_REGISTRY_PASSWORD"
end
end

def test_devcontainer_include_redis_skipping_solid
run_generator [destination_root, "--devcontainer", "--name=my-app", "--skip-solid"]

Expand Down
18 changes: 18 additions & 0 deletions railties/test/generators/devcontainer_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,24 @@ def test_redis_option_skip
end
end

def test_kamal_option_default
run_generator

assert_devcontainer_json_file do |devcontainer_json|
assert_includes devcontainer_json["features"].keys, "ghcr.io/devcontainers/features/docker-in-docker:2"
assert_equal "$KAMAL_REGISTRY_PASSWORD", devcontainer_json["containerEnv"]["KAMAL_REGISTRY_PASSWORD"]
end
end

def test_kamal_option_skip
run_generator ["--skip-kamal"]

assert_devcontainer_json_file do |devcontainer_json|
assert_not_includes devcontainer_json["features"].keys, "ghcr.io/devcontainers/features/docker-in-docker:2"
assert_not_includes devcontainer_json["containerEnv"].keys, "KAMAL_REGISTRY_PASSWORD"
end
end

def test_system_test_option_default
copy_application_system_test_case

Expand Down

0 comments on commit bd52ba1

Please sign in to comment.