-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathJenkinsfile
237 lines (218 loc) · 8.57 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
pipeline {
/**
* Jenkins 选项和运行时选项
*/
agent { // any
node { // 可选
label "Python3"
}
}
options{
timestamps()
disableConcurrentBuilds() // 禁止并行,每次只允许一个构建。
timeout(time: 30, unit: 'MINUTES') // java.util.concurrent.TimeUnit
}
/**
* 自定义环境变量
*/
environment {
GIT_URL = 'git@github.com:jeffwji/Dashboard.git'
CREDENTIAL = 'Github'
BRANCH = 'develop'
PYPI_CREDENTIAL = "PypiRepo"
}
/**
* 编译过程由多个 Stage(阶段)构成.
*/
stages {
/**
* 每个阶段下可再分 step
*/
stage('Clear up workspace') {
steps {
// 打印环境变量
sh 'printenv'
// 清理缓存缓存
echo "Delete workspace ${workspace}"
dir("${workspace}") {
deleteDir()
}
dir("${workspace}@tmp") {
deleteDir()
}
}
}
/**
* Dload source code
*/
stage('Get code from Github') {
steps {
script{ // Java script
println('Get code from Github') // println 等效于 echo
}
git branch: "${env.BRANCH}", credentialsId: "${env.CREDENTIAL}", url: "${env.GIT_URL}" // 使用 env 名称空间下的环境变量
}
}
/**
* Install dependency for coverage and unittest(nosetests) and language checking
*/
stage('Install testing dependency') {
steps {
echo 'Initial virtual environment'
dir("${workspace}") {
sh "python3 -m venv .venv"
}
echo 'Install testing dependency'
/**
* withPythonEnv 函数需要 Jenkins Pyenv plugin 支持
*/
withPythonEnv("${workspace}/.venv/bin/"){
dir("${workspace}") {
sh 'apk add --no-cache py3-qt5 freetype zlib-dev jpeg-dev'
sh 'sed "s/^PyQt5/#PyQt5/" -i requirements.txt'
sh 'pip install wheel nose coverage nosexcover pylint twine pytest'
sh 'pip install -r requirements.txt'
sh 'pip list'
}
}
}
}
/**ocker
* Run coverage or regular unit test
*/
stage('Testing') {
steps {
echo 'Run test cases'
withPythonEnv("${workspace}/.venv/bin/"){
dir("${workspace}") {
// sh 'python setup.py test'
sh 'nosetests -sv --with-xunit --xunit-file=nosetests.xml --with-xcoverage --xcoverage-file=coverage.xml'
}
}
}
}
/**
* Call SonarQube scanner
* It will load in coverage and other reports generated from previous step.
*/
stage('Sonar scan') {
steps {
withPythonEnv("${workspace}/.venv/bin/"){
dir("${workspace}") {
script {sonarHome=tool 'SonarQube Scanner'} // name is defined in `Global Tool Configuration`
withSonarQubeEnv('MySonarQube') { // name is defined in `Configure System`
sh 'echo workspace=${sonarHome}'
sh 'sonar-scanner \
-Dsonar.host.url=http://192.168.56.110:9000 \
-Dsonar.projectKey=Dashboard \
-Dsonar.projectVersion=1.0 \
-Dsonar.language=py \
-Dsonar.tests=./tests \
-Dsonar.exclusions=setup.py,**/__init__.py \
-Dsonar.sourceEncoding=UTF-8 \
-Dsonar.python.xunit.reportPath=nosetests.xml \
-Dsonar.python.coverage.reportPaths=coverage.xml \
-Dsonar.python.pylint=/usr/local/bin/pylint \
-Dsonar.python.pylint_config=.pylintrc \
-Dsonar.python.pylint.reportPath=pylint-report.txt'
}
}
}
timeout(time: 10, unit: 'MINUTES') {
waitForQualityGate abortPipeline: true
}
}
}
/**
* Package
*
* 关于 Python 版本规范:https://www.python.org/dev/peps/pep-0440/
*/
stage('Build') {
steps {
echo 'Build Dashboard'
withPythonEnv("${workspace}/.venv/bin/"){
dir("${workspace}") {
sh 'sed "s/^#PyQt5/PyQt5/" -i requirements.txt'
sh 'sed "1,2d" -i requirements.txt'
sh 'cat requirements.txt'
sh 'python setup.py egg_info -b.dev$(date "+%s") bdist_wheel'
}
}
}
}
/**
* Pushing to Nexus
*
* Run the following commands on client:
* pip config set global.index http://192.168.10.65:8081/repository/pypi-central/simple/
* pip config set global.index-url http://192.168.10.65:8081/repository/pypi-central/simple/
* pip config set global.trusted-host 192.168.10.65
* pip config set global.extra-index-url http://192.168.10.65:8081/repository/pypi/simple/
*/
stage('Pushing to Repository') {
steps {
echo 'Upload Dashboard'
withPythonEnv("${workspace}/.venv/bin/"){
dir("${workspace}") {
withCredentials([usernamePassword(credentialsId: "${env.PYPI_CREDENTIAL}", passwordVariable: 'pass', usernameVariable: 'user')]){
sh '''cat << EOF > .pypirc
[distutils]
index-servers=
internal_pypi
[internal_pypi]
repository: http://192.168.56.110:8081/repository/pypi/
username: ${user}
password: ${pass}
EOF'''
sh 'twine upload --config-file=.pypirc -r internal_pypi dist/*'
}
}
}
}
}
}
/**
* 构建后行为
*/
post{
// 总是执行
always{
echo "Always"
}
// 条件执行
success {
echo currentBuild.description = "Success" // currentBuild.description 会将信息带回控制面板
/**
emailext (
subject: "SUCCESSFUL: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'",
body: """<p>SUCCESSFUL: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':</p>
<p>Check console output at "<a href='${env.BUILD_URL}'>${env.JOB_NAME} [${env.BUILD_NUMBER}]</a>"</p>""",
recipientProviders: [[$class: 'DevelopersRecipientProvider'], [$class: 'RequesterRecipientProvider']]
)
*/
}
failure{
echo currentBuild.description = "Failure"
/**
emailext (
subject: "FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'",
body: """<p>FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':</p>
<p>Check console output at "<a href='${env.BUILD_URL}'>${env.JOB_NAME} [${env.BUILD_NUMBER}]</a>"</p>""",
recipientProviders: [[$class: 'DevelopersRecipientProvider'], [$class: 'RequesterRecipientProvider']]
)
*/
}
aborted{
echo currentBuild.description = "Aborted"
/**
emailext (
subject: "Aborted: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'",
body: """<p>ABORTED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':</p>
<p>Check console output at "<a href='${env.BUILD_URL}'>${env.JOB_NAME} [${env.BUILD_NUMBER}]</a>"</p>""",
recipientProviders: [[$class: 'DevelopersRecipientProvider'], [$class: 'RequesterRecipientProvider']]
)
*/
}
}
}