企业级持续集成实战系列(11):pipeline实现
本系列汇总,请查看这里:https://www.cnblogs.com/uncleyong/p/16721826.html

结合上面在Blue Ocean中设计的pipeline骨架,我们来完善并实现整个过程
parameters
获取分支,后续页面上可以选择要构建的分支
parameters {
gitParameter branch: '', branchFilter: 'origin/(.*)', defaultValue: '', description: '构建的分支', name: 'BRANCH', quickFilterEnabled: false, selectedValue: 'NONE', sortMode: 'NONE', tagFilter: '*', type: 'GitParameterDefinition'
}
environment
environment {
HARBOR_ADDRESS = "192.168.117.160"
IMAGE_NAME = "gift"
NAMESPACE = "gift"
}
agent
k8s作为构建环境(jenkins的slave是以k8s的pod形式创建的)
agent {
kubernetes {
cloud 'qzcsbj_kubernetes'
yaml '''apiVersion: v1
kind: Pod
spec:
containers:
- image: 'registry.cn-chengdu.aliyuncs.com/qzcsbj/jnlp:alpine'
name: jnlp
imagePullPolicy: IfNotPresent
args: [\'$(JENKINS_SECRET)\', \'$(JENKINS_NAME)\']
volumeMounts:
- mountPath: "/etc/localtime"
name: "localtime"
readOnly: false
- image: "registry.cn-chengdu.aliyuncs.com/qzcsbj/kubectl"
imagePullPolicy: "IfNotPresent"
name: "kubectl"
tty: true
command:
- "cat"
env:
- name: "LANGUAGE"
value: "en_US:en"
- name: "LANG"
value: "en_US.UTF-8"
volumeMounts:
- mountPath: "/etc/localtime"
name: "localtime"
readOnly: false
- image: "registry.cn-chengdu.aliyuncs.com/qzcsbj/docker"
imagePullPolicy: "IfNotPresent"
name: "docker"
tty: true
command:
- "cat"
env:
- name: "LANGUAGE"
value: "en_US:en"
- name: "LANG"
value: "en_US.UTF-8"
volumeMounts:
- mountPath: "/etc/localtime"
name: "localtime"
readOnly: false
- mountPath: "/var/run/docker.sock"
name: "dockersock"
readOnly: false
- image: "registry.cn-chengdu.aliyuncs.com/qzcsbj/maven"
imagePullPolicy: "IfNotPresent"
name: "maven"
tty: true
command:
- "cat"
env:
- name: "LANGUAGE"
value: "en_US:en"
- name: "LANG"
value: "en_US.UTF-8"
volumeMounts:
- mountPath: "/etc/localtime"
name: "localtime"
- mountPath: "/root/.m2/"
name: "m2dir"
readOnly: false
- image: "registry.cn-chengdu.aliyuncs.com/allure-commandline"
imagePullPolicy: "IfNotPresent"
name: "allure"
tty: true
command:
- "cat"
env:
- name: "LANGUAGE"
value: "en_US:en"
- name: "LANG"
value: "en_US.UTF-8"
volumeMounts:
- mountPath: "/etc/localtime"
name: "localtime"
readOnly: false
restartPolicy: "Never"
volumes:
- name: "dockersock"
hostPath:
path: "/var/run/docker.sock"
- name: "localtime"
hostPath:
path: "/usr/share/zoneinfo/Asia/Shanghai"
- name: "m2dir"
hostPath:
path: "/opt/m2"
'''
}
}
pull project code
stage('pull project code') {
parallel {
stage('ui build') {
when {
expression {
env.gitlabBranch == null
}
}
steps {
sh """
echo '=================开始拉取项目代码'
"""
git(url: 'git@192.168.117.180:qzcsbj/gift.git', branch: "${BRANCH}", credentialsId: 'qzcsbj_gitlab')
script {
COMMIT_ID = sh(returnStdout: true, script: "git log -n 1 --pretty=format:'%h'").trim()
TAG = BUILD_TAG + '-' + COMMIT_ID
println "Current branch is ${BRANCH}, Commit ID is ${COMMIT_ID}, Image TAG is ${TAG}"
}
}
post {
failure {
dingtalk (
robot:'dd01',
type:'MARKDOWN',
atAll: true,
title: "notice: 拉取项目代码失败",
text: ["#### '${JOB_NAME}'项目代码拉取失败\n - 构建:第'${BUILD_NUMBER}'次\n - 状态:'${currentBuild.result}'\n - [查看本次构建详情](${BUILD_URL})"]
)
}
}
}
stage('trigger build') {
steps {
sh """
echo "。。。"
"""
}
}
}
}
mvn package
build and push image
deploy to k8s
pull autotest code
stage("pull autotest code"){
steps{
sh """
echo '=================开始拉取自动化测试代码'
"""
git(
credentialsId: 'qzcsbj_gitlab',
url: 'git@192.168.117.180:root/apiautotest.git'
)
}
post {
failure {
dingtalk (
robot:'dd01',
type:'MARKDOWN',
atAll: true,
title: "notice: 拉取自动化测试代码失败",
text: ["#### 拉取自动化测试代码失败\n - 构建:第'${BUILD_NUMBER}'次\n - 状态:'${currentBuild.result}'\n - [查看本次构建详情](${BUILD_URL})"]
)
}
}
}
run autotest
allure report
__EOF__
本文作者:持之以恒(韧)
关于博主:擅长性能、全链路、自动化、企业级自动化持续集成(DevTestOps)、测开等
面试必备:项目实战(性能、自动化)、简历笔试,https://www.cnblogs.com/uncleyong/p/15777706.html
测试提升:从测试小白到高级测试修炼之路,https://www.cnblogs.com/uncleyong/p/10530261.html
欢迎分享:如果您觉得文章对您有帮助,欢迎转载、分享,也可以点击文章右下角【推荐】一下!
关于博主:擅长性能、全链路、自动化、企业级自动化持续集成(DevTestOps)、测开等
面试必备:项目实战(性能、自动化)、简历笔试,https://www.cnblogs.com/uncleyong/p/15777706.html
测试提升:从测试小白到高级测试修炼之路,https://www.cnblogs.com/uncleyong/p/10530261.html
欢迎分享:如果您觉得文章对您有帮助,欢迎转载、分享,也可以点击文章右下角【推荐】一下!

浙公网安备 33010602011771号