-
Notifications
You must be signed in to change notification settings - Fork 0
180 lines (168 loc) · 7.43 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
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
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/[email protected]
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_PASSWORD }}
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 }}
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/[email protected]
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_PASSWORD }}
script: |
LOG_FILE="log_file.txt"
export IMAGE_NAME=${{ github.event.inputs.image_name }}
if [ "$IMAGE_NAME" == "suhach0523/techeerism-nest" ]; then
ls
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: Capture Failure Details
if: always()
run: |
if [ "${{ steps.deploy.outcome }}" == "failure" ]; then
echo "FAILED_STEP=Deploy Step" >> $GITHUB_ENV
echo "FAILED_LOGS=$(cat log_file.txt)" >> $GITHUB_ENV
elif [ "${{ steps.copy.outcome }}" == "failure" ]; then
echo "FAILED_STEP=Copy Step" >> $GITHUB_ENV
echo "FAILED_LOGS=$(cat log_file.txt)" >> $GITHUB_ENV
fi
- name: Clean Up Files in Server
if: always()
uses: appleboy/[email protected]
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
- 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: |
if [ -f log_file.txt ]; then
FAILED_LOGS=$(cat log_file.txt)
else
FAILED_LOGS="No logs available"
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