某小型公司持續集成工具jenkins實踐(JAVA WEB、Android、IOS、html)

前言

本文多是網上最全的一篇全端jenkins部署解決方案介紹的文章,一直以來,領導都想解決代碼提交和打包問題,尤爲是小公司,打包流程混亂,形成線上版本和代碼庫git或svn中代碼不一致問題。加入jenkins陣營,解決衆多發包難題,顯得刻不容緩。java

研究過程

剛開始,我一直在docker下安裝jenkins,按時長租用阿里的主機,用阿里容器服務作實驗,反覆建立docker容器,創建多個jenkins節點。結果發現,在容器中配置各類變量比較複雜,各類開發環境不易快速部署,固然啦,是剛開始的對jenkins研究過少形成的。後來我採用了windows主機,配java、maven、Android環境,踩了一路坑,終於搞定了。可是我知道ios開發和編譯必須使用mac系統,原本想增長一個jenkins節點來實現ios的編譯。當配置好以後,我忽然改變了主意,與其兩臺電腦開着,不如索性把整個jenkins部署到mac系統下(畢竟咱們公司暫時沒有c#工程)。因而,全部的操做,全部的環境我又折騰了一遍。下面我逐一講解最終mac下實現全端jenkins持續集成,固然不包含c#,一路踩坑下來,我以爲這都不是事,流程都差很少。android

效果圖

jenkins首頁
jenkins首頁

構建歷史
構建歷史

安裝jenkins

我我的比較喜歡新事物,因此建議使用較高版本,我使用的2.61,目前最新版是2.75。
mac下使用brew安裝,終端執行ios

brew install jenkins複製代碼

等安裝完成後,終端運行jenkins便可啓動。git

jenkins複製代碼

安裝 xcode

這個比較簡單,直接從appstore下載最新版便可。
安裝後,將公司項目拉入xcode,先保證項目在本機編譯,這裏我登陸了app開發者賬號,以便下載各類證書及發佈。github

安裝 Android studio

這個到這裏下載www.android-studio.org/docker


同理將本地項目拉入,運行。這一步主要是怕開發環境引發的坑,先把項目跑起來,後面坑會少點。
設置
設置

進入設置,複製出sdk文件路徑,後面須要使用。我這裏是/Users/Shaolei/Library/Android/sdk
sdk路徑
sdk路徑

安裝git

由於咱們公司用的是git,若是貴公司使用svn,則同理,只需保git或者svn命令能夠敲出來便可。
我這裏的版本比較老,儘可能使用新的,我懶得換了。c#

mac:~ shaolei$ git version
git version 2.11.0複製代碼

安裝source tree

這是一個很是好用的git可視化工具,改天會具體介紹它的強大功能。
這款軟件不是必須的,只是爲了方便項目拉取,因此,你能夠跳過此步驟。windows

配置jenkins環境

這裏纔是相當重要的一項,本文的核心xcode

配置插件

下載插件
下載插件

  1. Android Emulator Plugin
    Starts an Android emulator with given properties before a build, then shuts it down after.複製代碼
  2. build timeout plugin
    This plugin allows builds to be automatically terminated after the specified amount of time has elapsed.複製代碼
  3. Email Extension Plugin
    This plugin is a replacement for Jenkins's email publisher 複製代碼
  4. Gradle Plugin
    This plugin allows Jenkins to invoke Gradle build scripts directly.複製代碼
  5. Keychains and Provisioning Profiles Management
    This plugin integrates management of keychain and provisioning files for iOS and OSX projects.複製代碼
  6. Maven Integration plugin
    This plug-in provides, for better and for worse, a deep integration of Jenkins and Maven: Automatic triggers between projects depending on SNAPSHOTs, automated configuration of various Jenkins publishers (Junit, ...).    複製代碼
  7. Pipeline
    A suite of plugins that lets you orchestrate automation, simple or complex. See Pipeline as Code with Jenkins for more details.    複製代碼
  8. Pipeline: GitHub Groovy Libraries
    Allows Pipeline Grrovy libraries to be loaded on the fly from GitHub.複製代碼
  9. SSH Slaves plugin
    Allows to launch agents over SSH, using a Java implementation of the SSH protocol.複製代碼
  10. Subversion Plug-inandroid-studio

  11. Timestamper

    Adds timestamps to the Console Output複製代碼
  12. Workspace Cleanup Plugin
    This plugin deletes the project workspace after a build is finished.複製代碼
  13. Xcode integration
    This plugin provides builders to build xcode projects, invoke agvtool and package .ipa files複製代碼

其中Android Emulator Plugin、Gradle Plugin是安卓必須插件;其中Keychains and Provisioning Profiles Management、Pipeline、Xcode integration是ios編譯必須插件;其中Maven Integration plugin、SSH Slaves plugin是maven項目必須插件,其餘的Email Extension Plugin這個是爲了編譯失敗的時候通知具體提交代碼開發人員郵件

配置系統設置

系統設置
系統設置


Local Maven Repository爲maven倉庫地址;若是本機沒有安裝maven,可能須要增長maven倉庫;
下面須要加入安卓環境變量,即上文中提到的那個sdk地址;


這裏配置github地址,郵件用戶名密碼,注意發件人的郵箱必須上面jenkins註冊是管理員郵箱是一個,不然一直失敗,這裏是個坑。

增長計劃

Android 項目job

建立安卓job

建立安卓項目
建立安卓項目

配置項目的git庫地址
配置項目的git庫地址
配置項目的git庫地址

下面配置觸發器,每隔1分鐘檢查一次git庫,也可使用subscribe訂閱,這裏暫不作講解。
配置觸發器
配置觸發器

設置編譯後,將文件拷貝到一個文件目錄。這裏使用smb協議鏈接到了一個內網文件服務器上,方便測試部門測試。
配置編譯及郵件通知
配置編譯及郵件通知

至於具體爲什麼執行2次拷貝,是由於項目同時編譯測試環境地址和真實環境地址,只有鏈接的服務器地址不通,其餘代碼都相同,具體實現可看另一篇博文 juejin.im/post/59302a…

ios項目job

和安卓相同的源代碼管理及觸發器不作介紹,請參照安卓,下文同理。


這裏須要配置ios項目的tagert,能夠經過xcode打開查看,勾選Pack application and build .ipa?,使jenkins生成ipa包

配置證書地址和密碼,這裏指的是 Keychain
Keychain path默認地址爲/Users/shaolei/Library/Keychains/login.keychain,其中shaolei爲mac登陸的用戶名,密碼就是登陸密碼。
失敗時郵件發送至影響代碼的配置請參照安卓,下文同理。
同時編譯兩個ipa包請查看 juejin.im/post/59648b…

java項目job


由於公司人員少,項目版本若是讓開發去控制,比較繁瑣,每一個開發得不停的增長版本號,這裏我使用jenkins去控制版本號,這樣也方便查詢和回滾代碼。
固然啦,實現war的升級和回滾可看另一篇文章 juejin.im/post/59633a…

cd ${WORKSPACE}
mvn clean
cd ${WORKSPACE}/******/src/main/resources/
echo ${BUILD_NUMBER} >ver.xml複製代碼

固然了,這裏用到了幾個關鍵詞變量,是jenkins特有的,更多變量參照下文:

CHANGE_AUTHOR
For a multibranch project corresponding to some kind of change request, this will be set to the username of the author of the proposed change, if supported; else unset.
CHANGE_AUTHOR_DISPLAY_NAME
For a multibranch project corresponding to some kind of change request, this will be set to the human name of the author, if supported; else unset.
CHANGE_AUTHOR_EMAIL
For a multibranch project corresponding to some kind of change request, this will be set to the email address of the author, if supported; else unset.
CHANGE_TARGET
For a multibranch project corresponding to some kind of change request, this will be set to the target or base branch to which the change could be merged, if supported; else unset.
BUILD_NUMBER
The current build number, such as "153"
BUILD_ID
The current build ID, identical to BUILD_NUMBER for builds created in 1.597+, but a YYYY-MM-DD_hh-mm-ss timestamp for older builds
…………………………………………等等複製代碼

h5項目job

這裏我就用了zip打包,部署時使用zip解壓到指定目錄便可。

cd ${WORKSPACE}
echo ${BUILD_NUMBER} >ver.txt
zip -r h5.zip ./ -x .git\* -x README.md -x .project -x .gitignore
cp ${WORKSPACE}/h5.zip /Volumes/***/H5/h5-${BUILD_NUMBER}.zip複製代碼

總結

本文全面講解了jenkins各項目持續繼承方案,由於涉及環境過多,坑仍是比較多的,但願自行填坑。

相關文章
相關標籤/搜索