deploy.sh
· 3.9 KiB · Bash
Raw
#!/bin/bash
set -e
deploy() {
local release_path="$1"
cd "${release_path}"
if [ -f "${release_path}/.deploy/deploy.sh" ]; then
/bin/bash .deploy/deploy.sh
else
echo "Starting deployment in ${release_path}"
docker compose up -d
fi
}
stop() {
local release_path="$1"
cd "${release_path}"
if [ -f "${release_path}/.deploy/stop.sh" ]; then
/bin/bash .deploy/stop.sh
else
echo "Stopping deployment in ${release_path}"
docker compose down
fi
}
cleanup() {
local base_dir="$1"
local current_release="$2"
local release
local listing=()
local releases=()
cd "${base_dir}"
# Get listing of directories, sorted chronologically, newest first
mapfile -t listing < <(ls -t -d -- */)
# Filter out symlinks for current and previous, and what was just released
for release in "${listing[@]}"; do
case "${release}" in
current/|previous/|"${current_release}/")
true
;;
*)
releases+=( "${release}" )
;;
esac
done
# Keep only the most recent 5 releases
if [[ ${#releases[@]} -gt 5 ]]; then
echo "Removing old releases"
for release in "${releases[@]:5}"; do
echo "- Removing ${release}"
rm -rf "${base_dir:?}/${release}"
done
fi
}
if [ "${GITHUB_TOKEN}" == "" ] || [ "${GITHUB_USERNAME}" == "" ];then
echo "Missing github login information"
exit 1;
fi
if [ $# -lt 2 ];then
echo "Missing required arguments"
echo ""
echo "Usage:"
echo " ${0} <repo> <sha>"
echo ""
echo "where <repo> is a repository under github.com/${GITHUB_USERNAME}, and"
echo "<sha> is the commit to deploy."
exit 1;
fi
# Get just the repo name
REPO=${1#*/}
SHA=$2
BASEDIR="/var/web/${REPO}"
# This directory must exist; usually it will be a mount to S3-compatible
# object storage
SITE_CONFIG_DIR="/mnt/site-config"
PREVIOUS=
# Prepare deployment directory, if it does not exist
if [ ! -d "${BASEDIR}" ];then
echo "Creating deployment directory ${BASEDIR}"
mkdir -p "${BASEDIR}"
fi
# Memoize "current" directory as PREVIOUS, if it exists
if [ -d "${BASEDIR}/current" ];then
PREVIOUS=$(realpath "${BASEDIR}/current")
fi
# Prepare new release
echo "Preparing release directory based on commit ${SHA}"
DEPLOY_DIR="${BASEDIR}/${SHA}"
git clone --depth=1 --recurse-submodules "https://${GITHUB_USERNAME}:${GITHUB_TOKEN}@github.com/${GITHUB_USERNAME}/${REPO}.git" "${DEPLOY_DIR}"
cd "${DEPLOY_DIR}"
git checkout "${SHA}"
# Get env file
if [ -f "${DEPLOY_DIR}/env-version" ];then
echo "Found env-version file; fetching production env"
ENV_FILE="${SITE_CONFIG_DIR}/$(cat "${DEPLOY_DIR}/env-version")"
if [ ! -f "${ENV_FILE}" ];then
echo "FAILED - site config file specified in env-version not found"
exit 1;
fi
cp "${ENV_FILE}" "${DEPLOY_DIR}/.env"
fi
# Build
cd "${DEPLOY_DIR}"
if [ -f "${DEPLOY_DIR}/.deploy/build.sh" ]; then
/bin/bash .deploy/build.sh
else
if [ -f "${DEPLOY_DIR}/.deploy/pre-build.sh" ];then
# This can be used to do things like create volumes
echo "Building containers"
echo "- Executing pre-build step"
/bin/bash .deploy/pre-build.sh
fi
docker compose build
fi
# DEPLOY
# Stop previous
if [ "${PREVIOUS}" != "" ];then
stop "${PREVIOUS}"
fi
# Start new
echo "Starting deployment"
set +e
if ! deploy "${DEPLOY_DIR}"; then
echo "FAILED deploying ${SHA}; rolling back"
echo "- Stopping deployment"
stop "${DEPLOY_DIR}"
if [ "${PREVIOUS}" != "" ];then
echo "- Restarting previous deployment"
deploy "${PREVIOUS}"
fi
exit 1
fi
set -e
# SUCCESS
cd "${BASEDIR}"
if [ "${PREVIOUS}" != "" ];then
ln -fsn "$(basename "${PREVIOUS}")" previous
fi
ln -fsn "${SHA}" current
echo "SUCCESS deploying ${SHA}"
# CLEANUP
cleanup "${BASEDIR}" "${SHA}"
1 | #!/bin/bash |
2 | |
3 | set -e |
4 | |
5 | deploy() { |
6 | local release_path="$1" |
7 | |
8 | cd "${release_path}" |
9 | if [ -f "${release_path}/.deploy/deploy.sh" ]; then |
10 | /bin/bash .deploy/deploy.sh |
11 | else |
12 | echo "Starting deployment in ${release_path}" |
13 | docker compose up -d |
14 | fi |
15 | } |
16 | |
17 | stop() { |
18 | local release_path="$1" |
19 | |
20 | cd "${release_path}" |
21 | if [ -f "${release_path}/.deploy/stop.sh" ]; then |
22 | /bin/bash .deploy/stop.sh |
23 | else |
24 | echo "Stopping deployment in ${release_path}" |
25 | docker compose down |
26 | fi |
27 | } |
28 | |
29 | cleanup() { |
30 | local base_dir="$1" |
31 | local current_release="$2" |
32 | local release |
33 | local listing=() |
34 | local releases=() |
35 | |
36 | cd "${base_dir}" |
37 | |
38 | # Get listing of directories, sorted chronologically, newest first |
39 | mapfile -t listing < <(ls -t -d -- */) |
40 | |
41 | # Filter out symlinks for current and previous, and what was just released |
42 | for release in "${listing[@]}"; do |
43 | case "${release}" in |
44 | current/|previous/|"${current_release}/") |
45 | true |
46 | ;; |
47 | *) |
48 | releases+=( "${release}" ) |
49 | ;; |
50 | esac |
51 | done |
52 | |
53 | # Keep only the most recent 5 releases |
54 | if [[ ${#releases[@]} -gt 5 ]]; then |
55 | echo "Removing old releases" |
56 | for release in "${releases[@]:5}"; do |
57 | echo "- Removing ${release}" |
58 | rm -rf "${base_dir:?}/${release}" |
59 | done |
60 | fi |
61 | } |
62 | |
63 | if [ "${GITHUB_TOKEN}" == "" ] || [ "${GITHUB_USERNAME}" == "" ];then |
64 | echo "Missing github login information" |
65 | exit 1; |
66 | fi |
67 | |
68 | if [ $# -lt 2 ];then |
69 | echo "Missing required arguments" |
70 | echo "" |
71 | echo "Usage:" |
72 | echo " ${0} <repo> <sha>" |
73 | echo "" |
74 | echo "where <repo> is a repository under github.com/${GITHUB_USERNAME}, and" |
75 | echo "<sha> is the commit to deploy." |
76 | exit 1; |
77 | fi |
78 | |
79 | # Get just the repo name |
80 | REPO=${1#*/} |
81 | SHA=$2 |
82 | BASEDIR="/var/web/${REPO}" |
83 | # This directory must exist; usually it will be a mount to S3-compatible |
84 | # object storage |
85 | SITE_CONFIG_DIR="/mnt/site-config" |
86 | PREVIOUS= |
87 | |
88 | # Prepare deployment directory, if it does not exist |
89 | if [ ! -d "${BASEDIR}" ];then |
90 | echo "Creating deployment directory ${BASEDIR}" |
91 | mkdir -p "${BASEDIR}" |
92 | fi |
93 | |
94 | # Memoize "current" directory as PREVIOUS, if it exists |
95 | if [ -d "${BASEDIR}/current" ];then |
96 | PREVIOUS=$(realpath "${BASEDIR}/current") |
97 | fi |
98 | |
99 | # Prepare new release |
100 | echo "Preparing release directory based on commit ${SHA}" |
101 | DEPLOY_DIR="${BASEDIR}/${SHA}" |
102 | git clone --depth=1 --recurse-submodules "https://${GITHUB_USERNAME}:${GITHUB_TOKEN}@github.com/${GITHUB_USERNAME}/${REPO}.git" "${DEPLOY_DIR}" |
103 | cd "${DEPLOY_DIR}" |
104 | git checkout "${SHA}" |
105 | |
106 | # Get env file |
107 | if [ -f "${DEPLOY_DIR}/env-version" ];then |
108 | echo "Found env-version file; fetching production env" |
109 | ENV_FILE="${SITE_CONFIG_DIR}/$(cat "${DEPLOY_DIR}/env-version")" |
110 | |
111 | if [ ! -f "${ENV_FILE}" ];then |
112 | echo "FAILED - site config file specified in env-version not found" |
113 | exit 1; |
114 | fi |
115 | |
116 | cp "${ENV_FILE}" "${DEPLOY_DIR}/.env" |
117 | fi |
118 | |
119 | # Build |
120 | cd "${DEPLOY_DIR}" |
121 | |
122 | if [ -f "${DEPLOY_DIR}/.deploy/build.sh" ]; then |
123 | /bin/bash .deploy/build.sh |
124 | else |
125 | if [ -f "${DEPLOY_DIR}/.deploy/pre-build.sh" ];then |
126 | # This can be used to do things like create volumes |
127 | echo "Building containers" |
128 | echo "- Executing pre-build step" |
129 | /bin/bash .deploy/pre-build.sh |
130 | fi |
131 | docker compose build |
132 | fi |
133 | |
134 | # DEPLOY |
135 | |
136 | # Stop previous |
137 | if [ "${PREVIOUS}" != "" ];then |
138 | stop "${PREVIOUS}" |
139 | fi |
140 | |
141 | # Start new |
142 | echo "Starting deployment" |
143 | |
144 | set +e |
145 | if ! deploy "${DEPLOY_DIR}"; then |
146 | echo "FAILED deploying ${SHA}; rolling back" |
147 | echo "- Stopping deployment" |
148 | stop "${DEPLOY_DIR}" |
149 | |
150 | if [ "${PREVIOUS}" != "" ];then |
151 | echo "- Restarting previous deployment" |
152 | deploy "${PREVIOUS}" |
153 | fi |
154 | exit 1 |
155 | fi |
156 | set -e |
157 | |
158 | # SUCCESS |
159 | cd "${BASEDIR}" |
160 | if [ "${PREVIOUS}" != "" ];then |
161 | ln -fsn "$(basename "${PREVIOUS}")" previous |
162 | fi |
163 | ln -fsn "${SHA}" current |
164 | echo "SUCCESS deploying ${SHA}" |
165 | |
166 | # CLEANUP |
167 | cleanup "${BASEDIR}" "${SHA}" |