TemplatesModules
Back to Modules
Git Clone Icon

Git Clone

By:
Clone a Git repository by URL and skip if it exists.
README
Variables (6)
Scripts (1)
Source

This module allows you to automatically clone a repository by URL and skip if it exists in the base directory provided.

1module "git-clone" {
2  source   = "registry.coder.com/modules/git-clone/coder"
3  version  = "1.0.18"
4  agent_id = coder_agent.example.id
5  url      = "https://github.com/coder/coder"
6}

Examples

Custom Path

1module "git-clone" {
2  source   = "registry.coder.com/modules/git-clone/coder"
3  version  = "1.0.18"
4  agent_id = coder_agent.example.id
5  url      = "https://github.com/coder/coder"
6  base_dir = "~/projects/coder"
7}

Git Authentication

To use with Git Authentication, add the provider by ID to your template:

1module "git-clone" {
2  source   = "registry.coder.com/modules/git-clone/coder"
3  version  = "1.0.18"
4  agent_id = coder_agent.example.id
5  url      = "https://github.com/coder/coder"
6}
7
8data "coder_git_auth" "github" {
9  id = "github"
10}

GitHub clone with branch name

To GitHub clone with a specific branch like feat/example

1# Prompt the user for the git repo URL
2data "coder_parameter" "git_repo" {
3  name         = "git_repo"
4  display_name = "Git repository"
5  default      = "https://github.com/coder/coder/tree/feat/example"
6}
7
8# Clone the repository for branch `feat/example`
9module "git_clone" {
10  source   = "registry.coder.com/modules/git-clone/coder"
11  version  = "1.0.18"
12  agent_id = coder_agent.example.id
13  url      = data.coder_parameter.git_repo.value
14}
15
16# Create a code-server instance for the cloned repository
17module "code-server" {
18  source   = "registry.coder.com/modules/code-server/coder"
19  version  = "1.0.18"
20  agent_id = coder_agent.example.id
21  order    = 1
22  folder   = "/home/${local.username}/${module.git_clone.folder_name}"
23}
24
25# Create a Coder app for the website
26resource "coder_app" "website" {
27  agent_id     = coder_agent.example.id
28  order        = 2
29  slug         = "website"
30  external     = true
31  display_name = module.git_clone.folder_name
32  url          = module.git_clone.web_url
33  icon         = module.git_clone.git_provider != "" ? "/icon/${module.git_clone.git_provider}.svg" : "/icon/git.svg"
34  count        = module.git_clone.web_url != "" ? 1 : 0
35}

Configuring git-clone for a self-hosted GitHub Enterprise Server running at github.example.com

1module "git-clone" {
2  source   = "registry.coder.com/modules/git-clone/coder"
3  version  = "1.0.18"
4  agent_id = coder_agent.example.id
5  url      = "https://github.example.com/coder/coder/tree/feat/example"
6  git_providers = {
7    "https://github.example.com/" = {
8      provider = "github"
9    }
10  }
11}

GitLab clone with branch name

To GitLab clone with a specific branch like feat/example

1module "git-clone" {
2  source   = "registry.coder.com/modules/git-clone/coder"
3  version  = "1.0.18"
4  agent_id = coder_agent.example.id
5  url      = "https://gitlab.com/coder/coder/-/tree/feat/example"
6}

Configuring git-clone for a self-hosted GitLab running at gitlab.example.com

1module "git-clone" {
2  source   = "registry.coder.com/modules/git-clone/coder"
3  version  = "1.0.18"
4  agent_id = coder_agent.example.id
5  url      = "https://gitlab.example.com/coder/coder/-/tree/feat/example"
6  git_providers = {
7    "https://gitlab.example.com/" = {
8      provider = "gitlab"
9    }
10  }
11}

Git clone with branch_name set

Alternatively, you can set the branch_name attribute to clone a specific branch.

For example, to clone the feat/example branch:

1module "git-clone" {
2  source      = "registry.coder.com/modules/git-clone/coder"
3  version     = "1.0.18"
4  agent_id    = coder_agent.example.id
5  url         = "https://github.com/coder/coder"
6  branch_name = "feat/example"
7}

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:

1module "git-clone" {
2  source      = "registry.coder.com/modules/git-clone/coder"
3  version     = "1.0.18"
4  agent_id    = coder_agent.example.id
5  url         = "https://github.com/coder/coder"
6  folder_name = "coder-dev"
7  base_dir    = "~/projects/coder"
8}