Installing SonarQube on AWS EC2 and integration with Jenkins

Sonar-qube

We are going to install SonarQube on AWS EC2 instance. If you have not already done so, please login to your AWS account and launch an EC2 instance with RHEL OS. For more information on how to launch an instance, please read this post.

Step 1: Create a new RDS database for SonarQube (you may use an existing MySQL instance)

  • Go to RDS Instances
  • Launch a new DB instance
  • Select MySQL
  • Select Dev/Test
  • Click Next
  • Select the instance class (t2.micro should be good for testing)
  • Multi-AZ Deployment – Select No
  • Enter instance name
  • Enter master username and password
  • Click Next
  • Enter Database name – sonar
  • Click on Launch Instance
  • Once the database instance is ready, connect to your instance using MySQL Workbench (or any other client for that matter) and run the following commands

    CREATE USER 'sonar' IDENTIFIED BY 'sonar';
    GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar';
    GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar';
    FLUSH PRIVILEGES;

    Step 2: Now launch and connect to your AWS EC2 instance.

    Update your instance. It’s always a good habit.

    sudo yum update

    Get Sonar repository

    sudo wget -O /etc/yum.repos.d/sonar.repo http://downloads.sourceforge.net/project/sonar-pkg/rpm/sonar.repo

    Install Sonar package

    sudo yum install sonar

    Step 3: Configure SonarQube properties

    sudo vi /opt/sonar/conf/sonar.properties

    Locate following lines in the file and update the username and password:

    sonar.jdbc.username=sonar
    sonar.jdbc.password=sonar

    Scroll down to the MySQL section and update the Database Endpoint.

    #----- MySQL 5.6 or greater
    # Only InnoDB storage engine is supported (not myISAM).
    # Only the bundled driver is supported. It can not be changed.
    sonar.jdbc.url=jdbc:mysql://[REPLACE_WITH_RDS_ENDPOINT]:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useC onfigs=maxPerformance

    Save the file (Esc):wq!

    Step 4: Getting Sonar up & running

    Start Sonar and make sure it starts automatically at system startup.

    sudo service sonar start
    sudo chkconfig sonar on

    Navigate to the SonarQube Url http://[SonarQube-Instance-IP]:9000 (Default port 9000) to confirm it is running.

    If you don’t see the site getting loaded check the logs.

    sudo cat /opt/sonar/logs/sonar.log

    If you don’t see lines at the end like these then you may have to dig through to find the issue.

    2017.02.01 19:48:22 INFO app[][o.s.p.m.Monitor] Process[ce] is up
    2017.02.01 19:48:22 INFO app[][o.s.application.App] SonarQube is up

    Step 5: Integrating SonarQube with Jenkins

    Create Auth Token
    You will need the Token information from SonarQube when you configure Jenkins with SonarQube. So let’s get started.

    • Connect to your SonarQube instance at http://[SonarQube-Instance-IP]:9000/users
    • Click on Create User.
    • Enter Jenkins for Login and Name.
    • Enter Password and click ok.
    • Under Groups, click on the token image. Click on All and check sonar-administrators box.
    • Under Tokens column, click on Update token. token
    • Enter token name and click on Generate.
    • Copy token. Make sure you copy it now, you won’t be able to see it again!

    Configure SonarQube Server

    • Login to your Jenkins instance with administrative user.
    • Navigate to Manage Jenkins >> Configure System
    • Under “SonarQube Servers” section, add the SonarQube Server URL and an Auth Token to access it.

    Now, the last thing we need to do is configure SonarQube Scanner

    • Navigate to Manage Jenkins >> Global Tool Configuration
    • Under “SonarQube Scanner”, click SonarQube Scanner installations
    • Enter Name
    • Check Install Automatically
    • Install from Maven Central. Pick the latest version.
    • Click Save.

    That’s it! Good luck