-
Notifications
You must be signed in to change notification settings - Fork 0
192 lines (179 loc) · 8.29 KB
/
cd.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
188
189
190
191
192
name: Deploy Stacks
on:
workflow_dispatch:
inputs:
image_tag:
description: "Version to deploy"
required: true
image_name:
description: "Name of the image to deploy"
required: true
replicas:
description: "Number of replicas"
required: true
# environment:
# description: "Environment to deploy"
# required: true
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Deploy the app
run: echo "Deploying the app..."
- name: Echo inputs
run: |
echo "Image tag = ${{ github.event.inputs.image_tag }}"
echo "Image name = ${{ github.event.inputs.image_name }}"
echo "Replicas = ${{ github.event.inputs.replicas }}"
- name: Copy file to Server
id: copy
uses: appleboy/[email protected]
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_PASSWORD }}
source: nest-stack.yml, parser-stack.yml, crawler-stack.yml, scale_nest.sh, scale_parser.sh, scale_crawler.sh
target: /home/ubuntu/
- name: Deploy to Server
id: deploy
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_PASSWORD }}
capture_stdout: 'true'
script: |
LOG_FILE="log_file.txt"
export IMAGE_TAG=${{ github.event.inputs.image_tag }}
export SERVICE_REPLICAS=${{ github.event.inputs.replicas }}
export IMAGE_NAME=${{ github.event.inputs.image_name }}
export DATABASE_URL=${{ secrets.DATABASE_URL }}
export REDIS_HOST=${{ secrets.REDIS_HOST }}
export REDIS_PORT=${{ secrets.REDIS_PORT }}
export REDIS_PASSWORD=${{ secrets.REDIS_PASSWORD }}
export EMAIL_USER=${{ secrets.EMAIL_USER }}
export EMAIL_PASS=${{ secrets.EMAIL_PASS }}
export JWT_SECRET=${{ secrets.JWT_SECRET }}
export SLACK_SECRET=${{ secrets.SLACK_DEPLOY_SECRET }}
export RABBITMQ_HOST=${{ secrets.RABBITMQ_HOST }}
export RABBITMQ_DEFAULT_USER=${{ secrets.RABBITMQ_DEFAULT_USER }}
export RABBITMQ_DEFAULT_PASS=${{ secrets.RABBITMQ_DEFAULT_PASS }}
export SWAGGER_USER=${{ secrets.SWAGGER_USER }}
export SWAGGER_PASSWORD=${{ secrets.SWAGGER_PASSWORD }}
export AWS_S3_ACCESS_KEY=${{ secrets.AWS_S3_ACCESS_KEY }}
export AWS_S3_SECRET_ACCESS_KEY=${{ secrets.AWS_S3_SECRET_ACCESS_KEY }}
export AWS_S3_BUCKET_NAME=${{ secrets.AWS_S3_BUCKET_NAME }}
export AWS_REGION=${{ secrets.AWS_REGION }}
export REDIS_URL=${{ secrets.REDIS_URL }}
export RABBITMQ_URL=${{ secrets.RABBITMQ_URL }}
export GOOGLE_AUTH_TYPE=${{ secrets.GOOGLE_AUTH_TYPE }}
export GOOGLE_AUTH_PROJECT_ID=${{ secrets.GOOGLE_AUTH_PROJECT_ID }}
export GOOGLE_AUTH_PRIVATE_KEY_ID=${{ secrets.GOOGLE_AUTH_PRIVATE_KEY_ID }}
export GOOGLE_AUTH_PRIVATE_KEY=${{ secrets.GOOGLE_AUTH_PRIVATE_KEY }}
export GOOGLE_AUTH_CLIENT_EMAIL=${{ secrets.GOOGLE_AUTH_CLIENT_EMAIL }}
export GOOGLE_AUTH_CLIENT_ID=${{ secrets.GOOGLE_AUTH_CLIENT_ID }}
export GOOGLE_FOLDER_ID=${{ secrets.GOOGLE_FOLDER_ID }}
export ARCHIVE_FOLDER_ID=${{ secrets.ARCHIVE_FOLDER_ID }}
if [ "$SERVICE_REPLICAS" == "1" ]; then
export SERVICE_REPLICAS="2"
fi
if [ "$IMAGE_NAME" == "suhach0523/techeerism-nest" ]; then
echo "Deploying Nest Stack..." | tee -a $LOG_FILE
docker stack deploy -c nest-stack.yml techeerzip --detach=true | tee -a $LOG_FILE
elif [ "$IMAGE_NAME" == "suhach0523/techeerism-parser" ]; then
echo "Deploying Parser..." | tee -a $LOG_FILE
docker stack deploy -c parser-stack.yml techeerzip --detach=true | tee -a $LOG_FILE
elif [ "$IMAGE_NAME" == "suhach0523/techeerism-crawler" ]; then
echo "Deploying Crawler..." | tee -a $LOG_FILE
docker stack deploy -c crawler-stack.yml techeerzip --detach=true | tee -a $LOG_FILE
else
echo "Invalid image name"
exit 1
fi
- name: Scale Down if Needed
if: ${{ github.event.inputs.replicas }} == "1"
id: scale_down
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_PASSWORD }}
capture_stdout: 'true'
script: |
LOG_FILE="log_file.txt"
export IMAGE_NAME=${{ github.event.inputs.image_name }}
if [ "$IMAGE_NAME" == "suhach0523/techeerism-nest" ]; then
echo "Scaling Down Nest" | tee -a $LOG_FILE
chmod +x /home/ubuntu/scale_nest.sh | tee -a $LOG_FILE
/home/ubuntu/scale_nest.sh | tee -a $LOG_FILE
elif [ "$IMAGE_NAME" == "suhach0523/techeerism-parser" ]; then
echo "Scaling Down Parser" | tee -a $LOG_FILE
chmod +x /home/ubuntu/scale_parser.sh | tee -a $LOG_FILE
/home/ubuntu/scale_parser.sh | tee -a $LOG_FILE
elif [ "$IMAGE_NAME" == "suhach0523/techeerism-crawler" ]; then
echo "Scaling Down Crawler" | tee -a $LOG_FILE
chmod +x /home/ubuntu/scale_crawler.sh | tee -a $LOG_FILE
/home/ubuntu/scale_crawler.sh | tee -a $LOG_FILE
else
echo "Invalid image name"
exit 1
fi
- name: Final Message To Slack
if: always()
env:
IMAGE_NAME: ${{ github.event.inputs.image_name }}
DOCKER_IMAGE_TAG: ${{ github.event.inputs.image_tag }}
SLACK_SECRET: ${{ secrets.SLACK_DEPLOY_SECRET }}
run: |
cat <<EOF > log_file.txt
${{ steps.scale_down.outputs.stdout }}
EOF
if grep -q "task: non-zero exit (1)" log_file.txt; then
echo "FAILED_STEP=Scale" >> $GITHUB_ENV
elif grep -q failure log_file.txt; then
echo "FAILED_STEP=Scale" >> $GITHUB_ENV
fi
if [ "$FAILED_STEP" != "" ]; then
echo "The step '$FAILED_STEP' failed."
curl -X POST https://techeer-029051b54345.herokuapp.com/api/v1/deploy/status \
-H "Content-Type: application/json" \
-d '{
"status": "failed",
"imageName": "'"$IMAGE_NAME"'",
"imageTag": "'"$DOCKER_IMAGE_TAG"'",
"failedStep": "'"$FAILED_STEP"'",
"logs": "'"$FAILED_LOGS"'",
"secret": "'"$SLACK_SECRET"'",
"jobURL": "'"$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID"'"
}'
else
echo "All steps succeeded!"
curl -X POST https://techeer-029051b54345.herokuapp.com/api/v1/deploy/status \
-H "Content-Type: application/json" \
-d '{
"status": "success",
"imageName": "'"$IMAGE_NAME"'",
"imageTag": "'"$DOCKER_IMAGE_TAG"'",
"failedStep": "'"$FAILED_STEP"'",
"logs": "'"$FAILED_LOGS"'",
"secret": "'"$SLACK_SECRET"'",
"jobURL": "'"$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID"'"
}'
fi
- name: Clean Up Files in Server
if: always()
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_PASSWORD }}
script: |
rm -f /home/ubuntu/nest-stack.yml
rm -f /home/ubuntu/parser-stack.yml
rm -f /home/ubuntu/crawler-stack.yml
rm -f /home/ubuntu/scale_nest.sh
rm -f /home/ubuntu/scale_parser.sh
rm -f /home/ubuntu/scale_crawler.sh
rm -f /home/ubuntu/log_file.txt