Skip to content

Commit ee219a8

Browse files
authored
fix(git-clone): propagate pre/post-clone script failures (#891)
## Description Fix git-clone module to fail fast when `pre_clone_script` or `post_clone_script` returns a non-zero exit code. Previously, both scripts were executed but their exit codes were never checked — a failing pre-clone hook (e.g., a prerequisite check that calls `exit 1`) was silently ignored and cloning continued. This broke the advertised "validate prerequisites before cloning" behavior and could leave workspaces starting with unmet preconditions. ## Type of Change - [ ] New module - [ ] New template - [x] Bug fix - [ ] Feature/enhancement - [ ] Documentation - [ ] Other ## Module Information **Path:** `registry/coder/modules/git-clone` **New version:** `v1.3.1` **Breaking change:** [ ] Yes [x] No ## Testing & Validation - [x] Tests pass (`bun test`) - [x] Code formatted (`bun fmt`) - [x] Changes tested locally ## Related Issues - #887 (comment) - #60 - #86
1 parent 4ca251f commit ee219a8

3 files changed

Lines changed: 47 additions & 13 deletions

File tree

registry/coder/modules/git-clone/README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ This module allows you to automatically clone a repository by URL and skip if it
1414
module "git-clone" {
1515
count = data.coder_workspace.me.start_count
1616
source = "registry.coder.com/coder/git-clone/coder"
17-
version = "1.3.0"
17+
version = "1.3.1"
1818
agent_id = coder_agent.example.id
1919
url = "https://github.com/coder/coder"
2020
}
@@ -28,7 +28,7 @@ module "git-clone" {
2828
module "git-clone" {
2929
count = data.coder_workspace.me.start_count
3030
source = "registry.coder.com/coder/git-clone/coder"
31-
version = "1.3.0"
31+
version = "1.3.1"
3232
agent_id = coder_agent.example.id
3333
url = "https://github.com/coder/coder"
3434
base_dir = "~/projects/coder"
@@ -43,7 +43,7 @@ To use with [Git Authentication](https://coder.com/docs/v2/latest/admin/git-prov
4343
module "git-clone" {
4444
count = data.coder_workspace.me.start_count
4545
source = "registry.coder.com/coder/git-clone/coder"
46-
version = "1.3.0"
46+
version = "1.3.1"
4747
agent_id = coder_agent.example.id
4848
url = "https://github.com/coder/coder"
4949
}
@@ -70,7 +70,7 @@ data "coder_parameter" "git_repo" {
7070
module "git_clone" {
7171
count = data.coder_workspace.me.start_count
7272
source = "registry.coder.com/coder/git-clone/coder"
73-
version = "1.3.0"
73+
version = "1.3.1"
7474
agent_id = coder_agent.example.id
7575
url = data.coder_parameter.git_repo.value
7676
}
@@ -105,7 +105,7 @@ Configuring `git-clone` for a self-hosted GitHub Enterprise Server running at `g
105105
module "git-clone" {
106106
count = data.coder_workspace.me.start_count
107107
source = "registry.coder.com/coder/git-clone/coder"
108-
version = "1.3.0"
108+
version = "1.3.1"
109109
agent_id = coder_agent.example.id
110110
url = "https://github.example.com/coder/coder/tree/feat/example"
111111
git_providers = {
@@ -125,7 +125,7 @@ To GitLab clone with a specific branch like `feat/example`
125125
module "git-clone" {
126126
count = data.coder_workspace.me.start_count
127127
source = "registry.coder.com/coder/git-clone/coder"
128-
version = "1.3.0"
128+
version = "1.3.1"
129129
agent_id = coder_agent.example.id
130130
url = "https://gitlab.com/coder/coder/-/tree/feat/example"
131131
}
@@ -137,7 +137,7 @@ Configuring `git-clone` for a self-hosted GitLab running at `gitlab.example.com`
137137
module "git-clone" {
138138
count = data.coder_workspace.me.start_count
139139
source = "registry.coder.com/coder/git-clone/coder"
140-
version = "1.3.0"
140+
version = "1.3.1"
141141
agent_id = coder_agent.example.id
142142
url = "https://gitlab.example.com/coder/coder/-/tree/feat/example"
143143
git_providers = {
@@ -159,7 +159,7 @@ For example, to clone the `feat/example` branch:
159159
module "git-clone" {
160160
count = data.coder_workspace.me.start_count
161161
source = "registry.coder.com/coder/git-clone/coder"
162-
version = "1.3.0"
162+
version = "1.3.1"
163163
agent_id = coder_agent.example.id
164164
url = "https://github.com/coder/coder"
165165
branch_name = "feat/example"
@@ -177,7 +177,7 @@ For example, this will clone into the `~/projects/coder/coder-dev` folder:
177177
module "git-clone" {
178178
count = data.coder_workspace.me.start_count
179179
source = "registry.coder.com/coder/git-clone/coder"
180-
version = "1.3.0"
180+
version = "1.3.1"
181181
agent_id = coder_agent.example.id
182182
url = "https://github.com/coder/coder"
183183
folder_name = "coder-dev"
@@ -196,7 +196,7 @@ If not defined, the default, `0`, performs a full clone.
196196
module "git-clone" {
197197
count = data.coder_workspace.me.start_count
198198
source = "registry.coder.com/coder/git-clone/coder"
199-
version = "1.3.0"
199+
version = "1.3.1"
200200
agent_id = coder_agent.example.id
201201
url = "https://github.com/coder/coder"
202202
depth = 1
@@ -212,7 +212,7 @@ This is useful for preparing the environment or validating prerequisites before
212212
module "git-clone" {
213213
count = data.coder_workspace.me.start_count
214214
source = "registry.coder.com/coder/git-clone/coder"
215-
version = "1.3.0"
215+
version = "1.3.1"
216216
agent_id = coder_agent.example.id
217217
url = "https://github.com/coder/coder"
218218
pre_clone_script = <<-EOT
@@ -235,7 +235,7 @@ This is useful for running initialization tasks like installing dependencies or
235235
module "git-clone" {
236236
count = data.coder_workspace.me.start_count
237237
source = "registry.coder.com/coder/git-clone/coder"
238-
version = "1.3.0"
238+
version = "1.3.1"
239239
agent_id = coder_agent.example.id
240240
url = "https://github.com/coder/coder"
241241
post_clone_script = <<-EOT

registry/coder/modules/git-clone/main.test.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,13 +250,14 @@ describe("git-clone", async () => {
250250
const state = await runTerraformApply(import.meta.dir, {
251251
agent_id: "foo",
252252
url: "fake-url",
253+
base_dir: "/tmp",
253254
post_clone_script: "echo 'Post-clone script executed'",
254255
});
255256
const output = await executeScriptInContainer(
256257
state,
257258
"alpine/git",
258259
"sh",
259-
"mkdir -p ~/fake-url && echo 'existing' > ~/fake-url/file.txt",
260+
"mkdir -p /tmp/fake-url && echo 'existing' > /tmp/fake-url/file.txt",
260261
);
261262
expect(output.stdout).toContain("Running post-clone script...");
262263
expect(output.stdout).toContain("Post-clone script executed");
@@ -273,4 +274,35 @@ describe("git-clone", async () => {
273274
expect(output.stdout).toContain("Pre-clone script executed");
274275
expect(output.stdout).toContain("Cloning fake-url to ~/fake-url...");
275276
});
277+
278+
it("fails when pre-clone script fails", async () => {
279+
const state = await runTerraformApply(import.meta.dir, {
280+
agent_id: "foo",
281+
url: "fake-url",
282+
pre_clone_script: "echo 'Pre-clone script failed'; exit 42",
283+
});
284+
const output = await executeScriptInContainer(state, "alpine/git");
285+
expect(output.exitCode).toBe(42);
286+
expect(output.stdout).toContain("Running pre-clone script...");
287+
expect(output.stdout).toContain("Pre-clone script failed");
288+
expect(output.stdout).not.toContain("Cloning fake-url to ~/fake-url...");
289+
});
290+
291+
it("fails when post-clone script fails", async () => {
292+
const state = await runTerraformApply(import.meta.dir, {
293+
agent_id: "foo",
294+
url: "fake-url",
295+
base_dir: "/tmp",
296+
post_clone_script: "echo 'Post-clone script failed'; exit 43",
297+
});
298+
const output = await executeScriptInContainer(
299+
state,
300+
"alpine/git",
301+
"sh",
302+
"mkdir -p /tmp/fake-url && echo 'existing' > /tmp/fake-url/file.txt",
303+
);
304+
expect(output.exitCode).toBe(43);
305+
expect(output.stdout).toContain("Running post-clone script...");
306+
expect(output.stdout).toContain("Post-clone script failed");
307+
});
276308
});

registry/coder/modules/git-clone/run.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/usr/bin/env bash
22

3+
set -euo pipefail
4+
35
REPO_URL="${REPO_URL}"
46
CLONE_PATH="${CLONE_PATH}"
57
BRANCH_NAME="${BRANCH_NAME}"

0 commit comments

Comments
 (0)