feat(git-clone): custom destination folder name (#287)
This commit is contained in:
@@ -153,3 +153,20 @@ module "git-clone" {
|
|||||||
branch_name = "feat/example"
|
branch_name = "feat/example"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Git clone with different destination folder
|
||||||
|
|
||||||
|
By default, the repository will be cloned into a folder matching the repository name. You can use the `folder_name` attribute to change the name of the destination folder to something else.
|
||||||
|
|
||||||
|
For example, this will clone into the `~/projects/coder/coder-dev` folder:
|
||||||
|
|
||||||
|
```tf
|
||||||
|
module "git-clone" {
|
||||||
|
source = "registry.coder.com/modules/git-clone/coder"
|
||||||
|
version = "1.0.12"
|
||||||
|
agent_id = coder_agent.example.id
|
||||||
|
url = "https://github.com/coder/coder"
|
||||||
|
folder_name = "coder-dev"
|
||||||
|
base_dir = "~/projects/coder"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -79,6 +79,22 @@ describe("git-clone", async () => {
|
|||||||
expect(state.outputs.branch_name.value).toEqual("");
|
expect(state.outputs.branch_name.value).toEqual("");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("repo_dir should match base_dir/folder_name", async () => {
|
||||||
|
const url = "git@github.com:coder/coder.git";
|
||||||
|
const state = await runTerraformApply(import.meta.dir, {
|
||||||
|
agent_id: "foo",
|
||||||
|
base_dir: "/tmp",
|
||||||
|
folder_name: "foo",
|
||||||
|
url,
|
||||||
|
});
|
||||||
|
expect(state.outputs.repo_dir.value).toEqual("/tmp/foo");
|
||||||
|
expect(state.outputs.folder_name.value).toEqual("foo");
|
||||||
|
expect(state.outputs.clone_url.value).toEqual(url);
|
||||||
|
const https_url = "https://github.com/coder/coder.git";
|
||||||
|
expect(state.outputs.web_url.value).toEqual(https_url);
|
||||||
|
expect(state.outputs.branch_name.value).toEqual("");
|
||||||
|
});
|
||||||
|
|
||||||
it("branch_name should not include query string", async () => {
|
it("branch_name should not include query string", async () => {
|
||||||
const state = await runTerraformApply(import.meta.dir, {
|
const state = await runTerraformApply(import.meta.dir, {
|
||||||
agent_id: "foo",
|
agent_id: "foo",
|
||||||
|
|||||||
@@ -50,6 +50,12 @@ variable "branch_name" {
|
|||||||
default = ""
|
default = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
variable "folder_name" {
|
||||||
|
description = "The destination folder to clone the repository into."
|
||||||
|
type = string
|
||||||
|
default = ""
|
||||||
|
}
|
||||||
|
|
||||||
locals {
|
locals {
|
||||||
# Remove query parameters and fragments from the URL
|
# Remove query parameters and fragments from the URL
|
||||||
url = replace(replace(var.url, "/\\?.*/", ""), "/#.*/", "")
|
url = replace(replace(var.url, "/\\?.*/", ""), "/#.*/", "")
|
||||||
@@ -64,7 +70,7 @@ locals {
|
|||||||
# Extract the branch name from the URL
|
# Extract the branch name from the URL
|
||||||
branch_name = var.branch_name == "" && local.tree_path != "" ? replace(replace(local.url, local.clone_url, ""), "/.*${local.tree_path}/", "") : var.branch_name
|
branch_name = var.branch_name == "" && local.tree_path != "" ? replace(replace(local.url, local.clone_url, ""), "/.*${local.tree_path}/", "") : var.branch_name
|
||||||
# Extract the folder name from the URL
|
# Extract the folder name from the URL
|
||||||
folder_name = replace(basename(local.clone_url), ".git", "")
|
folder_name = var.folder_name == "" ? replace(basename(local.clone_url), ".git", "") : var.folder_name
|
||||||
# Construct the path to clone the repository
|
# Construct the path to clone the repository
|
||||||
clone_path = var.base_dir != "" ? join("/", [var.base_dir, local.folder_name]) : join("/", ["~", local.folder_name])
|
clone_path = var.base_dir != "" ? join("/", [var.base_dir, local.folder_name]) : join("/", ["~", local.folder_name])
|
||||||
# Construct the web URL
|
# Construct the web URL
|
||||||
|
|||||||
Reference in New Issue
Block a user