Code Review with Bitbucket + Sonarqube + Jenkins on AWS
Published: 2023-11-25 03:58:04

*My first post. 
Note to myself.
Assuming you have experience to create instance in EC2.*

The idea is to prepare code review pipeline using Jenkins and SonarQube at AWS and code hosted at Bitbucket.

## 1. Preparing instance for SonarQube.

Create instance for SonarQube using below AMI.

`SolveDevOps-SonarQube-Server-Ubuntu20.04-20230521-b57b35de-e3336ad7-93a2-4e7c-8ed8-227cfbf25da4`
`ami-078bf9fd6661b31ee`

Once instance is ready, try access through web using http://{instance.public.ipv4.dns}:9000. 

Login using below credential.
username: admin
password: {instance_id}

Once login, it should bring you to one-time update password.

## 2. Sonarqube - Create a project.

Let's start by create your own project by click **Manually**. 
- Key-in **Project display name**.
- Key-in **Project key**. _(Value should auto populate based on Project display name)_
- By default, **Main branch name** already set as **main**. Key-in manually if you prefer different branch.
- Click **Set Up** button.

Now let's generate SonarQube token.
- From top navigation click **Administrator**
- Click on **Security**
- Click on **Users**
- Click on icon **Update tokens** under column **Tokens** for **Administrator**.
- Enter **Token Name**, choose **Expires in** and click on **Generate** button.
- Copy your new token. token should start with **squ_**xxxxxxx

## 3. Preparing instance for Jenkins

Create instance for Jenkins using below AMI.

`SolveDevOps-Jenkins-Server-Ubuntu20.04-20240511-af3afa48-c43188bb-dfd9-4b72-9df8-272230591cbc`
`ami-0b53d8f417f68ec1e`

Once instance is ready, access through web using http://{instance.public.ipv4.dns}:8080

Follow the instruction to complete the installation. 
- It should ask you SSH to Jenkins instance to retrieve the temporary password at _/var/lib/jenkins/secrets/initialAdminPassword_.
- Choose **Install suggested plugins**.
- Wait until installation completed. Once completed it should take you to **Create First Admin User** form to create admin account. Key-in all fields and click **Save and Continue** button.
- At **Instance Configuration**, click **Save and Finish** button.
- Jenkins is ready!!

## 4. Jenkins configurations

Install plugin SonarQube.
- At left bar, click **Manage Jenkins**
- Under **System Configuration**, click **Manage Plugins**
- Click **Available plugins** and search **SonarQube**.
- Select **SonarQube Scanner** and click **Download now and install after restart** button.
- Wait until downloaded successful and select **Restart Jenkins when installation is complete and no jobs are running**.
- Jenkins server restarting. Please wait. It should bring you to dashboard page (or login page).

Next steps.
- At left bar, click **Manage Jenkins**
- Under **System Configuration**, click **Global Tool Configuration**
- Under **JDK**
   - Click **Add JDK** button.
   - Key-in any name under field **Name**.
   - Select **Install automatically**.
- Under **SonarQube Scanner**
   - Click **Add SonarQube Scanner** button.
   - Key-in any name under field **Name**. Example "sonarscanner" (to be used for "tool name").
   - Select **Install automatically**.
   - Field **Version**, please ensure to select latest version.
- Finally click **Save** button.

Next steps.
- At left bar, click **Manage Jenkins**.
- Under **System Configuration**, click **Configure System**.
- Under **SonarQube servers**, select **Environment variables**.
- Click **Add SonarQube** button.
- Key-in any name under field **Name**. Example "SonarQube" (to be used in withSonarQubeEnv).
- Under field **Server URL**, key-in SonarQube URL.
- Under field **Server authentication token**, click **Add** button. _In case if you experience nothing happen when click **Add** button, try click **Save **button first. This should redirect back to dashboard and navigate back to this page and click again **Add** button. You should see **Jenkins **button. Click it_.
- Under field **Kind**, select **Secret text**.
- Under field **Secret**, key-in SonarQube token string.
- Click **Add** button.
- Once popup closed, select **Secret text**.
- Click **Save **button.

SSH to Jenkins server and run below command line.
1. apt-get update
2. sudo apt install openjdk-17-jre-headless

Resources:
[1. askubuntu.com](https://askubuntu.com/questions/1411666/java-program-compiled-by-more-recent-version-of-jre-but-i-have-the-most-recent#:~:text=from%20your%20error%20you%20have%20to%20know%20that,some%20version%20of%20the%20JRE%20on%20your%20system)

## 5. Bitbucket - steps to create access token

At your bitbucket project repo 
- at left bar select **Repository settings**
- under Security select **Access tokens**
- click **Create Repository Access Token** button 
- enter access token's **name**
- select **Read** under **Repository** section
- click **Create** button

Once done please remember to keep the secret key.

Resources:
[1. support.atlassian.com](https://support.atlassian.com/bitbucket-cloud/docs/repository-access-tokens/)

## 6. Create Jenkins pipeline
- Click **New Items**.
- Key-in any name under field **Name**.
- Select **Pipeline** and click **OK** button.
- Scroll down to **Pipeline** section, copy paste below script and click **Save** button.

```
pipeline {
    agent any

    stages {
        stage('Bitbucket clone') {
            steps {
                git branch: 'master', url: '{https x-token-auth from bitbucket}'
            }
        }
        
        stage('Run SonarQube') {
            steps {
                script {
                    def scannerHome = tool name: 'sonarscanner', type: 'hudson.plugins.sonar.SonarRunnerInstallation'
                    def sonarProjectKey = '{sonar project key}'
                    def sonarSources = '.'
                    def sonarToken = '{sonar_token}'
                    def sonarURL = '{sonar_url}'
                    
                    withSonarQubeEnv('SonarQube') {
                        sh "${scannerHome}/bin/sonar-scanner -Dsonar.projectKey=${sonarProjectKey} -Dsonar.sources=${sonarSources} -Dsonar.token=${sonarToken} -Dsonar.host=${sonarURL}"
                    }
                }
            }
        }
    }
}
```
- Click **Build Now**.

Good luck and have fun.