Deploy your Node.js app in production and use BitBucket to automate your deployment.

Upload your code in BitBucket

There are several steps to be followed during your initial setup. So, let’s start with creating an SSH key to authenticate to your BitBucket account from your local machine.

Totally, there are two types of authentication called SSH and HTTPS.

This is the one time process and we don’t want to use HTTPS option to authenticate because we need to provide our credentials everytime whenever we pull, push or commit from your machine.

SSH Authentication mechanism goes below.

  1. Generating a new SSH key.
  2. Add your SSH key to the ssh-agent
  3. Add the SSH key to your GitHub account

Step 1: Generating a new SSH key.

Open Terminal.

$ cd ~/.ssh/ #To navigate to your default SSH key location.

Paste the text below, substituting in your BitBucket email address.

$ ssh-keygen -t rsa -b 4096 -C "[email protected]"

This creates a new ssh key, using the provided email as a label.

When you’re prompted to “Enter a file in which to save the key,” press Enter. This accepts the default file location.

Enter a file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]

At the prompt, press enter twice without entering any password as of now. That’s it, you have created your SSH key successfully called id_rsa.

Step 2: Add your SSH key to the ssh-agent

Start the ssh-agent in the background.

$ eval "$(ssh-agent -s)"

Add your SSH private key to the ssh-agent.

$ ssh-add -K ~/.ssh/id_rsa

Step 3: Adding a new SSH key to your GitHub account

Copy the SSH key to your clipboard.

$ pbcopy < ~/.ssh/id_rsa.pub
# Copies the contents of the id_rsa.pub file to your clipboard

If you don’t have pbcopy, you can use any of your editors to open this file and copy its whole content. I use sublime and if you want to open the above file in a sublime editor, issue the following command.

$ subl ~/.ssh/id_rsa.pub 
# Either issue this or above command. Don't execute both.

Let’s go to our BitBucket account and paste the key. Navigate to your BitBucket account settings and click the SSH Keys. (Refer the image).

BitBucket Account Settings Page.

Click Add Key and give the label name as per your wish. Let me name it as MY SSH KEY. And in the key box, paste your copied key and click Add key.

That’s it. Now, you are authenticated to perform actions in your BitBucket account from your terminal. Let’s push our code and later deploy it in EC2.

Deploy your code in BitBucket.

First, let’s create a repository in BitBucket, where we going to push our code.

Go to create repository in your BitBucket account or click this URL to navigate to the repo creation page. It looks like below.

Enter your app name called my-app and click Create repository.

Note: Do not include README as of now. In an image, I have included README file.

You will be navigated to your repo source page. Click the clone button which is available at the top right corner. Refer image for info.

And copy the highlighted text alone as given in the image. Since we are going to refer this repo to our local app, git clone is not needed.

Create a file called .gitignore in your root directory to ignore unnecessary files to get uploaded in our BitBucket repo. Paste the below content and save the file. Refer the completed repository source code for more information about structure.

# See http://help.github.com/ignore-files/ for more about ignoring files.
# dependencies
node_modules
# testing
coverage
# production
build
# misc
.DS_Store
.env
npm-debug.log

Now navigate to your app directory (cd /my-app) and execute the below commands one by one to link your local repo to your BitBucket repo and push your changes.

git init
git add --all
git commit -m "Initial Commit"
git remote add origin
[email protected]:Balasubramani/my-app.git
git pull origin master
git push -u origin master

Done! Your repository is now available in Bitbucket Server.

If you face any error like, Merge issues while issuing git pull origin master just type :q! and press enter to exit and discard the commit message. It is unnecessary at this stage.

read original article here