-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathJenkinsfile
143 lines (116 loc) · 4.29 KB
/
Jenkinsfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
pipeline {
// 어떠한 에이전트에서도 실행 가능함을 표현
agent any
environment {
// jenkins 가 관리하는 도구의 위치는 이와 같이 환경 변수로 저장 가능
SONAR_SCANNER_HOME = tool name: 'sonar-scanner', type: 'hudson.plugins.sonar.SonarRunnerInstallation'
}
parameters {
booleanParam(defaultValue: isDeploymentNecessary(), description: '배포 포함 여부', name: 'DEPLOY_ENABLED')
}
options {
buildDiscarder logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '5', numToKeepStr: '5')
githubProjectProperty(displayName: '', projectUrlStr: 'https://github.com/fastcampus-jenkins/fastcampus-jenkins')
// 디폴트 checkout skip 설정 제거
}
triggers {
issueCommentTrigger('.*(test this|build this|deploy this).*')
}
// stages > stage > steps 순으로 구성
stages {
stage('Build') {
steps {
// withGradle 을 하면, Gradle 로그를 해석
dir("projects/spring-app") {
withGradle {
sh "./gradlew build"
}
}
}
}
stage('SonarScanner') {
when {
expression {
return isSonarQubeNecessary()
}
}
steps {
// sonarqube 환경하에서, 실행
withSonarQubeEnv("sonarqube-server") {
sh """
${env.SONAR_SCANNER_HOME}/bin/sonar-scanner \
-Dsonar.host.url=http://sonarqube:9000 \
-Dsonar.projectKey=sample \
-Dsonar.projectBaseDir=${WORKSPACE}/projects/spring-app
"""
}
// quality gate 통과시 까지 대기
timeout(time: 1, unit: 'MINUTES') {
// Parameter indicates whether to set pipeline to UNSTABLE if Quality Gate fails
// true = set pipeline to UNSTABLE, false = don't
waitForQualityGate abortPipeline: true
}
}
}
}
post {
always {
scanForIssues tool: ktLint(pattern: '**/ktlint/**/*.xml')
junit '**/test-results/**/*.xml'
jacoco sourcePattern: '**/src/main/kotlin'
script {
if (isNotificationNecessary()) {
mineRepository()
emailext attachLog: true, body: email_content(), subject: email_subject(), to: 'junoyoon@gmail.com'
slackSend(channel: "#jenkins", message: "${custom_msg(currentBuild.currentResult)}")
}
}
}
success {
script {
if (params.DEPLOY_ENABLED == true) {
archiveArtifacts artifacts: 'projects/spring-app/build/libs/*-SNAPSHOT.jar', followSymlinks: false
build(
job: 'pipeline-deploy',
parameters: [booleanParam(name: 'ARE_YOU_SURE', value: "true")],
wait: false,
propagate: false
)
}
if (isPr()) {
echo "pipeline-deploy 실행"
if (env.CHANGE_ID) {
pullRequest.comment('This PR invoked pipeline-deploy..')
}
}
}
}
}
}
// pipeline 바깥쪽 영역은 groovy 사용 가능
def email_content() {
return '''이 이메일은 중요한 것이여!!
${DEFAULT_CONTENT}
'''
}
def email_subject() {
return '빌드통지!! - ${DEFAULT_SUBJECT}'
}
def custom_msg(status) {
return " $status: Job [${env.JOB_NAME}] Logs path: ${env.BUILD_URL}/consoleText"
}
def isSonarQubeNecessary() {
return isMainOrDevelop()
}
def isDeploymentNecessary() {
return isMainOrDevelop() || (env.GITHUB_COMMENT ?: "").contains("deploy this")
}
def isNotificationNecessary() {
return !isPr()
}
def isMainOrDevelop() {
return (env.BRANCH_NAME == "develop" || env.BRANCH_NAME == "main")
}
def isPr() {
return env.BRANCH_NAME.startsWith("PR-")
}