Skip to content

Commit 2a44c60

Browse files
committed
Fix setup command coming before npm install
Setup command should be after npm packages are installed to avoid errors. Also added support for setup command in config, so that users don't have to run setup command everytime
1 parent fdd5b11 commit 2a44c60

2 files changed

Lines changed: 20 additions & 16 deletions

File tree

deploy.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ SERVERS="libly.liny.studio," # list of servers to deploy to. Comma separated. Th
77
TYPE="domain" # type of lookup (domain or ip)
88
SERVICES="" # Service names to restart after deployment. Best to pass this to on the command line. Defining it here will cause all services to be restarted which is a bad idea.
99
NODE_HOME="./api" # use relative path
10+
SETUP_COMMAND="cd ~/.Libly/current/client && npm run build" # Command to run after deployment. This is optional. If not defined, the script will not run any command after deployment.
1011
# This should contain the file name of the js file to modify. This file should be reserved for API HOSTNAME definition. It is optional.
1112
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.
1213
SSH_USER="ubuntu"
@@ -26,6 +27,7 @@ export SERVERS
2627
export SERVICES
2728
export TYPE
2829
export NODE_HOME
30+
export SETUP_COMMAND
2931
export JSHOST
3032
export SSH_USER
3133
export SSH_PORT

deploy.sh

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ for i in "$@"; do
7777
if [[ "$setup" != "full" && "$setup" != "only" ]]; then
7878
echo -e "${BRed}Error: Invalid setup option '$setup'. Please use 'full' or 'only'. ${Color_Off}"
7979
exit 1
80-
else
80+
elif [ -z "$SETUP_COMMAND" ]; then
8181
echo -e "\nYou included the setup option '$setup'. (Please be careful with this option, as it can cause issues if not used correctly.)"
8282
printf "${BCyan}How many commands do you want to run on the remote server(s)?: ${Color_Off}"
8383
read -r NUMBER
@@ -139,7 +139,7 @@ if [ -z "$PROJECT_NAME" ]; then
139139
echo -e "${Cyan}Please note that the project name must match the name of the folder in which the project is located.${Color_Off}"
140140
fi
141141

142-
echo -e "${BGreen}Welcome to the ${BBlue}${PROJECT_NAME}'s ${BGreen}Deployment!${Color_Off}\n"
142+
echo -e "\n${BGreen}Welcome to the ${BBlue}${PROJECT_NAME} project ${BGreen}deployment!${Color_Off}\n"
143143

144144
# Check if host command is available
145145
if ! command -v host &> /dev/null
@@ -162,7 +162,7 @@ if [ "$TYPE" == "domain" ]; then
162162
exit 1
163163
fi
164164
done
165-
echo -e "${Green}Done! All domain names are valid.${Color_Off}\n"
165+
echo -e "${Green}Done! All domain names are valid.${Color_Off}"
166166
else
167167
# Check for valid IP addresses
168168
for s in "${SERVERS[@]}"; do
@@ -252,8 +252,8 @@ if [ ! -d "$inputFile" ]; then
252252
fi
253253

254254
# Countdown
255-
echo -e "${BCyan}After this operation, the deployed version will be stored in the folder 'versions/'"
256-
echo -e "Deployment will commence in 10 seconds. Check if you entered correct information. \nPress ctrl c, to cancel if you made a mistake.${Color_Off}\n"
255+
echo -e "${Cyan}After this operation, the deployed version will be stored in the folder 'versions/'"
256+
echo -e "${Color_Off}Deployment will commence in 10 seconds. Check if you entered correct information. \nPress ctrl c, to cancel if you made a mistake.\n"
257257

258258
spinner="/|\\-/"
259259
for ((j=10; j>0; j--)); do
@@ -276,16 +276,16 @@ if [ ! -d "$inputFile/versions" ]; then
276276
fi
277277

278278
# Create and transfer archive, then deploy to servers
279-
tar --exclude="versions" --exclude=".git" -czf "$inputFile/versions/$file.tgz" -C "$inputFile" .
280-
echo -e "${Cyan}Archive created successfully. Beginning deployment...${Color_Off}\n"
279+
tar --exclude="versions" --exclude=".git" --exclude="node_modules" -czf "$inputFile/versions/$file.tgz" -C "$inputFile" .
280+
echo -e "\n${Cyan}Archive created successfully. Beginning deployment...${Color_Off}\n"
281281

282282
for i in "${SERVERS[@]}"; do
283283
(
284-
echo -e "${BBlue}Deploying on server: ${BYellow}\t$i\t...\n${Color_Off}"
284+
echo -e "${BBlue} Deploying on server: ${BYellow}\t$i\t...\n${Color_Off}"
285285
if [[ "$*" =~ (^|[[:space:]])--apt-update($|[[:space:]]) ]]; then
286286
echo -e "${Cyan}Running apt-get update...${Color_Off}"
287287
ssh -i "$SSH_KEY" "$SSH_USER@$i" "sudo apt-get update"
288-
echo -e "${Cyan}Server updated successfully.${Color_Off}"
288+
echo -e "\n${Cyan}Server updated successfully.${Color_Off}\n"
289289
fi
290290

291291
scp -i "$SSH_KEY" "$inputFile/versions/$file.tgz" "$SSH_USER@$i:/tmp/"
@@ -295,27 +295,29 @@ for i in "${SERVERS[@]}"; do
295295
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"
296296
echo -e "${Cyan}Here are the new contents of the releases directory:${Color_Off}\n"
297297

298+
define_api=$([ -n "$JSHOST" ] && echo "sed -i 's#undefined#\\\"https://$i/api\\\"#' $JSHOST" || echo "echo 'No JSHOST Defined. Skipping modification.'")
299+
298300
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"
299301
echo -e "\n${Green}Finished making a symbolic link for the new release. Deleting old releases...${Color_Off}\n"
300-
ssh -i "$SSH_KEY" "$SSH_USER@$i" "cd $DEPLOY_DIR/ && ls -ut | grep $PROJECT_NAME | tail -n +$((KEEP + 1)) | xargs rm -rf"
302+
ssh -i "$SSH_KEY" "$SSH_USER@$i" "cd $DEPLOY_DIR/ && ls -ut | grep $PROJECT_NAME | tail -n +$((KEEP + 1)) | xargs rm -rf && cd $DEPLOY_DIR/$file && $define_api"
301303
cd "$PROJECT_NAME/versions" && ls -ut | grep "$PROJECT_NAME" | tail -n +$((KEEP + 1)) | xargs rm -rf && cd - > /dev/null
302304

303305
echo -e "${Cyan}Deleted old releases, keeping the last $KEEP versions.${Color_Off}\n"
304-
if [ "$setup" == 'full' ]; then
305-
ssh -T -i "$SSH_KEY" "$SSH_USER@$i" "$SETUP_COMMAND"
306-
echo -e "${Cyan}Setup command executed successfully.${Color_Off}\n"
307-
fi
308306

309307
if [[ "$*" =~ (^|[[:space:]])--npm($|[[:space:]]) ]]; then
310308
echo -e "${Cyan}Running npm install on the latest version...${Color_Off}\n"
311309
ssh -i "$SSH_KEY" "$SSH_USER@$i" "cd $DEPLOY_DIR/$file/$NODE_HOME && if which npm >/dev/null 2>&1; then npm install && echo -e '${Cyan}Npm install was a success.${Color_Off}\n'; else echo -e '${Red}Npm not found. Please install it.\n${Color_Off}'; fi"
312310
fi
313311

314-
define_api=$([ -n "$JSHOST" ] && echo "sed -i 's#undefined#\\\"$i\\\"#' $JSHOST" || echo "echo 'No JSHOST Defined. Skipping modification.'")
312+
if [ "$setup" == 'full' ]; then
313+
ssh -T -i "$SSH_KEY" "$SSH_USER@$i" "$SETUP_COMMAND"
314+
echo -e "${Cyan}Setup command executed successfully.${Color_Off}\n"
315+
fi
315316

317+
echo -e "${Yellow}BEGIN Server MOTD: \n${Color_Off}"
316318
ssh -T -i "$SSH_KEY" "$SSH_USER@$i" <<EOF
319+
echo -e "\n${Yellow}END Server MOTD.${Color_Off}\n"
317320
cd $DEPLOY_DIR/$file/ && sudo rm -rf /tmp/$file.tgz
318-
$define_api
319321
if [ -n "${restart_services[@]}" ]; then
320322
if sudo systemctl status ${restart_services} >/dev/null 2>&1; then
321323
echo "\n${Green}✅ Services are running fine.${Color_off} Restarting: ${restart_services[@]}"

0 commit comments

Comments
 (0)