-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgirlysheet.yml
246 lines (230 loc) · 8.13 KB
/
girlysheet.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
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
# ===================================
# Author: @aglorhythm / girlysheet.cloud ✿
# Description: This script is retrieved by azure pipelines.
# The docker image is build and pushed in another azure pipeline.
# I cannot add different environments because of budget purpose -
# I use the preprod as prod simply for demonstration
# ===================================
trigger:
branches:
include:
- development
resources:
pipelines:
- pipeline: dockerBuild
source: Build_Docker_Image
trigger:
branches:
include:
- development
variables:
- group: TofuSecrets
- group: Commands
- group: Acr
- name: TF_ROOT
${{ if eq( variables['Build.SourceBranchName'], 'development' ) }}:
value: $(Build.SourcesDirectory)/tofu/preprod
${{ else }}:
value: $(Build.SourcesDirectory)/tofu/prod
- name: TF_BUCKET
${{ if eq( variables['Build.SourceBranchName'], 'development' ) }}:
value: $(Build.SourcesDirectory)/tofu/preprod/scripts
${{ else }}:
value: $(Build.SourcesDirectory)/tofu/prod/scripts
- name: ANS_ROOT
value: $(Build.SourcesDirectory)/ansible
- name: ANS_INVENTORY
value: $(Build.SourcesDirectory)/ansible/resources
- name: KEY_NAME
${{ if eq( variables['Build.SourceBranchName'], 'development' ) }}:
value: girlysheet_front_key_pair_preprod.pem
${{ else }}:
value: girlysheet_front_key_pair_prod.pem
- name: KEY_SECRET
${{ if eq( variables['Build.SourceBranchName'], 'development' ) }}:
value: girlysheet_front_privkey_preprod
${{ else }}:
value: girlysheet_front_privkey_prod
stages:
- stage: Validate
jobs:
- job: tofu_validate
pool:
name: Default
steps:
- checkout: self
- script: |
cd $(TF_BUCKET)
echo "Changed directory:"
pwd
tofu init -input=false
tofu validate
displayName: 'Validate Tofu deployment - bucket'
continueOnError: false
- script: |
cd $(TF_ROOT)
echo "Changed directory:"
pwd
tofu init -input=false
tofu validate
displayName: 'Validate Tofu deployment - root'
continueOnError: false
- stage: Plan
condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/development'))
jobs:
- job: tofu_plan
pool:
name: Default
steps:
- checkout: self
- script: |
cd $(TF_BUCKET)
echo "Changed directory:"
pwd
tofu init -input=false -reconfigure
tofu plan -refresh=true -out=tfplan_bucket -var-file="$(TF_ROOT)/$(COMMON_VARS)"
ls -lah
env:
TF_VAR_ovh_application_key: $(TF_VAR_ovh_application_key)
TF_VAR_ovh_application_secret: $(TF_VAR_ovh_application_secret)
TF_VAR_ovh_consumer_key: $(TF_VAR_ovh_consumer_key)
TF_VAR_access_key: $(TF_VAR_access_key)
TF_VAR_secret_key: $(TF_VAR_secret_key)
TF_VAR_domain_name: $(TF_VAR_domain_name)
TF_VAR_ssh_port: $(TF_VAR_ssh_port)
TF_VAR_email: $(TF_VAR_email)
displayName: 'Plan Tofu changes - Bucket'
continueOnError: false
- script: |
cd $(TF_ROOT)
echo "Changed directory:"
pwd
echo "Using OVH Application Key: ${TF_VAR_ovh_application_key:0:4}****"
echo "Using OVH Application Secret: ${TF_VAR_ovh_application_secret:0:4}****"
echo "Using OVH Consumer Key: ${TF_VAR_ovh_consumer_key:0:4}****"
tofu init -input=false -reconfigure
TF_LOG=DEBUG tofu plan -refresh=true -out=tfplan_root -var-file="$(TF_ROOT)/$(COMMON_VARS)"
ls -lah
env:
TF_VAR_ovh_application_key: $(TF_VAR_ovh_application_key)
TF_VAR_ovh_application_secret: $(TF_VAR_ovh_application_secret)
TF_VAR_ovh_consumer_key: $(TF_VAR_ovh_consumer_key)
TF_VAR_access_key: $(TF_VAR_access_key)
TF_VAR_secret_key: $(TF_VAR_secret_key)
TF_VAR_domain_name: $(TF_VAR_domain_name)
TF_VAR_ssh_port: $(TF_VAR_ssh_port)
TF_VAR_email: $(TF_VAR_email)
displayName: 'Plan Tofu changes - Root'
continueOnError: false
- publish: $(TF_BUCKET)/tfplan_bucket
artifact: TofuPlanBucket
displayName: 'Publish Tofu plan for the s3 bucket'
- publish: $(TF_ROOT)/tfplan_root
artifact: TofuPlanRoot
displayName: 'Publish Tofu plan for the root script'
- stage: Apply
condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/development'))
jobs:
- job: tofu_apply
pool:
name: Default
steps:
- checkout: self
- download: current
artifact: TofuPlanBucket
displayName: 'Download Tofu plan for the s3 bucket'
- download: current
artifact: TofuPlanRoot
displayName: 'Download Tofu plan for the root script'
- script: |
echo "COMMON_VARS value: "$(COMMON_VARS)""
echo "bucket path value: $(Pipeline.Workspace)/TofuPlanBucket/tfplan_bucket"
cd $(TF_BUCKET)
echo "Changed directory:"
pwd
tofu init -input=false -reconfigure
tofu apply $(Pipeline.Workspace)/TofuPlanBucket/tfplan_bucket
displayName: 'Apply Tofu changes - Bucket'
continueOnError: false
- script: |
cd $(TF_ROOT)
echo "Changed directory:"
pwd
tofu init -input=false -reconfigure
tofu apply $(Pipeline.Workspace)/TofuPlanRoot/tfplan_root
displayName: 'Apply Tofu changes - Root'
continueOnError: false
- script: |
echo "New host file:"
ls $(Build.SourcesDirectory)/ansible
cat $(Build.SourcesDirectory)/ansible/resources/hosts
echo "New group vars file:"
ls $(Build.SourcesDirectory)/ansible/resources/group_vars
cat $(Build.SourcesDirectory)/ansible/resources/group_vars/*.yml
displayName: 'Check new created resources'
continueOnError: false
- publish: $(Build.SourcesDirectory)/ansible/resources
artifact: AnsibleResources
displayName: 'Publish Ansible resources'
- stage: Configure
condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/development'))
jobs:
- job: ansible_configure
pool:
name: Default
steps:
- checkout: self
- download: current
artifact: AnsibleResources
displayName: 'Download Ansible resources'
- script: |
pwd
ls -la $(ANS_ROOT)
echo "host file:"
cat $(Pipeline.Workspace)/AnsibleResources/hosts
echo "group_vars file:"
ls $(Pipeline.Workspace)/AnsibleResources/group_vars
cat $(Pipeline.Workspace)/AnsibleResources/group_vars/*.yml
echo "root:" $(ANS_ROOT)
cd $(ANS_ROOT)
ansible --version
echo "Variables passed:"
echo ${BACKEND_URL} ${FRONTEND_URL}
sleep 30
displayName: 'Check Ansible resources'
- script: |
pwd
cd $(ANS_ROOT)
echo "Changed directory:"
ls -la
mkdir key
cd key
aws secretsmanager get-secret-value --secret-id $(KEY_SECRET) --region eu-west-3 --query SecretString --output text > $(KEY_NAME)
echo "Retrieved key:"
ls -la
displayName: 'Get key for GS Server'
continueOnError: false
- script: |
pwd
cd $(ANS_ROOT)
echo "Changed directory:"
pwd
ls -la
echo "Key file:"
ls -la $(ANS_ROOT)/key
chmod 400 $(ANS_ROOT)/key/$(KEY_NAME)
ls -la $(ANS_ROOT)/key
echo "Resources:"
ls -la $(Pipeline.Workspace)/AnsibleResources
export ANSIBLE_HOST_KEY_CHECKING=False
echo "ACR vars:"
echo $(ACR_PASSWORD) $(ACR_SERVER) $(ACR_USER)
ansible-playbook -i $(Pipeline.Workspace)/AnsibleResources/hosts gs-playbook.yml -vvv -e "ansible_ssh_private_key_file=$(ANS_ROOT)/key/$(KEY_NAME) backend_url=$(BACKEND_URL) frontend_url=$(FRONTEND_URL) acr_server=$(ACR_SERVER) acr_password=$(ACR_PASSWORD) acr_username=$(ACR_USER)"
displayName: 'Run Ansible playbooks'
env:
ANSIBLE_HOST_KEY_CHECKING: "False"
ACR_PASSORD: $(ACR_PASSWORD)
ACR_SERVER: $(ACR_SERVER)
ACR_USER: $(ACR_USER)
FRONTEND_URL: $(FRONTEND_URL)
BACKEND_URL: $(BACKEND_URL)