Skip to content

Commit da5a2ba

Browse files
iamriajulDevelopmentCatsmatifali
authored
feat(git-clone module): added post_clone_script. (#357)
Co-authored-by: DevCats <christofer@coder.com> Co-authored-by: Atif Ali <atif@coder.com>
1 parent 63cad25 commit da5a2ba

File tree

4 files changed

+71
-12
lines changed

4 files changed

+71
-12
lines changed

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

Lines changed: 33 additions & 10 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.1.2"
17+
version = "1.2.0"
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.1.2"
31+
version = "1.2.0"
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.1.2"
46+
version = "1.2.0"
4747
agent_id = coder_agent.example.id
4848
url = "https://github.com/coder/coder"
4949
}
@@ -69,7 +69,7 @@ data "coder_parameter" "git_repo" {
6969
module "git_clone" {
7070
count = data.coder_workspace.me.start_count
7171
source = "registry.coder.com/coder/git-clone/coder"
72-
version = "1.1.2"
72+
version = "1.2.0"
7373
agent_id = coder_agent.example.id
7474
url = data.coder_parameter.git_repo.value
7575
}
@@ -103,7 +103,7 @@ Configuring `git-clone` for a self-hosted GitHub Enterprise Server running at `g
103103
module "git-clone" {
104104
count = data.coder_workspace.me.start_count
105105
source = "registry.coder.com/coder/git-clone/coder"
106-
version = "1.1.2"
106+
version = "1.2.0"
107107
agent_id = coder_agent.example.id
108108
url = "https://github.example.com/coder/coder/tree/feat/example"
109109
git_providers = {
@@ -122,7 +122,7 @@ To GitLab clone with a specific branch like `feat/example`
122122
module "git-clone" {
123123
count = data.coder_workspace.me.start_count
124124
source = "registry.coder.com/coder/git-clone/coder"
125-
version = "1.1.2"
125+
version = "1.2.0"
126126
agent_id = coder_agent.example.id
127127
url = "https://gitlab.com/coder/coder/-/tree/feat/example"
128128
}
@@ -134,7 +134,7 @@ Configuring `git-clone` for a self-hosted GitLab running at `gitlab.example.com`
134134
module "git-clone" {
135135
count = data.coder_workspace.me.start_count
136136
source = "registry.coder.com/coder/git-clone/coder"
137-
version = "1.1.2"
137+
version = "1.2.0"
138138
agent_id = coder_agent.example.id
139139
url = "https://gitlab.example.com/coder/coder/-/tree/feat/example"
140140
git_providers = {
@@ -155,7 +155,7 @@ For example, to clone the `feat/example` branch:
155155
module "git-clone" {
156156
count = data.coder_workspace.me.start_count
157157
source = "registry.coder.com/coder/git-clone/coder"
158-
version = "1.1.2"
158+
version = "1.2.0"
159159
agent_id = coder_agent.example.id
160160
url = "https://github.com/coder/coder"
161161
branch_name = "feat/example"
@@ -173,7 +173,7 @@ For example, this will clone into the `~/projects/coder/coder-dev` folder:
173173
module "git-clone" {
174174
count = data.coder_workspace.me.start_count
175175
source = "registry.coder.com/coder/git-clone/coder"
176-
version = "1.1.2"
176+
version = "1.2.0"
177177
agent_id = coder_agent.example.id
178178
url = "https://github.com/coder/coder"
179179
folder_name = "coder-dev"
@@ -192,9 +192,32 @@ If not defined, the default, `0`, performs a full clone.
192192
module "git-clone" {
193193
count = data.coder_workspace.me.start_count
194194
source = "registry.coder.com/modules/git-clone/coder"
195-
version = "1.1.0"
195+
version = "1.2.0"
196196
agent_id = coder_agent.example.id
197197
url = "https://github.com/coder/coder"
198198
depth = 1
199199
}
200200
```
201+
202+
## Post-clone script
203+
204+
Run a custom script after cloning the repository by setting the `post_clone_script` variable.
205+
This is useful for running initialization tasks like installing dependencies or setting up the environment.
206+
207+
```tf
208+
module "git-clone" {
209+
count = data.coder_workspace.me.start_count
210+
source = "registry.coder.com/coder/git-clone/coder"
211+
version = "1.2.0"
212+
agent_id = coder_agent.example.id
213+
url = "https://github.com/coder/coder"
214+
post_clone_script = <<-EOT
215+
#!/bin/bash
216+
echo "Repository cloned successfully!"
217+
# Install dependencies
218+
npm install
219+
# Run any other initialization tasks
220+
make setup
221+
EOT
222+
}
223+
```

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ describe("git-clone", async () => {
3030
url: "fake-url",
3131
});
3232
const output = await executeScriptInContainer(state, "alpine/git");
33-
expect(output.exitCode).toBe(128);
3433
expect(output.stdout).toEqual([
3534
"Creating directory ~/fake-url...",
3635
"Cloning fake-url to ~/fake-url...",
3736
]);
37+
expect(output.stderr.join(" ")).toContain("fatal");
38+
expect(output.stderr.join(" ")).toContain("fake-url");
3839
});
3940

4041
it("repo_dir should match repo name for https", async () => {
@@ -244,4 +245,20 @@ describe("git-clone", async () => {
244245
"Cloning https://github.com/michaelbrewer/repo-tests.log to ~/repo-tests.log on branch feat/branch...",
245246
]);
246247
});
248+
249+
it("runs post-clone script", async () => {
250+
const state = await runTerraformApply(import.meta.dir, {
251+
agent_id: "foo",
252+
url: "fake-url",
253+
post_clone_script: "echo 'Post-clone script executed'",
254+
});
255+
const output = await executeScriptInContainer(
256+
state,
257+
"alpine/git",
258+
"sh",
259+
"mkdir -p ~/fake-url && echo 'existing' > ~/fake-url/file.txt",
260+
);
261+
expect(output.stdout).toContain("Running post-clone script...");
262+
expect(output.stdout).toContain("Post-clone script executed");
263+
});
247264
});

registry/coder/modules/git-clone/main.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ variable "depth" {
6262
default = 0
6363
}
6464

65+
variable "post_clone_script" {
66+
description = "Custom script to run after cloning the repository. Runs always after git clone, even if the repository already exists."
67+
type = string
68+
default = null
69+
}
70+
6571
locals {
6672
# Remove query parameters and fragments from the URL
6773
url = replace(replace(var.url, "/\\?.*/", ""), "/#.*/", "")
@@ -81,6 +87,8 @@ locals {
8187
clone_path = var.base_dir != "" ? join("/", [var.base_dir, local.folder_name]) : join("/", ["~", local.folder_name])
8288
# Construct the web URL
8389
web_url = startswith(local.clone_url, "git@") ? replace(replace(local.clone_url, ":", "/"), "git@", "https://") : local.clone_url
90+
# Encode the post_clone_script for passing to the shell script
91+
encoded_post_clone_script = var.post_clone_script != null ? base64encode(var.post_clone_script) : ""
8492
}
8593

8694
output "repo_dir" {
@@ -120,6 +128,7 @@ resource "coder_script" "git_clone" {
120128
REPO_URL : local.clone_url,
121129
BRANCH_NAME : local.branch_name,
122130
DEPTH = var.depth,
131+
POST_CLONE_SCRIPT : local.encoded_post_clone_script,
123132
})
124133
display_name = "Git Clone"
125134
icon = "/icon/git.svg"

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ BRANCH_NAME="${BRANCH_NAME}"
66
# Expand home if it's specified!
77
CLONE_PATH="$${CLONE_PATH/#\~/$${HOME}}"
88
DEPTH="${DEPTH}"
9+
POST_CLONE_SCRIPT="${POST_CLONE_SCRIPT}"
910

1011
# Check if the variable is empty...
1112
if [ -z "$REPO_URL" ]; then
@@ -52,5 +53,14 @@ if [ -z "$(ls -A "$CLONE_PATH")" ]; then
5253
fi
5354
else
5455
echo "$CLONE_PATH already exists and isn't empty, skipping clone!"
55-
exit 0
56+
fi
57+
58+
# Run post-clone script if provided
59+
if [ -n "$POST_CLONE_SCRIPT" ]; then
60+
echo "Running post-clone script..."
61+
echo "$POST_CLONE_SCRIPT" | base64 -d > /tmp/post_clone.sh
62+
chmod +x /tmp/post_clone.sh
63+
cd "$CLONE_PATH"
64+
/tmp/post_clone.sh
65+
rm /tmp/post_clone.sh
5666
fi

0 commit comments

Comments
 (0)