pipeline jnlp

通过默认的jnlp

  1. jenkins->系统管理->系统设置->新增一个云(kubernetes)
  2. kubernetes相应设置:
  • Name: kubernetes_jnlp
  • Kubernetes URL: http://127.0.0.1:8080(通过http访问不用添加certificate key,不然要使用6443的https协议:API Server证书经过知名CA签名的)
  • Disable https certifiacate check: 勾选
  • Kubernetes Namespace: default
  • Credentials: none
  • Jenkins URL: http://192.168.0.14:9191/jenkins
  • 如果在K8S集群内部可以直接访问Jenkins URL,则Jenkins tunnel不需要填写

pipeline scripts

指定kubernetes的相应pod(jnlp)来执行pipeline:

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
pipeline {
agent {
kubernetes {
label 'mypod'
cloud 'kubernetes_jnlp'
//podTemplate {
// inheritFrom "mypod"
//}
}
}

parameters {
//git代码路径【参数值对外隐藏】
string(name:'repoUrl', defaultValue: 'http://git.galaxymx.com/yunwei/oms.git', description: 'git代码路径')
string(name:'repoBranch', defaultValue: 'master', description: 'git分支名称')
//pom.xml的相对路径
string(name:'pomPath', defaultValue: 'pom.xml', description: 'pom.xml的相对路径')
//war包的相对路径
string(name:'warLocation', defaultValue: '/data/app/war/target/*.war', description: 'war包的相对路径 ')
//服务器参数采用了组合方式,避免多次选择,使用docker为更佳实践【参数值对外隐藏】
choice(name: 'server',choices:'192.168.1.107,9090,*****,*****\n192.168.1.60,9090,*****,*****', description: '测试服务器列表选择(IP,JettyPort,Name,Passwd)')
//测试服务器的dubbo服务端口
string(name:'dubboPort', defaultValue: '31100', description: '测试服务器的dubbo服务端口')
//单元测试代码覆盖率要求,各项目视要求调整参数
string(name:'lineCoverage', defaultValue: '20', description: '单元测试代码覆盖率要求(%),小于此值pipeline将会失败!')
//若勾选在pipelie完成后会邮件通知测试人员进行验收
booleanParam(name: 'isCommitQA',description: '是否邮件通知测试人员进行人工验收',defaultValue: false )
}

//常量参数,初始确定后一般不需更改
environment {
//def ITEMNAME = ""
def DESTPATH = "/data/wwwroot/devops"
def SRCPATH = "~/.jenkins/workspace/oms"
def CRED_ID = 'd4a8ba52-0ea0-4f60-86f5-95a4fe0eaa84'
//测试人员邮箱地址
def QA_EMAIL = 'hanniusshine@gmail.com'
}

options {
//保持构建的最大个数
buildDiscarder(logRotator(numToKeepStr: '10'))
}
//定期检查开发代码更新,工作日每晚4点做daily build
triggers {
pollSCM('H 4 * * 1-5')
}
//pipeline运行结果通知给触发者
post{
success{
script {
wrap([$class: 'BuildUser']) {
mail to: "${BUILD_USER_EMAIL }",
subject: "PineLine '${JOB_NAME}' (${BUILD_NUMBER}) result",
body: "${BUILD_USER}'s pineline '${JOB_NAME}' (${BUILD_NUMBER}) run success\n请及时前往${env.BUILD_URL}进行查看"
}
}
}
failure{
script {
wrap([$class: 'BuildUser']) {
mail to: "${BUILD_USER_EMAIL }",
subject: "PineLine '${JOB_NAME}' (${BUILD_NUMBER}) result",
body: "${BUILD_USER}'s pineline '${JOB_NAME}' (${BUILD_NUMBER}) run failure\n请及时前往${env.BUILD_URL}进行查看"
}
}

}
unstable{
script {
wrap([$class: 'BuildUser']) {
mail to: "${BUILD_USER_EMAIL }",
subject: "PineLine '${JOB_NAME}' (${BUILD_NUMBER})结果",
body: "${BUILD_USER}'s pineline '${JOB_NAME}' (${BUILD_NUMBER}) run unstable\n请及时前往${env.BUILD_URL}进行查看"
}
}
}
}

//pipeline的各个阶段场景
stages {
stage('代码拉取'){
steps {
echo "checkout from ${params.repoUrl}"
//git url: 'http://git.galaxymx.com/yunwei/oms.git', branch: 'master'
git credentialsId:CRED_ID, url:params.repoUrl, branch:params.repoBranch
}
}
stage('目录检查') {
steps {
echo "检查${DESTPATH}目录是否存在"
script{
//def resultUpdateshell = sh script: 'ansible webapp -m shell -a "ls -d ${DESTPATH}"'
def resultUpdateshell = sh script: 'ls -d ${DESTPATH}'
if (resultUpdateshell == 0) {
skip = '0'
return
}
}
}
}
stage('nginx服务检查') {
steps {
echo "检查nginx进程是否存在"
script{
def resultUpdateshell = sh script: 'ps aux|grep nginx|grep -v grep'
if (resultUpdateshell == 0) {
skip = '0'
return
}
}
}
}
stage('tomcat服务检查') {
steps {
echo "检查tomcat进程是否存在"
script{
def resultUpdateshell = sh script: 'ps aux|grep tomcat|grep -v grep'
if (resultUpdateshell == 0) {
skip = '0'
return
}
}
}
}
stage('发布确认') {
steps {
input "检查完成,是否发布?"
}
}
stage('代码推送') {
steps {
echo "code sync"
//sh "ansible ${ITEMNAME} -m synchronize -a 'src=${SRCPATH}/ dest=${DESTPATH}/ rsync_opts=-avz,--exclude=.git,--delete'"
sh "rsync -avz ${SRCPATH}/ ${DESTPATH}/ --exclude=.git --delete"
}
}
}
}

因为默认的jnlp容器是使用的官方的:jenkins/jnlp-slave:alpine
容器里面是没有:/data/wwwroot/devops目录,所以上面构建在”检查目录是否存在”步骤则返回0结束。



本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!