-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.gitlab-ci.yml
More file actions
304 lines (283 loc) · 7.46 KB
/
Copy path.gitlab-ci.yml
File metadata and controls
304 lines (283 loc) · 7.46 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# GitLab CI Pipeline for Firewall GitOps
# This pipeline validates, plans, and applies firewall configurations
variables:
TERRAFORM_VERSION: '1.13.0'
TF_ROOT: '${CI_PROJECT_DIR}/terraform'
SELECTED_CLUSTER:
value: 'none'
options:
- example
- production
- development
- none
description: 'Select the cluster to deploy'
GITLAB_API_URL: '${CI_API_V4_URL}'
GITLAB_PROJECT_ID: '${CI_PROJECT_ID}'
GITLAB_USERNAME: 'gitlab-ci-token'
GITLAB_PASSWORD: '${CI_JOB_TOKEN}'
# PAN-OS Provider Variables
PANOS_HOSTNAME: '${PANOS_HOSTNAME}'
PANOS_USERNAME: '${PANOS_USERNAME}'
PANOS_PASSWORD: '${PANOS_PASSWORD}'
PANOS_API_KEY: '${PANOS_API_KEY}'
PANOS_PROTOCOL: '${PANOS_PROTOCOL:-https}'
PANOS_PORT: '${PANOS_PORT:-443}'
PANOS_TIMEOUT: '${PANOS_TIMEOUT:-10}'
PANOS_SKIP_VERIFY_CERTIFICATE: '${PANOS_SKIP_VERIFY_CERTIFICATE:-true}'
# Cache Terraform plugins and modules per cluster
cache:
key: '${CI_COMMIT_REF_SLUG}-${CLUSTER_NAME}'
paths:
- ${TF_ROOT}/.terraform
- ${TF_ROOT}/.terraform.lock.hcl
stages:
- validate
- plan
- apply
- cleanup
# Base job template
.terraform_base:
image:
name: hashicorp/terraform:$TERRAFORM_VERSION
entrypoint: ['']
before_script:
- ./scripts/deploy.sh -c $CLUSTER_NAME -a init
# YAML Validation
validate_yaml:
stage: validate
image: python:3.11-alpine
before_script:
- pip install -r requirements.txt
script:
- |
echo "Validating YAML configurations..."
echo "Using JSON schemas from schemas/ directory"
python3 scripts/validate_yaml.py
artifacts:
reports:
junit: validation-results.xml
when: always
expire_in: 1 week
rules:
- changes:
- clusters/**/*.yaml
- clusters/**/*.yml
- schemas/*.json
- scripts/validate_yaml.py
# Terraform Format Check for Example
terraform_fmt_example:
extends: .terraform_base
stage: validate
variables:
CLUSTER_NAME: 'example'
script:
- ./scripts/deploy.sh -c $CLUSTER_NAME -a fmt
rules:
- changes:
- clusters/example/**/*.yaml
- clusters/example/**/*.yml
- terraform/**/*.tf
- modules/**/*.tf
# Terraform Format Check for Development
terraform_fmt_development:
extends: .terraform_base
stage: validate
variables:
CLUSTER_NAME: 'development'
script:
- ./scripts/deploy.sh -c $CLUSTER_NAME -a fmt
rules:
- changes:
- clusters/development/**/*.yaml
- clusters/development/**/*.yml
- terraform/**/*.tf
- modules/**/*.tf
# Terraform Format Check for Production
terraform_fmt_production:
extends: .terraform_base
stage: validate
variables:
CLUSTER_NAME: 'production'
script:
- ./scripts/deploy.sh -c $CLUSTER_NAME -a fmt
rules:
- changes:
- clusters/production/**/*.yaml
- clusters/production/**/*.yml
- terraform/**/*.tf
- modules/**/*.tf
# Terraform Validation for Example
terraform_validate_example:
extends: .terraform_base
stage: validate
variables:
CLUSTER_NAME: 'example'
script:
- ./scripts/deploy.sh -c $CLUSTER_NAME -a validate
rules:
- changes:
- clusters/example/**/*.yaml
- clusters/example/**/*.yml
- terraform/**/*.tf
- modules/**/*.tf
# Terraform Validation for Development
terraform_validate_development:
extends: .terraform_base
stage: validate
variables:
CLUSTER_NAME: 'development'
script:
- ./scripts/deploy.sh -c $CLUSTER_NAME -a validate
rules:
- changes:
- clusters/development/**/*.yaml
- clusters/development/**/*.yml
- terraform/**/*.tf
- modules/**/*.tf
# Terraform Validation for Production
terraform_validate_production:
extends: .terraform_base
stage: validate
variables:
CLUSTER_NAME: 'production'
script:
- ./scripts/deploy.sh -c $CLUSTER_NAME -a validate
rules:
- changes:
- clusters/production/**/*.yaml
- clusters/production/**/*.yml
- terraform/**/*.tf
- modules/**/*.tf
# Security Scan
security_scan:
stage: validate
image:
name: bridgecrew/checkov:latest
entrypoint: ['']
script:
- |
echo "Running security scan on Terraform configurations..."
checkov -d terraform/ --framework terraform --output cli --quiet
checkov -d modules/ --framework terraform --output cli --quiet
allow_failure: true
rules:
- changes:
- terraform/**/*.tf
- modules/**/*.tf
.plan_template: &plan_template
extends: .terraform_base
stage: plan
resource_group: terraform-$CLUSTER_NAME
script:
- ./scripts/deploy.sh -c $CLUSTER_NAME -a plan
artifacts:
name: 'terraform-plan-$CLUSTER_NAME'
paths:
- ${TF_ROOT}/plan-*.tfplan
- ${TF_ROOT}/plan-*.json
expire_in: 1 week
reports:
terraform: ${TF_ROOT}/plan-*.json
# Terraform Apply
.apply_template: &apply_template
extends: .terraform_base
stage: apply
resource_group: terraform-$CLUSTER_NAME
# variables:
# CLUSTER_NAME: '${SELECTED_CLUSTER}'
script:
- ./scripts/deploy.sh -c $CLUSTER_NAME -a apply
artifacts:
name: 'terraform-apply-$CLUSTER_NAME'
paths:
- ${TF_ROOT}/terraform.tfstate
expire_in: 1 month
# Dynamic plan jobs will be created for each changed cluster
plan_example:
<<: *plan_template
variables:
CLUSTER_NAME: 'example'
rules:
- if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
changes:
- clusters/example/**/*.yaml
- clusters/example/**/*.yml
- terraform/**/*.tf
- modules/**/*.tf
plan_development:
<<: *plan_template
variables:
CLUSTER_NAME: 'development'
rules:
- if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
changes:
- clusters/development/**/*.yaml
- clusters/development/**/*.yml
- terraform/**/*.tf
- modules/**/*.tf
plan_production:
<<: *plan_template
variables:
CLUSTER_NAME: 'production'
rules:
- if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
changes:
- clusters/production/**/*.yaml
- clusters/production/**/*.yml
- terraform/**/*.tf
- modules/**/*.tf
# Manual approval for production deployments
approve_production:
stage: plan
image: alpine:latest
script:
- echo "Production deployment requires manual approval"
when: manual
allow_failure: false
rules:
- if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
- changes:
- clusters/production/**/*.yaml
- clusters/production/**/*.yml
- terraform/**/*.tf
- modules/**/*.tf
apply_example:
<<: *apply_template
variables:
CLUSTER_NAME: 'example'
dependencies:
- plan_example
rules:
- if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
changes:
- clusters/example/**/*.yaml
- clusters/example/**/*.yml
- terraform/**/*.tf
- modules/**/*.tf
apply_development:
<<: *apply_template
variables:
CLUSTER_NAME: 'development'
dependencies:
- plan_development
rules:
- if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
changes:
- clusters/development/**/*.yaml
- clusters/development/**/*.yml
- terraform/**/*.tf
- modules/**/*.tf
apply_production:
<<: *apply_template
variables:
CLUSTER_NAME: 'production'
dependencies:
- plan_production
- approve_production
rules:
- if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
changes:
- clusters/production/**/*.yaml
- clusters/production/**/*.yml
- terraform/**/*.tf
- modules/**/*.tf