-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto_deploy.sh
executable file
·112 lines (88 loc) · 2.94 KB
/
auto_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
#!/usr/bin/env bash
APP=$1
BRANCH=$2
REPO=$3
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
SCRIPT=`basename $0`
LOCK=/tmp/${SCRIPT}_${APP}.lock
[ -f $LOCK ] && exit
touch $LOCK
WORKFLOW_STATUS_SCRIPT=${SCRIPT_DIR}/get_latest_action_status.rb
source /home/${APP}/.profile &> /dev/null
source /home/${APP}/.${APP} &> /dev/null
LAST_TIMESTAMP=/home/$APP/.autodeploy
touch $LAST_TIMESTAMP
BUNDLE_LOG=/tmp/$$.bundle.log
DB_LOG=/tmp/$$.db.log
ASSETS_LOG=/tmp/$$.assets.log
STATUS=/tmp/$$.status.log
LOG=/tmp/${SCRIPT}_${APP}.log
echo > $LOG
cd /home/${APP}/${APP}
workflow_run=`$WORKFLOW_STATUS_SCRIPT $REPO $BRANCH`
conclusion=`echo $workflow_run | cut -d' ' -f2`
run_time=`echo $workflow_run | cut -d' ' -f1`
run_unixtime=`date "+%s" -d "$run_time"`
last_run=`cat $LAST_TIMESTAMP`
last_run=`expr $last_run + 0`
time_diff=`expr $run_unixtime - $last_run`
if [ $time_diff -lt 60 ] ; then
echo "Detected no new workflow run. Nothing to do." >> $LOG
[ -f $LOCK ] && rm $LOCK
exit
fi
if [ "$conclusion" == "success" ] ; then
echo "New workflow run was successful." >> $LOG
git_status=`git fetch 2>/dev/null && git status 2>/dev/null`
up_to_date=`git fetch 2>/dev/null && git status 2>/dev/null|grep 'is up to date'`
if [ "$up_to_date" != "" ]; then
echo "No new changes detected. Abort." >> $LOG
echo $run_unixtime > $LAST_TIMESTAMP
[ -f $LOCK ] && rm $LOCK
exit
fi
ok_to_pull=`git fetch 2>/dev/null && git status 2>/dev/null|grep 'can be fast-forwarded'`
if [ "$ok_to_pull" == "" ]; then
echo $git_status >> $LOG
echo "It does not look safe to pull new changes. Abort." >> $LOG
[ -f $LOCK ] && rm $LOCK
exit
fi
changes=`git pull origin $BRANCH 2>/dev/null|grep Already`
if [ "$changes" != "Already up to date." ]; then
echo $changes >> $LOG
echo "Deploying new changes ..." >> $LOG
RAILS_ENV=production bundle install &> $BUNDLE_LOG
bundle_failed=$?
RAILS_ENV=production DATABASE_URL=$DATABASE_URL bundle exec rake db:migrate &> $DB_LOG
db_failed=$?
RAILS_ENV=production DATABASE_URL=$DATABASE_URL bundle exec rake assets:precompile &> $ASSETS_LOG
assets_failed=$?
if [ $bundle_failed -eq 1 ]; then
cat $BUNDLE_LOG
fi
if [ $db_failed -eq 1 ]; then
cat $DB_LOG
fi
if [ $assets_failed -eq 1 ]; then
diff $DB_LOG $ASSETS_LOG
diff_errors=$?
if [ $diff_errors -eq 1 ]; then
cat $ASSETS_LOG
fi
fi
if [ $bundle_failed -eq 0 ] && [ $db_failed -eq 0 ] && [ $assets_failed -eq 0 ]; then
touch /tmp/auto_restart_${APP}.service
echo "New changes deployed successfully."
echo $run_unixtime > $LAST_TIMESTAMP
fi
fi
else
echo $workflow_run >> $LOG
echo "GHA Workflow did not complete successfully. Nothing to do." >> $LOG
fi
[ -f $BUNDLE_LOG ] && rm $BUNDLE_LOG
[ -f $DB_LOG ] && rm $DB_LOG
[ -f $ASSETS_LOG ] && rm $ASSETS_LOG
[ -f $STATUS ] && rm $STATUS
[ -f $LOCK ] && rm $LOCK