Repository navigation
Expand file tree
/
Copy pathdeploy.sh
More file actions
136 lines (110 loc) · 4.64 KB
/
Copy pathdeploy.sh
File metadata and controls
136 lines (110 loc) · 4.64 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
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
#!/bin/bash
# This script builds and deploys a Jekyll site to staging and live.
# If the latest commit is tagged and the tag starts with 'release',
# it deploys the existing build (in _site) to live (i.e. production).
# Otherwise it builds the site and deploys it to the staging server.
# Along with test.sh, it can also deploy previews to subdirectories,
# when you tag a commit with a tag starting with 'preview'.
#
# To set up: in your continuous-deployment service's settings,
# add these environment variables, using your server details:
# DEPLOY_METHOD_LIVE
# DEPLOY_METHOD_STAGING
# DEPLOY_METHOD_PREVIEWS
# DESTINATIONPATH_STAGING
# DESTINATIONPATH_LIVE
# DESTINATIONPATH_PREVIEWS
# If using SSH:
# SSH_STAGING
# SSH_LIVE
# SSH_PREVIEWS
# If using FTP:
# FTP_HOST_LIVE
# FTP_USER_LIVE
# FTP_PASSWORD_LIVE
# FTP_HOST_PREVIEWS
# FTP_USER_PREVIEWS
# FTP_PASSWORD_PREVIEWS
# FTP_HOST_STAGING
# FTP_USER_STAGING
# FTP_PASSWORD_STAGING
# Notes on each of these below.
# DEPLOY_METHOD...
# Set the method for each destination to SSH or LFTP.
# SSH is faster (with rsync) but requires higher-level server access.
# LFTP is the default, if a given DEPLOY_METHOD is not set.
# DESTINATIONPATH...
# e.g. public_html
# Very often, this will be the public_html folder on your server.
# Do not include a slash at the end. If SSH, and your user connects # directly to the destination folder, then leave this empty or do not set it.
# The _PREVIEWS path sets the directory where previews will be deployed. This is often public_html.
# SSH_PREVIEWS, e.g. username@example.com
# SSH_STAGING, e.g. username@staging.example.com
# SSH_LIVE, e.g. username@example.com
# These are only necessary for SSH deployment (uses rsync).
# FTP... variables are only necessary for FTP deployment (uses LFTP).
# Set these for each of your destination servers.
echo "Starting deployment..."
# Unshallow to get tags
if [ -f ${HOME}/clone/.git/shallow ];
then
git fetch --unshallow || true
fi
# Get the short hash of the latest commit
commit=$(git log --pretty=format:"%h" -n 1^ 2>&1)
# Get the tag on the last commit
tag=$(git describe --tags --exact-match $commit 2>&1)
# Get commit author
committer=$(git show --format="%aN <%aE>" $commit)
# Get the time this script was run
timestamp=$(date +%Y-%m-%d:%H-%M-%S)
# Set the path and name for the deploy-version file
versionFile="$HOME/cache/_site/deploy-version-$commit.txt"
# If the tag starts with 'release',
# then deploy the latest build to live (i.e. production).
# Otherwise, deploy to staging.
if [[ $tag* =~ ^release.*$ ]]
then
# Add the release tag name and new date to the deploy-version file
echo "Release tag: $tag" >> "$versionFile"
echo "Deployed to live: $timestamp" >> "$versionFile"
# Sync the cached built files to the live server
echo "Deploying $tag to live server..."
if [[ $DEPLOY_METHOD_LIVE == "SSH" ]]
then
rsync -a "$HOME/cache/_site/" "$SSH_LIVE":"$DESTINATIONPATH_LIVE"
else
lftp -c "open -u $FTP_USER_LIVE,$FTP_PASSWORD_LIVE $FTP_HOST_LIVE; set ssl:verify-certificate no; mirror -R '$HOME/cache/_site/' '$DESTINATIONPATH_LIVE'"
fi
echo "Live site deployed."
else
# Write to a deploy-version file, named for the commit
# and containing a timestamp.
echo "Last commit: $commit" >> "$versionFile"
echo "Last commit by: $committer" >> "$versionFile"
echo "Deployed to staging: $timestamp" >> "$versionFile"
# Sync the built files to the staging server.
# If it's a preview, deploy to the preview folder.
echo "Deploying $commit by $committer to staging server..."
if [[ $tag* =~ ^preview.*$ ]]
then
if [[ $DEPLOY_METHOD_PREVIEWS == 'SSH' ]]
then
rsync -a "$HOME/cache/_site/" "$SSH_PREVIEWS":"$DESTINATIONPATH_PREVIEWS/$tag"
else
# If your server can't handle a queue of FTP commands,
# you may need to remove `set ftp:sync-mode false;`
lftp -c "open -u $FTP_USER_PREVIEWS,$FTP_PASSWORD_PREVIEWS $FTP_HOST_PREVIEWS; set ssl:verify-certificate no; set ftp:sync-mode false; mirror -R '$HOME/cache/_site/' '$DESTINATIONPATH_PREVIEWS/$tag'"
fi
else
if [[ $DEPLOY_METHOD_STAGING == 'SSH' ]]
then
rsync -a "$HOME/cache/_site/" "$SSH_STAGING":"$DESTINATIONPATH_STAGING"
else
# If your server can't handle a queue of FTP commands,
# you may need to remove `set ftp:sync-mode false;`
lftp -c "open -u $FTP_USER_STAGING,$FTP_PASSWORD_STAGING $FTP_HOST_STAGING; set ssl:verify-certificate no; set ftp:sync-mode false; mirror -R '$HOME/cache/_site/' '$DESTINATIONPATH_STAGING'"
fi
fi
echo "Staging site deployed."
fi