1. Jenkins Item 생성 (Pull Request)
2. webhook token 설정
3. Parameters 정의
3.1. parameter catch 방법 선택
3.2. 사용할 변수들을 정의한다.
3.3. 정의한 변수들
1. SOURCE_BRANCH
-- PR 타켓 branch
-- expression: $.pullrequest.source.branch.name
2. COMMENT_URL
-- pipeline에서 정의한 stage 수행 시 comment 기록를 위한 url
-- expression: $.pullrequest.links.comments.href
3. DECLINE_URL
-- pipeline에서 정의한 stage 실패 시 PR decline을 위한 url
-- expression: $.pullrequest.links.decline.href
4. REPO_URL
-- git repository url
-- expression: $.pullrequest.destination.repository.links.html.href4. Pipeline 작성
node {
withCredentials([usernameColonPassword(credentialsId: 'bitbucket-auth-id-pw', variable: 'USER')]) {
stage ('Clone') { //clone 타켓 branch
try {
git branch: '$SOURCE_BRANCH', credentialsId: 'bitbucket-auth-id-pw', url: '$REPO_URL'
} catch(e) {
sh '''
curl -X POST -u "$USER" $COMMENT_URL -d '{"content": {"raw": "[CLONE] FAIL"}}' -H 'Content-Type: application/json';
curl -X POST -u "$USER" $DECLINE_URL -d '{"content": {"raw": "[CLONE] FAIL"}}' -H 'Content-Type: application/json';
echo [CLONE] FAIL;
exit 1;
'''
}
}
stage ('Build') { //테스트 없이 소스코드 빌드 ( 컴파일 오류 체크 ), 실패 시 해당 PR comment 등록 및 decline
sh '''
{
echo [BUILD - BACKEND]
chmod +x ./gradlew
./gradlew clean build -x test \
&& curl -X POST -u "$USER" $COMMENT_URL -d '{"content": {"raw": "[BUILD] SUCCESS"}}' -H 'Content-Type: application/json' > /dev/null 2>&1 \
&& echo [BUILD] SUCCESS;
}||\
{
curl -X POST -u "$USER" $COMMENT_URL -d '{"content": {"raw": "[BUILD] FAIL"}}' -H 'Content-Type: application/json';
curl -X POST -u "$USER" $DECLINE_URL -d '{"content": {"raw": "[BUILD] FAIL"}}' -H 'Content-Type: application/json';
echo [BUILD] FAIL;
exit 1;
}
'''
}
}
}> [작업로그] CI/CD 구축 #1 - CI/CD 시나리오
> [작업로그] CI/CD 구축 #2 - Docker, Docker Compose 설치
> [작업로그] CI/CD 구축 #3 - BitBucket 설정 및 Jenkins 설치