Skip to content

CICD Pipeline with GitHub, Jenkins, SonarQube, Docker and AWS Cloud

Notifications You must be signed in to change notification settings

samsorrahman/Jenkins-SonarQube-Docker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sonar

  TABLE OF CONTENTS
Launch Instances
Install Jenkins
Creating a Pipeline in Jenkins Server
Adding a Webhooks
Starting a SonarQube Server
Installing Docker
Password-based Server Connectivity
Building the Image and running the container

Launch Instances

I have created 3 t2.medium instances

  • Instance for Jenkins
  • Instance for Sonarqube
  • Instance for Docker
  • kl

    Install Jenkins

    Update the system

      sudo apt update
      clear
    

    Now we have to install java first before installing Jenkins

      sudo apt install openjdk-11-jre -y
    

    Now we have to paste the Jenkins commands

       curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee \
       /usr/share/keyrings/jenkins-keyring.asc > /dev/null
       echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
       https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
      /etc/apt/sources.list.d/jenkins.list > /dev/null
      sudo apt-get update
      sudo apt-get install jenkins
    

    Now we have to allow port 8080 in the Jenkins server security group inbound settings

    dfa34375-6e63-4d8d-89cb-0f80ac0947b3

    Now use the public api of instance with port 8080 to view the Jenkins server

    26f464b3-38c6-44a6-8c64-efe30006a062

    Copy the command which was highlighted in the above picture and use the command

      sudo cat /var/lib/jenkins/secrets/initialAdminPassword
    

    It will show the output a password for the Jenkins server

    b5e9e13c-c112-4a7d-814b-c07d59cc7f54

    Copy the password and paste it to the Jenkins server

    8a19b65f-eb48-4207-93e3-50beb321bde0

    Click on -> Install suggested plugins
    You can set admin credentials

    b3f4de45-9dd9-4bb9-b0c4-484d164cb15b

    Jenkins Installation done

    041a6a53-4f76-430a-9319-73809d38c2bf

    Creating a Pipeline in Jenkins Server

    In this, I have selected the Freestyle project

    11c93238-30ac-4bc6-b012-d79f966ee0b4

    In the source code management, I have selected the Git option

    66935e93-ec40-4750-a131-e047a6d8518f

    Also select branch of your git

    8775a904-c2b2-4e3f-968e-f417bd256bb9

    Adding a Webhooks

    Now open the settings of your Git Repository, select webhooks

    scre1

    Click to Add Webhook, that time it will ask you to enter your password,
    once you enter your password, you're able to add Webhooks.


  • Enter the Jenkins URL such as http://35.175.189.203:8080/
  • and extension github-webhook after the URL
  • screen1

    Click to Add Webhook

    screen2

    Now in Build Triggers, select the GitHub hook trigger for GITScm polling
    (because this function can trigger the pipeline automatically whenever we make changes to the repository.


    171388f4-083d-44d5-b14d-ce5fb0a97a2f


    Click to save.
    Without Webhook I have clicked to Build now and it is working perfectly fine

    404296de-eab1-4716-b32d-dca80826936b

    Now time to verify Webhooks, I have clicked on the workspace in Jenkins,
    and here text.txt file is not present which I am going to create to test the Webhook

    1574a688-abf8-4d83-865f-3ff2f1a44ac7

    I have visited the Git Repository and created a new file by the name of test.txt

    Capture

    I committed the file
    Now I am back to the Jenkins server to check #2 build auto trigger and it is working fine.

    4ae8d251-1475-4e18-ae55-f56265f7a56c

    26f8a021-1f65-48fb-bca9-143934b82fa9

    Now test.txt file is showing in the workspace

    48cb2544-1d36-43e4-a784-c07125771cf7

    Starting a SonarQube Server

    Start the Sonarqube Instance
      sudo apt update
      clear
    

    We need to install Java on the server but 17 version earlier we were using 11 version

      sudo apt install openjdk-17-jre -y
    

    Search for the SonarQube website and download the community for the free version

    SonarQube Download Copy the link and paste it to the terminal of the SonarQube Instance


    a097c83d-a980-4906-9279-763b2e385ea7

    sudo adduser sonarqube
    

    4a6cc6dc-fb48-4645-9548-bced7f50ccec

    Use the command below with wget

      wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-9.4.0.54424.zip
    

    17688fc3-752f-4eb5-8afb-e59d487c385c

      sudo apt install unzip
      unzip *
      ls
    

    Now the unzipped file is showing

    9d2ee9e6-e2d5-4dde-8f2c-7c98ff07db5b

      cd sonarqube-9.4.0.54424 
    ls
    

    7652b9ab-5f85-47dc-a623-74beec0c2a52

    Go to the bin folder

      cd bin
      cd linux-x86-64
      ls
    

    c611fd78-564a-4f99-b1a5-3022d670b404

    Now start the sonar server by using the command

    ./sonar.sh start

    And also we need to port 9000 in the inbound setting of the security group

    f71b771d-2e6b-4222-9fba-16257693f97c

    Now it's time to check the URL http://54.88.67.190:9000/

    aebca526-2027-4d5e-9db1-6669e38a6e0f

    Update the password

    eb284491-e258-4175-b1fa-426dc7d1ecf7

    After updating the password we can access the Sonarqube

    Follow Me on GitHub for more Projects

    c357ffa8-b7b4-456a-b0e8-38b0d8e5c32e

    Now I have selected the project type Manually

    3e18d31f-db24-42e8-9acc-ead266df5dac

    After this, I have selected the CI platform, Jenkins

    437ac7bb-2cc7-45d2-8bcd-e5c174a7b181

    Selected the DevOps platform GitHub

    e77a9a76-7892-499e-b0ec-6ce9361916ed

    You can just simply select the options Configure Analysis

    db0b7319-bfd1-4a70-999b-12992a580ea6 Then select Continue

    3fd08744-ed4b-4bdc-b36c-7fde63de368a

    Also, click on Continue here

    a9e8783c-2ce7-4f17-9fc3-f61d351a9048

    From here I have selected the Others

    af8aff44-1aed-4fd9-9558-b093ade1e739

    Copy the ProjectKey for now you can keep this Key in your notepad

    sonar.projectKey=Sample-website

    Finish the tutorial

    f035e680-dfe2-41a7-b1aa-15144a0bf77c

    Now click to settings, we need to create token

    b5ed0424-cf55-463b-a61b-a7426d134e3a

    Now I have created the Token and copy the token for now you can keep this token in your notepad

    f7fe49a0-bd90-42fa-8eec-b61ff3c3c9d0

    Now back to Jenkins, Manage Jenkins and Install the plugin

    c2721c5c-2872-4348-ba76-feccf58e5229

    Install one more plugin

    ce35b5e1-f610-413a-b77c-3d9e626ef9c7

    Restart Jenkins after installation of plugins
    Now Go to the Global Tool Configuration

  • Scrolling Down
  • Click on SonarQube Scanner
  • Give any name and rest default
  • 8e21a183-c392-4c7c-b8bc-d8febb5eabef

    After saving it, Click to Manage Jenkins again and click on Configure System

    a5254716-8d78-4868-868c-1361e24ba543

    Configure System

  • Scrolling down
  • Click on the SonarQube server
  • Click to add
  • Give any name
  • Paste the Sonarqube URL
  • Save

    4226949e-573b-43bc-b393-7481d3f02d7f

    After Saving, Click Configure of the Pipeline in Jenkins

  • Click to Build Environment
  • Select Execute SonarQube Scanner in Build Steps
  • 2d2e35f5-1eba-40bb-9ecd-52c3201c1b77

    Ignore everything just paste the key here rest default and saved it

    1776e453-a5df-4016-ae70-d323fd0ef8f0

    Now back to Manage Jenkins, select Configure System again

    d661e861-8799-43b1-8bf7-fe03881a5e02

    Now we have to add token here

    66a78a49-44a9-4e93-af36-6f8c93646dd3

  • Select Secret text
  • ID - any name
  • 2f8cbfd0-bc9c-4022-a7ee-040c54c9751b

    Select Token after adding and click on save

    95a01889-fa07-4c40-8149-625dda8e1050

    Now go back to Pipeline to verify whether it's working or not, it's absolutely working fine

    Capture

    df

    Now going to check SonarQube, it's perfectly working fine

    ldfdj

    Once our code is passed now I am going to deploy it on Docker

    Installing Docker

    Started Instance
    Install Docker
    Update the apt package index and install packages to allow apt to use a repository over HTTPS:
        sudo apt-get update
        sudo apt-get install \
        ca-certificates \
        curl \
        gnupg
    

    Add Docker’s official GPG key:

      sudo install -m 0755 -d /etc/apt/keyrings
      curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
      sudo chmod a+r /etc/apt/keyrings/docker.gpg
    

    Use the following command to set up the repository:

      echo \
      "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
      "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
      sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
    

    Update the apt package index:

    sudo apt update
    

    To install the latest version, run:

      sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
    

    Password-based Server Connectivity

    Now I am going to make connection between Jenkins server and Docker server

    After running all the commands go back to the Jenkins server

      sudo su jenkins
    

    Open Docker server

      sudo su 
      nano /etc/ssh/sshd_config
    

    Uncomment this first 8fc77d61-25fc-43b3-92c0-89168303bdff

    And change the Password Authentication to Yes ldd

    systemctl restart sshd
    

    Back to Jenkins Server
    Now you can see it's asking for the password earlier it's showing permission denied ;d

    Now we have to change the password of the Docker Ubuntu user
    Back to Docker Server

    passwd ubuntu
    

    Capture

    Back to Jenkins Server

    ssh ubuntu@172.31.22.109
    

    Access done now b52a9131-8c1c-4061-acb8-001b98da03a6

    Now I am going to generate SSH Key in Jenkins Server

    ddd After generating keygen I entered the command

    ssh-copy-id ubuntu@172.31.22.109

    After running the command enter the password jkls

    ssh ubuntu@172.31.22.109
    

    Now we don't need to password again anymore

    Back to Jenkins again

  • Manage Jenkins
  • Configure system
  • Server group center
  • dlf

    Now we have to add a server list d

    Now go to Pipeline
    Configure
    Post-build action
    Add build step

    f

    Now I am gonna build the pipeline to verify whether it's working or not
    And it seems to be working Capture

    Open Docker Server, as we can our file got created here c

    Open the Git Repository now

  • Create a Dockerfile
  • Commit the file
  • inthels

    See Auto trigger started

    build

    Now returned to Pipeline, configure

  • I have deleted the Remote shell
  • Clicked to execute shell
  • Created a folder in the Docker server

    ins

    Clicked to execute shell and fill the details, here I entered docker server IP and folder which is website

    love

    We have got the success message here and let us check our docker server

    jldfl

    All the files copied to the Docker server ddfjslfjld

    Building the Image and running the container

    Back to the Docker server and need to give permission so that we can run all the commands without sudo
    sudo 
      usermod -aG docker ubuntu 
      newgrp docker
    

    After giving the permission we are able to use the docker without sudo

    docker ps
    

    laila

    Now back to Jenkins

  • Click to Pipeline
  • Click to configure
  • Click to post-build actions
  • Select the Remote shell again in the build steps
  • I gave any random name
  • jklkaladfd

    Time to check our docker container got created or not
    It got created but we have to add port 8085 in the inbound setting of the security group

    ajjd

    Now time to check the public URL of the docker Instance http://54.227.42.50:8085/ (our code is successfully deployed on docker container)

    jkldldlfdfljdflja;df

    Don't forget to Follow me for more Projects