|
| 1 | +# Cloning Git Repositories |
| 2 | + |
| 3 | +<div style="padding: 0px; margin: 0px;"> |
| 4 | + <span style="vertical-align:middle;">Author: </span> |
| 5 | + <a href="https://github.com/BrunoQuaresma" style="text-decoration: none; color: inherit; margin-bottom: 0px;"> |
| 6 | + <span style="vertical-align:middle;">Bruno Quaresma</span> |
| 7 | + <img src="https://avatars.githubusercontent.com/u/3165839?v=4" width="24px" height="24px" style="vertical-align:middle; margin: 0px;"/> |
| 8 | + </a> |
| 9 | +</div> |
| 10 | +August 06, 2024 |
| 11 | + |
| 12 | +--- |
| 13 | + |
| 14 | +When starting to work on a project, engineers usually need to clone a Git |
| 15 | +repository. Even though this is often a quick step, it can be automated using |
| 16 | +the [Coder Registry](https://registry.coder.com/) to make a seamless Git-first |
| 17 | +workflow. |
| 18 | + |
| 19 | +The first step to enable Coder to clone a repository is to provide |
| 20 | +authorization. This can be achieved by using the Git provider, such as GitHub, |
| 21 | +as an authentication method. If you don't know how to do that, we have written |
| 22 | +documentation to help you: |
| 23 | + |
| 24 | +- [GitHub](https://coder.com/docs/admin/auth#github) |
| 25 | +- [GitLab self-managed](https://coder.com/docs/admin/external-auth#gitlab-self-managed) |
| 26 | +- [Self-managed git providers](https://coder.com/docs/admin/external-auth#self-managed-git-providers) |
| 27 | + |
| 28 | +With the authentication in place, it is time to set up the template to use the |
| 29 | +[Git Clone module](https://registry.coder.com/modules/git-clone) from the |
| 30 | +[Coder Registry](https://registry.coder.com/) by adding it to our template's |
| 31 | +Terraform configuration. |
| 32 | + |
| 33 | +```hcl |
| 34 | +module "git-clone" { |
| 35 | + source = "registry.coder.com/modules/git-clone/coder" |
| 36 | + version = "1.0.12" |
| 37 | + agent_id = coder_agent.example.id |
| 38 | + url = "https://github.com/coder/coder" |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +> You can edit the template using an IDE or terminal of your preference, or by |
| 43 | +> going into the |
| 44 | +> [template editor UI](https://coder.com/docs/templates/creating#editing-templates). |
| 45 | +
|
| 46 | +You can also use |
| 47 | +[template parameters](https://coder.com/docs/templates/parameters) to customize |
| 48 | +the Git URL and make it dynamic for use cases where a template supports multiple |
| 49 | +projects. |
| 50 | + |
| 51 | +```hcl |
| 52 | +data "coder_parameter" "git_repo" { |
| 53 | + name = "git_repo" |
| 54 | + display_name = "Git repository" |
| 55 | + default = "https://github.com/coder/coder" |
| 56 | +} |
| 57 | +
|
| 58 | +module "git-clone" { |
| 59 | + source = "registry.coder.com/modules/git-clone/coder" |
| 60 | + version = "1.0.12" |
| 61 | + agent_id = coder_agent.example.id |
| 62 | + url = data.coder_parameter.git_repo.value |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +> If you need more customization, you can read the |
| 67 | +> [Git Clone module](https://registry.coder.com/modules/git-clone) documentation |
| 68 | +> to learn more about the module. |
| 69 | +
|
| 70 | +Don't forget to build and publish the template changes before creating a new |
| 71 | +workspace. You can check if the repository is cloned by accessing the workspace |
| 72 | +terminal and listing the directories. |
0 commit comments