Skip to content

Commit 5191839

Browse files
elijah-robertssfang97
authored and
sfang97
committed
feat: Add support to create projects from templates
1 parent 454510b commit 5191839

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

gitlab/resource_gitlab_project.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,18 @@ var resourceGitLabProjectSchema = map[string]*schema.Schema{
193193
Type: schema.TypeBool,
194194
Optional: true,
195195
},
196+
"template_name": {
197+
Type: schema.TypeString,
198+
Optional: true,
199+
},
200+
"use_custom_template": {
201+
Type: schema.TypeBool,
202+
Optional: true,
203+
},
204+
"group_with_project_templates_id": {
205+
Type: schema.TypeInt,
206+
Optional: true,
207+
},
196208
}
197209

198210
func resourceGitlabProject() *schema.Resource {
@@ -259,6 +271,9 @@ func resourceGitlabProjectCreate(d *schema.ResourceData, meta interface{}) error
259271
OnlyAllowMergeIfAllDiscussionsAreResolved: gitlab.Bool(d.Get("only_allow_merge_if_all_discussions_are_resolved").(bool)),
260272
SharedRunnersEnabled: gitlab.Bool(d.Get("shared_runners_enabled").(bool)),
261273
RemoveSourceBranchAfterMerge: gitlab.Bool(d.Get("remove_source_branch_after_merge").(bool)),
274+
TemplateName: gitlab.String(d.Get("template_name").(string)),
275+
UseCustomTemplate: gitlab.Bool(d.Get("use_custom_template").(bool)),
276+
GroupWithProjectTemplatesID: gitlab.Int(d.Get("group_with_project_templates_id").(int)),
262277
}
263278

264279
// need to manage partial state since project creation may require
@@ -282,6 +297,9 @@ func resourceGitlabProjectCreate(d *schema.ResourceData, meta interface{}) error
282297
"only_allow_merge_if_all_discussions_are_resolved",
283298
"shared_runners_enabled",
284299
"remove_source_branch_after_merge",
300+
"template_name",
301+
"use_custom_template",
302+
"group_with_project_templates_id",
285303
}
286304

287305
if v, ok := d.GetOk("path"); ok {

website/docs/r/project.html.markdown

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,37 @@ resource "gitlab_project" "example" {
2323
}
2424
```
2525

26+
### Template Examples
27+
28+
Project templates can pre-populate a new project with the necessary files to get you started quickly.
29+
30+
### Built-In Template Project
31+
32+
Gitlab has [built-in templates](https://docs.gitlab.com/ee/gitlab-basics/create-project.html#built-in-templates), sourced from the following groups:
33+
* [project-templates](https://gitlab.com/gitlab-org/project-templates)
34+
* [pages](https://gitlab.com/pages)
35+
36+
37+
```hcl
38+
resource "gitlab_project" "template" {
39+
name = "template"
40+
template_name = "gatsby"
41+
}
42+
```
43+
44+
### Custom Template Project
45+
46+
GitLab administrators and users can configure their own [Custom project templates](https://docs.gitlab.com/ee/gitlab-basics/create-project.html#custom-project-templates-premium)
47+
48+
```hcl
49+
resource "gitlab_project" "custom_template" {
50+
name = "my-new-project"
51+
template_name = "my-custom-template"
52+
use_custom_template= true
53+
group_with_project_templates_id = 1
54+
}
55+
```
56+
2657
## Argument Reference
2758

2859
The following arguments are supported:
@@ -81,6 +112,11 @@ The following arguments are supported:
81112

82113
* `initialize_with_readme` - (Optional) Create master branch with first commit containing a README.md file.
83114

115+
* `template_name` - (Optional) Create project from existing template. When used without `use_custom_template`, name of a built-in project template. When used with `use_custom_template`, name of a custom project template
116+
117+
* `use_custom_template` - (Optional) Use either custom instance or group (with `group_with_project_templates_id`) project template
118+
119+
* `group_with_project_templates_id` - (Optional) For group-level custom templates, specifies ID of group from which all the custom project templates are sourced. Leave empty for instance-level templates. Requires use_custom_template to be true
84120
## Attributes Reference
85121

86122
The following additional attributes are exported:

0 commit comments

Comments
 (0)