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?

More than 1 year has passed since last update.

Github ActionsでSonarScannerを実行し、Laravelプロジェクトを解析する

Last updated at Posted at 2023-06-26

概要

GitHubリポジトリの特定ブランチに対するPushをトリガとしてSonarScannerで解析を行い、SonarQubeに結果を送信するGitHub Actionを実装する

前提

  • SonarQube上にプロジェクトを作成してあり、以下の3点が把握できている
    • projectKey
    • hostUrl
    • トークン
  • GitHub上にリポジトリが存在していて、Action,workflow,secretに対する権限があるアカウントまたはPersonal Access Tokenを持っている

※本稿ではSonarQubeのプロジェクト作成や、GithHubのPersonalAccessTokenの作成方法については触れない

手順

GitHub Actions ワークフローの作成

  • 設定したいgithubリポジトリをブラウザで開いて、Actionsタブをクリック
  • 「New workflow」をクリック
  • 「set up a workflow yourself」をクリックして、新しいymlファイルの編集画面へ遷移
  • https://github.com/marketplace/actions/official-sonarqube-scan に記載されている、「The workflow YAML file will usually look something like this:」以下のサンプルをコピーして、編集画面に貼る。
  • 貼った内容を一部編集する。編集後のymlは以下の通り。
scan-by-sonar.yml
name: Analyze by Sonner Scanner
on:
  push:
    branches:
      - develop
jobs:
  sonarqube:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
      with:
        fetch-depth: 0
    - name: SonarQube Scan
      uses: sonarsource/sonarqube-scan-action@master
      with:
        args: >
          -Dsonar.projectKey=YOUR-PROJECT-KEYON-SONAR-QUBE
      env:
        SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
        SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
        LC_ALL: "ja_JP.UTF-8"

  • 編集内容についての補足
    • YOUR-PROJECT-KEYON-SONAR-QUBE の部分をSonarQube上のプロジェクトキーに書き換える
    • SonarQube CommunityEditionを利用する場合、解析対象のブランチは1つのみという制約がある。したがって、本来CIでやりたい、PullRequest作成時に解析回してコードレビューの一部を自動化、的な使い方はできない(たぶん)

      You'll set up your build according to your SonarQube edition:
      Community Edition: Community Edition doesn't support multiple branches, so you should only analyze your main branch. You can restrict analysis to your main branch by setting it as the only branch in your on.push.branches configuration in your workflow YAML file, and not using on.pull_request.

    • このプロジェクトでは、developブランチを解析対象にしているため、developブランチへのpush時にワークフローが起動するよう設定している
    • SONAR_TOKEN、SONAR_HOST_URL についてはGitHub Secretsで設定(後述)

GitHub Secretsの設定

https://docs.github.com/ja/actions/security-guides/encrypted-secrets を見ながら、前掲のymlファイルで利用する変数2点(SONAR_TOKEN、SONAR_HOST_URL)の値を設定する。設定値は、SonarQube側の管理画面で、プロジェクト設定などから確認できる。

sonar-project.properties の作成

workflow側の設定ファイルには、解析を実施するための最低限の情報のみ記載しつつ、アプリケーションの実装に依存して変更が必要になりそうなパラメータ等については、こちらのファイルに書いていく。

  • リポジトリのプロジェクトルートに設置すること。
  • 設置しなければデフォルトの設定で解析されるので、置かなくてもOK
sonar-project.properties
sonar.source=app,config,database,resources,routes,tests
  • このサンプルではLaravelのプロジェクトを解析対象としているので、解析したいソースコードが置いてあるディレクトリを複数指定

補足、備考

Personal Access TokenのPermission

  • github workflowの設定ファイルを開発環境で更新してpushする際、workflowに対するWriteが可能なパーミッションがPersonal Access Tokenに設定されていないとエラーになる。
  • windowsの場合↓見ながらあたらしいTokenに変更

参考ドキュメント一覧

Scan your code with SonarQube

暗号化されたシークレット (GitHubドキュメント)

GitHub Actions を理解する (GitHubドキュメント)

Github integration (SonarQubeドキュメント)

analyzing-projects-with-github-actions の項目を参照。

Analysis Parameters (SonarQubeドキュメント)

GitHub Actionで解析実行する際のパラメータ関連はここを見ながら

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?