-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathpost-receive
More file actions
executable file
·79 lines (67 loc) · 2.89 KB
/
post-receive
File metadata and controls
executable file
·79 lines (67 loc) · 2.89 KB
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
#!/bin/bash
###
# CONFIG VARS
###
TARGET="/home/madslab/net.madslab/test" # the directory where the files need to be deployed
GIT_DIR="/home/madslab/data/test-deploy.git" # the current git bare repository path
BRANCH="production" # the branch to deploy (default to `production`)
# In case you want to restart your app after deploy, you need to fill:
RESTART=false # a boolean to enable restart (default to `false`)
API_KEY="" # your api key you can find in your *profile* section
ACCOUNT="" # the account name your site is associated to
PASSWORD="" # the account password your site is associated to
SITE_ID="000000" # the reference ID of your site you can find in your *sites* section
NPM_INSTALL_CHECK=false # a boolean to enable if your site is a _Node.js_ site, and if you want to automatically run `npm install` if your package.json changed since the last push (default to `false`)
# Function checking if the "package.json" file has been changed since last push
# MIT © Sindre Sorhus - sindresorhus.com / Adapted by J.M. Cléry for Alwaysdata
npm_install_check() {
# git hook to run a command after `git push` if a specified file was changed
changed_files="$(git diff --name-only HEAD^ HEAD)"
check_run() {
if [ $(echo "$changed_files" | grep "$1") ]; then
cd $TARGET
echo "'package.json' has been updated! Running 'npm install' automatically for you..."
eval "$2"
cd $GIT_DIR
fi
}
# Run `npm install` ONLY if "package.json" has changed
# check_run package.json "npm install --silent --no-audit --no-progress > /dev/null"
check_run package.json "npm install > /dev/null"
}
###
# DO NOT EDIT BELOW UNLESS YOU KNOW WHAT YOU'RE DOING
###
while read oldrev newrev ref; do
# only checking out the branch to deploy
if [[ $ref = refs/heads/$BRANCH ]]; then
echo "Ref '$ref' received."
if [ ! -d "$TARGET" ]; then
echo "'${TARGET}' dir is missing, creating it"
mkdir -p $TARGET
fi
echo "Deploying '${BRANCH}' branch to production"
git --work-tree=$TARGET --git-dir=$GIT_DIR checkout --force $BRANCH
# If it's a Node.js website and `npm install` needs to be trigerred
if [ $NPM_INSTALL_CHECK = true ]; then
npm_install_check
fi
if [ "$RESTART" = true ]; then
echo "Restarting your web server ..."
encoded_token=$(echo "$API_KEY account=$ACCOUNT:$PASSWORD" | base64 -w 0)
status=$(curl --location --request POST "https://api.alwaysdata.com/v1/site/${SITE_ID}/restart/" --header "Authorization: Basic $encoded_token" --write-out '%{http_code}' --silent)
if [ "$status" = 204 ]; then
echo "✅ Site successfully restarted!"
else
echo "❌ An error occured when restarting your site, try to restart it manually"
exit 1
fi
fi
exit 0
else
echo "🙄 You pushed '$ref' branch,"
echo "but you set '${BRANCH}' branch as deploy branch."
echo "Exiting without error."
exit 0
fi
done