-
Notifications
You must be signed in to change notification settings - Fork 0
194 lines (168 loc) · 7.96 KB
/
main.yml
File metadata and controls
194 lines (168 loc) · 7.96 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
name: FastAPI Docker Build and Push
on:
push:
branches:
- main
env:
AWS_REGION : "us-east-1"
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
attestations: write
steps:
- name: set lower case owner name
run: |
echo "OWNER_LC=${OWNER,,}" >>${GITHUB_ENV}
env:
OWNER: '${{ github.repository_owner }}'
- uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
registry: ghcr.io/${{ env.OWNER_LC }}
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
with:
images: ghcr.io/${{ env.OWNER_LC }}/get-client-ip-python
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
push: true
tags: ghcr.io/${{ env.OWNER_LC }}/get-client-ip-python:latest
env:
DOCKER_CLI_EXPERIMENTAL: enabled
DOCKER_BUILDKIT: 1
deploy:
needs: build
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: configure aws credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::489172774606:role/gh-actions-role
role-session-name: pythonrolesession
aws-region: ${{ env.AWS_REGION }}
- name: Check if launch template exists
id: check-lt
run: |
LAUNCH_TEMPLATE_NAME="python-microservice-lt"
#TODO: find graceful solution in the future
if aws ec2 describe-launch-templates --launch-template-names python-microservice-lt > lt.json 2>&1; then
if [ $(jq '.LaunchTemplates | length' lt.json) -eq 0 ]; then
echo "existslt=false" >> $GITHUB_ENV
else
echo "existslt=true" >> $GITHUB_ENV
fi
else
echo "existslt=false" >> $GITHUB_ENV
fi
- name: create launch template
if: env.existslt == 'false'
run: |
aws ec2 create-launch-template \
--launch-template-name python-microservice-lt \
--version-description python-microservice-for-Auto-Scaling-Version-1 \
--launch-template-data '{"NetworkInterfaces":[{"DeviceIndex":0,"AssociatePublicIpAddress":true,"Groups":["sg-0abe937fd14a5c9f8"],"DeleteOnTermination":true}],"ImageId":"ami-0bb84b8ffd87024d8","InstanceType":"t2.nano","KeyName":"proj_2-main-key","UserData":"IyEvYmluL2Jhc2gKc3VkbyB5dW0gdXBkYXRlIC15CnN1ZG8geXVtIGluc3RhbGwgLXkgZG9ja2VyCnN1ZG8gc2VydmljZSBkb2NrZXIgc3RhcnQKc3VkbyB1c2VybW9kIC1hIC1HIGRvY2tlciBlYzItdXNlcgpzdWRvIGRvY2tlciBwdWxsIGdoY3IuaW8vcHJheGlzYXBwZGV2L2dldC1jbGllbnQtaXAtcHl0aG9uOmxhdGVzdApzdWRvIGRvY2tlciBydW4gLWQgLXAgODA6ODA4MCBnaGNyLmlvL3ByYXhpc2FwcGRldi9nZXQtY2xpZW50LWlwLXB5dGhvbjpsYXRlc3Q="}' --region us-east-1
- name: Get Latest Launch Template Version
id: get-lt-version
if: env.existslt == 'true'
run: |
LAUNCH_TEMPLATE_NAME="python-microservice-lt"
echo "LAUNCH_TEMPLATE_NAME=$LAUNCH_TEMPLATE_NAME" >> $GITHUB_ENV
LATEST_VERSION=$(aws ec2 describe-launch-templates --launch-template-names $LAUNCH_TEMPLATE_NAME --query 'LaunchTemplates[0].LatestVersionNumber' --output text)
echo "LATEST_VERSION=$LATEST_VERSION" >> $GITHUB_ENV
shell: bash
- name: Increment Latest Version
if: env.existslt == 'true'
run: |
NEW_VERSION=$((LATEST_VERSION + 1))
echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_ENV
shell: bash
- name: Update launch template
if: env.existslt == 'true'
run: |
aws ec2 create-launch-template-version \
--launch-template-name python-microservice-lt \
--version-description python-microservice-for-Auto-Scaling-Version-${{ env.NEW_VERSION }} \
--source-version 1 \
--launch-template-data '{"NetworkInterfaces":[{"DeviceIndex":0,"AssociatePublicIpAddress":true,"Groups":["sg-0abe937fd14a5c9f8"],"DeleteOnTermination":true}],"ImageId":"ami-0bb84b8ffd87024d8","InstanceType":"t2.nano","KeyName":"proj_2-main-key","UserData":"IyEvYmluL2Jhc2gKc3VkbyB5dW0gdXBkYXRlIC15CnN1ZG8geXVtIGluc3RhbGwgLXkgZG9ja2VyCnN1ZG8gc2VydmljZSBkb2NrZXIgc3RhcnQKc3VkbyB1c2VybW9kIC1hIC1HIGRvY2tlciBlYzItdXNlcgpzdWRvIGRvY2tlciBwdWxsIGdoY3IuaW8vcHJheGlzYXBwZGV2L2dldC1jbGllbnQtaXAtcHl0aG9uOmxhdGVzdApzdWRvIGRvY2tlciBydW4gLWQgLXAgODA6ODA4MCBnaGNyLmlvL3ByYXhpc2FwcGRldi9nZXQtY2xpZW50LWlwLXB5dGhvbjpsYXRlc3Q="}' --region us-east-1
- name: Check if Auto Scaling group exists
id: check-asg
run: |
if aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names python-microservice-asg > asg.json 2>&1; then
if [ $(jq '.AutoScalingGroups | length' asg.json) -eq 0 ]; then
echo "existsasg=false" >> $GITHUB_ENV
else
echo "existsasg=true" >> $GITHUB_ENV
fi
else
echo "existslt=false" >> $GITHUB_ENV
fi
- name: Create auto-scaling group
if: env.existsasg == 'false'
run: |
aws autoscaling create-auto-scaling-group \
--auto-scaling-group-name python-microservice-asg \
--launch-template LaunchTemplateName=python-microservice-lt \
--target-group-arns arn:aws:elasticloadbalancing:us-east-1:489172774606:targetgroup/AFTERHOURS-API-GATEWAY/0b261e6bbaaba820 \
--health-check-type ELB \
--health-check-grace-period 300 \
--availability-zones us-east-1a us-east-1b us-east-1c \
--desired-capacity 0 \
--max-size 0 \
--min-size 0 \
- name: Update autoscaling group
run: |
aws autoscaling update-auto-scaling-group \
--auto-scaling-group-name python-microservice-asg \
--launch-template LaunchTemplateName="python-microservice-lt",Version='$Latest'
- name: Create target tracking scaling policy
run: |
aws autoscaling put-scaling-policy \
--auto-scaling-group-name python-microservice-asg \
--policy-name TargetTrackingPolicy \
--policy-type TargetTrackingScaling \
--target-tracking-configuration '{
"PredefinedMetricSpecification": {
"PredefinedMetricType": "ASGAverageCPUUtilization"
},
"TargetValue": 50.0
}'
- name: Check for existing instance refresh
id: check-refresh
run: |
AUTO_SCALING_GROUP_NAME="python-microservice-asg"
# Describe the instance refreshes and filter for in-progress ones
REFRESH_STATUS=$(aws autoscaling describe-instance-refreshes --auto-scaling-group-name $AUTO_SCALING_GROUP_NAME --query 'InstanceRefreshes[?Status==`InProgress`].Status' --output text)
if [ "$REFRESH_STATUS" == "InProgress" ]; then
echo "refresh_in_progress=true" >> $GITHUB_ENV
else
echo "refresh_in_progress=false" >> $GITHUB_ENV
fi
shell: bash
- name: Trigger instance refresh
if: env.refresh_in_progress == 'false'
run: |
AUTO_SCALING_GROUP_NAME="python-microservice-asg"
# Start a new instance refresh
aws autoscaling start-instance-refresh \
--auto-scaling-group-name $AUTO_SCALING_GROUP_NAME \
--preferences '{"MinHealthyPercentage": 90, "InstanceWarmup": 300}'
shell: bash