Step-by-step procedure Jenkins CI/CD with Github Integration

Step-by-step procedure Jenkins CI/CD with Github Integration

Hello #techbhairavi followers, I am sharing beginner friendly easy step-by-step procedure to Deploy a code with Jenkins CI/CD and GitHub Integration

  1. Create an AWS EC2 Instance and run the console.

  2. Run the "sudo apt update" command

  3. Install Jenkins Binary files from https://pkg.origin.jenkins.io/debian/ and Install Jenkins to EC2 instance.

  4. update EC2 instance "sudo apt-get update"

  5. Run commands and start the service

    • sudo systemctl enable jenkins

    • sudo systemctl start jenkins

    • sudo systemctl status jenkins

  6. Expose port 8080 for Jenkins in AWS

  7. Open jenkins port with your IP(ex: 192.168.10.0:8080) and run Jenkins to register the details

  8. Copy from jenkins display " /var/lib/jenkins/secrets/initialAdminPassword" and run this in aws console

  9. cat /var/lib/jenkins/secrets/initialAdminPassword

    • o/p password copy and paste into jenkins page.
  10. Jenkins configured now create new item fill the following details

    • source code "Github url"

    • credential = private key added here*

      *Generate key through EC2 instance via SSH-keygen command and copy keys to integrate git and jenkins.

  11. Go to Github settings -> SSH & GPG Keys -> add new key

    • copy public key rd_rsa.pub (from AWS console)-> Add.
  12. Go to Jenkins, Build the code if successful then copy path (from console tab example: /var/lib/jenkins/workspace/todo-node-app) paste it in AWS EC2 with cd command (Ex: cd /var/lib/jenkins/workspace/todo-node-app)

  13. verify through "ls" code copied to terminal.

  14. Follow Developer Instructions from README.md

  15. Run code (EXPOSE code to AWS security group as per instruction)

  16. To Run this on any environment we have to make a Dockerfile(if not provided by the developer)

    1. sudo apt install docker.io

    2. vim Dockerfile

FROM node:12.2.0-alpine

WORKDIR app

COPY . .

RUN npm install

EXPOSE 8000

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

  1. Build a container through this Dockerfile

  2. Run "docker build . -t todo-node-app" (if error comes run the below command)

  3. sudo usermod -a -G docker $USER

  4. reboot system and run docker build command again.

  5. Run container in daemon mode

    docker run -d --name node-todo-app -p 8000:8000 todo-node-app

    verify via "docker ps" command

  6. Expose port on 8000 in AWS

    verify by open page on browser( ex. 192.169.1.0:8000)

  7. To automate this process run through Jenkins

  8. Open jenkins

    Dashboard-->todo-node-app-->configuration-->Build steps

    Add lines

    docker build . -t todo-node-app

    docker run -d --name node-todo-app -p 8000:8000 todo-node-app

    save and click build now

    (if permission denied error occurs then run

    chmod 777 /var/lib/jenkins/workspace/todo-node-app

    sudo usermod -a -G docker jenkins

    systemctl restart jenkins )

  9. Integrate webhook to automate full process (before that kill container "docker kill container name")

  10. Install plugins in jenkins

    Dashboard-->manage jenkins-->plugin manager-->Github integration -> Install-->restart jenkins

  11. Configure webhook in github

    verify keys exist in github settings.

  12. Go to repositories setting-->webhooks-->add webhook-->payload url=jenkins url/github-webhook/ (ex. 192.168.1.0:8080/github-webhook)

    set content type= application/json-->add webhook-->verify with tick

  13. Go to job in jenkins-->configure-->Build trigger-->github hook trigger for GITscm polling-->save

  14. Make change in Github code-->changes commit

    (Make sure your container is not in a running state)

    Congratulations!!!!! You have successfully deployed the code. If you like my content don't forget to follow me on LinkedIn https://www.linkedin.com/in/bhairaviw/

    Sources=>

    Github: https://github.com/techbhairavi/nodejs-todo-cicd/tree/master

    Thanks for reading!!