Setting Up SonarQube on a GCP VM Instance

SonarQube is a powerful tool for continuous inspection of code quality. Setting it up on a GCP VM instance can help you leverage its capabilities in a scalable and flexible environment. 

Follow these steps to get started:

Prerequisites

  • Google Cloud Account: Ensure you have a GCP account with billing enabled.
  • VM Instance: Create a VM instance on GCP. For this guide, we'll use a Debian-based instance.
  • Java: SonarQube requires Java 11. Make sure it's installed on your VM.
  • Database: SonarQube needs a database. We'll use PostgreSQL for this setup.

Step 1: Create a VM Instance
  • Navigate to the GCP Console: Go to the GCP Console.
  • Create a New VM Instance:
    • Go to Compute Engine > VM instances.
    • Click Create Instance.
    • Choose a name, region, and machine type (e.g., n1-standard-1).
    • Select a boot disk with Debian 10.
    • Allow HTTP and HTTPS traffic under Firewall.
    • Click Create.
Step 2: Install Java
  • SSH into Your VM: Click SSH next to your VM instance in the GCP Console.
  • Update Package List:
sudo apt update

  • Install Java 11:
sudo apt install openjdk-11-jdk -y

Step 3: Install PostgreSQL
  • Install PostgreSQL:
sudo apt install postgresql postgresql-contrib -y

  • Start PostgreSQL Service:
sudo systemctl start postgresql
sudo systemctl enable PostgreSQL
  • Create a Database and User:
sudo -i -u postgres
psql
CREATE DATABASE sonarqube;
CREATE USER sonar WITH ENCRYPTED PASSWORD 'your_password';
GRANT ALL PRIVILEGES ON DATABASE sonarqube TO sonar;
\q
exit

Step 4: Install and Configure SonarQube
  • Download SonarQube:
wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-9.9.0.65466.zip
  • Unzip SonarQube:
sudo apt install unzip
unzip sonarqube-9.9.0.65466.zip
sudo mv sonarqube-9.9.0.65466 /opt/SonarQube

  • Configure SonarQube:
    • Edit the sonar.properties file:
sudo nano /opt/sonarqube/conf/sonar.properties

    • Set the database connection parameters:
sonar.jdbc.username=sonar
sonar.jdbc.password=your_password
sonar.jdbc.url=jdbc:postgresql://localhost/sonarqube

Step 5: Start SonarQube
  • Create a SonarQube User:
sudo adduser --system --no-create-home --group --disabled-login sonarqube
sudo chown -R sonarqube:sonarqube /opt/SonarQube

  • Start SonarQube:
sudo su - sonarqube
/opt/sonarqube/bin/linux-x86-64/sonar.sh start

Step 6: Access SonarQube
  • Open SonarQube in Your Browser:
    • Navigate to http://YOUR_VM_EXTERNAL_IP:9000.
    • Log in with the default credentials (admin/admin).
Conclusion

Congratulations! You have successfully set up SonarQube on a GCP VM instance. You can now start analyzing your code and improving its quality. For more advanced configurations and usage, refer to the SonarQube documentation.

Feel free to ask if you need any more details or run into any issues!

Checkout:

Comments

Popular posts from this blog

How to update build number in Azure DevOps pipeline?

How to get latest build ID from Azure DevOps pipeline?

How to install AWS System Manager (SSM) Agent on windows using PowerShell?