-
Notifications
You must be signed in to change notification settings - Fork 4
/
deploy.sh
executable file
·126 lines (98 loc) · 2.87 KB
/
deploy.sh
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
#!/bin/bash
# Script for merging any changes in `staging` to `main` branch.
# Since we're using CD, this triggers a deploy on the `production`
# environment as well.
# ---------------------------------------------------------------
# Make sure we fail and exit if an error is encountered
set -e
# Prints text with colour based on the message's importance level
print_text () {
if [ "$2" == "info" ]
then
COLOR="96m"
elif [ "$2" == "success" ]
then
COLOR="92m"
elif [ "$2" == "warning" ]
then
COLOR="93m"
elif [ "$2" == "danger" ]
then
COLOR="91m"
else
COLOR="0m"
fi
STARTCOLOR="\e[$COLOR"
ENDCOLOR="\e[0m"
printf "$STARTCOLOR%b$ENDCOLOR$ENDCHARACTER" "$1";
}
# Exits the script after printing a warning message
exit_with_message () {
print_text "$1\n" "warning"
exit 1
}
print_text "\nRDV-Insertion: Merge and deploy\n" "info"
print_text "------------------------\n\n"
# Gets the current branch by looking for `*` character
# - `s/` is for substitution
# - `/p` prints the result
current_branch=$(git branch | sed -n -e 's/^\* \(.*\)/\1/p')
# Make sure that we are on staging branch
print_text "Branch check: "
if [ $current_branch != "staging" ]
then
print_text "Failed\n" "danger"
exit_with_message "To be able to deploy you need to be on the 'staging' branch. Aborting."
else
print_text "OK\n" "success"
fi
print_text "\n"
# Get the git status
# - `porcelain` flag returns the output in an easy to parse format for scripts
st=$(git status --porcelain 2> /dev/null)
# Make sure that the status is clean
print_text "Git status check: "
if [[ "$st" != "" ]];
then
print_text "Failed\n" "danger"
exit_with_message "To be able to deploy, 'git status' should be clean. Aborting."
else
print_text "OK\n" "success"
fi
print_text "\n"
git pull -r origin staging
print_text "Pulling latest 'staging' changes: "
print_text "OK\n" "success"
print_text "\n"
# Pushing to main
git checkout main
print_text "Moving to 'main': "
print_text "OK\n" "success"
print_text "\n"
git pull origin main
print_text "Pulling latest 'main' changes: "
print_text "OK\n" "success"
print_text "\n"
git_log_output=$(git log main..staging)
echo "Voici ce qui va être déployé en production :"
echo "--------------------------------------------"
echo "$git_log_output"
echo "--------------------------------------------"
read -p "Souhaitez-vous toujours déployer ? (y/n): " deploy_choice
if [ "$deploy_choice" == "y" ]; then
git merge --ff-only staging
print_text "Merging 'staging' changes: "
print_text "OK\n" "success"
print_text "\n"
git push origin main
print_text "Pushing to 'main': "
print_text "OK\n" "success"
print_text "\n"
git checkout staging
print_text "All done!\n" "success"
print_text "Scalingo will take care of starting the deployment.\n" "info"
else
git checkout staging
echo "Déploiement annulé"
exit 1
fi