Git

[Spring] 소나큐브(SonarQube) CI 파이프라인 구축 (with Gitea)

테런 2024. 7. 5. 16:06
Overview
  • Spring Boot (3 버전)에서 소나큐브(SonarQube) CI 파이프라인 구축 (with Gitea)

 

Prerequisite
* Spring Boot 3.2.7
* Java 17
* Gradle 8.1
* Sonarqube 5.0.0.4638
* Jacoco 0.8.11
* Gitea 설치 - [Git] Docker를 통한 Gitea 구축하기(GitLab과 차이)
* 소나큐브(SonarQube) 설치 및 Spring Boot 3 설정 - [Spring Boot 3] 소나큐브(SonarQube), JaCoCo 연동하여 정적 분석
* 설치 확인
Demo 애플리케이션(Spring Boot 3)에는 Sonarqube, JaCoCo가 연동되어 있어야하고, Gitea에 코드가 Push된 상태여야 합니다. 이후, 계속 진행 부탁드립니다.

 

Gitea Action Runner 생성
// Create Actions Runner Config
// act_runner 버전은 현재 기준으로 0.2.10이 최신입니다.
$ docker run --entrypoint="" --rm -it gitea/act_runner:0.2.10 act_runner generate-config > config.yaml
// Create Actions Runner
// ${INSTANCE_URL} = http://xx.xx.xx.xx:3000/
// ${REGISTRATION_TOKEN} = Gitea > 설정 > Actions > Runners > Create new Runner > Token 확인
// ${RUNNER_NAME} = 이름 설정
// ${RUNNER_LABELS} = 라벨 설정
$ vi runner-compose.yaml
version: "3.8"
services:
  runner:
    image: gitea/act_runner:0.2.10
    environment:
      CONFIG_FILE: /config.yaml
      GITEA_INSTANCE_URL: "${INSTANCE_URL}"
      GITEA_RUNNER_REGISTRATION_TOKEN: "${REGISTRATION_TOKEN}"
      GITEA_RUNNER_NAME: "${RUNNER_NAME}"
      GITEA_RUNNER_LABELS: "${RUNNER_LABELS}"
    volumes:
      - ./config.yaml:/config.yaml
      - ./data:/data
      - /var/run/docker.sock:/var/run/docker.sock

$ docker-compose -f runner-compose.yaml up -d

// Container 이미지 중 gitea/act_runner:0.2.10이 실행되어있는 것을 확인
$ docker ps


Gitea UI에서 그림과 같이 나온다면 성공적으로 Runner를 생성한 것 입니다.

 

Gitea CI
// demo 애플리케이션 진입
$ cd demo

// Action Runner는 .gitea 디렉토리를 바라보고 있다.
$ mkdir -p ./.gitea/workflows && cd ./.gitea/workflows

// CI 파이프라인 구축
$ vi gitea-ci.yaml
on:
  push:
    branches:
      - master

name: SonarQube Workflow
jobs:
  build:
    name: Build
    runs-on: ubuntu-22.04
    steps:
    - name: Check out Repository
      uses: actions/checkout@v4
      with:
        # Disabling shallow clone is recommended for improving relevancy of reporting
        fetch-depth: 0
    - name: Set up JDK 17
      uses: actions/setup-java@v4
      with:
        java-version: '17'
        distribution: 'temurin'
    - name: Grant execute permission for gradlew
      run: chmod +x gradlew
    - name: Build with Gradle
      run: ./gradlew build
    - name: Sonarqube Scan
      run: ./gradlew sonar -Dsonar.host.url={SonarQube URL} -Dsonar.token={Token}

$ git push 후 완료

// CI 파이프라인은 다음과 같습니다.
1. Git의 master 브랜치에 push 하는것이 트리거가 되어 Action Runner가 동작을 시작합니다.
2. Check out repository code
3. Java 17 설치
4. gradlew 권한 설정
5. gradle build
6. Sonarqube Scan

Gitea > Actions 탭 확인

 

SonarQube 결과 확인

 

참고한 사이트
* https://kdev.ing/static-analysis-performed-by-sonarqube-and-github-action/
* https://github.com/SonarSource/sonar-scanning-examples
* https://jeeneee.dev/springboot/springboot-2-introduction-of-sonarqube/
* https://sonarsource.atlassian.net/browse/SONARGRADL-135
* https://velog.io/@joon6093/%EA%B9%83%EC%95%A1%EC%85%98-CI-%EA%B5%AC%EC%B6%95
* https://docs.sonarsource.com/sonarqube/9.9/