-
Notifications
You must be signed in to change notification settings - Fork 0
/
.gitlab-ci.yml
187 lines (169 loc) · 4.62 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
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
image: docker:20
variables:
PIP_CACHE_DIR: $CI_PROJECT_DIR/.cache/pip
REPOSITORY_URL: $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$REPOSITORY_NAME
services:
- docker:20-dind
# Cache downloaded dependencies and plugins between builds.
# To keep cache across branches add key: $CI_JOB_NAME
cache: &global_cache
key: ${CI_COMMIT_REF_SLUG}
paths:
- $PIP_CACHE_DIR
# Installation of OS and CLI requirements.
.configuration:
before_script:
- apk add py-pip
- pip install awscli --cache-dir $PIP_CACHE_DIR
# We need to login to AWS CLI first before continuing.
# Set the AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY and AWS_DEFAULT_REGION first
- $(aws ecr get-login --no-include-email --region "${AWS_DEFAULT_REGION}")
# Deployment to the AWS ECS Service
.deploy:
extends: .configuration
script:
- echo "Updating ECS service..."
- aws ecs update-service --force-new-deployment --region "${AWS_DEFAULT_REGION}" --cluster "${AWS_CLUSTER_NAME}-${$CI_ENVIRONMENT_NAME}" --service "${AWS_SERVICE_NAME}"
- echo "Updated ECS service."
tags:
- MicroRunner
# Docker Build
.docker_build:
extends: .configuration
script:
- echo "Building docker image..."
- docker pull $REPOSITORY_URL:latest || true
- DOCKER_BUILDKIT=1 docker build --cache-from $REPOSITORY_URL:latest -t $REPOSITORY_URL:latest --build-arg BUILDKIT_INLINE_CACHE=1 .
- echo "Done building docker image."
- echo "Pushing images..."
- docker tag $REPOSITORY_URL:latest $REPOSITORY_URL:$CI_ENVIRONMENT_NAME
# The commit SHA is for archiving purposes
- docker tag $REPOSITORY_URL:latest $REPOSITORY_URL:$CI_COMMIT_SHA
- docker push $REPOSITORY_URL:latest
- docker push $REPOSITORY_URL:$CI_ENVIRONMENT_NAME
# The commit SHA is for archiving purposes
- docker push $REPOSITORY_URL:$CI_COMMIT_SHA
- echo "Pushed images."
tags:
- MediumRunner
# 1. Test
# 2. Build
# 3. Tag
# 4. Deploy
stages:
- test
- build
- tag
- deploy
# Test the Merge Request
Test Merge Request:
stage: test
image: gradle:7-jdk20
cache:
<<: *global_cache
key: merge_requests
script:
- echo "Running tests..."
- SPRING_PROFILES_ACTIVE=ci gradle test --info
- echo "Tests completed."
tags:
- MediumRunner
only:
- merge_requests
# Per-Branch test
Test:
stage: test
image: gradle:7-jdk20
script:
- echo "Running tests..."
- SPRING_PROFILES_ACTIVE=ci gradle test --info
- echo "Tests completed."
tags:
- MediumRunner
only:
- develop
- /^release/.*$/i
- staging
- main
# Build the Docker Build for DEV using the JAR file created from the build stage
Build for Dev:
stage: build
extends: .docker_build
environment:
name: dev
only:
- develop
# Build the Docker Build for Testing using the JAR file created from the build stage
Build for Testing:
stage: build
extends: .docker_build
environment:
name: test
only:
- /^release/.*$/i
# Build the Docker Build for Staging using the JAR file created from the build stage
Build for Staging:
stage: build
extends: .docker_build
environment:
name: staging
only:
- staging
# Build the Docker Build for Production using the JAR file created from the build stage
Build for Production:
stage: build
extends: .docker_build
environment:
name: prod
only:
- main
# Tag the docker image based on the pushed repository tag
Tag:
stage: tag
extends: .configuration
script:
- echo "Tagging docker image..."
- docker pull $REPOSITORY_URL:$CI_COMMIT_SHA
- docker tag $REPOSITORY_URL:$CI_COMMIT_SHA $REPOSITORY_URL:$CI_COMMIT_TAG
- docker push $REPOSITORY_URL:$CI_COMMIT_TAG
- echo "Tagged docker image."
tags:
- MicroRunner
only:
- tags
# Deploy the image into the Dev cluster
Deploy to Dev:
stage: deploy
extends: .deploy
environment:
# We want to isolate AWS_CLUSTER_NAME and AWS_SERVICE_NAME per environment
name: dev
only:
- develop
# Deploy the image into the Testing cluster
Deploy to Testing:
stage: deploy
extends: .deploy
environment:
# We want to isolate AWS_CLUSTER_NAME and AWS_SERVICE_NAME per environment
name: test
only:
- /^release/.*$/i
# Deploy the image into the Staging cluster
Deploy to Staging:
stage: deploy
extends: .deploy
environment:
# We want to isolate AWS_CLUSTER_NAME and AWS_SERVICE_NAME per environment
name: staging
only:
- staging
# Deploy the image into the Production cluster
Deploy to Production:
stage: deploy
extends: .deploy
environment:
# We want to isolate AWS_CLUSTER_NAME and AWS_SERVICE_NAME per environment
name: prod
only:
- main