
트리거 -> Stage(step1, step2 ..) -> Stage(step1, step2,...) -> ... -> 알림여러 개의 stage를 갖는 스크립트 작성
pipeline{
agent any
stages{
stage("First Stage"){
steps{
echo "Step1. Hello World"
}
}
stage("Second Stage"){
steps{
echo "Step2. Hello World"
echo "Step3. Hello World"
}
}
}
}







pipeline의 구문
pipeline{
agent any //에이전트 지정
triggers { cron('* * * * *') } //1분마다 수행
options { timeout(time: 5)} //5분이상 실행되면 중단
//시작하기 전에 boolean 형 파라미터를 요청
parameters {
booleanParam(name: 'DEBUG_BUILD', defaultValue: true,
description: 'Is it the debug build')
}
stages{
stage('Sample'){
environment {NAME = 'RAPA'} //NAME이라는 환경 변수를 설정
//DEBUG_BUILD 가 true 인 경우
when { expression { return params.DEBUG_BUILD} }
steps{
echo "Hello from $NAME"
script {
def browsers = ['chrome', 'firefox']
for(int i=0; i<browsers.size(); ++i){
echo "Testing the ${browsers[i]} browser"
}
}
}
}
}
post {always {echo "I will always say Hello again"}} //실행 중 오류 발생 여부 와 상관없이 출력
}
cron trigger 에 의해 분마다 수행된다.

Options: 파이프라인에서만 사용하는 옵션을 정의
Environment: 빌드에서 사용하는 환경 변수를 Key-Value 형태로 정의
Parameters: 파이프라인을 시작할 때 제공되는 사용자 파라미터 정의
Stage: step을 논리적으로 그룹화
When: 스테이지가 어떤 조건에 실행되어야 하는지를 정의
Tools: 설치할 도구를 정의하고 PATH에 추가
Input: 매개변수 입력
Parallel: 병렬로 실행할 스테이지를 지정
Matrix: 특정 스테이지에서 병렬로 실행할 매개변수의 조합을 지정


pipeline{
agent any
stages{
stage("Checkout"){
steps{
git url:'https://github.com/yachae1101/jenkins-test.git', branch:'main'
}
}
}
}
javac 파일명으로 컴파일을 하지만 gradle에서는 ./gradlew compileJava
git init
git add .
git commit -m "init"
git branch -M main
git remote add origin https://github.com/yachae1101/calculator.git
git push origin main
pipeline{
agent any
stages{
stage("Checkout"){
steps{
git url:'https://github.com/yachae1101/calculator.git', branch:'main'
}
}
stage("Compile"){
steps{
sh "chmod +x ./gradlew"
sh "./gradlew compileJava"
}
}
}
}
컴파일을 하고 난 후 단위 테스트를 수행
JUnit 을 이용해서 수행: .gradlew test
실행: .gradlew bootRun
프로젝트에 Service 클래스를 추가하고 작성(Calculator)
import org.springframework.stereotype.Service;
@Service
public class Calculator {
public int sum(int a, int b){
return a + b;
}
}
Controller 클래스를 만들어서 사용자의 요청을 처리하는 메서드를 작성(CalculatorController)
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequiredArgsConstructor
public class CalculatorController {
private final Calculator calculator;
@RequestMapping("/sum")
String sum(@RequestParam("a") Integer a,
@RequestParam("b") Integer b){
return String.valueOf(calculator.sum(a, b));
}
}
실행 한 후 브라우저에서 입력: http://localhost:8080/sum?a=10&b=20
10 + 20 의 결과값인 30 이 출력된다.

test 디렉토리에 테스트를 위한 클래스를 추가하고 작성(CalculatorTest)
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class CalculatorTest {
private Calculator calculator = new Calculator();
@Test
public void testSum(){
assertEquals(5, calculator.sum(2, 3));
}
}
build.gradle의 dependencies 부분에 라이브러리 의존성 추가하고 리빌드 버튼을 클릭
testImplementation 'junit:junit:4.13.2'
./gradlew test 명령으로 테스트를 수행

jenkins script 파일에 Test stage를 추가
pipeline{
agent any
stages{
stage("Checkout"){
steps{
git url:'https://github.com/yachae1101/calculator.git', branch:'main'
}
}
stage("Compile"){
steps{
sh "chmod +x ./gradlew"
sh "./gradlew compileJava"
}
}
stage("Test"){
steps{
sh "./gradlew test"
}
}
}
}
IntelliJ에서 git push 를 진행
Build를 수행
Checkout을 하고, Compile을 하고 Test Stage에서 JUnit을 이용해서 단위 테스트까지 수행한 것을 확인할 수 있다.

pipeline{
agent any
stages {
stage("Permission"){
steps{
sh "chmod +x ./gradlew"
}
}
stage("Compile"){
steps{
sh "./gradlew compileJava"
}
}
stage("Test"){
steps{
sh "./gradlew test"
}
}
}
}


JaCoCo를 이용한 코드 커버리지 과정
JaCoCo 를 그래들 구성에 추가
: build.gradle 파일의 plugins 부분에 id 'jacoco'

빌드 중단 조건을 설정하고자 하는 경우에는 build.gradle에 설정을 추가
jacocoTestCoverageVerification{
violationRules {
rule{
limit{
minimum = 0.2
}
}
}
}

터미널에서 실행
./gradlew test jacocoTestCoverageVerification

보고서 만들기
./gradlew test jacocoTestReport
보고서 확인: build/reports/jacoco/test/html/index.html


code coverage 스테이지를 Jenkinsfile의 파이프라인에 추가 후 push
pipeline{
agent any
stages {
stage("Permission"){
steps{
sh "chmod +x ./gradlew"
}
}
stage("Compile"){
steps{
sh "./gradlew compileJava"
}
}
stage("Test"){
steps{
sh "./gradlew test"
}
}
stage("Code Coverage"){
steps{
sh "./gradlew jacocoTestCoverageVerification"
sh "./gradlew jacocoTestReport"
}
}
}
}
Jenkins에서 Build
추가된 Code Coverage Stage까지 수행하고 build가 완료되었다.
