Skip to content

Commit 5b89c53

Browse files
committed
Remove ssh variable guessing and support ssh configuration
1 parent 72fbe91 commit 5b89c53

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

deploy.conf

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ SERVICES="" # Service names to restart after deployment. Best to pass this to on
99
NODE_HOME="./api" # use relative path
1010
# This should contain the file name of the js file to modify. This file should be reserved for API HOSTNAME definition. It is optional.
1111
JSHOST="web_client/static/scripts/API_HOST.js" # Inside the file, the definition should be like this: "export const API_HOST = undefined;" undefined will be replaced with the hostname of the server.
12+
SSH_USER="ubuntu"
13+
SSH_PORT="22" # Currently not used. The script will use the default port 22 as most servers are using this port.
14+
SSH_KEY="~/.ssh/id_rsa" # Path to your SSH private key
1215

1316
log_message() {
1417
local message="$1"
@@ -20,7 +23,14 @@ export PROJECT_NAME
2023
export DEPLOY_DIR
2124
export LOG_FILE
2225
export SERVERS
23-
export TYPE
2426
export SERVICES
27+
export TYPE
2528
export NODE_HOME
29+
export JSHOST
30+
export SSH_USER
31+
export SSH_PORT
32+
export SSH_KEY
33+
export SSH_USER
34+
export SSH_PORT
35+
export SSH_KEY
2636
export -f log_message

deploy.sh

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/bash
1+
#!/usr/bin/env bash
22
## ./deploy.sh
33
## Deploys a web app to given SERVERS
44
## Copyright (C) 2025 Buffer Park. All rights reserved.
@@ -75,7 +75,7 @@ for i in "$@"; do
7575
SETUP_COMMAND="$SETUP_COMMAND$command && "
7676
echo -e "${Green}Command $i added: $command${Color_Off}"
7777
done
78-
SETUP_COMMAND="${SETUP_COMMAND%&& }" # Remove trailing '&&'
78+
SETUP_SSH_USERCOMMAND="${SETUP_COMMAND%&& }" # Remove trailing '&&'
7979
echo -e "${Green}Setup command set to: $SETUP_COMMAND${Color_Off}"
8080
fi
8181
elif [[ $i =~ ^--project= ]]; then
@@ -172,7 +172,7 @@ if [ -n "$ROLLBACK" ]; then
172172
fi
173173
for i in "${SERVERS[@]}"; do
174174
echo -e "${BBlue}Rolling back on server: ${BYellow}\t$i\t...\n${Color_Off}"
175-
ssh ubuntu@"$i" "bash -c '
175+
ssh -i "$SSH_KEY" "$SSH_USER@$i" "bash -c '
176176
cd $DEPLOY_DIR/
177177
rm -rf ../current
178178
f=\$(ls -ut | grep "$PROJECT_NAME" | head -n +$((ROLLBACK)) | tail -n +$((ROLLBACK)))
@@ -195,7 +195,7 @@ if [ "$setup" == "only" ]; then
195195
echo -e "${Green}Skipping deployment and going for server setup. This will fail if there is no deployed version.${Color_Off}"
196196
echo -e "${BBlue}Running Setup command: ${Cyan} $SETUP_COMMAND ...${Color_Off}\n"
197197
runcommand=$([[ "$*" =~ (^|[[:space:]])--npm($|[[:space:]]) ]] && echo "$SETUP_COMMAND && cd $DEPLOY_DIR/../current/$NODE_HOME && npm install" || echo "$SETUP_COMMAND")
198-
ssh ubuntu@"$i" "bash -c '
198+
ssh -i "$SSH_KEY" "$SSH_USER@$i" "bash -c '
199199
$runcommand
200200
if sudo systemctl status $restart_services >/dev/null 2>&1; then
201201
echo \"✅ Services are running fine. Restarting: $restart_services\" && sudo systemctl restart $restart_services
@@ -251,37 +251,37 @@ for i in "${SERVERS[@]}"; do
251251
echo -e "${BBlue}Deploying on server: ${BYellow}\t$i\t...\n${Color_Off}"
252252
if [[ "$*" =~ (^|[[:space:]])--apt-update($|[[:space:]]) ]]; then
253253
echo -e "${Cyan}Running apt-get update...${Color_Off}"
254-
ssh ubuntu@"$i" "sudo apt-get update"
254+
ssh -i "$SSH_KEY" "$SSH_USER@$i" "sudo apt-get update"
255255
echo -e "${Cyan}Server updated successfully.${Color_Off}"
256256
fi
257257

258-
scp "$inputFile/versions/$file.tgz" "ubuntu@$i:/tmp/"
258+
scp -i "$SSH_KEY" "$inputFile/versions/$file.tgz" "$SSH_USER@$i:/tmp/"
259259
echo ""
260260
echo -e "${Green}Copied archive file to server:${Color_Off}\t$i in directory:\t/tmp/\n"
261261
echo -e "${Blue}Extracting archive to:${Color_Off}\t$DEPLOY_DIR/ ..."
262-
ssh ubuntu@"$i" "mkdir -p $DEPLOY_DIR/new && tar -xzf /tmp/$file.tgz -C $DEPLOY_DIR/new && mv $DEPLOY_DIR/new $DEPLOY_DIR/$file"
262+
ssh -i "$SSH_KEY" "$SSH_USER@$i" "mkdir -p $DEPLOY_DIR/new && tar -xzf /tmp/$file.tgz -C $DEPLOY_DIR/new && mv $DEPLOY_DIR/new $DEPLOY_DIR/$file"
263263
echo -e "${Cyan}Here are the new contents of the releases directory:${Color_Off}\n"
264264

265-
ssh ubuntu@"$i" "sudo rm -rf $DEPLOY_DIR/new/ && ls $DEPLOY_DIR/ | sed 's/^/\t\t\t/' && sudo rm -rf $DEPLOY_DIR/../current && ln -s $DEPLOY_DIR/$file $DEPLOY_DIR/../current"
265+
ssh -i "$SSH_KEY" "$SSH_USER@$i" "sudo rm -rf $DEPLOY_DIR/new/ && ls $DEPLOY_DIR/ | sed 's/^/\t\t\t/' && sudo rm -rf $DEPLOY_DIR/../current && ln -s $DEPLOY_DIR/$file $DEPLOY_DIR/../current"
266266
echo -e "\n${Green}Finished making a symbolic link for the new release. Deleting old releases...${Color_Off}\n"
267-
ssh ubuntu@"$i" "cd $DEPLOY_DIR/ && ls -ut | grep "$PROJECT_NAME" | tail -n +$((KEEP + 1)) | xargs rm -rf"
267+
ssh -i "$SSH_KEY" "$SSH_USER@$i" "cd $DEPLOY_DIR/ && ls -ut | grep "$PROJECT_NAME" | tail -n +$((KEEP + 1)) | xargs rm -rf"
268268
ls "$PROJECT_NAME/versions" -ut | grep "$PROJECT_NAME" | tail -n +$((KEEP + 1)) | xargs rm -rf
269269

270270
echo -e "${Cyan}Deleted old releases, keeping the last $KEEP versions.${Color_Off}\n"
271271
if [ "$setup" == 'full' ]; then
272272
echo -e "${Cyan}Running Setup command...${Black} ${SETUP_COMMAND}${Color_Off}"
273-
ssh -tt ubuntu@"$i" "$SETUP_COMMAND"
273+
ssh -i "$SSH_KEY" -tt "$SSH_USER@$i" "$SETUP_COMMAND"
274274
echo -e "${Cyan}Setup completed successfully.${Color_Off}\n"
275275
fi
276276

277277
if [[ "$*" =~ (^|[[:space:]])--npm($|[[:space:]]) ]]; then
278278
echo -e "${Cyan}Running npm install on the latest version...${Color_Off}"
279-
ssh ubuntu@"$i" "cd $DEPLOY_DIR/$file/$NODE_HOME && npm install"
279+
ssh -i "$SSH_KEY" "$SSH_USER@$i" "cd $DEPLOY_DIR/$file/$NODE_HOME && npm install"
280280
echo -e "${Cyan}Npm install was a success.${Color_Off}\n"
281281
fi
282282

283283
define_api=$([ -n "$JSHOST" ] && echo "sed -i 's#undefined#\\\"$i\\\"#' $JSHOST" || echo "echo 'No JSHOST Defined. Skipping modification.'")
284-
ssh ubuntu@"libly.liny.studio" bash <<EOF
284+
ssh -i "$SSH_KEY" "$SSH_USER@$i" bash <<EOF
285285
cd $DEPLOY_DIR/$file/ && sudo rm -rf /tmp/$file.tgz
286286
$define_api
287287
if sudo systemctl status $restart_services >/dev/null 2>&1; then

0 commit comments

Comments
 (0)