-
Notifications
You must be signed in to change notification settings - Fork 0
/
.gitlab-ci.yml
106 lines (99 loc) · 2.3 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
---
variables:
TF_HTTP_ADDRESS: ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/terraform/state/${ENV}
TF_HTTP_LOCK_ADDRESS: ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/terraform/state/${ENV}/lock
TF_HTTP_UNLOCK_ADDRESS: ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/terraform/state/${ENV}/lock
TF_HTTP_LOCK_METHOD: POST
TF_HTTP_UNLOCK_METHOD: DELETE
stages:
- review
- validate
- plan
- deploy
- verify
set_deploy_env:stage:
stage: review
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
script:
- echo "ENV=stage" >> review.env
artifacts:
reports:
dotenv: review.env
set_deploy_env:prod:
stage: review
rules:
- if: '$CI_COMMIT_BRANCH == "main"'
script:
- echo "ENV=prod" >> review.env
artifacts:
reports:
dotenv: review.env
tf_validate:
stage: validate
rules:
- when: always
needs: []
image:
name: "hashicorp/terraform:1.3.7"
entrypoint: [""]
script:
- terraform init
- terraform validate
- terraform fmt -check=true
tf_plan:
stage: plan
rules:
- if: '$CI_COMMIT_BRANCH == "main"'
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
needs:
- job: set_deploy_env:stage
optional: true
- job: set_deploy_env:prod
optional: true
- job: tf_validate
image:
name: "hashicorp/terraform:1.3.7"
entrypoint: [""]
script:
- terraform init
- terraform plan -var-file=common.tfvars -var-file=$ENV.env.tfvars -out plan
artifacts:
name: plan
paths:
- plan
tf_apply:
stage: deploy
rules:
- if: '$CI_COMMIT_BRANCH == "main"'
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
needs:
- job: set_deploy_env:stage
optional: true
- job: set_deploy_env:prod
optional: true
- job: tf_plan
image:
name: "hashicorp/terraform:1.3.7"
entrypoint: [""]
script:
- terraform init
- terraform apply plan
verify:
stage: verify
rules:
- if: '$CI_COMMIT_BRANCH == "main"'
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
needs:
- job: set_deploy_env:stage
optional: true
- job: set_deploy_env:prod
optional: true
- job: tf_apply
image:
name: "shdu/pyats-genie:latest"
entrypoint: [""]
script:
- cd tests
- source /pyats/bin/activate
- pyats run job test_job.py -t $ENV.yml