Easily Deploy JavaScript Apps to GoDaddy — Part 1: Deploy Script
I’ve been running an Angular app to help Peralta Community College students find classes for the past few years. Recently I decided to renovate it. It was fun but I ran into pain and misery when deploying my changes. The site is hosted on a GoDaddy shared server which I have FTP access to. Every time I made a change I would need to upload the file to the right directory, overwrite the existing file, and hope that I included all the changes. This was an error prone process that led to a great deal of frustration and false bugs. So, I wrote a script that will bundle, minify, and package the app then upload it to GoDaddy with just a Bash script and a few Gulp tasks.
In this article, I demonstrate the deploy script and in a future article I will share the Gulp tasks.
Summary & Code
Hosting your app on GoDaddy or another hosting provider that gives SSH access? Do you want to deploy your app without the hassle of dealing with SFTP/FTP? If you already have SSH access and a key on your computer, try this deploy script.
What You Need
- GoDaddy hosting with CPanel access or any hosting provided that gives you SSH access
- OpenSSH — comes with macOS and Linux; Windows needs some setup
SSH Access and Credentials
GoDaddy makes it easy to enable SSH access. Follow these steps.
Afterward, go to the Godaddy dashboard by exiting the cPanel and going back to the GoDaddy main site, then clicking Dashboard
on the top left.
On the right side of the dashboard, click Server
:
Then click Manage
under SSH access
. Copy the SSH username, host, and port number. You will need that information for the Bash script.
Create an SSH Key
You can also create your own SSH key using GoDaddy’s CPanel. I found this easier and faster than creating my own key locally and uploading it to the server.
From the dashboard click cPanel Admin
on the top right.
Then scroll down to the Security
tab and click SSH Access
.
Click Manage SSH Keys
, then click Generate a New Key
. Name it deploy_key
. Do not put a password on the key. Doing so will force you to enter the password every time you use the key, which complicates our script. For our use case, the default options are fine.
Add the SSH Key
Once the key is created, click View/Download
from the Manage SSH Keys
page. Then click download and save it here ~/.ssh
—this folder might be hidden, but it should be in your root folder, nonetheless.
You need to change the permissions of the SSH Key to be stricter. Run this command which restricts read and write access to the key: chmod 600 ~/.ssh/deploy_key
Create a Deploy Script
In your project directory, create a Bash script and name it deploy.sh
.
This script will do the following:
- connect to the server via SSH:
ssh -i ~/.ssh/deploy_key username@app.com <<EOF
- delete the existing deployment:
cd build && rm -rf *
- disconnect from SSH:
exit
- upload the new deployment using SFTP:
scp -r -i ~/.ssh/DEPLOY_KEY ./build/* USERNAME@APP.COM:/build/
All together the script looks like this.
#!/bin/sh
echo "deploying..."ssh -i ~/.ssh/deploy_key USERNAME@APP.COM <<EOF
cd build
rm -rf *
exit
EOFscp -r -i ~/.ssh/deploy_key ./build/* USERNAME@APP.COM:/DESTINATION/echo "deploy finished"
You will need to change the username and address, USERNAME@APP.COM
as well the destination folder, DESTINATION
. You can get the username and address from the SSH Setup
on GoDaddy’s dashboard, documented in the steps above. The destination folder needs to be the folder you are serving your app from currently. Back up this folder before running this script because it will delete everything in it!!! I didn’t run into issues, but you might have a few files and folders in the destination folder that you might want to keep.
Make the Deploy Script Executable
Run this command in the directory where the deploy script is: chmod +x deploy.sh
Your done! You can run script via terminal from the same directory: ./deploy.sh
What’s next?
I will be publishing an article on how to bundle and minify JS and CSS in the coming weeks. If I have the time, I will also share how to deploy different builds.