|
| 1 | +# SonarCloud + GitLab Integration Setup Guide |
| 2 | + |
| 3 | +This guide will help you set up merge request decorations from SonarCloud on your GitLab instance. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +- Access to your on-premise GitLab instance |
| 8 | +- Admin access to your SonarCloud organization (`sonarcloud-demos`) |
| 9 | +- SonarCloud token (you already have: `f24b28da21d78fd71b9f9a8e6c3460806b025011`) |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +## Step 1: Generate GitLab Personal Access Token |
| 14 | + |
| 15 | +1. Go to your GitLab instance: `https://your-gitlab-url.com` |
| 16 | +2. Navigate to **User Settings** → **Access Tokens** |
| 17 | +3. Create a new token with: |
| 18 | + - **Name**: `SonarCloud Integration` |
| 19 | + - **Scopes**: Select `api` (required for merge request decorations) |
| 20 | + - **Expiration**: Set as needed (90 days or No expiration) |
| 21 | +4. Click **Create personal access token** |
| 22 | +5. **Copy the token** (you won't see it again!) |
| 23 | + |
| 24 | +--- |
| 25 | + |
| 26 | +## Step 2: Configure GitLab Integration in SonarCloud |
| 27 | + |
| 28 | +1. Go to [SonarCloud](https://sonarcloud.io) |
| 29 | +2. Navigate to your organization: **sonarcloud-demos** |
| 30 | +3. Go to **Administration** → **General Settings** → **DevOps Platform Integrations** |
| 31 | +4. Click **GitLab** tab |
| 32 | +5. Click **Add GitLab instance** or **Edit configuration** |
| 33 | +6. Enter: |
| 34 | + - **Configuration Name**: Your GitLab instance name (e.g., "Company GitLab") |
| 35 | + - **GitLab URL**: Your on-premise GitLab URL (e.g., `https://gitlab.yourcompany.com`) |
| 36 | + - **Personal Access Token**: Paste the token from Step 1 |
| 37 | +7. Click **Save** |
| 38 | + |
| 39 | +--- |
| 40 | + |
| 41 | +## Step 3: Configure Project for GitLab |
| 42 | + |
| 43 | +1. In SonarCloud, go to your project: **demo-java-security** |
| 44 | + - Direct link: https://sonarcloud.io/project/configuration?id=SonarCloud-Demos_demo-java-security |
| 45 | +2. Go to **Administration** → **General Settings** → **Pull Requests** |
| 46 | +3. Configure: |
| 47 | + - **Provider**: GitLab |
| 48 | + - **GitLab instance**: Select your configured instance |
| 49 | + - **Repository identifier**: Your GitLab project path (e.g., `group/demo-java-security`) |
| 50 | +4. Click **Save** |
| 51 | + |
| 52 | +--- |
| 53 | + |
| 54 | +## Step 4: Add SonarCloud Token to GitLab CI/CD Variables |
| 55 | + |
| 56 | +1. In your GitLab project, go to **Settings** → **CI/CD** |
| 57 | +2. Expand **Variables** |
| 58 | +3. Click **Add Variable** |
| 59 | +4. Add: |
| 60 | + - **Key**: `SONAR_TOKEN` |
| 61 | + - **Value**: `f24b28da21d78fd71b9f9a8e6c3460806b025011` |
| 62 | + - **Type**: Variable |
| 63 | + - **Flags**: |
| 64 | + - ✅ Protect variable (recommended) |
| 65 | + - ✅ Mask variable (recommended) |
| 66 | + - ❌ Expand variable reference |
| 67 | +5. Click **Add variable** |
| 68 | + |
| 69 | +--- |
| 70 | + |
| 71 | +## Step 5: Update .gitlab-ci.yml |
| 72 | + |
| 73 | +The `.gitlab-ci.yml` file has been created in your project root. You need to: |
| 74 | + |
| 75 | +1. Review the file and update if needed: |
| 76 | + - Project key: `SonarCloud-Demos_demo-java-security` |
| 77 | + - Organization: `sonarcloud-demos` |
| 78 | + - Branches to analyze: `main`, `develop`, and all merge requests |
| 79 | + |
| 80 | +2. Commit and push the `.gitlab-ci.yml` to your GitLab repository: |
| 81 | + ```bash |
| 82 | + git add .gitlab-ci.yml |
| 83 | + git commit -m "Add SonarCloud analysis to GitLab CI" |
| 84 | + git push origin main |
| 85 | + ``` |
| 86 | + |
| 87 | +--- |
| 88 | + |
| 89 | +## Step 6: Test Merge Request Decoration |
| 90 | + |
| 91 | +1. Create a new branch in GitLab: |
| 92 | + ```bash |
| 93 | + git checkout -b test-sonarcloud-integration |
| 94 | + ``` |
| 95 | + |
| 96 | +2. Make a small change (e.g., add a comment to a file) |
| 97 | + |
| 98 | +3. Commit and push: |
| 99 | + ```bash |
| 100 | + git add . |
| 101 | + git commit -m "Test SonarCloud integration" |
| 102 | + git push origin test-sonarcloud-integration |
| 103 | + ``` |
| 104 | + |
| 105 | +4. Create a Merge Request in GitLab |
| 106 | + |
| 107 | +5. Watch the CI/CD pipeline run |
| 108 | + |
| 109 | +6. After the `sonarcloud-check` job completes, you should see: |
| 110 | + - ✅ SonarCloud quality gate status in the MR |
| 111 | + - 💬 Comments on the MR with issue details |
| 112 | + - 📊 Quality metrics displayed |
| 113 | + |
| 114 | +--- |
| 115 | + |
| 116 | +## Troubleshooting |
| 117 | + |
| 118 | +### MR decorations not appearing? |
| 119 | + |
| 120 | +**Check:** |
| 121 | +1. GitLab token has `api` scope |
| 122 | +2. Repository identifier matches your GitLab project path exactly |
| 123 | +3. SonarCloud analysis completed successfully (check pipeline logs) |
| 124 | +4. The branch has a merge request open |
| 125 | + |
| 126 | +**Verify in GitLab pipeline logs:** |
| 127 | +``` |
| 128 | +Analysis report uploaded in XXXms |
| 129 | +ANALYSIS SUCCESSFUL, you can browse https://sonarcloud.io/... |
| 130 | +``` |
| 131 | + |
| 132 | +### Pipeline failing? |
| 133 | + |
| 134 | +**Common issues:** |
| 135 | +1. `SONAR_TOKEN` variable not set in GitLab |
| 136 | +2. Maven not finding dependencies (check cache configuration) |
| 137 | +3. Java version mismatch (ensure Java 17 is used) |
| 138 | + |
| 139 | +**Check logs:** |
| 140 | +```bash |
| 141 | +# In GitLab, go to CI/CD → Pipelines → Select the failed job → View logs |
| 142 | +``` |
| 143 | + |
| 144 | +--- |
| 145 | + |
| 146 | +## What You'll Get |
| 147 | + |
| 148 | +### Merge Request Decorations Include: |
| 149 | + |
| 150 | +1. **Quality Gate Status**: Pass/Fail badge |
| 151 | +2. **New Issues**: Count and severity breakdown |
| 152 | +3. **Coverage**: Code coverage changes |
| 153 | +4. **Duplications**: Duplicate code percentage |
| 154 | +5. **Inline Comments**: Issues directly on changed code lines |
| 155 | +6. **Analysis Summary**: Link to full SonarCloud report |
| 156 | + |
| 157 | +### Example MR Comment: |
| 158 | +``` |
| 159 | +SonarCloud Quality Gate: Failed |
| 160 | +
|
| 161 | +🐛 Bugs: 2 (1 High, 1 Medium) |
| 162 | +🔒 Vulnerabilities: 1 (1 Critical) |
| 163 | +💡 Code Smells: 5 (3 Major, 2 Minor) |
| 164 | +📊 Coverage: 65.2% (+2.1%) |
| 165 | +🔁 Duplications: 3.5% (+0.5%) |
| 166 | +
|
| 167 | +View full report: https://sonarcloud.io/... |
| 168 | +``` |
| 169 | + |
| 170 | +--- |
| 171 | + |
| 172 | +## Configuration Reference |
| 173 | + |
| 174 | +### SonarCloud Properties (in .gitlab-ci.yml) |
| 175 | + |
| 176 | +```yaml |
| 177 | +-Dsonar.projectKey=SonarCloud-Demos_demo-java-security |
| 178 | +-Dsonar.organization=sonarcloud-demos |
| 179 | +-Dsonar.host.url=https://sonarcloud.io |
| 180 | +-Dsonar.login=${SONAR_TOKEN} # Automatically used |
| 181 | +``` |
| 182 | + |
| 183 | +### Additional Options |
| 184 | + |
| 185 | +```yaml |
| 186 | +# Analyze specific branches only |
| 187 | +only: |
| 188 | + - merge_requests |
| 189 | + - main |
| 190 | + - develop |
| 191 | + |
| 192 | +# Skip SonarCloud on certain commits |
| 193 | +except: |
| 194 | + variables: |
| 195 | + - $CI_COMMIT_MESSAGE =~ /\[skip-sonar\]/ |
| 196 | +``` |
| 197 | +
|
| 198 | +--- |
| 199 | +
|
| 200 | +## Resources |
| 201 | +
|
| 202 | +- [SonarCloud GitLab Integration Docs](https://docs.sonarsource.com/sonarcloud/getting-started/gitlab/) |
| 203 | +- [GitLab CI/CD with Maven](https://docs.gitlab.com/ee/ci/examples/maven.html) |
| 204 | +- [SonarCloud Project](https://sonarcloud.io/project/overview?id=SonarCloud-Demos_demo-java-security) |
| 205 | +
|
| 206 | +--- |
| 207 | +
|
| 208 | +## Need Help? |
| 209 | +
|
| 210 | +Contact your DevOps team or refer to the SonarSource community forum: https://community.sonarsource.com/ |
| 211 | +
|
| 212 | +
|
0 commit comments