jenkinsfile的使用:通过jenkinsfile + k8s container实现简单的自动化测试
一、jenkinsfile语法Jenkinsfile 是 Jenkins 2.x 核心特性 Pipeline 的脚本,由Groovy语言实现。Pipeline支持:Declarative(在Pipeline 2.5中引入)和Scripted Pipeline两种格式。注意:两种格式使用语法不同,比如声明式中post{}块在脚本式中就不适用。两种语法对应方式详见:https://www.jenkin
·
一、jenkinsfile语法
Jenkinsfile 是 Jenkins 2.x 核心特性 Pipeline 的脚本,由Groovy语言实现。Pipeline支持:Declarative(在Pipeline 2.5中引入)和Scripted Pipeline两种格式。
注意:两种格式使用语法不同,比如声明式中post{}块在脚本式中就不适用。两种语法对应方式详见:https://www.jenkins.io/zh/doc/book/pipeline/jenkinsfile/
二、jenkins上创建流水线项目
在下图处编写流水线:(pipeline的使用语法可以用“pipeline语法生成器”生成,包括插件的使用语法)
三、使用k8s container实现简单的自动化测试
1.简单的k8s-jenkins-pipeline:
def label = "mypod-${UUID.randomUUID().toString()}"
podTemplate(label: label, cloud: 'kubernetes') {
node(label) {
stage('Run shell') {
sh 'sleep 130s'
sh 'echo hello world.'
}
}
}
2.可以使用参数化构建
/**
* 使用参数的形式传入源码库地址、目标分支、构建工具及语言类型
* 使用时可以在本文件中手动输入上述参数的值
* 也可以在jenkins的job配置页中的"参数化构建过程"项中进行配置
**/
properties(
[
parameters([
string(defaultValue:'https://xxxxx.git', description:'source url', name:'Source'),
string(defaultValue:'master', description:'source branch', name:'Branch'),
string(defaultValue:'maven3.3.9', description:'build tool', name:'BTool'),
string(defaultValue:'jdk-1.8', description:'language', name:'Lang'),
string(defaultValue:'此处为凭证id', description: 'credentialsId for git account', name: 'gitCredentialsId')
])
]
)
/**
* 本例执行下载代码及编译构建流程
**/
def cloudName = 'kubernetes'
def podName = 'pipeline'
def podLabel = 'piplable'
podTemplate(cloud: cloudName, name: podName, label: podLabel ,nodeUsageMode:'EXCLUSIVE',
containers: [
//---------------------------------------------------------------------------------
// 使用git-lfs拉取git服务器上的大文件,根据实际情况添加此段
containerTemplate(
name: 'xxx',
image: "镜像地址/xxx-slave:git-lfs",
args: 'xxxx'
),
// --------------------------------------------------------------------------------
containerTemplate(
name: 'build-container',
image: "镜像地址/maven:3.3.9-jdk-8-alpine",
alwaysPullImage: true,
ttyEnabled: true,
command: 'cat'
)
]
) {
node(podLabel) {
stage('下载源码') {
checkout([
$class: 'GitSCM',
branches: [[name: "${params.Branch}"]],
doGenerateSubmoduleConfigurations: false,
extensions: [
// -------------------------------------------------------
// 如果需要使用git-lfs拉取git服务器上的大文件,需要填写本段内容
[$class: 'GitLFSPull']
// -------------------------------------------------------
],
submoduleCfg: [],
userRemoteConfigs: [[
credentialsId: "凭证",
url: "${params.Source}"]]]
)
}
stage('编译构建') {
container('build-container') {
echo 'excute unit testing ......'
sh 'mvn -B clean install'
}
}
}
}
3.在做自动化测试时需要生成测试报告,还需要无论测试case有无失败,都要执行生成测试报告这一步,在声明式pipeline中可以使用post块进行处理:
pipeline {
agent any
stages {
stage('Test') {
steps {
sh 'make check'
}
}
}
post {
always {
junit '**/target/*.xml' //产生测试报告
}
failure {
mail to: team@example.com, subject: 'The Pipeline failed :('
}
}
}
在脚本式pipeline里用try finally处理
node {
/* .. snip .. */
stage('Test') {
try {
sh 'make check'
}
finally {
junit '**/target/*.xml' //产生测试报告
}
}
/* .. snip .. */
}
//或者将多个stage放在try里
node{
try{
stage('下载代码'){}
stage('变异构建'){}
}finally{
产生某种测试报告
}
}
更多推荐
所有评论(0)