-
Notifications
You must be signed in to change notification settings - Fork 0
133 lines (111 loc) · 5.34 KB
/
staging.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
name: Deploy on Staging
on:
push:
branches:
- development
jobs:
staging:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v2
- name: SSH and Cleanup
run: |
echo "Setting up Git Config"
mkdir -p ~/.ssh/
echo "Setting up SSH Keys"
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
ssh-keyscan -t rsa ${{ secrets.HOST }} >> ~/.ssh/known_hosts
echo "SSH Keys Setup Completed"
echo "SSHing into Staging Server"
ssh -o StrictHostKeyChecking=no ${{ secrets.USERNAME }}@${{ secrets.HOST }} -p ${{ secrets.PORT }} -i ~/.ssh/id_rsa <<'EOF'
# Change to the project directory
cd ~/ims-website
# Remove dangling containers
docker container prune -f
# Calculate Docker images size in bytes
IMAGES_SIZE=$(docker images -q --no-trunc | xargs -r docker inspect --format='{{.Size}}' | awk '{s+=$1} END {print s}')
# Convert 10GB to bytes (10 * 1024 * 1024 * 1024)
IMAGES_LIMIT=10737418240
# Check if image size exceeds the limit and delete oldest images if necessary
if [ "$IMAGES_SIZE" -gt "$IMAGES_LIMIT" ]; then
echo "Images size exceeded 10GB. Deleting oldest images until size is below limit..."
for img in $(docker images --format '{{.ID}} {{.Created}}' | sort -k2 | awk '{print $1}'); do
docker rmi $img
IMAGES_SIZE=$(docker images -q --no-trunc | xargs -r docker inspect --format='{{.Size}}' | awk '{s+=$1} END {print s}')
if [ "$IMAGES_SIZE" -le "$IMAGES_LIMIT" ]; then
break
fi
done
else
echo "Images size is within limit. Keeping 5 most recent images."
docker images --format "{{.Repository}}:{{.Tag}}\t{{.CreatedSince}}" | sort -k2 -r | awk 'NR>5 {print $1}' | xargs -I {} docker rmi {}
fi
# Remove dangling images
docker image prune -f
# Calculate Docker build cache size
CACHE_SIZE=$(docker system df -v | grep 'Build Cache' | awk '{print $3}' | tr -d 'G')
# Check if build cache size exceeds 5GB and prune if necessary
if [ "$CACHE_SIZE" -gt "5" ]; then
echo "Build cache size exceeded 5GB. Cleaning..."
docker builder prune -af
else
echo "Build cache size is within limit."
fi
# Continue with your existing deployment steps
git pull origin development
docker compose build
docker compose up -d
exit
EOF
echo "Deployment and Cleanup on Staging Server Completed"
- name: Gathering Email and Mail List
id: prepare_email
run: |
deployment_time=$(TZ=Asia/Kolkata date)
git fetch --unshallow
last_commit_hash=$(git rev-parse HEAD)
author_name=$(git log -1 --format='%an')
author_email=$(git log -1 --format='%ae')
branch_name=$(git rev-parse --abbrev-ref HEAD)
commit_message=$(git log -1 --pretty=%B)
files_changed=$(git diff-tree --no-commit-id --name-only -r $last_commit_hash)
template_path="./templates/email/notify_deployment-staging.html"
content=$(cat $template_path)
content=${content//DEPLOYMENT_TIME/$deployment_time}
content=${content//AUTHOR_NAME/$author_name}
content=${content//AUTHOR_EMAIL/$author_email}
content=${content//BRANCH_NAME/$branch_name}
content=${content//COMMIT_MESSAGE/$commit_message}
content=${content//FILES_CHANGED/$files_changed}
ssh -o StrictHostKeyChecking=no ${{ secrets.USERNAME }}@${{ secrets.HOST }} -p ${{ secrets.PORT }} -i ~/.ssh/id_rsa <<EOF
cd ~/ims-website
mkdir -p emails
echo "$content" > emails/notify_deployment-staging.html
exit
EOF
emailPath="email.html"
echo "::add-mask::$emailPath"
emailListPath="mail_list.csv"
echo "::add-mask::$emailListPath"
scp -P ${{ secrets.PORT }} -i ~/.ssh/id_rsa ${{ secrets.USERNAME }}@${{ secrets.HOST }}:~/ims-website/emails/notify_deployment-staging.html ./$emailPath
echo "::set-output name=emailPath::$emailPath"
scp -P ${{ secrets.PORT }} -i ~/.ssh/id_rsa ${{ secrets.USERNAME }}@${{ secrets.HOST }}:~/ims-website/mail_list.csv ./$emailListPath
mail_list=$(cat $emailListPath)
echo "::add-mask::$mail_list"
echo "::set-output name=mail_list::$mail_list"
echo "Email Prepared"
- name: Send Email
uses: dawidd6/action-send-mail@v2
with:
server_address: ${{ secrets.MAIL_SERVER_ADDRESS }}
server_port: ${{ secrets.MAIL_SERVER_PORT }}
username: ${{ secrets.MAIL_SERVER_UID }}
password: ${{ secrets.MAIL_SERVER_PASS }}
subject: IMS Corporate website Deployment on Staging Server | ${{ github.repository }}
body: file://${{ steps.prepare_email.outputs.emailPath }}
content_type: text/html
from: Github | Brahmware <${{ secrets.MAIL_SERVER_UID }}>
to: ${{ secrets.LEAD_NAME }} <${{ secrets.LEAD_EMAIL }}>
bcc: ${{ steps.prepare_email.outputs.mail_list }}