Jenkins pipeline으로 Slack notification 받기
June/개발도구

Jenkins pipeline으로 Slack notification 받기

개요

Jenkins는 현재 웹솔루션 개발단에서 사용하는 써드파티 툴이다.
젠킨스를 통하여 빌드, 업로드, 배포 등이 이뤄지는데, 각 단계별로 적절한 알람을 받아보자.
알람을 전달받는 매체는 slack 이라는 메신저를 사용한다.

구조

1) 젠킨스 파이프라인을 통하여 AWS 코드 디플로이를 트리거
2) 배포의 시작과 종료 알람 전달
3) 배포의 성공 실패 여부 알람 전달
4) 문법 : Groovy 

단계

1) Slack 채널 생성

2) 채널 설정 -> Add apps 클릭

3) Jenkins CI 플러그인 설치

4) TOKEN 생성
 > TOKEN은 항상 보안유지에 신경 쓸 것!
 > Jenkins CI 트리거를 발생시키는 주소 :
    https://{your-workspace-name}.slack.com/services/hooks/jenkins-ci/

5) Jenkins pipeline script 작성

import groovy.transform.Field

@Field String defaultChannel = '#testingnotification'
@Field String channelToken = 'yourtoken'

def sendStart(msg) {
    sendMsg(defaultChannel, '61E858', '*[PR] '+ msg +' 배포시작*')
}
def sendSuccess(msg) {
    sendMsg(defaultChannel, '61E858', '*[PR] '+ msg +' 배포완료*')
}
def sendError(msg) {
    sendMsg(defaultChannel, 'E80503', '*[PR] '+ msg +' 배포실패*')
}
def sendMsg(chnl, color, msg) {
    slackSend baseUrl: 'https://midasitwebop-test.slack.com/services/hooks/jenkins-ci/', channel: chnl,  color: color, message: msg , token: channelToken
}

> 자신의 필요에 맞게 메시지를 만들어주고 node에 추가해준다.

6) 결과 확인