Skip to content

Commit d9b6fae

Browse files
Add a --dry-run flag to livebook deploy CLI (#3109)
Co-authored-by: Jonatan Kłosko <jonatanklosko@gmail.com>
1 parent 8fc6e8c commit d9b6fae

2 files changed

Lines changed: 77 additions & 6 deletions

File tree

lib/livebook_cli/deploy.ex

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ defmodule LivebookCLI.Deploy do
1717
--org-token Token from your Livebook Teams organization
1818
--teams-key Teams key from your Teams workspace
1919
--deployment-group-id The ID of the deployment group you want to deploy to
20+
--dry-run Perform a sanity check but do not actually deploy
2021
2122
The --help option can be given to print this notice.
2223
@@ -35,7 +36,8 @@ defmodule LivebookCLI.Deploy do
3536
@switches [
3637
org_token: :string,
3738
teams_key: :string,
38-
deployment_group_id: :integer
39+
deployment_group_id: :integer,
40+
dry_run: :boolean
3941
]
4042

4143
@impl true
@@ -56,7 +58,8 @@ defmodule LivebookCLI.Deploy do
5658
paths: paths,
5759
session_token: opts[:org_token],
5860
teams_key: opts[:teams_key],
59-
deployment_group_id: opts[:deployment_group_id]
61+
deployment_group_id: opts[:deployment_group_id],
62+
dry_run?: opts[:dry_run] || false
6063
}
6164
end
6265

@@ -152,7 +155,8 @@ defmodule LivebookCLI.Deploy do
152155
|> Livebook.FileSystem.File.resolve("files/")
153156

154157
with {:ok, content} <- File.read(path),
155-
{:ok, app_deployment} <- prepare_app_deployment(path, content, files_dir) do
158+
{:ok, app_deployment} <- prepare_app_deployment(path, content, files_dir),
159+
:continue <- ensure_skip_on_dry_run(app_deployment, config.dry_run?) do
156160
case Livebook.Teams.deploy_app_from_cli(
157161
team,
158162
app_deployment,
@@ -212,6 +216,19 @@ defmodule LivebookCLI.Deploy do
212216
end
213217
end
214218

219+
defp ensure_skip_on_dry_run(app_deployment, dry_run?) do
220+
if dry_run? do
221+
message = """
222+
* #{app_deployment.title} skipped due to --dry-run
223+
"""
224+
225+
log_info(message)
226+
:ok
227+
else
228+
:continue
229+
end
230+
end
231+
215232
defp add_error(errors, key, message) do
216233
Map.update(errors, key, [message], &[message | &1])
217234
end

test/livebook_teams/cli/deploy_test.exs

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ defmodule LivebookCLI.Integration.DeployTest do
380380
deployment_group = TeamsRPC.create_deployment_group(node, org: org, url: @url)
381381
hub_id = team.id
382382

383-
# Notebook without app settings (deploymeny should fail)
383+
# Notebook without app settings (deployment should fail)
384384
invalid_title = "Invalid App"
385385
invalid_slug = "invalid-#{Utils.random_short_id()}"
386386
invalid_app_path = Path.join(tmp_dir, "#{invalid_slug}.livemd")
@@ -481,9 +481,63 @@ defmodule LivebookCLI.Integration.DeployTest do
481481
end)
482482
end)
483483
end
484+
485+
test "successfully runs without deploying when dry run requested", %{
486+
team: team,
487+
node: node,
488+
org: org,
489+
tmp_dir: tmp_dir
490+
} do
491+
title = "Test CLI Deploy App"
492+
slug = Utils.random_short_id()
493+
app_path = Path.join(tmp_dir, "#{slug}.livemd")
494+
{key, _} = TeamsRPC.create_org_token(node, org: org)
495+
deployment_group = TeamsRPC.create_deployment_group(node, org: org, url: @url)
496+
app_folder = TeamsRPC.create_app_folder(node, org: org)
497+
498+
hub_id = team.id
499+
deployment_group_id = to_string(deployment_group.id)
500+
app_folder_id = to_string(app_folder.id)
501+
502+
stamp_notebook(app_path, """
503+
<!-- livebook:{"app_settings":{"access_type":"public","app_folder_id":"#{app_folder_id}","slug":"#{slug}"},"hub_id":"#{hub_id}"} -->
504+
505+
# #{title}
506+
507+
## Test Section
508+
509+
```elixir
510+
IO.puts("Hello from CLI deployed app!")
511+
```
512+
""")
513+
514+
output =
515+
ExUnit.CaptureIO.capture_io(fn ->
516+
assert deploy(
517+
key,
518+
team.teams_key,
519+
deployment_group.id,
520+
app_path,
521+
["--dry-run"]
522+
) == :ok
523+
end)
524+
525+
assert output =~ "* Preparing to deploy notebook #{slug}.livemd"
526+
assert output =~ " * #{title} skipped due to --dry-run"
527+
528+
refute_receive {:app_deployment_started,
529+
%{
530+
title: ^title,
531+
slug: ^slug,
532+
deployment_group_id: ^deployment_group_id,
533+
app_folder_id: ^app_folder_id,
534+
hub_id: ^hub_id,
535+
deployed_by: "CLI"
536+
}}
537+
end
484538
end
485539

486-
defp deploy(org_token, teams_key, deployment_group_id, path) do
540+
defp deploy(org_token, teams_key, deployment_group_id, path, extra_flags \\ []) do
487541
paths =
488542
if is_list(path) do
489543
path
@@ -509,7 +563,7 @@ defmodule LivebookCLI.Integration.DeployTest do
509563
teams_key,
510564
"--deployment-group-id",
511565
deployment_group_id
512-
] ++ paths
566+
] ++ extra_flags ++ paths
513567
)
514568
end
515569
end

0 commit comments

Comments
 (0)