-
Notifications
You must be signed in to change notification settings - Fork 328
/
Copy pathci.jenkinsfile
178 lines (129 loc) · 5.29 KB
/
ci.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!groovy
@Library('jenkinslibrary@master') _
//func from shareibrary
def build = new org.devops.build()
def deploy = new org.devops.deploy()
def tools = new org.devops.tools()
def gitlab = new org.devops.gitlab()
def toemail = new org.devops.toemail()
def sonar = new org.devops.sonarqube()
def sonarapi = new org.devops.sonarapi()
def nexus = new org.devops.nexus()
def artifactory = new org.devops.artifactory()
def runOpts
//env
String buildType = "${env.buildType}"
String buildShell = "${env.buildShell}"
String deployHosts = "${env.deployHosts}"
String srcUrl = "${env.srcUrl}"
String branchName = "${env.branchName}"
String artifactUrl = "${env.artifactUrl}"
if ("${runOpts}" == "GitlabPush"){
branchName = branch - "refs/heads/"
currentBuild.description = "Trigger by ${userName} ${branch}"
gitlab.ChangeCommitStatus(projectId,commitSha,"running")
env.runOpts = "GitlabPush"
} else {
userEmail = "[email protected]"
}
//pipeline
pipeline{
agent { node { label "build"}}
stages{
stage("CheckOut"){
steps{
script{
println("${branchName}")
tools.PrintMes("获取代码","green")
checkout([$class: 'GitSCM', branches: [[name: "${branchName}"]],
doGenerateSubmoduleConfigurations: false,
extensions: [],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: 'gitlab-admin-user', url: "${srcUrl}"]]])
}
}
}
stage("Build"){
steps{
script{
tools.PrintMes("执行打包","green")
//build.Build(buildType,buildShell)
artifactory.main(buildType,buildShell)
artifactory.PushArtifact()
//上传制品
//nexus.main("nexus")
//发布制品
//sh " wget ${artifactUrl} && ls "
//deploy.SaltDeploy("${deployHosts}","test.ping")
//deploy.AnsibleDeploy("${deployHosts}","-m ping ")
}
}
}
stage("QA"){
steps {
script{
tools.PrintMes("搜索项目","green")
result = sonarapi.SerarchProject("${JOB_NAME}")
println(result)
if (result == "false"){
println("${JOB_NAME}---项目不存在,准备创建项目---> ${JOB_NAME}!")
sonarapi.CreateProject("${JOB_NAME}")
} else {
println("${JOB_NAME}---项目已存在!")
}
tools.PrintMes("配置项目质量规则","green")
qpName="${JOB_NAME}".split("-")[0] //Sonar%20way
sonarapi.ConfigQualityProfiles("${JOB_NAME}","java",qpName)
tools.PrintMes("配置质量阈","green")
sonarapi.ConfigQualityGates("${JOB_NAME}",qpName)
tools.PrintMes("代码扫描","green")
sonar.SonarScan("test","${JOB_NAME}","${JOB_NAME}","src")
sleep 30
tools.PrintMes("获取扫描结果","green")
result = sonarapi.GetProjectStatus("${JOB_NAME}")
println(result)
if (result.toString() == "ERROR"){
toemail.Email("代码质量阈错误!请及时修复!",userEmail)
error " 代码质量阈错误!请及时修复!"
} else {
println(result)
}
}
}
}
}
post {
always{
script{
println("always")
}
}
success{
script{
println("success")
if ("${runOpts}" == "GitlabPush"){
gitlab.ChangeCommitStatus(projectId,commitSha,"success")
}
toemail.Email("流水线成功",userEmail)
}
}
failure{
script{
println("failure")
if ("${runOpts}" == "GitlabPush"){
gitlab.ChangeCommitStatus(projectId,commitSha,"failed")
}
toemail.Email("流水线失败了!",userEmail)
}
}
aborted{
script{
println("aborted")
if ("${runOpts}" == "GitlabPush"){
gitlab.ChangeCommitStatus(projectId,commitSha,"canceled")
}
toemail.Email("流水线被取消了!",userEmail)
}
}
}
}