gloriaJun / til

Lessoned Learned
3 stars 0 forks source link

Create and Configure Jenkins job #144

Open gloriaJun opened 2 years ago

gloriaJun commented 2 years ago
gloriaJun commented 2 years ago

Create Multi branch Job

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

new-item

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

new-item-info
gloriaJun commented 2 years ago

Configure NodeJs Version on pipeline

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

nodejs-plugin

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

Pipeline Script

pipeline { agent any

tools { nodejs "nodejs12.13.0" }

stages {
   stage('Node') {
     steps {
       sh "which node"
       sh "node --version"
     }
  }

} }


- Scripted Pipeline
```groovy
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 commented 2 years ago

Generate Junit Report

Installation jest-junit

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

yarn add --dev jest-junit 

Configure to generate report file

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

// or reporters: [ 'default', [ 'jest-junit', { outputDirectory: '/tests/coverages/reports', outputName: 'test-junit-report.xml', }, ], ],


- package.json
```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 commented 2 years ago

Integration with SonarQube

SonarQube 프로젝트 생성

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

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

  1. 를 클릭해서 project를 생성한다. ![image](https://user-images.githubusercontent.com/25721616/166667120-752be89d-2591-4f22-b520-4ce2962ee887.png)
  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가 등록이 되어야한다.

#!/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"