From 26c41a4df44e81492037b0ea44bb30162ee8b991 Mon Sep 17 00:00:00 2001 From: ikkz Date: Sat, 16 May 2026 12:50:45 +0800 Subject: [PATCH] fix(git-clone): propagate clone script failures --- registry/coder/modules/git-clone/README.md | 24 ++++++------- registry/coder/modules/git-clone/main.test.ts | 34 ++++++++++++++++++- registry/coder/modules/git-clone/run.sh | 2 ++ 3 files changed, 47 insertions(+), 13 deletions(-) diff --git a/registry/coder/modules/git-clone/README.md b/registry/coder/modules/git-clone/README.md index 9c61941c3..3336770f5 100644 --- a/registry/coder/modules/git-clone/README.md +++ b/registry/coder/modules/git-clone/README.md @@ -14,7 +14,7 @@ This module allows you to automatically clone a repository by URL and skip if it module "git-clone" { count = data.coder_workspace.me.start_count source = "registry.coder.com/coder/git-clone/coder" - version = "1.3.0" + version = "1.3.1" agent_id = coder_agent.example.id url = "https://github.com/coder/coder" } @@ -28,7 +28,7 @@ module "git-clone" { module "git-clone" { count = data.coder_workspace.me.start_count source = "registry.coder.com/coder/git-clone/coder" - version = "1.3.0" + version = "1.3.1" agent_id = coder_agent.example.id url = "https://github.com/coder/coder" base_dir = "~/projects/coder" @@ -43,7 +43,7 @@ To use with [Git Authentication](https://coder.com/docs/v2/latest/admin/git-prov module "git-clone" { count = data.coder_workspace.me.start_count source = "registry.coder.com/coder/git-clone/coder" - version = "1.3.0" + version = "1.3.1" agent_id = coder_agent.example.id url = "https://github.com/coder/coder" } @@ -70,7 +70,7 @@ data "coder_parameter" "git_repo" { module "git_clone" { count = data.coder_workspace.me.start_count source = "registry.coder.com/coder/git-clone/coder" - version = "1.3.0" + version = "1.3.1" agent_id = coder_agent.example.id url = data.coder_parameter.git_repo.value } @@ -105,7 +105,7 @@ Configuring `git-clone` for a self-hosted GitHub Enterprise Server running at `g module "git-clone" { count = data.coder_workspace.me.start_count source = "registry.coder.com/coder/git-clone/coder" - version = "1.3.0" + version = "1.3.1" agent_id = coder_agent.example.id url = "https://github.example.com/coder/coder/tree/feat/example" git_providers = { @@ -125,7 +125,7 @@ To GitLab clone with a specific branch like `feat/example` module "git-clone" { count = data.coder_workspace.me.start_count source = "registry.coder.com/coder/git-clone/coder" - version = "1.3.0" + version = "1.3.1" agent_id = coder_agent.example.id url = "https://gitlab.com/coder/coder/-/tree/feat/example" } @@ -137,7 +137,7 @@ Configuring `git-clone` for a self-hosted GitLab running at `gitlab.example.com` module "git-clone" { count = data.coder_workspace.me.start_count source = "registry.coder.com/coder/git-clone/coder" - version = "1.3.0" + version = "1.3.1" agent_id = coder_agent.example.id url = "https://gitlab.example.com/coder/coder/-/tree/feat/example" git_providers = { @@ -159,7 +159,7 @@ For example, to clone the `feat/example` branch: module "git-clone" { count = data.coder_workspace.me.start_count source = "registry.coder.com/coder/git-clone/coder" - version = "1.3.0" + version = "1.3.1" agent_id = coder_agent.example.id url = "https://github.com/coder/coder" branch_name = "feat/example" @@ -177,7 +177,7 @@ For example, this will clone into the `~/projects/coder/coder-dev` folder: module "git-clone" { count = data.coder_workspace.me.start_count source = "registry.coder.com/coder/git-clone/coder" - version = "1.3.0" + version = "1.3.1" agent_id = coder_agent.example.id url = "https://github.com/coder/coder" folder_name = "coder-dev" @@ -196,7 +196,7 @@ If not defined, the default, `0`, performs a full clone. module "git-clone" { count = data.coder_workspace.me.start_count source = "registry.coder.com/coder/git-clone/coder" - version = "1.3.0" + version = "1.3.1" agent_id = coder_agent.example.id url = "https://github.com/coder/coder" depth = 1 @@ -212,7 +212,7 @@ This is useful for preparing the environment or validating prerequisites before module "git-clone" { count = data.coder_workspace.me.start_count source = "registry.coder.com/coder/git-clone/coder" - version = "1.3.0" + version = "1.3.1" agent_id = coder_agent.example.id url = "https://github.com/coder/coder" pre_clone_script = <<-EOT @@ -235,7 +235,7 @@ This is useful for running initialization tasks like installing dependencies or module "git-clone" { count = data.coder_workspace.me.start_count source = "registry.coder.com/coder/git-clone/coder" - version = "1.3.0" + version = "1.3.1" agent_id = coder_agent.example.id url = "https://github.com/coder/coder" post_clone_script = <<-EOT diff --git a/registry/coder/modules/git-clone/main.test.ts b/registry/coder/modules/git-clone/main.test.ts index 922f40284..af900eeff 100644 --- a/registry/coder/modules/git-clone/main.test.ts +++ b/registry/coder/modules/git-clone/main.test.ts @@ -250,13 +250,14 @@ describe("git-clone", async () => { const state = await runTerraformApply(import.meta.dir, { agent_id: "foo", url: "fake-url", + base_dir: "/tmp", post_clone_script: "echo 'Post-clone script executed'", }); const output = await executeScriptInContainer( state, "alpine/git", "sh", - "mkdir -p ~/fake-url && echo 'existing' > ~/fake-url/file.txt", + "mkdir -p /tmp/fake-url && echo 'existing' > /tmp/fake-url/file.txt", ); expect(output.stdout).toContain("Running post-clone script..."); expect(output.stdout).toContain("Post-clone script executed"); @@ -273,4 +274,35 @@ describe("git-clone", async () => { expect(output.stdout).toContain("Pre-clone script executed"); expect(output.stdout).toContain("Cloning fake-url to ~/fake-url..."); }); + + it("fails when pre-clone script fails", async () => { + const state = await runTerraformApply(import.meta.dir, { + agent_id: "foo", + url: "fake-url", + pre_clone_script: "echo 'Pre-clone script failed'; exit 42", + }); + const output = await executeScriptInContainer(state, "alpine/git"); + expect(output.exitCode).toBe(42); + expect(output.stdout).toContain("Running pre-clone script..."); + expect(output.stdout).toContain("Pre-clone script failed"); + expect(output.stdout).not.toContain("Cloning fake-url to ~/fake-url..."); + }); + + it("fails when post-clone script fails", async () => { + const state = await runTerraformApply(import.meta.dir, { + agent_id: "foo", + url: "fake-url", + base_dir: "/tmp", + post_clone_script: "echo 'Post-clone script failed'; exit 43", + }); + const output = await executeScriptInContainer( + state, + "alpine/git", + "sh", + "mkdir -p /tmp/fake-url && echo 'existing' > /tmp/fake-url/file.txt", + ); + expect(output.exitCode).toBe(43); + expect(output.stdout).toContain("Running post-clone script..."); + expect(output.stdout).toContain("Post-clone script failed"); + }); }); diff --git a/registry/coder/modules/git-clone/run.sh b/registry/coder/modules/git-clone/run.sh index 03050349d..76928a406 100644 --- a/registry/coder/modules/git-clone/run.sh +++ b/registry/coder/modules/git-clone/run.sh @@ -1,5 +1,7 @@ #!/usr/bin/env bash +set -euo pipefail + REPO_URL="${REPO_URL}" CLONE_PATH="${CLONE_PATH}" BRANCH_NAME="${BRANCH_NAME}"