Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create and Configure Jenkins job #144

Open
gloriaJun opened this issue Apr 25, 2022 · 4 comments
Open

Create and Configure Jenkins job #144

gloriaJun opened this issue Apr 25, 2022 · 4 comments
Assignees
Labels

Comments

@gloriaJun
Copy link
Owner

gloriaJun commented Apr 25, 2022

@gloriaJun gloriaJun self-assigned this Apr 25, 2022
@gloriaJun
Copy link
Owner Author

gloriaJun commented Apr 25, 2022

Create Multi branch Job

새로운 Item 에서 Multi branch를 선택해서 Job을 생성한다.

new-item

Credential에 정의한 GitHub App 인증 정보를 이용하여 github repo를 연동한다.
new-item-info

@gloriaJun
Copy link
Owner Author

Configure NodeJs Version on pipeline

pipeline script 내부에서 특정 nodejs 버전을 사용하도록 정의하기 위해서는 NodeJs plguin을 설치해준다.

nodejs-plugin

Jenkins 관리 -> Global Tools Configuration 에서 사용할 NodeJs을 선택하여 등록한다.
image

Pipeline Script

  • Declarative Pipeline
#!/usr/bin/env groovy
 
pipeline {
   agent any
 
   tools {
      nodejs "nodejs12.13.0"
    }
 
    stages {
       stage('Node') {
         steps {
           sh "which node"
           sh "node --version"
         }
      }
   }
}
  • Scripted Pipeline
node {
  env.NODEJS_HOME = tool name: 'nodejs12.13.0'
  env.PATH="${env.NODEJS_HOME}/bin:${env.PATH}"
 
  stage ('Env') {
    sh "which node"
    sh "node --version"
  }
}

@gloriaJun
Copy link
Owner Author

gloriaJun commented May 4, 2022

Generate Junit Report

Installation jest-junit

프로젝트에 jest-junit을 설치한다

yarn add --dev jest-junit 

Configure to generate report file

jest.config.js 또는 package.json에 report 파일 생성을 위한 설정을 해준다.

  • jest.config.js
reporters: ['default', 'jest-junit'],

// or
reporters: [
   'default',
   [
      'jest-junit',
      {
         outputDirectory: '<rootDir>/tests/coverages/reports',
         outputName: 'test-junit-report.xml',
      },
   ],
],
  • package.json
 "jest-junit": {
    "outputDirectory": "tests/coverages/reports",
    "outputName": "test-junit-report.xml"
  },

Configure jenkins

JUnit plugin을 설치한다.
image

jenkins pipeline 스크립트에서 test 수행 결과를 전달하기 위해 다음과 같이 추가해준다.

stage('Unit Test') {
   steps {
     sh "yarn run test:unit"
   }

    post {
      always {
        junit "tests/coverages/reports/test-junit-report.xml"
      }
    }
}

생성된 Report 확인

젠킨스스의 해당 job의 메인 화면에서는 아래와 같은 테스트 결과 그래프를 확인할 수 있다.
image

각 build job 아이템에서는 아래와 같은 테스트 리포트 파일이 생성된다.
image

또한 각 PR에서는 다음과 같이 Checks와 빌드 상태에서 테스트 진행 상태 및 결과를 확인할 수 있다.

image
image

@gloriaJun
Copy link
Owner Author

gloriaJun commented May 4, 2022

Integration with SonarQube

  • sonarQube Community Edition Version 8.9.x

SonarQube 프로젝트 생성

젠킨스와 연동하고자 하는 소나큐브 프로젝트를 생성한다.

단, project 생성을 위해서는 administrator 권한이 필요하다.

  1. → 를 클릭해서 project를 생성한다.
    image
  2. project 생성 과정에서 token을 생성 또는 "사용자 계정 → Security" (/account/security/)에서 생성한다.
    image

Configure Jenkins

Install SonarQube Scanner plugin

젠킨스 "플러그인 관리"에서 SonarQube Scanner 플러그인을 설치한다.

image

Add Credentail for sonarqube

sonarqube token 정보를 jenkins credential에 등록한다.

image

Configure SonarQube Server

"<Jenkins 관리> → <시스템 설정> → " 항목에 연동할 sonarqube 설정을 등록한다.

image

sonarqube scanner 설정

"Global Toll Configuration" 에서 SonarQube Scanner 설정을 해준다.

image

Jenkins pipeline 작성

sonarqube로 분석 결과를 전송하기 위해서는 아래와 같이 2개의 stage가 등록이 되어야한다.

  • SonarQube Analysis
    • 대상 SonarQube Server를 지정하여 분석을 진행하는 과정
    • withSonarQubeEnv는 Jenkins 관리 > 시스템 설정에 등록한 SonarQube Server Name과 매핑
  • SonarQube Quality Gate
    • SonarQube Server에서 분석 결과를 응답하기까지 대기하도록 하는 Stage, 대기 시간을 지정하여 무한정 대기하는 상태를 방지하도록 함
    • waitForQualityGate는 Server에서 분석을 완료하고 상태를 반환할때까지 파이프라인을 중단시키는 시간을 지정
#!/usr/bin/env groovy
 
pipeline {
  // ...SKIP
 
  stages {
    // ...SKIP
 
    stage('SonarQube Analysis') {
      environment {
        scannerHome = tool 'SonarQubeScanner'
      }
 
      steps {
        script{
          withCredentials([string(credentialsId: 'JPT1046', variable: 'TOKEN')]) {
            withSonarQubeEnv('SonarQubeServer') {
              sh "${scannerHome}/bin/sonar-scanner \
                    -Dsonar.projectKey=th-bank-webapp \
                    -Dsonar.sources=./src \
                    ${args} \
              "
            }
          }
        }
      }
    }
 
    stage('SonarQube Quality Gate') {    
      steps {       
        script{
          timeout(time: 10, unit: 'MINUTES') {
            waitForQualityGate abortPipeline: false
          }
        }
      }
    }   
  }
}

jest 및 lint 결과를 SonarQube로 전송하기

jest 결과를 SonarQube로 전송하기

jest 수행 결과를 sonarqube로 전송하기 위한 report를 생성하기 위해서는 해당 format으로 전환해주기 위해 jest-sonar 패키지를 설치해준다.

yarn add -D jest-sonar

jest 수행 시에 sonar report를 생성하도록 추가해준다.

reporters: [
  'default',
  [
    'jest-sonar',
    {
      outputDirectory: '<rootDir>/tests/coverages/reports',
      outputName: 'test-sonar-report.xml',
      reportedFilePath: 'absolute',
    },
  ],
],
Lint 결과를 SonarQube로 전송하기

sonarqube로 전송하기 위한 lint report 파일 생성을 위해 CI pipeline의 lint stage에 다음과 같이 추가해준다.

sh "yarn lint -- -f json -o eslint.json"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant