-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path.gitlab-ci.yml
144 lines (129 loc) · 4.51 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
# This pipe line has been created for Protein DevOps Engineer Bootcamp Final project
# With gratitude to Yunus Yasar who has been dedicated DevOps tutor during the bootcamp.
variables: # Variables to be used to pass data to jobs.
DOCKERHUB_LOGIN: some_login
DOCKERHUB_PASS: somepass
IMAGE_NAME: react
TAG_LATEST: $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$IMAGE_NAME:latest
DOCKER_TLS_CERTDIR: ""
AWS_REGION : eu-central-1
AWS_ACCOUNT_ID : 356079734857
stages: # List of stages for jobs, and their order of execution
- build
- test
- image-push
- deploy
build: # Stage 1: Build the project
stage: build
image: node
script:
- echo "Start building App"
- npm install # Install dependencies
- npm run build # Build the project
- echo "Build successfully!"
artifacts:
paths:
- build/ # Path to the build output
test: # Stage 2: Run tests
stage: test
image: node
script:
- echo "Testing App"
- CI=true npm test # Run tests with CI=true, because we are in a CI environment and we don't want an interactive prompt.
dependencies:
- build
image-push-amazon: # Stage 3-a: Build the docker image and push it to AWS ECR
stage: image-push
image: docker:latest
services:
- name: docker:20.10.17-dind
before_script: # Before the script, we need to install necessary packages.
- apk add --no-cache python3 py3-pip
- pip3 install --no-cache-dir awscli
script: # Push the image to AWS ECR
- aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com
- docker pull $TAG_LATEST || true
- docker build --cache-from $TAG_LATEST -t $TAG_LATEST .
- docker push $TAG_LATEST
- echo "Contain has been built successfuly and pushed to AWS ECR"
dependencies:
- test
image-push-docker: # Stage 3-b: Build the docker image and push it to Docker Hub
stage: image-push
image: docker:latest
services:
- name: docker:20.10.17-dind
script:
- docker build -t react .
- docker login -u="${DOCKERHUB_LOGIN}" -p="${DOCKERHUB_PASS}"
- docker image tag react:latest ${DOCKERHUB_LOGIN}/react:latest
- docker image push ${DOCKERHUB_LOGIN}/react:latest
- echo "Contain has been built successfuly and pushed to Docker Hub"
dependencies:
- test
# Stage 4-a: Deploy the app to AWS S3
s3-deployment:
stage: deploy
dependencies:
- build
image: python:latest
script:
- echo "deploying app"
- pip install awscli
- aws s3 sync ./build/ s3://protein-bootcamp-prod/ # Deploy the app to AWS S3
# Stage 4-b-1: Deploy the app to AWS ECS cluster, in this stage with terraform we first validate our terraform files
terraform-validate:
stage: deploy
image:
name: hashicorp/terraform:light
entrypoint:
- '/usr/bin/env'
- 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'
before_script:
- cd ./terraform-ecs-fargate
- export AWS_ACCESS_KEY=${AWS_ACCESS_KEY_ID}
- export AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
- rm -rf .terraform
- terraform --version
- terraform init
script:
- terraform validate
# Stage 4-b-2: Deploy the app to AWS ECS cluster, in this stage with terraform we first plan our terraform files and check if everything is okay
terraform-plan:
stage: deploy
image:
name: hashicorp/terraform:light
entrypoint:
- '/usr/bin/env'
- 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'
before_script:
- cd ./terraform-ecs-fargate
- export AWS_ACCESS_KEY=${AWS_ACCESS_KEY_ID}
- export AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
- rm -rf .terraform
- terraform --version
- terraform init
script:
- terraform plan
dependencies:
- terraform-validate
# Stage 4-b-3: Deploy the app to AWS ECS cluster, in this stage with terraform we apply our terraform files and check if everything was okay in previous step
terraform-apply:
stage: deploy
image:
name: hashicorp/terraform:light
entrypoint:
- '/usr/bin/env'
- 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'
before_script:
- cd ./terraform-ecs-fargate
- export AWS_ACCESS_KEY=${AWS_ACCESS_KEY_ID}
- export AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
- rm -rf .terraform
- terraform --version
- terraform init
script:
- terraform apply -auto-approve
dependencies:
- terraform-plan
when: manual