Jenkins流水線pipeline
-
Jenkins流水線pipeline - 流水線的概念
- 腳本式流水線
- Jenkins pipeline
- 流水線結構簡介
- Jenkinsfile管理流水線
- 編碼代碼執行命令
- Jenkins自帶流水線語法提示器
- 擴展--流水線語法-寫文件和讀文件
- 讀取指定目錄的文件
流水線的概念
- 工廠:實體產品的生產環節,如:華為手機生產流水線
- IT企業:軟件生產環節,如:需求調研,需求設計,概要設計,詳細設計,編碼,單元測試,集成測試,系統測試,用戶驗收測試,交付
- 市場需求調研--可行性研究--產品項目立項--需求調研開發--設計開發測試--發布運行維護
腳本式流水線
- pipeline的出現代表企業人員可以更自由的通過代碼來實現不同的工作流程
- 兩種語法結構
- 聲明式:語法繁瑣
- 腳本式:語法簡潔
Jenkins pipeline
Jenkins2.0開始加入的功能
pipeline:用代碼定義一起軟件的生產過程:構建-單元測試-自動化測試-性能-安全-交付
流水線結構簡介
腳本式語法
node定義腳本任務執行在哪台機器
node('機器的標簽')
{
待執行的任務
}
- node:節點(某台機器),執行任務的具體環境
- stage:環節,表示一組操作,通常用來邏輯划分,stage表示某個環節,對應的是視圖中的小方塊,可以自由定義環節名稱和其中要執行的代碼(可以沒有,但是建議有)
創建一個流水線型的任務
輸入名字,選擇流水線類型,點擊確定
切換到流水線,輸入測試腳本
node('gavin_win10'){
echo '執行pipeline測試'
}
點擊保存,然后點擊立即構建進行測試
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-workspace\workspace\test_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測試'
}
}
發現沒有視圖,小方塊
需要安裝pipeline插件
在插件管理菜單中搜索pipeline,然后點擊安裝,重啟Jenkins即可
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
解決;
設置為盡可能的使用該節點
Jenkinsfile管理流水線
- 在項目跟目錄下創建一個Jenkinsfile文件,輸入以下測試內容
node('gavin_win10'){
stage('webapi測試'){
echo '執行webapi測試'
}
stage('webui測試'){
echo '執行webui測試'
}
stage('生成測試報告'){
echo '執行生成測試報告'
}
stage('郵件通知'){
echo '執行郵件通知'
}
}
-
配置Jenkins流水線
https://gitee.com/gavinxiexingfu/test_combat.git
Generic Webhook Trigger- 配置觸發器
Generic Webhook Trigger只需要配置token即可
- 配置觸發器
-
配置gitee倉庫webhook
http://ciqiu123.ngrok2.xiaomiqiu.cn/generic-webhook-trigger/invoke?token=abc@1 -
推送Jenkinsfile到gitee倉庫
等待Jenkins自動構建成功
在Jenkinsfile里面執行腳本相當於在Jenkins的workspace下面執行命令
但是發現workspace下面沒有當前項目
原因是還沒有拉取項目,只是執行了Jenkinsfile文件
解決:
node('gavin_win10'){
checkout scm //檢出代碼--作用相當於git clone/pull代碼
編碼代碼執行命令
直接用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-workspace\workspace\test_pipeline_style_combat\tc\D-綆$悊鍛樼櫥褰?-s'
原因:
Jenkinsfile文件編碼格式為utf-8,而命令行執行會把中文編碼成gbk
解決:
可以嘗試把Jenkinsfile改成gbk試試
發現更加不行了
恢復原來的編碼格式
把中文目錄改成英文目錄
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-workspace\workspace\test_pipeline_style_combat>pytest tc/D-admin-login -s --alluredir=tmp/report --clean-alluredir
ImportError while loading conftest 'D:\jenkins-workspace\workspace\test_pipeline_style_combat\tc\D-admin-login\conftest.py'.
tc\D-admin-login\conftest.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中
終於可以測試通過了
tc\D-admin-login\test_organizapi.py ..
tc\D-admin-login\D-需求部\test_organizations.py ....
tc\D-admin-login\D-需求部\D-簽約對象\合同類型\test_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自帶流水線語法提示器
郵件通知也是一樣做法
讀文件
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-workspace\workspace\test_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-workspace\workspace\test_pipeline_style_demo1\testdir\sute1
[Pipeline] {
[Pipeline] readFile
[Pipeline] echo
testcase1
testcase2
testcase3