$JENKINS_URL/pipeline-syntax/ 에서 각 스텝별로 UI로 구성해서 pipeline script snippet 을 만들수있다.declarative-linter 명령으로 lintenv.JOB_NAMEenv.BUILD_NUMBERenv.BUILD_URLparams.PARAMETER_NAMEenv.PARAMETER_NAMEpost {
always {
archiveArtifacts artifacts: 'build/libs/**/*.jar', fingerprint: true
junit 'build/reports/**/*.xml'
}
}
pipeline { agent any environment { MY_ENV = "${env.BRANCH_NAME}" } // 아래에서 MY_ENV 변수로 사용 }
steps에서 script 블록을 둔다. 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" }
SUCCESS, UNSTABLE, FAILURE로 변경가능하다.error “메시지” : 상태를 FAILURE로 변경하면서 메시지 출력혹은 직접 지정
currentBuild.result = 'UNSTABLE' // SUCCESS, UNSTABLE, FAILURE
GIT_BRANCH가 Job 파라미터 이름stage('Git Checkout') { steps { git poll: true, changelog: true, url: 'git@github.com:....git', branch: params.GIT_BRANCH credentialsId: '...' } }
agent none 으로 받고, 후속 작업에 대해서 agent를 지정해야 해당 agent가 입력에 의해 blocking 되는 것을 막을 수 있다.http://jenkinshost/scriptApproval/) 링크가 출력됨. 이것을 클릭하고서 추가한다.$JENKINS_HOME/scriptApproval.xml을 다음과 같이 편집하고 Jenkins를 재시작한다.<?xml version='1.0' encoding='UTF-8'?> <scriptApproval plugin="script-security@1.23"> <approvedScriptHashes> </approvedScriptHashes> <approvedSignatures> <string>method hudson.model.Actionable getAction java.lang.Class</string> <string>method hudson.model.Cause getShortDescription</string> <string>method hudson.model.Run getCauses</string> <string>method hudson.tasks.test.AbstractTestResultAction getFailCount</string> <string>method hudson.tasks.test.AbstractTestResultAction getFailureDiffString</string> <string>method hudson.tasks.test.AbstractTestResultAction getSkipCount</string> <string>method hudson.tasks.test.AbstractTestResultAction getTotalCount</string> <string>method org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper getRawBuild</string> </approvedSignatures> <aclApprovedSignatures/> <approvedClasspathEntries/> <pendingScripts/> <pendingSignatures/> <pendingClasspathEntries/> </scriptApproval>
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} """) } } }