0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Github Actionsで静的解析のSonarQubeをJava(Spring Boot)環境にCICDしてみる

Last updated at Posted at 2025-08-03

本記事はSonarQubeやCI周りの記載で、Githubのアカウント作成やJavaの環境構築は記載していないです。

EVENT

SonarQubeをCICDに導入したい

SOLUTION

1. SonarQubeアカウント作成、リポジトリ選択

下記へアクセスして「今すぐ始める」ボタンを押下して画面の手順を進めていきます。

2. My ProjectsのAnalysis Methodを参照する

作成したMy ProjectsのAdministration -> Analysis Methodへ移動します

image.png

ページへ飛ぶと後の手順が書いてあるのでそれを参照していきます。

3. Githubでシークレットキーを登録する

ここからは公式サイトに書いてある通りの手順になります。
「Settings > Secrets and variables > Actions」のリンクを押して「New repository secret」を押下します。

image.png
SonarQubeに書いてある「In the Name field, enter 」をNameへコピペ。「In the Value field, enter」をSecretへコピペします。
image.png

4. GradleとCI.ymlにコードを書いていく

  • Gradle(build.gradle)

    plugins {
      id "org.sonarqube" version "6.2.0.5505"
    }
    
    sonar {
      properties {
        property "sonar.projectKey", "<hoge>"
        property "sonar.organization", "<hoge>"
        property "sonar.host.url", "https://sonarcloud.io"
      }
    }
    
  • CI(.github/workflows/build.yml)
    下記ジョブ周り等は公式の書き方と異なる記載になってます。

    name: Java CI Action
    on:
        push:
            branches: [ "main" ]
        pull_request:
            branches: [ "main" ]
    
    jobs:
        build:
            runs-on: ubuntu-latest
    
            steps:
              - name: Checkout code
                uses: actions/checkout@v4
    
              - name: Setup Java JDK
                uses: actions/setup-java@v4.7.1
                with:
                  java-version: '21'
                  distribution: 'temurin'
                  # cache: 'gradle'
    
              - name: Grant execute permission for gradlew
                run: chmod +x ./app/gradlew
    
              # - name: Setup Gradle
              #   uses: gradle/actions/setup-gradle@v4
                
              # - name: Build with Gradle
              #   working-directory: ./app
              #   run: ./gradlew build
    
              # SonarQubeの追加箇所
              - name: Cache SonarQube packages
                uses: actions/cache@v4
                with:
                  path: ~/.sonar/cache
                  key: ${{ runner.os }}-sonar
                  restore-keys: ${{ runner.os }}-sonar
              - name: Cache Gradle packages
                uses: actions/cache@v4
                with:
                  path: ~/.gradle/caches
                  key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle') }}
                  restore-keys: ${{ runner.os }}-gradle
              - name: Build and analyze
                env:
                  SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
                working-directory: ./app
                run: ./gradlew build sonar --info
    

5. Githubリポジトリへプッシュ & Github Actionsの確認

VSCodeのソース管理からプッシュをしてGithub Actionsを確認します。

image.png

無事成功しているのが確認できました。
最後にSonarQubeを開いて実行結果確認します。

image.png

ちゃんと(雑なテスト結果にエラーを吐いているので)反映されてそうですね。

おわり。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?