Skip to content

Commit 8919054

Browse files
Update CICD in AWS
1 parent faf63cb commit 8919054

File tree

11 files changed

+486
-118
lines changed

11 files changed

+486
-118
lines changed

.github/workflows/README.md

+47-24
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,65 @@
1-
# Workflows
1+
# CI/CD Deploy
22

3-
## Development - Build and Unittest
3+
## Workflows
44

5-
File: [development_pipeline.yml](development_pipeline.yml)
5+
### Development - Build and Unittest
66

7-
Event: On **Pull Request** → any branch into **develop**
7+
#### File: [development_pipeline.yml](development_pipeline.yml)
88

9-
Jobs:
9+
**Event:** On Pull Request → any branch into develop
1010

11-
- Build
12-
- Unit Test
11+
**Jobs:**
1312

14-
### Description:
13+
- Install dependencies (caches)
14+
- Run isort
15+
- Run black
16+
- Run flake8
17+
- Build images (caches)
18+
- Push images to Docker Hub
1519

16-
## Staging - CI/CD Pipeline
20+
**Description:**
21+
This workflow is triggered on Pull Requests into the develop branch. It ensures a clean and standardized codebase by installing dependencies, checking code formatting with isort, black, and flake8, and finally building and pushing Docker images to Docker Hub.
1722

18-
File: [staging_pipeline.yml](staging_pipeline.yml)
23+
### Staging - CI/CD Pipeline
1924

20-
Event: On **Pull Request** → any branch into **staging**
25+
#### File: [staging_pipeline.yml](staging_pipeline.yml)
2126

22-
Jobs:
27+
**Event:** On Pull Request → any branch into staging
2328

24-
- Build
25-
- Unit Test
26-
- Deploy
29+
**Jobs:**
2730

28-
### Description:
31+
- Install dependencies (caches)
32+
- Run isort
33+
- Run black
34+
- Run flake8
35+
- Build images (caches)
36+
- Push images to Docker Hub
37+
- Create infrastructure
38+
- Configure infrastructure
39+
- Deploy application using Docker Compose
40+
- Clean up following the concept of A/B deploy
2941

30-
## Production - CI/CD Pipeline
42+
**Description:**
43+
This pipeline is designed for the staging environment and is triggered on Pull Requests into the staging branch. It includes steps to ensure code quality, build and push Docker images, create and configure necessary infrastructure, and deploy the application using Docker Compose. The cleanup process follows the A/B deployment concept.
3144

32-
File: [production_pipeline.yml](production_pipeline.yml)
45+
### Production - CI/CD Pipeline
3346

34-
Event: On **Pull Request** → any branch into **main**
47+
#### File: [production_pipeline.yml](production_pipeline.yml)
3548

36-
Jobs:
49+
**Event:** On Pull Request → any branch into master
3750

38-
- Build
39-
- Test
40-
- Deploy
51+
**Jobs:**
4152

42-
### Description:
53+
- Install dependencies (caches)
54+
- Run isort
55+
- Run black
56+
- Run flake8
57+
- Build images (caches)
58+
- Push images to Docker Hub
59+
- Create infrastructure
60+
- Configure infrastructure
61+
- Deploy application using Docker Compose
62+
- Clean up following the concept of A/B deploy
63+
64+
**Description:**
65+
The production pipeline is triggered on Pull Requests into the master branch, indicating changes are ready for deployment to the production environment. It follows a similar process to the staging pipeline but is specifically tailored for the production environment. The cleanup process adheres to the A/B deployment concept, ensuring a smooth transition between versions.

.github/workflows/ansible/deploy-app.yml renamed to .github/workflows/ansible/deploy_applications.yml

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
- name: "configuration play."
2+
- name: "Deploy applications"
33
hosts: web
44
user: ubuntu
55
become: true
@@ -9,11 +9,9 @@
99
- ansible_host_key_checking: false
1010
- ansible_stdout_callback: yaml
1111

12-
- mlflow_image_name: vectornguyen76/mlflow
13-
- mlflow_tag_name: latest
12+
- api_image_image: vectornguyen76/flask_template_image
13+
- api_image_tag: latest
1414

15-
- model_predictor_image_name: vectornguyen76/model_predictor
16-
- model_predictor_tag_name: latest
1715
pre_tasks:
1816
- name: "wait 600 seconds for target connection to become reachable/usable."
1917
wait_for_connection:
@@ -34,6 +32,7 @@
3432
- python3-pip
3533
- virtualenv
3634
- python3-setuptools
35+
- unzip
3736
state: latest
3837
update_cache: true
3938

.github/workflows/ansible/roles/deploy/tasks/main.yml

+9-21
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,23 @@
66

77
- name: "Copy compressed app folder"
88
copy:
9-
src: "artifact-app.tar.gz"
10-
dest: "/home/ubuntu/server/artifact-app.tar.gz"
9+
src: "artifact.zip"
10+
dest: "/home/ubuntu/server/artifact.zip"
1111

1212
- name: "Extract app"
13-
unarchive:
13+
ansible.builtin.unarchive:
1414
remote_src: yes
15-
src: "/home/ubuntu/server/artifact-app.tar.gz"
15+
src: "/home/ubuntu/server/artifact.zip"
1616
dest: "/home/ubuntu/server"
1717

18-
- name: Pull mlflow image
18+
- name: Pull api image
1919
community.docker.docker_image:
20-
name: "{{ mlflow_image_name }}"
21-
tag: "{{ mlflow_tag_name }}"
20+
name: "{{ api_image }}"
21+
tag: "{{ api_tag }}"
2222
source: pull
2323

24-
- name: Pull model_predictor image
25-
community.docker.docker_image:
26-
name: "{{ model_predictor_image_name }}"
27-
tag: "{{ model_predictor_tag_name }}"
28-
source: pull
29-
30-
- name: Run mlflow container
31-
become: True
32-
shell:
33-
chdir: /home/ubuntu/server
34-
cmd: "docker compose -f deployment/mlflow/docker-compose.yml up -d"
35-
36-
- name: Run model_predictor container
24+
- name: Run docker compose
3725
become: True
3826
shell:
3927
chdir: /home/ubuntu/server
40-
cmd: "bash deployment/deploy.sh deploy_run_predictor data/model_config/phase-1/prob-1/model-1.yml data/model_config/phase-1/prob-2/model-1.yml 5040"
28+
cmd: "docker compose up -d"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## Create Stack
2+
3+
aws cloudformation create-stack --stack-name server --template-body file://server.yml --parameters file://server-parameters.json --region us-east-1
4+
5+
## Delete Stack
6+
7+
aws cloudformation delete-stack --stack-name server --region us-east-1

.github/workflows/files/ec2-parameters.json renamed to .github/workflows/cloudformations/server-parameters.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
[
2+
{
3+
"ParameterKey": "EnvironmentName",
4+
"ParameterValue": "Search-Engine"
5+
},
26
{
37
"ParameterKey": "VpcCIDR",
48
"ParameterValue": "10.0.0.0/16"
@@ -12,7 +16,7 @@
1216
"ParameterValue": "t3.medium"
1317
},
1418
{
15-
"ParameterKey": "KeyPair",
19+
"ParameterKey": "KeyPairName",
1620
"ParameterValue": "my-keypair"
1721
},
1822
{

.github/workflows/files/ec2.yml renamed to .github/workflows/cloudformations/server.yml

+16-30
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
AWSTemplateFormatVersion: 2010-09-09
2-
Description: Creates EC2
2+
Description: Creates EC2 Server
33
Parameters:
4+
EnvironmentName:
5+
Description: An environment name that will be prefixed to resource names
6+
Type: String
7+
48
VpcCIDR:
59
Description: "VPC range"
610
Type: String
@@ -38,7 +42,7 @@ Resources:
3842
EnableDnsSupport: true
3943
Tags:
4044
- Key: Name
41-
Value: "Main VPC"
45+
Value: !Sub ${EnvironmentName} VPC
4246

4347
InternetGateway:
4448
Type: AWS::EC2::InternetGateway
@@ -56,12 +60,12 @@ Resources:
5660
PublicSubnet:
5761
Type: AWS::EC2::Subnet
5862
Properties:
59-
AvailabilityZone: "us-east-1a"
63+
AvailabilityZone: !Select [0, !GetAZs ""]
6064
VpcId: !Ref VPC
6165
CidrBlock: !Ref PublicSubnetCIDR
6266
Tags:
6367
- Key: Name
64-
Value: "Public Subnet AZ1"
68+
Value: !Sub ${EnvironmentName} Public Subnet
6569

6670
PublicRouteTable:
6771
Type: AWS::EC2::RouteTable
@@ -88,14 +92,18 @@ Resources:
8892
SecurityGroup:
8993
Type: AWS::EC2::SecurityGroup
9094
Properties:
91-
GroupName: mySecurityGroup
95+
GroupName: !Sub ${EnvironmentName}-SecurityGroup
9296
GroupDescription: Allow http to client host
9397
VpcId: !Ref VPC
9498
SecurityGroupIngress:
9599
- IpProtocol: tcp
96100
FromPort: 80
97101
ToPort: 80
98102
CidrIp: 0.0.0.0/0
103+
- IpProtocol: tcp
104+
FromPort: 443
105+
ToPort: 443
106+
CidrIp: 0.0.0.0/0
99107
- IpProtocol: tcp
100108
FromPort: 22
101109
ToPort: 22
@@ -108,18 +116,11 @@ Resources:
108116
FromPort: 3000
109117
ToPort: 3000
110118
CidrIp: 0.0.0.0/0
111-
- IpProtocol: tcp
112-
FromPort: 5040
113-
ToPort: 5040
114-
CidrIp: 0.0.0.0/0
115119
SecurityGroupEgress:
116120
- IpProtocol: "-1"
117121
FromPort: -1
118122
ToPort: -1
119123
CidrIp: 0.0.0.0/0
120-
Tags:
121-
- Key: Name
122-
Value: SecurityGroup
123124

124125
# Server EC2 Instance
125126
ServerInstance:
@@ -135,27 +136,12 @@ Resources:
135136
- !Ref SecurityGroup
136137
SubnetId: !Ref PublicSubnet
137138
BlockDeviceMappings:
138-
- DeviceName: "/dev/sdk"
139+
- DeviceName: "/dev/sda1"
139140
Ebs:
140-
VolumeSize: 20
141-
# UserData:
142-
# Fn::Base64: !Sub |
143-
# #!/bin/bash
144-
# sudo apt-get update -y
145-
# sudo apt-get install ca-certificates curl gnupg
146-
# sudo install -m 0755 -d /etc/apt/keyrings
147-
# curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
148-
# sudo chmod a+r /etc/apt/keyrings/docker.gpg
149-
# echo \
150-
# "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
151-
# "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
152-
# sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
153-
# sudo apt-get update -y
154-
# apt-get install -y docker-ce
155-
# docker run -p 80:8080 tomcat:8.0
141+
VolumeSize: 50
156142
Tags:
157143
- Key: Name
158-
Value: ServerInstance
144+
Value: !Sub ${EnvironmentName}-Instance
159145

160146
Outputs:
161147
EC2InstanceConnection:

.github/workflows/development_pipeline.yml

+16-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
name: development
22
on:
33
pull_request:
4-
branches: develop
4+
branches:
5+
- develop
56

67
push:
78
branches:
@@ -21,24 +22,26 @@ jobs:
2122
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
2223
steps:
2324
- name: Checkout Repository
24-
uses: actions/checkout@v2
25-
26-
- name: Cache Python dependencies
27-
uses: actions/cache@v2
28-
with:
29-
path: ~/.cache/pip
30-
key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }}
31-
restore-keys: |
32-
${{ runner.os }}-pip-
25+
uses: actions/checkout@v3
3326

34-
- name: Set up Python 3.9
35-
uses: actions/setup-python@v2
27+
- name: Set up Python
28+
uses: actions/setup-python@v4
3629
with:
37-
python-version: 3.9
30+
python-version: "3.9"
31+
cache: "pip"
3832

3933
- name: Install Python dependencies
4034
run: python -m pip install -r requirements.txt
4135

36+
- name: Run isort
37+
run: isort --check-only --profile=black .
38+
39+
- name: Run black
40+
run: black --check .
41+
42+
- name: Run flake8
43+
run: flake8 --ignore=E501,W503,F401 .
44+
4245
- name: Unit Tests and Integration Tests
4346
env:
4447
DATABASE_TEST_URL: postgresql://db_user:db_password@localhost/db_test

0 commit comments

Comments
 (0)