Skip to content

Commit b3e5bf8

Browse files
authored
Merge pull request #670 from willianpaixao/add-example-3
Adding a first complete example code
2 parents 42a38e4 + 0296247 commit b3e5bf8

File tree

4 files changed

+198
-0
lines changed

4 files changed

+198
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Terraform provider example
2+
3+
## Getting started
4+
5+
### Gitlab access token
6+
7+
First create an access token, with the `api` scope and save that in a secure place (you won't be able to see it again). See the [documentation](https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html) for further information.
8+
The simplest way to provide the token is in a variable file:
9+
10+
```shell
11+
$ ACCESS_TOKEN="YOUR_GITLAB_TOKEN"
12+
$ echo gitlab_token = "\"${ACCESS_TOKEN}\"" >> terraform.tfvars
13+
```
14+
15+
### Kick-starting the backend
16+
17+
Then you need to initialize the state file, by simply replacing the variables and running the following command:
18+
19+
```shell
20+
$ USERNAME="YOUR_GITLAB_USERNAME"
21+
$ ACCESS_TOKEN="YOUR_GITLAB_TOKEN"
22+
$ PROJECT_ID="12345678"
23+
$ STATE_NAME="default"
24+
25+
$ ADDRESS="https://gitlab.com/api/v4/projects/${PROJECT_ID}/terraform/state/${STATE_NAME}"
26+
27+
$ terraform init \
28+
-backend-config="address=${ADDRESS}" \
29+
-backend-config="lock_address=${ADDRESS}/lock" \
30+
-backend-config="unlock_address=${ADDRESS}/lock" \
31+
-backend-config="username=${USERNAME}" \
32+
-backend-config="password=${ACCESS_TOKEN}" \
33+
-backend-config="lock_method=POST" \
34+
-backend-config="unlock_method=DELETE" \
35+
-backend-config="retry_wait_min=5"
36+
-backend-config="password=${ACCESS_TOKEN}"
37+
```
38+
39+
The available arguments and valid values can be seen in the [Terraform documentation](https://www.terraform.io/docs/language/settings/backends/http.html#configuration-variables).
40+
41+
Check the newly created state by running `terraform plan`.
42+
43+
## Creating the infrastructure
44+
45+
Once the backend is initiated, you can start to create your resources:
46+
47+
```shell
48+
$ terraform apply
49+
```
50+
51+
## Destroying all resources
52+
53+
Similarly, all resources can be deleted or scheduled for deletion by running:
54+
55+
```shell
56+
$ terraform destroy
57+
```
58+
59+
## References
60+
61+
1. [GitLab managed Terraform State](https://docs.gitlab.com/ee/user/infrastructure/terraform_state.html)
62+
2. [Infrastructure as code with Terraform and GitLab](https://docs.gitlab.com/ee/user/infrastructure/index.html)

examples/gitlab-managed-state/main.tf

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
terraform {
2+
backend "http" {
3+
}
4+
5+
required_providers {
6+
gitlab = {
7+
source = "gitlabhq/gitlab"
8+
version = "3.7.0"
9+
}
10+
}
11+
}
12+
13+
provider "gitlab" {
14+
token = var.gitlab_token
15+
}
16+
17+
resource "gitlab_group" "group" {
18+
name = "My Group"
19+
path = "my-group"
20+
description = "Promoting Open Source Projects"
21+
visibility_level = "public"
22+
23+
lifecycle {
24+
prevent_destroy = true
25+
}
26+
}
27+
28+
resource "gitlab_group_membership" "group_membership" {
29+
for_each = var.group_members
30+
31+
group_id = gitlab_group.group.id
32+
user_id = each.key
33+
access_level = each.value
34+
}
35+
36+
resource "gitlab_project" "api" {
37+
name = "api"
38+
description = "An example project"
39+
namespace_id = gitlab_group.group.id
40+
41+
only_allow_merge_if_all_discussions_are_resolved = true
42+
only_allow_merge_if_pipeline_succeeds = true
43+
remove_source_branch_after_merge = true
44+
45+
container_registry_enabled = false
46+
lfs_enabled = false
47+
packages_enabled = false
48+
request_access_enabled = false
49+
shared_runners_enabled = false
50+
snippets_enabled = false
51+
wiki_enabled = false
52+
53+
tags = setunion(var.tags, ["api", "backend", "rest"])
54+
}
55+
56+
resource "gitlab_branch_protection" "main" {
57+
project = gitlab_project.api.id
58+
branch = "main"
59+
push_access_level = "developer"
60+
merge_access_level = "developer"
61+
}
62+
63+
resource "gitlab_project_approval_rule" "default" {
64+
project = gitlab_project.api.id
65+
name = "Minimum one approval required"
66+
approvals_required = 1
67+
}
68+
69+
resource "gitlab_project_level_mr_approvals" "default" {
70+
project_id = gitlab_project.api.id
71+
merge_requests_author_approval = false
72+
merge_requests_disable_committers_approval = true
73+
reset_approvals_on_push = true
74+
}
75+
76+
resource "gitlab_pipeline_trigger" "default" {
77+
project = gitlab_project.api.id
78+
description = "Used to trigger builds in bulk"
79+
}
80+
81+
resource "gitlab_deploy_token" "default" {
82+
project = gitlab_project.api.id
83+
name = "Default deploy token"
84+
scopes = ["read_repository", "read_registry"]
85+
}
86+
87+
resource "gitlab_label" "bug" {
88+
project = gitlab_project.api.id
89+
name = "bug"
90+
description = "issue for flagging bugs and/or errors"
91+
color = "#ff0000"
92+
}
93+
94+
resource "gitlab_label" "documentation" {
95+
project = gitlab_project.api.id
96+
name = "documentation"
97+
description = "issue for documentation updates"
98+
color = "#00ff00"
99+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
output "id" {
2+
value = gitlab_project.api.id
3+
description = "Integer that uniquely identifies the project within the gitlab install"
4+
}
5+
6+
output "path_with_namespace" {
7+
value = gitlab_project.api.path_with_namespace
8+
description = "The path of the repository with namespace"
9+
}
10+
11+
output "web_url" {
12+
value = gitlab_project.api.web_url
13+
description = "URL that can be used to find the project in a browser"
14+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
variable "gitlab_token" {
2+
type = string
3+
description = "GitLab personal access token"
4+
sensitive = true
5+
}
6+
7+
variable "group_members" {
8+
description = <<EOF
9+
All members of the group and its [access level](https://docs.gitlab.com/ee/user/permissions.html#project-members-permissions).
10+
Possible values are: `guest`, `reporter`, `developer`, `maintainer`, `owner`
11+
EOF
12+
type = map(string)
13+
default = {
14+
"1234567" = "owner"
15+
"2345678" = "developer"
16+
}
17+
}
18+
19+
variable "tags" {
20+
type = list(string)
21+
description = "A list of tags (topics) of the project"
22+
default = ["gitlab", "terraform"]
23+
}

0 commit comments

Comments
 (0)