GitLab vs GitHub: Know your Go to Platform ( GitHub vs GitLab)

Well, nowadays mostly people in IT need some form of coding and automation skills to manage Infrastructure such as Amazon cloud deployments, Azure deployment or applications deployments on datacenter. Yes you heard it right, certainly you need a way to store the code.

The code cannot be kept on your local machines as many people simultaneously work and to efficiently manage your code , versions of code you need a proper way. To achieve this we have two best products in market that is GitLab and GitHub.

This side shanky and in this yet another GitLab vs GitHub: Know your Go to Platform tutorial, I am going to give you in depth overview and detailed knowledge about both products GitLab and GitHub. Rest I leave up to you to consider which one suits you better after going through this entire tutorial.

Lets dive in right now.

Prerequisites

This guide will have some practical’s in between so make sure you to have following things before in hand.

  • GitLab account and GitHub Account.

Table of Content

  1. Prerequisites
  2. What is GitLab?
  3. How to access GitLab?
  4. GitLab installation
  5. What is GitLab runner and GitLab Runner Installation
  6. Configure GitLab Runner Configuration
  7. GitLab Runner Docker
  8. GitLab Runner SSH
  9. GitLab Runner Commands
  10. GitLab Namespaces
  11. GitLab Access Management
  12. Configure LDAP in GitLab
  13. Configure AWS Cognito in GitLab
  14. Authenticating with GitLab from Git
  15. Git Credential Manager
  16. Create a personal access token in GitLab
  17. Clone GitLab repository using personal access token 
  18. Creating a Project Label in GitLab
  19. Git Commit in GitLab
  20. Creating first GitLab CI/CD pipeline
  21. Configure GitLab Pipelines: Declaring Variables , Rules in GitLab Jobs
  22. Declaring Services in GitLab Jobs
  23. GitLab code Dependency scanning
  24. GitLab code scanning in containers
  25. GitLab Environments and Deployments
  26. Authenticate GitLab with AWS
  27. What is GitHub?
  28. Getting started with your GitHub account
  29. How to access GitHub?
  30. What is GitHub Desktop
  31. GitHub Webhooks
  32. Authenticating with GitHub from Git 
  33. GitHub HTTPS Authentication with GitHub cli
  34. GitHub HTTPS Authentication with Git Credential Manager
  35. Generating Personal Access Token in GitHub
  36. GitHub code scanning
  37. GitHub code scanning in containers
  38. Advance GitHub code scanning
  39. GitHub Status
  40. GitHub Pricing
  41. Conclusion

What is GitLab?

GitLab is a code hosting platform for collaboration and version control. GitLab platform allows you to deliver software faster and efficiently, while strengthening security and compliance.

How to access GitLab?

You can access GitLab using various option as shown below.

  • GitLab SaaS: You can simply use this by signing in.
  • Install GitLab Self Managed your own instance.

GitLab installation

You can install GitLab on several cloud providers, or use one of the following methods.

  • You can install GitLab using Linux Package, Helm chart, Docker (The GitLab Docker images are monolithic images of GitLab running all the necessary services in a single container.), Source.
  • You can install GitLab on AWS, GCP, Azure.
# Ubuntu/Debian
sudo apt update && sudo apt install gitlab-ee

# RHEL/CentOS 6 and 7
sudo yum install gitlab-ee

# RHEL/CentOS 8
sudo dnf install gitlab-ee

# SUSE
sudo zypper install gitlab-ee

Note: GitLab is developed for Linux-based operating systems. It does not run on Microsoft Windows.

What is GitLab runner and GitLab Runner Installation

GitLab Runners are the agents that are responsible for all of the running CI/CD pipelines and GitLab CI/CD features.

GitLab Runner can be installed and used on GNU/Linux, macOS, FreeBSD, and Windows. You can install it in a container, by downloading a binary manually or by using a repository for rpm/deb packages.

To install GitLab on ubuntu and Linux Operating system first you need to add repo and use apt-get command.

  • Add the official GitLab repository:
curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh" | sudo bash
curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh" | sudo bash
  • Now run the below commands.
sudo apt-get install gitlab-runner
sudo yum install gitlab-runner

Configure GitLab Runner Configuration

To configure more settings in GitLab runner you can modify (config.tml) file on the below locations.

/etc/gitlab-runner/  # This is for unix machines
./ # on other systems

GitLab Runner Docker

  • Lets checkout the configuration for where we need to specify the volume for a docker container.
[runners.docker]
  host = ""
  hostname = ""
  tls_cert_path = "/Users/ayufan/.boot2docker/certs"
  image = "ruby:2.7"
  privileged = false
  disable_cache = true
  volumes = ["/path/to/volume/in/container"]

GitLab Runner SSH

  • Configure SSH connection with GitLab runner.
[runners.ssh]
  host = "my-production-server"
  port = "22"
  user = "root"
  password = "production-server-password"
  identity_file = ""

GitLab Runner Commands

GitLab runner command allows you to register, manage and run your builds. Lets checkout quickly few of important commands that one should know to perform actions on GitLab commands.

  • To take help from GitLab runner commands.
gitlab-runner --help
  • Run Gitlab runner with debug mode on.
gitlab-runner run --debug
  • To register GitLab runner.
gitlab-runner register --name my-runner --url http://gitlab.example.com --registration-token my-registration-token
  • To Run GitLab runner as service.
gitlab -runner install --service myservice --syslog true --working-directory ~/mygitlabrunner --user root  --password ***
gitlab-runner uninstall  # This command stops and uninstalls GitLab Runner from being run as an service.
gitlab-runner start        # This command starts the GitLab Runner service.
gitlab-runner stop       # This command stops the GitLab Runner service.
gitlab-runner restart   # This command stops and then starts the GitLab Runner service.
gitlab-runner status    # This command prints the status of the GitLab Runner service

GitLab Namespaces

If you need to organize your projects accordingly then you can create a namespace. Different projects with different requirements or configurations can be kept within different namespaces.

The namespace for user account is:

https://gitlab.com/<user&gt;

GitLab Access Management

GitLab contains various projects and within projects members are created which are either user or groups. Each member gets role assigned to it so that they can access the projects accordingly. User can be part of single group or multiple groups.

When your project belongs to a group, project members inherit their role from the group. With group access tokens, you can use a single token to, Perform actions for groups, manage the projects within the group.

Note: However you cannot use group access tokens to create other group, project, or personal access tokens.

Configure LDAP in GitLab

This section is a optional as it requires higher level authentication where users login to GitLab using AD SSO.

  • To configure LDAP in GitLab edit the /etc/gitlab/gitlab.rb file as shown below and save the file.
gitlab_rails['ldap_enabled'] = true
gitlab_rails['ldap_servers'] = {
  'main' => {
    'label' => 'LDAP',
    'host' =>  'ldap.mydomain.com',
    'port' => 636,
    'uid' => 'sAMAccountName',
    'encryption' => 'simple_tls',
    'base' => 'dc=example,dc=com',
  }
}
  • reconfigure GitLab
sudo gitlab-ctl reconfigure

Configure AWS Cognito in GitLab

To enable the AWS Cognito OAuth 2.0 OmniAuth provider, register your application with Cognito. This process generates a Client ID and Client Secret for your application. To enable AWS Cognito as an authentication provider, complete the following steps. You can modify any settings you configure later.

  1. Sign in to the AWS console.
  2. From the Services menu, select Cognito.
  3. Select Manage User Pools and then in the upper-right corner, select Create a user pool.
  4. Enter the user pool name and then select Step through settings.
  5. Under How do you want your end users to sign in?, select Email address or phone number and Allow email addresses.
  6. Under Which standard attributes do you want to require?, select email.
  7. Configure the remaining settings to suit your needs. In the basic setup, these settings do not affect GitLab configuration.
  8. In the App clients settings:
    1. Select Add an app client.
    2. Add the App client name.
    3. Select the Enable username password based authentication checkbox.
  9. Select Create app client.
  10. Set up the AWS Lambda functions for sending emails and finish creating the user pool.
  11. After creating the user pool, go to App client settings and provide the required information:
    • Enabled Identity Providers – select all
    • Callback URL – https://<your_gitlab_instance_url>/users/auth/cognito/callback
    • Allowed OAuth Flows – Authorization code grant
    • Allowed OAuth 2.0 Scopes – emailopenid, and profile
  12. Save changes for the app client settings.
  13. Under Domain name, include the AWS domain name for your AWS Cognito application.
  14. Under App Clients, find your app client ID. Select *Show details to display the app client secret. These values correspond to the OAuth 2.0 Client ID and Client Secret. Save these values.

Authenticating with GitLab from Git

You need a way to authenticate to GitLab from Git and to accomplish this below sections will guide you to authenticate.

Git Credential Manager GitLab

In case of Git over HTTPS, Git Credential Manager (GCM) offers an alternative to personal access tokens. By default, GCM authenticates using OAuth, opening GitLab in your web browser.

Once it’s installed and configured, Git Credential Manager is called implicitly by Git. You don’t have to do anything special, and GCM isn’t intended to be called directly by the user

Create a personal access token in GitLab

Personal access tokens can be an alternative to OAuth2 and used to:

  • Authenticate with the GitLab API.
  • Authenticate with Git using HTTP Basic Authentication.

You can create as many personal access tokens as you like.

  1. In the upper-right corner, select your avatar.
  2. Select Edit profile.
  3. On the left sidebar, select Access Tokens.
  4. Enter a name and optional expiry date for the token.
  5. Select the desired scopes.
  6. Select Create personal access token.

Clone GitLab repository using personal access token 

Its important to know how to clone a repository using PAT. Lets checkout below command.

git clone https://<username>:<personal_token>@gitlab.com/gitlab-org/gitlab.git

Note: When 2Factor Authentication is enabled, you can’t use your password to authenticate with Git over HTTPS or the GitLab API. You can use a personal access token instead

Creating a Project Label in GitLab

With labels, you can organize and tag your work, and track the work items you’re interested in. Lets dive in and see how to create Labels in GitLab.

  • Navigate to GitLab, then to Main menu and projects then project information.
  • Create a new Label.

Git Commit in GitLab

Commit message: A commit message identifies what is being changed and why. In GitLab, you can add keywords to the commit message to perform one of the following actions:

Creating first GitLab CI/CD pipeline

Before we create the the pipeline, lets learn some basic terms first.

  • CI (Continuous Integration): Pushing changes in code such as scripts etc. multiple times a day can introduce the risk so each change submitted to an application, even to development branches, is built and tested automatically and continuously. 
  • CD (Continuous delivery): Not only is your application built and tested each time a code change is pushed to the codebase, the application is also deployed continuously. Continuous Delivery checks the code automatically, but it requires human intervention to manually and strategically trigger the deployment of the changes
  • CD (Continuous deployment) is another step beyond Continuous Integration, similar to Continuous Delivery. The difference is that instead of deploying your application manually, you set it to be deployed automatically.

Make sure, before you start creating your first GitLab CI/CD pipeline, you have:

  • At least one GitLab runner active.
  • A project in GitLab
  • The Maintainer or Owner role for the project.
  • To check if GitLab runner is available navigate to Settings and then to CI/CD.
  • Create a blank Dockerfile in the repository. The job will run on GitLab Docker executor.
  • Next, create a .gitlab-ci.yml file so that runner can structure and order of jobs. On the left sidebar, select Repository > Files and create the file and copy/paste the below code.
    • Each Job each has a specific Stage and stage defines the sequential execution of the Jobs. Jobs, which define what to do and Stages, which define when to run the jobs.
    • Use needs keyword to run jobs out of stage order.
    • Use rules to skip the jobs or specify when to run the job.
    • Use the default keyword to specify additional configurations that are applied to all jobs.
build-job: 
    stage: build 
    script:
       - echo "Hello, $GITLAB_USER_LOGIN"
test-job1:
    stage: test
    script:
       - echo "Testing CI/CD Pipelin test Job 1"       
test-job2:
    stage: test
    script: 
       - echo "Testing CI/CD Pipelin test Job 2"
deploy-job:
    stage: deploy
    script:
       - echo "Deploy from the $CI_COMMIT_BRANCH branch" 

  • You should have below files in the repository.
  • To View the status of your pipeline and jobs navigate to CI/CD and then pipelines.

  • Further to view the build job logs, click on it.
  • Note: variables: To push a commit without triggering a pipeline, add [ci skip] or [skip ci]

Configure GitLab Pipelines: Declaring Variables , Rules in GitLab Jobs

  • To add variables in the GitLab job.
variables:
  DEPLOY_ENVIRONMENT:
    value: "staging"
    options:
      - "production"
      - "staging"
      - "canary"
    description: "The deployment target. Set to 'staging' by default."
  • To add rules and workflow in the GitLab job. Rules apply to specific job however workflow apply to entire jobs.
workflow:
  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'

job1:
  script:
    - echo "This job runs in merge request pipelines"
  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'

job2:
  script:
    - echo "This job also runs in merge request pipelines"
  • GitLab CI/CD job token

When a pipeline job is about to run, GitLab generates a unique token and injects it as the CI_JOB_TOKEN predefined variable. You can also use the job token to authenticate and clone a repository from a private project in a CI/CD job. You can call on project from another project.

git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.example.com/<namespace>/<project>

Declaring Services in GitLab Jobs

When you configure CI/CD, you specify an image, which is used to create the container where your jobs run. To specify this image, you use the image keyword.

You can specify an additional image by using the services keyword. This additional image is used to create another container, which is available to the first container. The two containers have access to one another and can communicate when running the job.

default:
  before_script:
    - bundle install

test:2.6:
  image: ruby:2.6
  services:
    - postgres:11.7
  script:
    - bundle exec rake spec

test:2.7:
  image: ruby:2.7
  services:
    - postgres:12.2
  script:
    - bundle exec rake spec

GitLab code Dependency scanning

The Dependency Scanning feature can automatically find security vulnerabilities in your software dependencies while you’re developing and testing your applications.

include:
  - template: Jobs/Dependency-Scanning.gitlab-ci.yml


GitLab code scanning in containers

By including an extra Container Scanning job in your pipeline that scans for those vulnerabilities and displays them in a merge request, you can use GitLab to audit your Docker-based apps.

To enable container scanning, add the Container-Scanning.gitlab-ci.yml template to your .gitlab-ci.yml file. Also make sure GitLab CI/CD pipeline must include the test stage. If you’re using a third-party container registry, you might need to provide authentication credentials through the CS_REGISTRY_USER and CS_REGISTRY_PASSWORD configuration variables.

  • Below template builds your Docker image, pushes it to the container registry, and scans the image.
include:
  - template: Jobs/Build.gitlab-ci.yml
  - template: Security/Container-Scanning.gitlab-ci.yml

container_scanning:
  variables:
    CS_DEFAULT_BRANCH_IMAGE: $CI_REGISTRY_IMAGE/$CI_DEFAULT_BRANCH:$CI_COMMIT_SHA

GitLab Environments and Deployments

Environments describe where code is deployed. Each time GitLab CI/CD deploys a version of code to an environment, a deployment is created.

build-job: 
    stage: build 
    script:
       - echo "Hello, $GITLAB_USER_LOGIN"
test-job1:
    stage: test
    script:
       - echo "Testing CI/CD Pipelin test Job 1"       
test-job2:
    stage: test
    script: 
       - echo "Testing CI/CD Pipelin test Job 2"
deploy-job:
    stage: deploy
    environment:
      name: staging
    script:
       - echo "Deploy from the $CI_COMMIT_BRANCH branch" 

Authenticate GitLab with AWS

To use GitLab CI/CD to connect to AWS, you must authenticate. After you set up authentication, you can configure CI/CD to deploy.

  1. Sign on to your AWS account.
  2. Create an IAM user.
  3. Select your user to access its details. Go to Security credentials > Create a new access key.
  4. Note the Access key ID and Secret access key.
  5. In your GitLab project, go to Settings > CI/CD. Set the following CI/CD variables:Environment variable nameValueAWS_ACCESS_KEY_IDYour Access key ID.AWS_SECRET_ACCESS_KEYYour secret access key.AWS_DEFAULT_REGIONYour region code. You might want to confirm that the AWS service you intend to use is available in the chosen region.
  6. Variables are protected by default. To use GitLab CI/CD with branches or tags that are not protected, clear the Protect variable checkbox.

What is GitHub?

GitHub is a code hosting platform for version control and collaboration. You can perform below tasks within GitHub using Version control system ( VCS) called Git.

  • Create and use a repository
  • Start and manage a new branch
  • Make changes to a file and push them to GitHub as commits
  • Open and merge a pull request

Getting started with your GitHub account

To sign up for an account on GitHub.com, navigate to https://github.com/ and follow the prompts. To best use GitHub, you’ll need to set up Git. Git is responsible for everything GitHub-related that happens locally on your computer. 

Organizations are shared accounts where businesses and open-source projects can collaborate across many projects at once.

GitHub provides two types of Enterprise products:

  • GitHub Enterprise Cloud
  • GitHub Enterprise Server

The main difference between the products is that GitHub Enterprise Cloud is hosted by GitHub, while GitHub Enterprise Server is self-hosted.

How to access GitHub?

You can access GitHub using following methods. Note make sure you have git already installed and to download go to https://git-scm.com/downloads.

  • GitHub CLI to use GitHub from the command line. To install GitHub CLI follow here.
  • GitHub Desktop client that work locally but not on command line.
  • Gitbash
  • Browse to GitHub.com
  • GitHub API
  • GitHub Mobile is available as an Android and iOS app. To install go here

What is GitHub Desktop

With GitHub Desktop, you can interact with GitHub using a GUI instead of the command line or a web browser. You can use GitHub Desktop to complete most Git commands from your desktop, such as pushing to, pulling from, and cloning remote repositories, attributing commits, and creating pull requests, with visual confirmation of changes.

You can download GitHub Desktop from https://desktop.github.com/.

GitHub Webhooks

Webhooks allow you to build or set up integrations, such as GitHub Apps which subscribe to certain events on GitHub.com. When one of those events is triggered, we’ll send a HTTP POST payload to the webhook’s configured URL.

Webhooks can be used to update an external issue tracker, trigger CI builds, update a backup mirror, or even deploy to your production server.

When configuring a webhook, you can use the UI or API to choose which events will send you payloads. 

  • Go to settings on the repository.
  • Navigate to Webhooks and click on Add Webhook.
  • The payload URL is the URL of the server that will receive the webhook POST requests from the Github. Setting a webhook secret allows you to ensure that POST requests sent to the payload URL are from GitHub.

Note: You can configure to receive webhooks with GitHub CLI using below command.

gh extension install cli/gh-webhook

Authenticating with GitHub from Git 

GitHub HTTPS Authentication with GitHub cli

GitHub CLI will automatically store your Git credentials for you when you choose HTTPS as your preferred protocol for Git operations and answer “yes” to the prompt asking if you would like to authenticate to Git with your GitHub credentials.

gh auth login

GitHub HTTPS Authentication with Git Credential Manager

Git Credential Manager (GCM) is another way to store your credentials securely and connect to GitHub over HTTPS. 

The next time you clone an HTTPS URL that requires authentication, Git will prompt you to log in using a browser window. Once you’ve authenticated successfully, your credentials are stored in the Windows credential manager and will be used every time you clone an HTTPS URL. Git will not require you to type your credentials in the command line again unless you change your credentials.

GCM is included with Git for Windows. During installation you will be asked to select a credential helper, with GCM listed as the default. Once it’s installed and configured, Git Credential Manager is called implicitly by Git. You don’t have to do anything special, and GCM isn’t intended to be called directly by the user. 

To install Git Credential Manager , follow here.

Password-based authentication for Git has been removed in favor of more secure authentication methods use Personal Access Token. 

Generating Personal Access Token in GitHub

A token that is used in place of a password when performing Git operations over HTTPS with Git on the command line or the API. Also called a personal access token.

When you git clonegit fetchgit pull, or git push to a remote repository using HTTPS URLs on the command line, Git will ask for your GitHub username and password. When Git prompts you for your password, enter your personal access token.

For example, on the command line you would enter the following:

$ git clone https://github.com/USERNAME/REPO.git
Username: YOUR_USERNAME
Password: YOUR_TOKEN

Note: Personal access tokens can only be used for HTTPS Git operations. If your repository uses an SSH remote URL, you will need to switch the remote from SSH to HTTPS.

GitHub code scanning

You can use code scanning to find security vulnerabilities and errors in the code for your project on GitHub. To do code scanning you can use CodeQL analysis and third party pools.

  • CodeQL is the code analysis engine developed by GitHub to automate security checks. You can analyze your code using CodeQL and display the results as code scanning alerts.

To setup code scanning follow the below steps.

  • On GitHub.com, navigate to the main page of the repository and go to settings.
  • You can also integrate third party tools to scan your code such as Dependency Review.

GitHub code scanning in containers

If you’re configuring code scanning for a compiled language, and you’re building the code in a containerized environment, the analysis may fail with the error message “No source code was seen during the build.” This indicates that CodeQL was unable to monitor your code as it was compiled.

You must run CodeQL inside the container in which you build your code. 

name: "CodeQL"

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]
  schedule:
    - cron: '15 5 * * 3'

jobs:
  analyze:
    name: Analyze
    runs-on: ubuntu-latest
    permissions:
      security-events: write
      actions: read

    strategy:
      fail-fast: false
      matrix:
        language: [java]

    # Specify the container in which actions will run
    container:
      image: codeql-container:f0f91db

    steps:
      - name: Checkout repository
        uses: actions/checkout@v3
      - name: Initialize CodeQL
        uses: github/codeql-action/init@v2
        with:
          languages: ${{ matrix.language }}
      - name: Build
        run: |
          ./configure
          make
      - name: Perform CodeQL Analysis
        uses: github/codeql-action/analyze@v2

Advance GitHub code scanning

  • On GitHub.com, navigate to the main page of the repository.
  • Under your repository name, click  Actions.
  • Add new workflow.
  • Configure CodeQL Analysis and commit below yml.

GitHub Status

To get a status of entire GitHub about your projects and repositories you can access GitHub status website using https://www.githubstatus.com/.

GitHub Pricing

There are three plans available for GitHub as shown below.

  • FREE PLAN: Free of cost: The basics for individuals and organizations.
  • TEAM PLAN: Around $4 per month for one user which includes Advanced collaboration for individuals and organizations.
  • Enterprise PLAN: Around $20 per month for one user which includes Security, compliance, and flexible deployment

Conclusion

In this tutorial you learnt everything you should know to understand GitLab and GitHub and how these two products just makes your life as simple as breathing. GitHub is not open source however GitLab is an open source product.

Also in this tutorial you learn various concept that helps you identity how both product works and their features.

Now that you have sound knowledge about these products, so where do you plan to host your code in your next assignment?

Advertisement

How to create Node.js Docker Image and Push to DockerHub using Jenkins Pipeline

If you plan to run your node.js application, nothing could be better than running on the docker and safely storing it on Dockerhub.

Running an application on docker is a huge benefit because of its light-weighted technology and security. Docker images are stored safely on the dockerhub

In this tutorial, you will learn how to create a docker image for node.js applications and push it to dockerhub using the Jenkins pipeline.

Let’s get started.

Join 50 other followers

Table of Content

  1. What is Jenkins Pipeline?
  2. What is Jenkinsfile?
  3. What is node.js server-side javascript?
  4. Prerequisites
  5. How to Install node.js and node.js framework on ubuntu machine
  6. Creating Node.js Application
  7. How to create dockerfile for Node.js application
  8. Pushing all the code to GIT Repository: Git Push
  9. Creating Jenkinsfile to run docker image of Node.js application
  10. Configure Jenkins to Deploy Node.js Docker Image and Push to Dockerhub
  11. Conclusion

What is Jenkins Pipeline?

Jenkins Pipeline uses a group of plugins that help deliver a complete continuous delivery pipeline starting from building the code till deployment of the software right up to the customer.

Jenkins Pipeline plugin is automatically installed while installing the Jenkins with suggested plugins and allows you to write complex operations and code deployment as code using DSL language ( Domain-specific language).

Related: the-ultimate-guide-getting-started-with-Jenkins-pipeline

What is Jenkinsfile?

Jenkins Pipelines uses a text file called Jenkinsfile, which is checked into the repository. You define each stage and steps that need to be executed, such as building and compiling code to deploy in respective environments.

Jenkinsfile allows you to define steps in code format, which is easier and gives more ability to review. In case Jenkins stops, you can still continue to write Jenkinsfile. It can hold, wait, approve, stop Jenkins job and many other functionalities.

What is node.js server-side javascript?

JavaScript is a language used with other languages to create a web page add some dynamic features such as rollover or graphics, and Node.js is an open-source JavaScript runtime environment.

Node.js performs well as it contains only a single process without wasting much memory, CPU and never blocks any threads or processes and furthermore allows multiple connections simultaneously.

Node.js allows JavaScript developers to create apps in the front and backend.

Prerequisites

  • You must have ubuntu machine preferably 18.04 version + and if you don’t have any machine you can create a ec2 instance on AWS account
  • Docker and Jenkins must be installed on ubuntu machine.
  • Make sure you have git hub account and a repository created . If you don’t have follow here

How to Install node.js and node.js framework on ubuntu machine

Now that you know what is node.js and what are benefits of having node.js. In this section, let’s dive into how to Install node.js and node.js frameworks on ubuntu machines.

  • Log in to the Ubuntu machine using your favorite SSH client.
  • Now, create a folder named nodejs-jenkins under opt directory in the ubuntu machine.
cd /opt
mkdir nodejs-jenkins
cd nodejs-jenkins
  • Now install node.js on ubuntu machine using the below command.
sudo apt install nodejs
  • Next, Install node.js package manager. Installing node.js package manager allows to install modules node_modules inside the same directory.
sudo apt install npm
  • Further install Nodejs express Web Framework and initialize it. This command will generate package.json file containing the project, all the dependencies and metadata details that will be used to create a project and further application.
npm init
  • Further, add one dependency which is required by node.js application.
npm install express --save

Creating Node.js Application

Now that you have successfully installed the node.js application and framework required to create a node.js application. Let’s create a node.js application.

Assuming you are still logged into the ubuntu machine.

  • Firstly, create a file named main.js in the /opt/nodejs-jenkins directory and copy/paste the below code.
var express = require('express')    //Load express module with `require` directive
var app = express() 

//Define request response in root URL (/)
app.get('/', function (req, res) {
  res.send('Hello Welcome to Automateinfra.com')
})
app.listen(8081, function () {
  console.log('app listening on port 8081!')
})

How to create dockerfile for Node.js application

Previously you created a node.js application, which is great, but to deploy node.js on docker, you will need to create a dockerfile. Docker file creates customized docker images on top of docker images.

After creating the dockerfile, you can use the docker build command to create a new customized docker image.

Running docker containers using dockerfile
Running docker containers using dockerfile

Let’s jump in and create a dockerfile for the node.js application.

  • Create a file named dockerfile in the same /opt/nodejs-jenkins directory and copy/paste the below content.
FROM node:7              # Sets the base image
RUN mkdir -p /app
WORKDIR /app             # Sets the working directory in the container
COPY package.json /app   # copy the dependencies file to the working directory
RUN npm install          # Install dependencies
COPY . /app       # Copy the content of the local src directory to the working directory
EXPOSE 4200
CMD ["npm", "run", "start"]
  • Next, to create a node.js docker image using above dockerfile run the below command on ubuntu machine.
docker build .
Building the docker image using dockerfile
Building the docker image using dockerfile

Pushing all the code to GIT Repository: Git Push

Earlier, you created the node.js application and manually built the docker image to confirm that your code is absolutely fine and works. Let’s push all the files and code in the Git repository using various commands so that later Jenkins can use these files and build the application.

  • Now your directory /opt/nodejs-jenkins should have look as below.
checking the content of the directory
checking the content of the directory
  • Initialize a new repository in the same directory /opt/nodejs-jenkins by running git init command.
git init
  • Add all the file in git repository using the below command in the same directory /opt/nodejs-jenkins
git add .
  • Now, check the status of git repository with below command. If there are any errors they will appear else you are good to go to the next step.
git status
  • Commit your changes in git repository using the git commit command in the same directory /opt/nodejs-jenkins
 git commit -m "MY FIRST COMMIT"
  • Add the remote repository that you already had as a origin by running git remote add command.
git remote add origin https://github.com/Engineercloud/nodejs-jenkins.git
Adding the remote origin
Adding the remote origin
  • Finally push the changes in the remote branch . Once prompted specify your credentials.
git push -u origin master
Pushing the code in git repository
Pushing the code in git repository
  • Now, verify the code on github by navigating to the repository link.
Verifying the git repository
Verifying the git repository

Creating Jenkinsfile to run docker image of Node.js application

Let’s quickly jump into the creation of Jenkinsfile to run docker image of Node.js application to deploy an application using Jenkins.

  • Create a file named Jenkinsfile in the same /opt/nodejs-jenkins directory and copy/paste the below content. Make sure to change the sXXXXXXX410/dockerdemo as per you’re dockerhub username and repository name.
node {
     def app 
     stage('clone repository') {
      checkout scm  
    }
     stage('Build docker Image'){
      app = docker.build("sXXXXX410/dockerdemo")
    }
     stage('Test Image'){
       app.inside {
         sh 'echo "TEST PASSED"' 
      }  
    }
     stage('Push Image'){
       docker.withRegistry('https://registry.hub.docker.com', 'git') {            
       app.push("${env.BUILD_NUMBER}")            
       app.push("latest")   
   }
}
  • Now push the Jenkinsfile as well in github in the same repository that you used earlier. Now, your repository should look something like below.
Pushing the Jenkinsfile in the git repository
Pushing the Jenkinsfile in the git repository

Configure Jenkins to Deploy Node.js Docker Image and Push to Dockerhub

Now that you have all the code, including Jenkinsfile, in the Github repository and deploy the node.js application on docker using Jenkinsfile, you need to create a Jenkins Job. Let’s configure Jenkins by creating a new multi-branch pipeline Jenkins Job.

  • Create a new multibranch pipeline Jenkins Job named nodejs-image-dockerhub by clicking on new item and selecting multibranch pipeline on the Dashboard.
Creating a new multibranch pipeline Jenkins Job
Creating a new multibranch pipeline Jenkins Job
  • Now click on nodejs-image-dockerhub job and click on configure it with git URL and then hit save.
Configuring the Git URL in the Jenkins
Configuring the Git URL in the Jenkins
  • To connect to dockerhub you would need to add dockerhub credentials by clicking on the Dashboard ➔ Manage Jenkins ➔ Manage credentials ➔ click on global ➔ Add credentials.
Adding the dockerhub credentials in the Jenkins
Adding the dockerhub credentials in the Jenkins
  • Next, on Jenkins server add Jenkins user in the docker group so that Jenkins user has access to run docker commands.
sudo groupadd docker
sudo usermod -a -G docker jenkins
service docker restart
  • Further, make sure Jenkins users has sudo permissions by running below commands.
sudo vi  /etc/sudoers
jenkins ALL=(ALL) NOPASSWD: ALL
  • Now you are ready to run your first jenkins job. click on scan Multibranch pipeline job that will scan your branches in the repository. Then click on Branch and then click on Build Now.
Running the Jenkins Job
Running the Jenkins Job
  • The Jenkins job is completed successfully. Lets verify if Docker image has been successfully pushed to dockerhub by visiting Dockerhub repository.
Checking the docker image in dockerhub

Conclusion

In this tutorial, you learned what the Jenkins pipeline is, node.js is, and how to create a dockerfile, Jenkins file, and node.js application. Finally, you pushed the docker image to dockerhub using Jenkins.

So now you know how to deploy the node.js application on docker; which application do you plan to deploy next?

The Ultimate Guide for beginners: Getting started with Git commands

One of the best version control that I have used so far for managing multiple repositories and files in a smooth and distributed . The open source tool which we are talking here is Git the best control version. Please follow along to know the more on on how to work with Git , setup Git on ubuntu and windows machine & best commands and setup guide.

This tutorial is going to add a lot of valuable knowledge on Git in your pockets, Stay tuned !!

Table of Content

  1. Getting Started with Git
  2. Create a new git repository using graphical interface on GitHub
  3. Creating a new git repository using command line on ubuntu machine
  4. Getting Started with Git Commands for beginners
  5. Summary

Getting Started with Git

What is Repository ?

Repository is a place where you keep all your source code it could be project wise or you can create it for a particular technology or you can create even for a single file, it all depends on you and your requirement. Its always a good idea to provide the name of repository according to work which you are going to do. There are some cloud based source code tools which allows you to create your repositories such as bitbucket , GitLab, GitHub ,Subversion , SVN and Mercurial etc. where you can create your own repositories.

Create a new git repository using graphical interface on GitHub

  • Before we create the first repository make sure you have GitHub account created . If you don’t have please click here and Sign up.
  • If you have access to it then open your browser and go to GitHub website from here and click on Sign in.
  • Now click on New button to create a new repository
  • Now, provide a suitable name of the repository and you may keep it as public open to world if you wish people can see your code else keep it private. We are keeping as public as this is a demo tutorial and finally select create repository.
  • Now your repository is ready to be used. Please make sure you below steps which we will use later in this tutorial.
    • create a new repository on the command line and
    • push an existing repository from the command line

Creating a new git repository using command line on ubuntu machine

  • You must have ubuntu machine preferably 18.04 version + and if you don’t have any machine you can create a ec2 instance on AWS account
  • Recommended to have 4GB RAM
  • At least 5GB of drive space
  • SSH into your ubuntu machine
  • Now create a folder under /opt directory
cd /opt
mkdir git-demo
cd git-demo/
  • Initialize your new repository
git init
  • Create a file inside the same directory using the command
echo "My first change" > adding-new-file.txt
  • Now check the status of git repository using the command
git status
  • Add the file in git repository using the command
git add .
  • Again check the status of git repository using the command
git status
  • Commit your changes in git repository using the command
 git commit -m "MY FIRST COMMIT"
  • Add the remote repository which we created earlier as a origin.
git remote add origin https://github.com/Engineercloud/git-demo.git
  • Push the changes in the remote branch ( Enter your credentials when prompted)
git push -u origin master
  • Now Verify if the text file which we created is present in repository

Getting Started with Git Commands for beginners

Here we will work with most important commands of Git. Let us work with same directory which we created earlier.

cd /opt
mkdir git-demo
cd git-demo/
This image has an empty alt attribute; its file name is image-300.png
  • Check the status of the repository
git status
  • Add a file in the directory and run the command
echo "Adding new file again" > second_file.txt
  • Now check the status of repository again, it should show untracked files as they are added in repo yet
  • Add files in repository by using command
git add .
  • Check the status of git again
  • To check the status of git with short status
git status -s
  • Now edit the file which we created and check the status .
    • You will notice that there will be two things one is changes to be committed because we already added them using git add.
    • Also you will notice changes not staged for commit because we recently edited our file but didn’t add it.
echo "I am editing my second file " >> second_file.txt
  • Add the file in repository which we modified.
git add .

git status
  • Although we know that we modified a line and checked the status , but if you want to check the difference between the two you can use command.
git diff        # Git diff provides the information which is not committed
  • Also you can check the committed change so far by using the command.
git diff --cached      # Git diff --cached provides the information what is committed
  • Commit all your changes
git commit -m "Committing all my changes" .       # m here means message
  • To delete your staged file properly, that is how to delete those files which are already committed
git rm second_file.txt  # This command will also remove the file from the directory
  • If you wish to delete your staged file properly but keep the file in the directory for future use
git rm --cached second_file.txt  # This command will not remove the file from the directory
  • To check history of your commits in a git repos use a command
git log  # The command gives the list of commits in reversal order which is expected.
  • To check history of your commits with the difference between each commit we use -p flag
 git log -p -2    # -p stands for patch that is difference and 2 here is last 2 commits
  • There are some more commands to view the commits in more presentable way , lets checkout.
git log --pretty=format

git log --pretty=format:"%h %s" --graph

Summary

You should now have a very sound knowledge of what Git is , how to create Git repository using graphical mode as well as using command line tool . Also we discussed how to work with Git repositories using different commands. This tutorial consists of all the practical’s which were done on our lab server with lots of hard work and efforts.

Please share the word if you like it and hoping you get benefit out of this tutorial.

You can also visit : Introduction-to-git-a-version-control-getting-started-with-git-step-by-step