標籤:選項 .com 需要 home port eps 通過 簡單 agent
大家可能還沒搞清楚,Jenkins到底能做什麼?
本節內容利用Jenkins完成python程式的build、test、deployment,讓大家對Jenkins能做的事情有一個直觀的瞭解。
本節內容改寫自 https://jenkins.io/doc/tutorials/build-a-python-app-with-pyinstaller/。
1. Fork,Clone Github上的sample repo
這個Github上的sample repo包含了python程式的Buid, Test , Depolyment的代碼,所以我們需要先fork這個repo,然後clone到我們本地機器。
關於這段Python程式,也很簡單,就是計算兩個參數的和。repo中的程式碼封裝含了程式本體(計算和),還包含了unittest,build(通過Pyinstaller產生可執行檔)等一切需要的代碼及工具
1.1 登入Github,如果沒有的話,就去註冊一個
1.2 在Github上Fork simple-python-pyinstaller-app, 如果不知道如何fork,請參考 Fork A Repo
1.3 Clone 這個 repo到本地機器,方法是:
開啟Linux命令列
cd /home/<your-username>/GitHub/
git clone https://github.com/YOUR-GITHUB-ACCOUNT-NAME/simple-python-pyinstaller-app
(記得替換<your-username>和YOUR-GITHUB-ACCOUNT-NAME為你真正的路徑名和使用者名稱)
2. 在Jenkins中建立pipline project
pipline翻譯過來是“管道”的意思,其實這個你把整個Build、Test、Deployment的流程想象成一個管道,你的代碼在裡面流動,經過不同的階段,就好理解了。
2.1 登入Jenkins後,在初始介面的左上方點擊New Item
2.2 把pipline project的名字命名為你指定的,例如simple-python-pyinstaller-app
2.3 往下拉,選擇 Pipline, 點擊OK
2.4 選擇Pipline tab,然後往下拉看到Pipline Section:
Definition選擇Pipline Scirpt from SCM,這個選項能讓Jenkins能夠從Source Control Management(SCM)處獲得,這個SCM其實就是你clone到本地機器的repo
SCM選擇Git
Repository URL填你clone到本地機器的repo的路徑: /home/GitHub/simple-python-pyinstaller-app
點擊Save
3. 在Pipline中建立Jenkinsfile
Jenkinsfile實際上規定了Jenkins的Pipline流程中做了哪些事情。
在你的 /home/GitHub/simple-python-pyinstaller-app路徑下建立Jenkinsfile檔案,然後填入以下代碼:
pipeline { agent none stages { stage(‘Build‘) { agent { docker { image ‘python:2-alpine‘ } } steps { sh ‘python -m py_compile sources/add2vals.py sources/calc.py‘ } } stage(‘Test‘) { agent { docker { image ‘qnib/pytest‘ } } steps { sh ‘py.test --verbose --junit-xml test-reports/results.xml sources/test_calc.py‘ } post { always { junit ‘test-reports/results.xml‘ } } } stage(‘Deliver‘) { agent { docker { image ‘cdrx/pyinstaller-linux:python2‘ } } steps { sh ‘pyinstaller --onefile sources/add2vals.py‘ } post { success { archiveArtifacts ‘dist/add2vals‘ } } } }}
儲存檔案,然後運行以下命令以提交修改:
git add .
git commit -m "create the Jenkinsfile"
4. 用Blue Ocean運行Pipline然後觀察
回到Jenkins初始頁面,登入後,在左側列表選擇Blue Ocean,點擊"Run"
在這個頁面,你可以看到Pipline的運行情況
點擊每個Step,可以看到具體的運行情況,在跑哪個程式,出錯資訊,等。
如果想要查看自己這次跑的Pipline(我們稱之為Activity),那麼需要先點擊Blue Ocean,選中你的Project,然後點上方的Activity
如果需要運行產生的可執行檔,可以選擇你跑的這一次Pipline的Activity。找到可執行檔add2vals,下載然後執行:
chmod +x add2vals
./add2vals 1 2
本節我們大體明白了Jenkins到底能幫我們做些什麼:Build,Test,Deliver,瞭解了Jenkins的工作流程,以及Jenkinsfile規定了Pipline是如何啟動並執行。下一節我們將會使用Blue Ocean的便捷功能,使得我們只需要操作Web頁面,指定Build 、Test、 Deliver的內容,自動為我們產生Jenkinsfile檔案。
Jenkins簡明入門(二) -- 利用Jenkins完成Python程式的build、test、deployment