11.jenkins流水线pipeline

Jenkins流水线pipeline

流水线的概念

  • 工厂:实体产品的生产环节,如:华为手机生产流水线
  • IT企业:软件生产环节,如:需求调研,需求设计,概要设计,详细设计,编码,单元测试,集成测试,系统测试,用户验收测试,交付
  • 市场需求调研--可行性研究--产品项目立项--需求调研开发--设计开发测试--发布运行维护

脚本式流水线

  • pipeline的出现代表企业人员可以更自由的通过代码来实现不同的工作流程
  • 两种语法结构
    • 声明式:语法繁琐
    • 脚本式:语法简洁

Jenkins pipeline

Jenkins2.0开始加入的功能
pipeline:用代码定义一起软件的生产过程:构建-单元测试-自动化测试-性能-安全-交付

流水线结构简介

脚本式语法
node定义脚本任务执行在哪台机器

node('机器的标签')
{
    待执行的任务
}
  • node:节点(某台机器),执行任务的具体环境
  • stage:环节,表示一组操作,通常用来逻辑划分,stage表示某个环节,对应的是视图中的小方块,可以自由定义环节名称和其中要执行的代码(可以没有,但是建议有)

创建一个流水线型的任务
图 2

输入名字,选择流水线类型,点击确定
切换到流水线,输入测试脚本

node('gavin_win10'){
    echo '执行pipeline测试'
}

图 3
点击保存,然后点击立即构建进行测试

Console Output
Started by user unknown or anonymous
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on gavin_win10 in D:jenkins-workspaceworkspace	est_pipeline_style_demo1
[Pipeline] {
[Pipeline] echo
执行pipeline测试
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
node('gavin_win10'){
    stage('阶段1'){
        echo '执行pipeline测试'
    }
    stage('阶段2'){
        echo '执行pipeline测试'
    }
    stage('阶段3'){
        echo '执行pipeline测试'
    }
    stage('阶段4'){
        echo '执行pipeline测试'
    }
    
}

发现没有视图,小方块
图 4
需要安装pipeline插件
在插件管理菜单中搜索pipeline,然后点击安装,重启Jenkins即可
图 5

node和stage可以相互嵌套


stage('阶段1'){
    node(){
       sh "echo '执行pipeline测试'"
    }
    node('gavin_win10'){
        stage('阶段2'){
            echo '执行pipeline测试'
        }
        stage('阶段3'){
            echo '执行pipeline测试'
        }
        stage('阶段4'){
            bat "echo '执行pipeline测试'"
        }
    }
}

构建,控制台报错;
Console Output
Started by user gavin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] stage
[Pipeline] { (阶段1)
[Pipeline] node
Still waiting to schedule task
‘Jenkins’ is reserved for jobs with matching label expression

解决;
设置为尽可能的使用该节点
图 6

Jenkinsfile管理流水线

  • 在项目跟目录下创建一个Jenkinsfile文件,输入以下测试内容
node('gavin_win10'){
    stage('webapi测试'){
        echo '执行webapi测试'
    }
    stage('webui测试'){
        echo '执行webui测试'
    }
    stage('生成测试报告'){
        echo '执行生成测试报告'
    }
    stage('邮件通知'){
        echo '执行邮件通知'
    }
    
}

在Jenkinsfile里面执行脚本相当于在Jenkins的workspace下面执行命令
但是发现workspace下面没有当前项目
原因是还没有拉取项目,只是执行了Jenkinsfile文件
解决:
node('gavin_win10'){
checkout scm //检出代码--作用相当于git clone/pull代码

图 9

编码代码执行命令

直接用pytest的命令行方式来执行

stage('webapi测试'){
    pytest tc/D-管理员登录 -s --alluredir=tmp/report --clean-alluredir
    echo '执行webapi测试'
}

执行控制台报错了:
Console Output
Generic Cause
Obtained Jenkinsfile from git https://gitee.com/gavinxiexingfu/test_combat.git
Running in Durability level: MAX_SURVIVABILITY
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 4: expecting '}', found 'alluredir' @ line 4, column 32.
pytest tc/D-管理员登录 -s --alluredir=tmp/report --clean-alluredir
^

1 error

原因:在Windows下执行,需要把命令包在双引号中
bat "pytest tc/D-管理员登录 -s --alluredir=tmp/report --clean-alluredir"

继续推送构建
又报新的错误
INTERNALERROR> OSError: [WinError 123] 文件名、目录名或卷标语法不正确。: 'D:jenkins-workspaceworkspace est_pipeline_style_combat cD-绠$悊鍛樼櫥褰?-s'

原因:
Jenkinsfile文件编码格式为utf-8,而命令行执行会把中文编码成gbk

解决:
可以尝试把Jenkinsfile改成gbk试试
图 10
发现更加不行了
图 11
恢复原来的编码格式

把中文目录改成英文目录

node('gavin_win10'){
    checkout scm //检出代码--作用相当于git clone/pull代码
    stage('webapi测试'){
        bat "pytest tc/D-admin-login -s --alluredir=tmp/report --clean-alluredir"
        echo '执行webapi测试'
    }
    stage('webui测试'){
        echo '执行webui测试'
    }
    stage('生成测试报告'){
        echo '执行生成测试报告'
    }
    stage('邮件通知'){
        echo '执行邮件通知'
    }

}

构建又报错
D:jenkins-workspaceworkspace est_pipeline_style_combat>pytest tc/D-admin-login -s --alluredir=tmp/report --clean-alluredir
ImportError while loading conftest 'D:jenkins-workspaceworkspace est_pipeline_style_combat cD-admin-loginconftest.py'.
tcD-admin-loginconftest.py:5: in
from conf.env import g_email, g_pwd
E ModuleNotFoundError: No module named 'conf'

原因:
pytest不能正确的识别根目录,python path
解决1

在pytest前面加python -m
python -m会把当前目录加到python path中

终于可以测试通过了
tcD-admin-login est_organizapi.py ..
tcD-admin-loginD-需求部 est_organizations.py ....
tcD-admin-loginD-需求部D-签约对象合同类型 est_contracts.py ......

解决2:
在根目录下新建一个conftest.py文件即可

============================= 12 passed in 9.59s ==============================
[Pipeline] echo
执行webapi测试
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (webui测试)
[Pipeline] echo
执行webui测试
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (生成测试报告)
[Pipeline] echo
执行生成测试报告
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (邮件通知)
[Pipeline] echo
执行邮件通知1
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

Jenkins自带流水线语法提示器

图 12

图 13
邮件通知也是一样做法
图 14
读文件
图 15

node('gavin_win10'){
    checkout scm //检出代码--作用相当于git clone/pull代码
    stage('webapi测试'){
        bat "python -m pytest tc/D-admin-login -s --alluredir=tmp/report --clean-alluredir"
    }
    /*
    stage('webui测试'){
        bat "python -m pytest tc/D-webUI-login -s --alluredir=tmp/report"
    }
    */
    stage('生成测试报告'){
        allure results: [[path: 'tmp/report']]
    }
    stage('邮件通知'){
    //读取邮件模板内容
    email_content=readFile encoding: 'utf-8', file: 'email_template.html'
    println ${email_content}
    emailext body: ${email_content},subject: '构建通知:${BUILD_STATUS} - ${PROJECT_NAME} - Build # ${BUILD_NUMBER} !', to: '644481241@qq.com'
    }
}

构建报错了:

java.lang.NoSuchMethodError: No such DSL method '$' found among steps [acceptGiteeMR, addGiteeMRComment, archive, bat, build, catchError, checkout, deleteDir, dir, echo, emailext,

变量要放到双引号中

扩展--流水线语法-写文件和读文件

node('gavin_win10'){
    stage('阶段1'){
        echo '执行pipeline测试'
    }
    stage('阶段2'){
        echo '执行pipeline测试'
    }
    stage('阶段3'){
        echo '执行pipeline测试'
    }
    stage('阶段4'){
        echo '执行pipeline测试'
        //写文件
        writeFile encoding: 'utf8', file: 'test.conf', text: '执行安全测试'
        //读文件
        def content =readFile file: 'test.conf',encoding: 'utf8'
        println("${content}")
    }
    
}

Started by user gavin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline (hide)
[Pipeline] node
Running on gavin_win10 in D:jenkins-workspaceworkspace est_pipeline_style_demo1
[Pipeline] {
[Pipeline] stage
[Pipeline] { (阶段1)
[Pipeline] echo
执行pipeline测试
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (阶段2)
[Pipeline] echo
执行pipeline测试
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (阶段3)
[Pipeline] echo
执行pipeline测试
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (阶段4)
[Pipeline] echo
执行pipeline测试
[Pipeline] writeFile
[Pipeline] readFile
[Pipeline] echo
执行安全测试
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

读取指定目录的文件

node('gavin_win10'){
    stage('阶段1'){
        echo '执行pipeline测试'
    }
    stage('阶段2'){
        echo '执行pipeline测试'
    }
    stage('性能测试'){
        echo '执行pipe测试'
        dir('testdir/sute1') {
        def content =readFile file: 'testcase.txt',encoding: 'utf8'
        println("${content}")
        }
    }
    stage('阶段4'){
        echo '执行pipeline测试'
        //写文件
        writeFile encoding: 'utf8', file: 'test.conf', text: '执行安全测试'
        //读文件
        def content =readFile file: 'test.conf',encoding: 'utf8'
        println("${content}")
    }
    
}

执行pipe测试
[Pipeline] dir
Running in D:jenkins-workspaceworkspace est_pipeline_style_demo1 estdirsute1
[Pipeline] {
[Pipeline] readFile
[Pipeline] echo
testcase1
testcase2
testcase3

原文地址:https://www.cnblogs.com/xiehuangzhijia/p/15191917.html