Jenkins 빌드 에러시 Slack 연동 방법

Jenkins에서 빌드 에러시 이메일 등으로 Notification이 가능하지만, Slack을 이용한 Notification도 보편적이므로 Jenkins와 slack 연동 방법을 소개합니다.

1. Slack에 incoming-webhook 설정

  • Slack에서 Notification을 받을 채널을 생성 (예 #alert)
  • 채널에서 +Add an app 클릭
  • ‘Incoming WebHooks’를 검색하고 [Add Configuration] 클릭
  • Noti.를 받을 Slack의 채널을 선택하고 [Add Incoming WebHooks integration]을 클릭
  • 생성된 Webhook URL을 확인

2. Jenkins 설정

  • Slack Notification 플러그인 설치
  • 3. Jenkins에 New Item 생성
  • Pipeline 생성
  • Pipeline Script 작성
    - 테스트 수행 도중 강제로 exit 1을 시켜, catch를 통해 slack에 송신 Script

— Script 내용

node {
try {
stage('build') {
println('so far so good...')
}
stage('test') {
println('A test has failed!')
sh 'exit 1'
}
} catch(e) {
// mark build as failed
currentBuild.result = "FAILURE";
// send slack notification
slackSend (color: '#FF0000', message: "FAILED: Job
'${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
// throw the error
throw e;
}
}
  • 빌드 후 조치 추가 > Slack Notifications
  • Slack compatible app URL 입력
  • Integration Token Credential ID의 Add 버튼에서 Jenkins 선택
    - Kind : Secret text
    - Secret : Slack 화면의 Webhook URL의 뒷부분 복사
  • Slack 연동 테스트 : “Test Connection” 클릭
  • Slack의 #alert 채널에 들어온 메시지 확인

4. Build 수행

  • exit 1에 의해 에러가 발생되며, Slack에 문자 발송

5. Slack에 전송된 빌드 에러 메시지

--

--