-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitlab-ci.yml
148 lines (135 loc) · 3.07 KB
/
gitlab-ci.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
image: registry.gitlab.com/gitlab-org/terraform-images/stable:latest
variables:
TF_ROOT: ${CI_PROJECT_DIR}
TF_ADDRESS: ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/terraform/state/${TF_VAR_ENVIRONMENT}
TF_CLI_CONFIG_FILE: $CI_PROJECT_DIR/.terraformrc
cache:
key: production
paths:
- ${TF_ROOT}/.terraform
before_script:
- cd ${TF_ROOT}
- echo -e "credentials \"$CI_SERVER_HOST\" {\n token = \"$CI_JOB_TOKEN\"\n}" > $TF_CLI_CONFIG_FILE
stages:
- infra-prepare
- infra-build
- infra-deploy
- infra-destroy
init:
stage: infra-prepare
script:
- gitlab-terraform init
plan_develop:
stage: infra-build
script:
- cat "$TFVARIABLES" > "terraform.tfvars"
- echo -e "\nIMAGE_TAG = $IMAGE_TAG\n" >> terraform.tfvars
- gitlab-terraform plan
- gitlab-terraform plan-json
artifacts:
name: plan_develop
paths:
- ${TF_ROOT}/plan.cache
reports:
terraform: ${TF_ROOT}/plan.json
only:
- develop
environment: develop
plan_qa:
stage: infra-build
script:
- cat "$TFVARIABLES" > "terraform.tfvars"
- echo -e "\nIMAGE_TAG = $IMAGE_TAG\n" >> terraform.tfvars
- gitlab-terraform plan
- gitlab-terraform plan-json
artifacts:
name: plan_qa
paths:
- ${TF_ROOT}/plan.cache
reports:
terraform: ${TF_ROOT}/plan.json
only:
- qa
environment: qa
plan_prod:
stage: infra-build
script:
- cat "$TFVARIABLES" > "terraform.tfvars"
- echo -e "\nIMAGE_TAG = $IMAGE_TAG\n" >> terraform.tfvars
- gitlab-terraform plan
- gitlab-terraform plan-json
artifacts:
name: plan_prod
paths:
- ${TF_ROOT}/plan.cache
reports:
terraform: ${TF_ROOT}/plan.json
only:
- prod
environment: prod
apply_develop:
stage: infra-deploy
environment: develop
script:
- cat "$TFVARIABLES" > "terraform.tfvars"
- echo -e "\nIMAGE_TAG = $IMAGE_TAG\n" >> terraform.tfvars
- gitlab-terraform apply
dependencies:
- plan_develop
only:
- develop
apply_qa:
stage: infra-deploy
environment: qa
script:
- cat "$TFVARIABLES" > "terraform.tfvars"
- echo -e "\nIMAGE_TAG = $IMAGE_TAG\n" >> terraform.tfvars
- gitlab-terraform apply
dependencies:
- plan_qa
only:
- qa
apply_prod:
stage: infra-deploy
environment: prod
script:
- cat "$TFVARIABLES" > "terraform.tfvars"
- echo -e "\nIMAGE_TAG = $IMAGE_TAG\n" >> terraform.tfvars
- gitlab-terraform apply
dependencies:
- plan_prod
only:
- prod
destroy_develop:
stage: infra-destroy
environment: develop
script:
- cat "$TFVARIABLES" > "terraform.tfvars"
- gitlab-terraform destroy
dependencies:
- apply_develop
when: manual
only:
- develop
destroy_qa:
stage: infra-destroy
environment: qa
script:
- cat "$TFVARIABLES" > "terraform.tfvars"
- gitlab-terraform destroy
dependencies:
- apply_qa
when: manual
only:
- qa
destroy_prod:
stage: infra-destroy
environment: prod
script:
- cat "$TFVARIABLES" > "terraform.tfvars"
- gitlab-terraform destroy
dependencies:
- apply_prod
when: manual
only:
- prod