본문 바로가기
CICD

jenkins container - Spring 프로젝트 SonarQube 연동

by 장중앙 2024. 3. 10.

기존 Jenkins에서 Spring 프로젝트 CI/CD에 SonarQube 분석 연동

SonarQube는 EC2내에서 docker 컨테이너 실행

EC2내 SonarQube 실행

SonarQube 이미지 다운 및 실행

docker pull sonarqube
docker run -d --name sonarqube -p 9000:9000 sonarqube

 

젠킨스 이미지 다운 중 no space left on device 발생

https://jangcenter.tistory.com/142

 

[EC2] no space left on device

EC2에 SonarQube 이미지를 다운받는 중 no space left on device 발생 df -h 로 파티션 용량을 확인해보니 기본 EC2 볼륨의 8Gb를 대부분 사용해서 이미지 다운이 안됨 EC2의 볼륨 크기 및 파티션 용량 수정이

jangcenter.tistory.com

 

SonarQube 최초 접속

초기 ID/PW : admin

 

젠킨스에서의 연동을 위한 토큰 생성

administration - Security

 

SonarQube 프로젝트 생성

프로젝트명/브렌치명


SonarQube 분석을 위한 Spring 프로젝트 수정

build.gradle 코드 추가

plugins {
	...
	id "org.sonarqube" version "4.4.1.3373"
}

...

sonar {
	properties {
		// sonar.projectKey - SonarQube에서 프로젝트 식별명
		property "sonar.projectKey", "{SonarQube project name}"
		// sonar.projectName - SonarQube 대시보드에서 프로젝트 표기명
		property "sonar.projectName", "{SonarQube project name}"
	}
}

SonarQube 연동을 위한 Jenkins 설정

SonarQube Scanner 플러그인 설치

 

 

SonarQube에서 생성한 토큰 등록

ID : Jenkins내에서의 별칭

Secret : SonarQube 토큰 값

 

SonarQube 서버 등록

Jenkins관리 - System

name : Jenkins내 별칭

Server URL: SonarQube 서버 URL (ec2 public IP:9000)

Serer Authentication token : Jenkins Credential에 저장한 SonarQube 토큰

 

SonarQube Scanner 설정

Jenkins 관리 - Tools

 

Jenkins 파이프라인 코드 수정

pipeline {
    agent any

    environment {
        PROJECT_NAME      = ''             // 프로젝트 명
        GIT_URL           = ''             // git URL
        REMOTE_USER       = ''             // EC2 인스턴스의 사용자 이름
        REMOTE_HOST       = ''             // EC2 인스턴스의 퍼블릭 IP 주소
        DOCKER_CREDENTIAL = ''             // docker Credntial ID
        DOCKER_IMAGE      = ''
        DOCKER_USERNAME   = ''
        DOCKER_PASSWORD   = ''
    }

    stages {
        stage('Pre build') {
            steps {
                sh "git --version"
                git branch: 'dev', credentialsId: 'github_token', url: "${GIT_URL}"
                sh "chmod +x gradlew"
            }
        }
        stage('SonarQube analysis') {
            steps {
                // SonarQube-Server = sonarjenkins관리 - System - SonarQube servers 이름
                withSonarQubeEnv('SonarQube-Server') {
                    sh './gradlew sonar'
                }
            }
        }
        stage('Build image') {
            steps {
                echo 'Build Docker'
                sh "docker -v"
                
                script {
                    withCredentials([usernamePassword(credentialsId: "${DOCKER_CREDENTIAL}", usernameVariable: 'DOCKER_USERNAME', passwordVariable: 'DOCKER_PASSWORD')]) {
                        DOCKER_USERNAME = "${DOCKER_USERNAME}"
                        DOCKER_PASSWORD = "${DOCKER_PASSWORD}"
                        DOCKER_IMAGE = "${DOCKER_USERNAME}/${PROJECT_NAME}:latest"
                    }
                    
                    sh "docker build --platform linux/amd64 -t ${DOCKER_IMAGE} ."
                    sh "docker inspect ${DOCKER_IMAGE}"
                   
                }
                
            }
        }
        
        stage('Post build') {
            steps {
                echo 'Push Docker'
                script {
                    try {
                        sh "docker login -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD}"
                        sh "docker push ${DOCKER_IMAGE}"
                    } catch (Exception e) {
                        currentBuild.result = 'FAILURE' // 파이프라인을 실패로 표시
                        error "Failed to push Docker image: ${e.message}" // 에러 메시지 출력 후 파이프라인 중단

                    }
                }
            }
        }
        
        stage('Deploy to EC2') {
            steps {
                script {
                    def remote = [:]
                    withCredentials([sshUserPrivateKey(credentialsId: "EC2_SSH", keyFileVariable: 'key')]) {
                        remote.name = "Remote_EC2"
                        remote.host = "${REMOTE_HOST}"
                        remote.user = "${REMOTE_USER}"
                        remote.allowAnyHosts = true
                        remote.identityFile = key // Jenkins Credential에 등록된 SSH 키
                        
                        try {
                            echo 'Log in to docker in ec2'
                            sshCommand remote: remote, command: "docker login  -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD}"
                            
                            echo 'stop and remove container'
                            sshCommand remote: remote, command: "docker stop ${PROJECT_NAME}"
                            
                            sshCommand remote: remote, command: "docker rm ${PROJECT_NAME}"
                            
                            echo 'remove docker image'
                            sshCommand remote: remote, command: "docker rmi ${DOCKER_IMAGE}"
                        } catch (e) {
                            sh 'echo "fail to stop and remove container"'
                        }
                        sshCommand remote: remote, command: "docker pull ${DOCKER_IMAGE}"
                            
                        sshCommand remote: remote, command: "docker run -d --name ${PROJECT_NAME} -p 8080:8080 ${DOCKER_IMAGE}"
                    
                    }                    
                }
            }
        }
        
    }
}

 

'CICD' 카테고리의 다른 글

jenkins container - Vue 프로젝트 EC2 배포  (0) 2024.03.03
docker-compose  (0) 2023.04.08
Docker와 DockerFile  (2) 2023.04.08
[Kubernetes] Autoscaler(HPA)  (0) 2022.02.17
[Kubernetes] Ingress(Controller)  (0) 2022.02.08

댓글