목차

Jenkins Pipeline

Snippet Generator

lint

기본 환경변수

Build Parameter

Notification

Archive Artifact

post {
    always {
        archiveArtifacts artifacts: 'build/libs/**/*.jar', fingerprint: true
        junit 'build/reports/**/*.xml'
    }
}

변수 및 Script in Declarative

pipeline {
    agent any
    environment {
        MY_ENV = "${env.BRANCH_NAME}"
    }
    // 아래에서 MY_ENV 변수로 사용
}
steps {
    echo 'Building Container..'
 
    script {
        if (ENVIRONMENT_NAME == 'development') {
            ENV_NAME = 'Development'
        } else if (ENVIRONMENT_NAME == 'release') {
            ENV_NAME = 'Production'
        }
    }
    echo 'Building Branch: ' + env.BRANCH_NAME
    echo 'Build Number: ' + env.BUILD_NUMBER
    echo 'Building Environment: ' + ENV_NAME
 
    echo "Running your service with environemnt ${ENV_NAME} now"
}

강제 상태변경

혹은 직접 지정

currentBuild.result = 'UNSTABLE' // SUCCESS, UNSTABLE, FAILURE

Git Branch Parameter

stage('Git Checkout') {
    steps {
        git poll: true,
        changelog: true,
        url: 'git@github.com:....git',
        branch: params.GIT_BRANCH
        credentialsId: '...'
    }
}

Pipeline 에서 입력 값 받기

JUnit 테스트 결과 메일/Slack 발송

import jenkins.model.*
import hudson.tasks.test.AbstractTestResultAction
 
String testResultMessage = '테스트가 올바로 실행되지 못했음' // 이 변수에 테스트 메시지 저장
 
pipeline {
    stages {
 
        stage('Test Reports') {
            steps {
                junit '**/build/test-results/**/*.xml'
 
                script {
                    AbstractTestResultAction testResultAction =  currentBuild.rawBuild.getAction(AbstractTestResultAction.class)
                    if (testResultAction != null) {
                        def totalNumberOfTests = testResultAction.totalCount
                        def failedNumberOfTests = testResultAction.failCount
                        def failedDiff = testResultAction.failureDiffString
                        def skippedNumberOfTests = testResultAction.skipCount
                        def passedNumberOfTests = totalNumberOfTests - failedNumberOfTests - skippedNumberOfTests
                        testResultMessage = "Passed: ${passedNumberOfTests}; Failed: ${failedNumberOfTests} ${failedDiff}; Skipped: ${skippedNumberOfTests}  out of ${totalNumberOfTests}"
                    }
                }
            }
        }
    }
    post {
        failure {
            slackSend(channel: '#build-ci', color: '#FF0000', message: """빌드 실패: Job ${env.JOB_NAME} [${env.BUILD_NUMBER}] TargetBranch: ${params.GIT_BRANCH} (${env.BUILD_URL})
 - 테스트 실패 : ${testResultMessage}
""")
        }
    }
}

parallel pipeline

참조