Full-Stack Deployment Using Docker & GitHub Actions

 

Introduction:
Deploying a Node.js application involves several crucial steps to ensure efficient and scalable performance. In this project, we aim to containerize a Node.js application and deploy it on an Amazon EC2 instance. The process begins by obtaining the Docker image source code from a Git repository. This source code is then built into a Docker image and pushed to the Docker Hub for centralized storage and accessibility. Once the Docker image is available on Docker Hub, we proceed to pull it from the repository and run it as a container on an Amazon EC2 instance. This approach leverages the power of Docker for containerization and the scalability of AWS EC2, ensuring a streamlined and reliable deployment process.

Prerequisites

  1. AWS Account: Make sure you have an AWS account. 

  2. EC2 Instance: Create an EC2 instance and ensure you have SSH access. 

  3. Docker: Install Docker on your local machine and the EC2 instance. 

  4. GitHub Repository: Have a Node.js application hosted on GitHub.


  • Plan

The project plan involves three main phases:

  • Node.js Application Setup and GitHub Integration:

    • Setting up a Node.js application, ensuring proper version control with Git, and creating a GitHub repository.

    • Customizing Docker images through modifying configuration files and pushing them to Docker Hub.

  • GitHub Actions and Docker Workflow:

    • Leveraging GitHub Actions to automate the Continuous Integration/Continuous Deployment (CI/CD) workflow.

    • Building Docker images, handling secrets securely, and automating the deployment process to Docker Hub.

  • AWS EC2 and Self-Hosted Runners:

    • Configuring an AWS EC2 instance for self-hosted runners, including security group settings.

    • Installing necessary dependencies like .NET and libicu on the EC2 instance and following GitHub instructions to set up and monitor the self-hosted runner.


  • Step-by-Step Guide 


Step 1: Set up your Node.js Application

  • GOOGLE_DRIVE_FILE_ID: 

https://drive.google.com/drive/folders/133-UwNB8RdjJE-_mFFm3Oenq9pq-rNzZ?usp=drive_link

  • REPO_URL: 

https://github.com/Rizwantech5/NODEJS-APP.git

  • Install Git: 

If you don’t have Git installed on your computer, you need to download and install it. You can find the installation instructions for various operating systems one the official Git website: 

  • Clone the Repository:

  1. Open a command prompt or Git Bash on your local machine.

  2. Use the git clone command to clone the repository:

git clone https://github.com/Rizwantech5/NODEJS-APP.git

  • Open the Git Repository:

  • Use command

cd NODEJS-APP

  • Navigate to the .github Directory:

    • Inside the repository, locate the .github directory.

  • Open the Desired File:

    • Within the .github directory, find the file you want to modify (e.g., a workflow file, possibly named “cicd-workflow.yml” or similar).

  • Edit the File:

    • Open the file for editing.

  • Locate Docker Image Name:

    • Scroll through the file until you find the lines containing the Docker image name. These lines might be around line numbers 16, 18, 33, and 37.

  • Make Changes:

    • Change the Docker image name on lines 16, 18, 33, and 37. Replace the existing name with your preferred name. For example, change from:

  • image: rizwanshaukat/nodejs-app

    • to

  • image: YourDockerUsername/YourCustomImageName

  • Save the Changes:

    • After making the changes, look for an option to save the file. This is usually a “Commit Changes” button.

  • Commit Changes:

    • Use command 

git add .github
git commit -m “first commit”

  • Create a New Repository on GitHub:

    • In your GitHub account, click on the “+” sign in the top right corner and choose “New repository.”

    • Fill in the necessary information for your new repository, including a name and optional description.

  • Initialize with a README (Optional):

    • You can choose to initialize the repository with a README file if you wish.

  • Create Repository:

    • Click the “Create repository” button to create the new repository on GitHub.

  • Push the Modified File:

    • Once the repository is created. Use commands

Git remote remove origin

git remote add origin <your_repository_url>
git branch -M main
git push -u origin main

  • Replace <repository_url> with the actual URL of your new GitHub repository.

Step 2: GitHub

  • Navigate to Your Repository:

    • Open your GitHub repository in a web browser.

  • Go to “Settings”:

    • In the top-right corner of your repository, click on the “Settings” tab.

  • Navigate to “Secrets”:

    • In the left sidebar, find and click on “Secrets and variables”

    • Click on “Action”

  • Add a New Secret:

    • Click on the “New repository secret” button.

  • Enter Name and Value:

    • Provide a name for your secret (e.g., DOCKER_USERNAME) and the corresponding sensitive value (e.g., your Docker Hub username).

    • Click “Add secret” to save it. 

    • Again click  “New repository secret” button. 

    • Provide a name for your secret (e.g., DOCKER_PASSWORD) and the corresponding sensitive value (e.g., your Docker Hub password).

    • Click “Add secret” to save it.

  • Repeat for Additional Secrets:

    • If you have more secrets to add, repeat the process by clicking “New repository secret” again.

  • Navigate to “Actions”:

    • In the left sidebar, find and click on “Actions.”

  • Click on “Runners“:

    • Click on the “New Self-hosted runners” button.

    • In the Runner image click on “Linux” 

  • Follow the Instructions for Your Operating System:

    • GitHub provides instructions for various operating systems. Choose the appropriate tab for your operating system and follow the provided instructions.

Step 3: Configure AWS EC2 Instance

  • Setting Up GitHub Runners on EC2:

  • Create an EC2 Instance:

    • Launch an EC2 instance on AWS with an operating system of your choice (e.g., Amazon Linux, Ubuntu).

  • Connect to Your EC2 Instance:

    • Use putty or SSH to connect to your EC2 instance. Replace your-key.pem and your-instance-ip with your actual key file and EC2 instance IP:

ssh -iyour-key.pemec2-user@yourinstanceip

  • Select the Security Group Associated with Your EC2 Instance:

    • In the list of security groups, find and select the security group associated with your EC2 instance.

  • Edit the Inbound Rules:

    • In the “Inbound” tab at the bottom, click the “Edit inbound rules” button.

  • Add a Rule for Port 5000, 80:

    • Click the “Add Rule” button.

    • Set the following values for the new rule:

      • Type: Custom TCP Rule

      • Protocol: TCP

      • Port Range: 5000

      • Port Range: 80

      • Source: Anywhere (0.0.0.0/0) or specify a more restricted source based on your requirements.

  • Save the Changes:

    • After adding the rule, click the “Save rules” button to apply the changes.

  • Install .Net and install libicu library Follow the link below

https://learn.microsoft.com/en-us/dotnet/core/install/linux-centos

  • Open a terminal and run the following commands:

sudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm

  • Install the SDK:

sudo yum install dotnet-sdk-7.0

  • Install the runtime

sudo yum install aspnetcore-runtime-7.0
sudo yum install dotnet-runtime-7.0

  • installing the libicu library

sudo yum install libicu

  • Follow GitHub Instructions:

    • Go to the GitHub repository and navigate “Setting”. In the setting navigate “Action”. Click on “Runner”. Click on “New Self-hosted runners”.

    • Follow the provided instructions in the “Download” and “Configure” sections.

  • Handle libcu Library Errors (if any):

    • If any errors related to libcu libraries occur during the setup, refer to the previous instructions for troubleshooting.

  • Configure Runner Options:

    • When prompted, enter the requested information:

Enter the name of the runner group to add this runner to: [Press Enter for Default] > Press Enter
Enter the name of the runner: [Press Enter for ip-172-31-84-194] > Type “aws-ec2”
Enter any additional labels (ex. label-1,label-2): [Press Enter to skip] > Type “aws-ec2”
Enter the name of the work folder: [Press Enter for _work] > Press Enter

  • Run the Last Command:

    • Execute the final command provided in the “Runner” section to complete the setup.

  • Monitor Runner Activity:

    • After setting up the GitHub Runner on your EC2 instance, go to the Actions tab within your GitHub repository.

    • Use this tab to monitor the runner’s activity, check workflow runs, and view detailed logs.

  • Clicking the public IP address of an EC2 instance

  • If you have set up a web service on your EC2 instance running on port 5000

http://<instance-publicip>:5000/rest/getAllUsers