DevOps Project: CI/CD Pipeline by using Jenkins, Docker, GitHub, and AWS EC2.

Requirements:

  • Create a new item. For this, click New item.

  • After creating a New item, it will look like this:

  • Now give it any name and select the Freestyle project; after that, save it. In my case, I have given "My-First-node-todo_project".

  • Now give a short description of your project. Then select the GitHub project. When you select the GitHub project, it will have the required project URL. In this case, copy your GitHub-specific repository URL and paste it.

  • After that, in the Source Code Management option, select Git, and it will require the repository URL. Copy your repository URL and paste it.

    Now, in the Credentials option, we have to give our server credentials.

    For these credentials, Go to your Linux server terminal and create the ssh key by following the following command:

      ssh-keygen
    

    Now copy the public key and go to your GitHub account settings and click the SSH and GPG Keys option.

  • Now click New SSH Key.

  • Now give the title of the new SSH key, paste your public key from your Linux server, and click the Add SSH Key option.

  • Note: It will require your GitHub account password.

  • Once you add your public profile to GitHub. It means GitHub now has the public key for your Linux Server.

  • Now go to your Jenkins dashboard, and in the Credentials option, click Add and select Jenkins. It will show something like the below photo.

  • In the Domain option keep it the same as default.

    In the kind option, select SSH Username with Private Key.

    In the Scope option, keep it default.

    In the ID option, give a name as id: in my case, "github-jenkins"

    In the Description option, write a short description about id: in my case "This is for Jenkins and GitHub integration"

    In the username option, give your Linux server's user name: in my case, the username is "ubuntu"

  • Now, in the private key option, paste your private key that was created before on your Linux server. Click Enter directly and click Add. After that, paste the private key and click the Add option.

  • Once you add it, now go to the credentials option and select your key, which was created in the previous steps.

Note: It means you make a bridge connection between GitHub and Jenkins.

  • Now save this, and you will see this:

  • Now click Build Now. After building it go to console output it will be like this.

Note: Up until now, we just took codes from GitHub by using Jenkins. and our code will be stored in these directories. "/var/lib/jenkins/workspace/My-First-node-todo_prject" . If we go to this directory, then we will see all of the codes.

  • Now we have all the code in Jenkins from GitHub.

    Follow the REDME.me file in your GitHub or code directory.

    1. sudo apt-get install nodejs

    2. sudo apt install npm #(npm=node package management)

    3. sudo npm install #(this command is used for the install package.json file)

    4. node app.js #(If we run this command, then this app will run. This is from the developer)

Note: Run all of the above commands in your Linux server terminal. Keep in mind that it runs in the right directory.

  • Once we do all these steps, the app should run properly. If we cutoff Todolist running on http://0.0.0.0:8000 , this app will not work. To solve this issue, we will create a Docker container.

Dockerized the application:

  • Create Dockerfile
vim Dockerfile
#Dockerfile
FROM node:12.2.0-alpine
WORKDIR app
COPY . .
RUN npm install

EXPOSE 8000
CMD ["node","app.js"]

Note: FROM node:12.2.0-alpine > it will create an operating system.

WORKDIR app> where you want to do your work. (in background, it will create a folder and enter the folder )

COPY .. > copy all code from source to container.

RUN npm install > Install all dependencies.

EXPOSE 8000 > expose port.

CM ["node","app.js"] > This command will run the app.js file, which will run the application.

  • Now we have a Dockerfile, and we will create Docker images.
docker build . -t node-todo-image

Note: note-todo-image is my image name. It depends on you.

  • We now have a Docker image. We will run the container with the following commands:
docker run -d --name node-todo-container -p 8000:8000 node-todo-image:latest

Note: -d means daemon mode (run in the background).)

-p is for port mapping. Here node-todo-container is my container name, and node-todo-image is my image name, which was created in the previous step.

  • Once the container is created and running, reload the page, and it will show that the web application (todo app) is running.

Note: This means we run our application through Docker.

Now Run this App through Jenkins.

  • What we have done so far with Docker is done manually. Now automate this process through Jenkins.

  • First, kill the existing Docker container, which was created manually.

Docker kill <container_id>
  • Now go to the Jenkins configuration page, then go to the Build Triggers option and add build steps. Select Execute Shell. Write your Docker build and Docker run commands here and save them.

Note: If there is any Error with Docker daemon socket permission, then run the following command.

sudo chmod 777 /var/lib/jenkins/workspace/My-First-node-todo_prject
or
sudo usermod -a -G docker jenkins
and 
sudo systemctl restart jenkins

Note. Now we will see that the code is running without any errors. Jenkins now automates the whole process (build docker images, run docker containers, etc)

Automate the full process without a single click. Automatic trigger.

Note: For this step, First kill the Docker container and images.

  • First, install a Jenkins plugin.

Go to "Manage Jenkins", then "plugin", and select an available plugin. After that, Search for "github integration" . Now press "Install without restart" and click "Restart Jenkins when installation is completed and no jobs are running".

  • Now configure webhooks in your GitHub repository.

  • Go to your repository's "Setting " option and click "Webhooks". Now click "Add webhook". Now give your Jenkins payload URL [ ( like: http://0.0.0.0:8080/ ) after this, write: github-webhook/ ]

    So the Full URL is look like: 0.0.0.0:8080/github-webhook

    After that, In "contant type" option, select "application/json" and click Active webhook.

In my case:

  • Once it's done, it will look like the following figure:

  • Now go to your Jenkins project/Job and click Configure. Now Go to the "Build Triggers" option Select "GitHub hook trigger for GITScm polling" and now save it.

Note: Webhook is connected with Jenkins. Now, if we change anything in our code on GitHub, it will automatically build it.

######THE END