Skip to content

Commit f8e422a

Browse files
authored
Merge pull request rails#53327 from JoeDupuis/kamal-devcontainer
Add support for using Kamal inside of the generated devcontainer
2 parents 1a7f63a + 1647ea8 commit f8e422a

File tree

7 files changed

+45
-1
lines changed

7 files changed

+45
-1
lines changed

railties/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
* Add Kamal support for devcontainers
2+
3+
Previously generated devcontainer could not use docker and therefore Kamal.
4+
5+
*Joé Dupuis*
6+
17
## Rails 8.0.0.beta1 (September 26, 2024) ##
28

39
* Exit `rails g` with code 1 if generator could not be found.

railties/lib/rails/commands/devcontainer/devcontainer_command.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def devcontainer_options
2727
redis: !!((defined?(ActionCable) && !defined?(SolidCable)) || (defined?(ActiveJob) && !defined?(SolidQueue))),
2828
system_test: File.exist?("test/application_system_test_case.rb"),
2929
node: File.exist?(".node-version"),
30+
kamal: File.exist?("config/deploy.yml"),
3031
}
3132
end
3233

railties/lib/rails/generators/rails/app/app_generator.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ def devcontainer
266266
devcontainer_options = {
267267
database: options[:database],
268268
redis: options[:skip_solid] && !(options[:skip_action_cable] && options[:skip_active_job]),
269+
kamal: !options[:skip_kamal],
269270
system_test: depends_on_system_test?,
270271
active_storage: !options[:skip_active_storage],
271272
dev: options[:dev],

railties/lib/rails/generators/rails/devcontainer/devcontainer_generator.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ class DevcontainerGenerator < Base # :nodoc:
2626
class_option :dev, type: :boolean, default: false,
2727
desc: "For applications pointing to a local Rails checkout"
2828

29+
class_option :kamal, type: :boolean, default: true,
30+
desc: "Include configuration for Kamal"
31+
2932
source_paths << File.expand_path(File.join(base_name, "app", "templates"), base_root)
3033

3134
def create_devcontainer
@@ -80,6 +83,7 @@ def container_env
8083
@container_env["CAPYBARA_SERVER_PORT"] = "45678" if options[:system_test]
8184
@container_env["SELENIUM_HOST"] = "selenium" if options[:system_test]
8285
@container_env["REDIS_URL"] = "redis://redis:6379/1" if options[:redis]
86+
@container_env["KAMAL_REGISTRY_PASSWORD"] = "$KAMAL_REGISTRY_PASSWORD" if options[:kamal]
8387
@container_env["DB_HOST"] = database.name if database.service
8488

8589
@container_env
@@ -105,6 +109,7 @@ def features
105109

106110
@features["ghcr.io/rails/devcontainer/features/activestorage"] = {} if options[:active_storage]
107111
@features["ghcr.io/devcontainers/features/node:1"] = {} if options[:node]
112+
@features["ghcr.io/devcontainers/features/docker-outside-of-docker:1"] = {} if options[:kamal]
108113

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

railties/test/commands/devcontainer_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Rails::Command::DevcontainerTest < ActiveSupport::TestCase
2222
assert_match "redis: false", output
2323
assert_match "system_test: true", output
2424
assert_match "node: false", output
25+
assert_match "kamal: false", output
2526
end
2627

2728
test "generates dev container for without solid gems" do

railties/test/generators/app_generator_test.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1255,10 +1255,12 @@ def test_devcontainer
12551255
assert_devcontainer_json_file do |content|
12561256
assert_equal "my_app", content["name"]
12571257
assert_equal "45678", content["containerEnv"]["CAPYBARA_SERVER_PORT"]
1258+
assert_equal "$KAMAL_REGISTRY_PASSWORD", content["containerEnv"]["KAMAL_REGISTRY_PASSWORD"]
12581259
assert_equal "selenium", content["containerEnv"]["SELENIUM_HOST"]
12591260
assert_includes content["features"].keys, "ghcr.io/rails/devcontainer/features/activestorage"
12601261
assert_includes content["features"].keys, "ghcr.io/devcontainers/features/github-cli:1"
12611262
assert_includes content["features"].keys, "ghcr.io/rails/devcontainer/features/sqlite3"
1263+
assert_includes content["features"].keys, "ghcr.io/devcontainers/features/docker-outside-of-docker:1"
12621264
assert_includes(content["forwardPorts"], 3000)
12631265
end
12641266
assert_file(".devcontainer/Dockerfile") do |content|
@@ -1294,6 +1296,15 @@ def test_devcontainer
12941296
end
12951297
end
12961298

1299+
def test_devcontainer_skip_kamal
1300+
run_generator [destination_root, "--devcontainer", "--name=my-app", "--skip-kamal"]
1301+
1302+
assert_devcontainer_json_file do |devcontainer_json|
1303+
assert_not_includes devcontainer_json["features"].keys, "ghcr.io/devcontainers/features/docker-outside-of-docker:1"
1304+
assert_not_includes devcontainer_json["containerEnv"].keys, "KAMAL_REGISTRY_PASSWORD"
1305+
end
1306+
end
1307+
12971308
def test_devcontainer_include_redis_skipping_solid
12981309
run_generator [destination_root, "--devcontainer", "--name=my-app", "--skip-solid"]
12991310

@@ -1481,7 +1492,8 @@ def test_devcontainer_no_selenium_when_skipping_system_test
14811492
assert_not_includes compose_config["services"].keys, "selenium"
14821493
end
14831494
assert_devcontainer_json_file do |content|
1484-
assert_nil content["containerEnv"]
1495+
assert_not_includes content["containerEnv"].keys, "SELENIUM_HOST"
1496+
assert_not_includes content["containerEnv"].keys, "CAPYBARA_SERVER_PORT"
14851497
end
14861498
end
14871499

railties/test/generators/devcontainer_generator_test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,24 @@ def test_redis_option_skip
283283
end
284284
end
285285

286+
def test_kamal_option_default
287+
run_generator
288+
289+
assert_devcontainer_json_file do |devcontainer_json|
290+
assert_includes devcontainer_json["features"].keys, "ghcr.io/devcontainers/features/docker-outside-of-docker:1"
291+
assert_equal "$KAMAL_REGISTRY_PASSWORD", devcontainer_json["containerEnv"]["KAMAL_REGISTRY_PASSWORD"]
292+
end
293+
end
294+
295+
def test_kamal_option_skip
296+
run_generator ["--skip-kamal"]
297+
298+
assert_devcontainer_json_file do |devcontainer_json|
299+
assert_not_includes devcontainer_json["features"].keys, "ghcr.io/devcontainers/features/docker-outside-of-docker:1"
300+
assert_not_includes devcontainer_json["containerEnv"].keys, "KAMAL_REGISTRY_PASSWORD"
301+
end
302+
end
303+
286304
def test_system_test_option_default
287305
copy_application_system_test_case
288306

0 commit comments

Comments
 (0)