-
Notifications
You must be signed in to change notification settings - Fork 476
Expand file tree
/
Copy pathconfig.yml
More file actions
233 lines (213 loc) · 7.9 KB
/
Copy pathconfig.yml
File metadata and controls
233 lines (213 loc) · 7.9 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
# Use the latest 2.1 version of CircleCI pipeline process engine. See: https://circleci.com/docs/2.0/configuration-reference
version: 2.1
# Use a package of configuration called an orb.
orbs:
# Choose either one of the orbs below
# Declare a dependency on the welcome-orb
# welcome: circleci/welcome-orb@0.4.1
aws-cli: circleci/aws-cli@2.0.3
# Orchestrate or schedule a set of jobs
commands:
# Exercise: Reusable Job Code
print_pipeline_id:
parameters:
id:
type: string
steps:
- run: echo << parameters.id >>
# Exercise - Rollback
destroy_environment:
steps:
- run:
name: Destroy environment
# ${CIRCLE_WORKFLOW_ID} is a Built-in environment variable
# ${CIRCLE_WORKFLOW_ID:0:5} takes the first 5 chars of the variable CIRCLE_CI_WORKFLOW_ID
when: on_fail
command: |
aws cloudformation delete-stack --stack-name myStack-${CIRCLE_WORKFLOW_ID:0:5}
jobs:
# Exercise: Creating a Simple Workflow
# Exercise: Environment Variables
# Exercise: Reusable Job Code
print_greetings:
docker:
- image: circleci/node:13.8.0
steps:
- print_pipeline_id:
id: << pipeline.id >>
- run: echo HELLO
- run: echo WORLD
- run: echo $_env_name
# Exercise: Sharing Files (Job names may have changed)
upload_file:
docker:
- image: circleci/node:13.8.0
steps:
- run: echo "THIS IS A SAMPLE TEXT" > ~/output.txt
- persist_to_workspace:
root: ~/
paths:
- output.txt
download_file:
docker:
- image: circleci/node:13.8.0
steps:
- attach_workspace:
at: ~/
- run: cat ~/output.txt
# Exercise: Infrastructure Creation
# Exercise - Rollback
create_infrastructure:
docker:
- image: amazon/aws-cli
steps:
- checkout
- run:
name: Create Cloudformation Stack
command: |
aws cloudformation deploy \
--template-file template.yml \
--stack-name myStack-${CIRCLE_WORKFLOW_ID:0:5} \
--region us-east-1
# Fail the job intentionally to simulate an error.
# Uncomment the line below if you want to fail the current step
# - run: return 1
- destroy_environment
# Exercise: Config and Deployment
configure_infrastructure:
docker:
- image: python:3.9-alpine3.16
steps:
- checkout
- add_ssh_keys:
fingerprints: ["0c:55:f7:d7:ad:e3:73:19:0b:e6:c6:46:5c:04:76:94"] # You can get this ID in the section where you registered the SSH Key
- run:
name: Install dependencies
command: |
# install the dependencies needed for your playbook
apk add --update ansible
- run:
name: Configure server
command: |
ansible-playbook -i inventory.txt main4.yml
# Exercise: Smoke Testing
smoke_test:
docker:
- image: alpine:latest
steps:
- run: apk add --update curl
- run:
name: smoke test
command: |
URL="https://blog.udacity.com/"
# Test if website exists
if curl -s --head ${URL}
then
return 0
else
return 1
fi
- destroy_environment
# Exercise: Promote to Production - Job 1
# Prerequisite:
# 1. An S3 bucket (say `mybucket644752792305`) with a sample index.html created manually in your AWS console.
# 2. Enable the Static website hosting in that bucket.
# 3. Run the command below to create a CloudFront Distribution that will connect to the existing bucket.
# Note that the `--parameter-overrides` let you specify a value that override parameter value in the cloudfront.yml template file.
# We are assuming that the `PipelineID` parameter represents the bucket ID.
# aws cloudformation deploy \
# --template-file cloudfront.yml \
# --stack-name production-distro \
# --parameter-overrides PipelineID="mybucket644752792305" \ # Name of the S3 bucket you created manually.
# Job 1
# Executes the bucket.yml - Deploy an S3 bucket, and interface with that bucket to synchronize the files between local and the bucket.
# Note that the `--parameter-overrides` let you specify a value that override parameter value in the bucket.yml template file.
create_and_deploy_front_end:
docker:
- image: amazon/aws-cli
steps:
- checkout
- run:
name: Execute bucket.yml - Create Cloudformation Stack
command: |
aws cloudformation deploy \
--template-file bucket.yml \
--stack-name stack-create-bucket-${CIRCLE_WORKFLOW_ID:0:7} \
--parameter-overrides MyBucketName="mybucket-${CIRCLE_WORKFLOW_ID:0:7}"
# Uncomment the step below if yoou wish to upload all contents of the current directory to the S3 bucket
# - run: aws s3 sync . s3://mybucket-${CIRCLE_WORKFLOW_ID:0:7} --delete
# Exercise: Promote to Production - Job 2
# Fetch and save the pipeline ID (bucket ID) responsible for the last release.
get_last_deployment_id:
docker:
- image: amazon/aws-cli
steps:
- checkout
- run: yum install -y tar gzip
- run:
name: Fetch and save the old pipeline ID (bucket name) responsible for the last release.
command: |
aws cloudformation \
list-exports --query "Exports[?Name==\`PipelineID\`].Value" \
--no-paginate --output text > ~/textfile.txt
- persist_to_workspace:
root: ~/
paths:
- textfile.txt
# Exercise: Promote to Production - Job 3
# Executes the cloudfront.yml template that will modify the existing CloudFront Distribution, change its target from the old bucket to the new bucket - `mybucket-${CIRCLE_WORKFLOW_ID:0:7}`.
# Notice here we use the stack name `production-distro` which is the same name we used while deploying to the S3 bucket manually.
promote_to_production:
docker:
- image: amazon/aws-cli
steps:
- checkout
- run:
name: Execute cloudfront.yml
command: |
aws cloudformation deploy \
--template-file cloudfront.yml \
--stack-name production-distro \
--parameter-overrides PipelineID="mybucket-${CIRCLE_WORKFLOW_ID:0:7}"
# Exercise: Promote to Production - Job 4
# Destroy the previous production version's S3 bucket and CloudFormation stack.
clean_up_old_front_end:
docker:
- image: amazon/aws-cli
steps:
- checkout
- run: yum install -y tar gzip
- attach_workspace:
at: ~/
- run:
name: Destroy the previous production version's S3 bucket and CloudFormation stack.
# Use $OldBucketID environment variable or mybucket644752792305 below.
# Similarly, you can create and use $OldStackID environment variable in place of production-distro
command: |
export OldBucketID=$(cat ~/textfile.txt)
aws s3 rm "s3://${OldBucketID}" --recursive
# aws cloudformation delete-stack --stack-name production-distro
# aws cloudformation delete-stack --stack-name stack-create-bucket-${CIRCLE_WORKFLOW_ID:0:7}
# aws cloudformation delete-stack --stack-name myStack-${CIRCLE_WORKFLOW_ID:0:5}
workflows:
# Name the workflow "welcome"
my_workflow:
# Run the welcome/run job in its own container
jobs:
# - welcome/run
- print_greetings
- upload_file
- download_file:
requires:
- upload_file
- create_infrastructure
- configure_infrastructure
- create_and_deploy_front_end
- promote_to_production:
requires:
- create_and_deploy_front_end
- get_last_deployment_id
- clean_up_old_front_end:
requires:
- get_last_deployment_id
- promote_to_production