Jenkins-SonarQube-Docker project using Webhooks

Jenkins-SonarQube-Docker project using Webhooks

Downloading the code from GitHub, doing code analytics in SonarQube, deploying in the container called Docker tool and presenting website to the end-user.

  • Creating the instances - jenkins, sonarqube, docker

  • instance type = t2.medium for all 3 instances

  • security group for jenkins = SSH-22, 8080 port number

  • security group for sonarqube = SSH-22, 9000 port number

  • security group for docker = ALL

  • Use the Public-IP address of jenkins server with port number 8080,

    parameter => 'jenkinsPublicIP:8080'

  • Use 'cat /var/lib/jenkins/secrets/initialAdminPassword' in jenkins server to get initial Administrator password.

  • Click on the Install suggested plugins.

  • Create the Freestyle project and I named as 'jenkinsSonarDocker'

  • Copy the code from GitHub repository and paste it in the Jenkins GUI server as shown below.

    • Select the 'GitHub hook trigger for GITScm polling' for webhook- this is for auto trigger or auto build in jenkins GUI server, when code is updated or modified in GitHub repository.

      This is used while doing work in docker tool, later uploading Dockerfile in GitHub repository.

  • Go to the GitHub repository=> settings => Webhooks and create the webhook as shown in the below.

  • Give the trigger option for 'pull requests' and 'pushes'. So when there any pull and push in GitHub repo is automatically trigger in Jenkin GUI server.

  • After select the 'GitHub hook trigger for GITScm polling' in jenkins GUI server, click 'Apply' and 'Save', then click on 'build now'.

    Successfully code is downloaded in Jenkin.

  • Commands for Installation of sonarqube.

  • sonarqube link - https://www.sonarsource.com/products/sonarqube/downloads/success-download-community-edition/

    • After 'visudo' command as shown above in the picture, the below picture appears,

      Type 'sonar [tab button] ALL=(ALL) [tab button] NOPASSWD: ALL'

      After,

      control O button => Enter button => control X button.

    • Switch user by command 'su' to sonar as shown in below picture.

  • Open the sonarqube website through public-IP address of sonarqube server by putting '9000' port number.

    Parameter => 'sonarqubePublicIP:9000'

  • Create a local project and click on, shown in below pictures

  • Click on 'With Jenkins'

  • Note the 'sonar.ProjectKey' which is required for jenkins GUI.

  • Now, token need to generate, click on the Account and generate it note down it and required for jenkins GUI.

  • Now open the Jenkin GUI website, open the created project,

    Dashboard => Manager Jenkins => Plugins

    select 'SonarQube Scanner' and install.

    then, select 'SSH2 Easy' and install.

  • Dashboard => Manage Jenkins => Tools

    Give the name in 'SonarQube Scanner installations'

    'Apply' and 'Save'.

  • Dashboard => Manage Jenkins => System

    Configure the 'SonarQube installations' and generate the 'Server authentication token' by putting sonarqube generated token, shown in below.

    • Dashboard => jenkinsSonarDocker => configuration

      choose 'Build Steps', select ' Execute SonarQube Scanner' and apply the 'sonar.projectKey' which generated in sonarqube website in 'Analysis properties'

      'Apply', 'Save' and 'Build Now'

Successfully code tested in sonarqube website.

Next steps in docker Tool,

  • Commands for install docker.

    Yes, docker is installed.

  • Create a directory in the docker server.

  • So now, need to configure SSH password less connection from docker server to jenkins server.
  • In docker server itself type the command shown in below,

  • press 'i' button

    Uncomment the 'PubkeyAuthentication' and do 'PasswordAuthentication yes'

    then press 'esc' button => ':wq'

    • Type commands,

      \=> 'systemctl restart ssh'

      \=> 'passwd ubuntu'

      as shown in the below

  • Go to the jenkins server, switch user to the jenkins server

    'su jenkins'

    'ssh-keygen'

    'ssh-copy-id ubuntu@dockerprivateIP'

    • Put password and try logging into 'ssh ubuntu@dockerprivateIP'

    • And is successfully connected docker server to jenkins server

      This is done because server to server copy- here we need to copy the code from jenkins server to docker server by applying command 'scp' in Jenkin GUI.

  • Now come to the Jenkin GUI, go to created project and configuration- choose 'Build Steps' select ' Execute shell' and type the command as shown in below.

    parameter => 'scp -r sourcefile destinationfile'

  • Now come to docker server and you can check here, so it is deployed in docker server.

    So, Till now

    Downloaded from GitHub to Jenkin-Tested in SonarQube from Jenkin-Deployed code in docker from Jenkin.

    Now, run this code in the docker container and deploy in the website which can use by the end-user.

  • Go to Jenkin GUI, Dashboard => Manage Jenkins => Systems

    Select 'Server Group List' configure it 'Apply' 'Save'

    • again, Dashboard => Manage Jenkins => Systems

      Select 'Server List' configure it 'Apply' 'Save'

  • Come to docker server and type this command.

  • Come to Jenkin GUI,

    Open the created project and select the 'Build Steps' choose the 'Remote Shell' and type the command in the 'shell'.

    This command is to run the container.

    'Apply', 'Save'.

  • Now come to the GitHub open the code, creating the nginx Dockerfile.

    After immediate commit this code in GitHub, Automatically triggered or get build in Jenkin GUI.

    So the new nginx container must installed in docker server.

Yes, it is, here it installed.

  • So now take the public IP and deploy in the website, it must show the web presentation.

    Hurray, Finally it can use by end-user.

  • same as done for another code file, created nginx Dockerfile.

    This is how CI/CD Jenkin project done in docker using webhooks.