<div class="con artical-content editor-preview-side"><h3>Jenkins 持續集成使用教程</h3> <hr> <h4>用 jenkins 有什麼好處</h4> <ol> <li>經過規範化來完成,簡單,繁瑣,浪費時間的重複工做</li> <li>規範化工做,以避免出現低級錯誤</li> <li>實現隨時隨地任何人一鍵構建</li> <li>......</li> </ol> <h4>安裝 jenkins</h4> <p>以 Mac 設備爲例(Windows 步驟相似),幫你一步一步搭建好 jenkins。jenkins 屬於 java 項目 依賴於 java,須要先安裝 java jdk,具體安裝方式請自行百度。</p> <p>安裝 jenkins 有經常使用的三種方式。</p> <h5>使用 pgk 安裝包安裝</h5> <p>在 <a href="https://jenkins.io/" rel="nofollow" style="color: rgb(66, 133, 244);">官網</a> 下載 pgk 安裝包,使用安裝器安裝,按照流程安裝便可。此安裝方式會在 Mac 系統裏建立一個名爲 jenkins 用戶,jenkins 服務就部署在這個用戶下。</p> <p>具體步驟以下圖所示:</p> <p><img src="https://upload-images.jianshu.io/upload_images/1194012-6a2c3d6d42f35d31.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/620" alt="Jenkins 持續集成使用教程" style="cursor: pointer;"></p> <p><img src="https://upload-images.jianshu.io/upload_images/1194012-75539c56443392df.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/619" alt="Jenkins 持續集成使用教程" style="cursor: pointer;"></p> <p><img src="https://upload-images.jianshu.io/upload_images/1194012-126a68ae31c21e6e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/621" alt="Jenkins 持續集成使用教程" style="cursor: pointer;"></p> <p><img src="https://upload-images.jianshu.io/upload_images/1194012-1a24ed942db2a9d8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/621" alt="Jenkins 持續集成使用教程" style="cursor: pointer;"></p> <p><img src="https://upload-images.jianshu.io/upload_images/1194012-179ec0017a364cc5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/620" alt="Jenkins 持續集成使用教程" style="cursor: pointer;"></p> <p><img src="https://upload-images.jianshu.io/upload_images/1194012-982058c2d4701b31.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/623" alt="Jenkins 持續集成使用教程" style="cursor: pointer;"></p> <p>此種方式不推薦。</p> <h5>使用 war 安裝</h5> <p>直接去官網下載 war 文件,並 cd 到 jenkins.war 所在目錄,執行:</p> <p><code>java -jar jenkins.war --httpPort=8080</code></p> <p>war 包自帶 jetty 服務器,以上命令會自動啓服務器,並完成部署。</p> <p>此種方式安裝,會在當前用戶的根路徑下建立 .jenkins 隱藏文件,併產生多餘的配置文件,需手動管理,且不方便卸載。</p> <p>此種方式也不推薦。</p> <h5>使用 Homebrew 安裝</h5> <p><a href="https://brew.sh" rel="nofollow" style="color: rgb(66, 133, 244);">Homebrew</a> 做爲 macOS 下的包管理器使用起來異常的簡單快捷方便。<br>安裝 Homebrew 以前, 首先安裝 Command Line Tools for Xcode,命令以下:</p> <p><code>xcode-select --install</code></p> <p>Homebrew 依賴 Command Line Tools for Xcode,Xcode 命令行工具包含在 Xcode 中,若是 mac 設備已經安裝 Xcode,請忽略此步。</p> <p>安裝完上面的編譯依賴以後,經過下面的代碼完成 Homebrew 的安裝:</p> <p><code>ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"</code></p> <p>完成 Homebrew 安裝。</p> <p>可經過如下命令查看當前 Homebrew 管理的服務:</p> <p><code>brew services list</code></p> <p>使用 Hombrew 安裝 jenkins,執行如下命令:</p> <p><code>brew install jenkins</code></p> <p>此步,Homebrew 會下載並安裝自動完成全部依賴,包括 java。並會建立 ‘jenkins’ 命令。</p> <p>同時,卸載 jenkins 的方式爲:</p> <p><code>brew uninstall jenkins</code></p> <p>啓動 jenkins 服務的方式爲:</p> <p><code>brew services start jenkins</code></p> <p>使用此種方式啓用 jenkins 服務支持後臺運行,能夠關閉終端工具。</p> <p>也可使用 jenkins 命令來啓用 jenkins 服務。但此種方式不支持後臺運行,關閉命令行工具,服務自動關閉。</p> <p>另,經常使用命令還有:</p> <p>重啓 jenkins 服務的方式爲:</p> <p><code>brew services restart jenkins</code></p> <p>中止 jenkins 服務的方式爲:</p> <p><code>brew services stop jenkins</code></p> <p>經過此種方式安裝,不會在系統中產生垃圾文件和多餘的配置文件,全部的文件都是用 Homebrew 來管理,而且方便卸載。</p> <p>此種方式強烈推薦。</p> <p>如安以上方式安裝後,<a href="http://localhost:8080" rel="nofollow" style="color: rgb(66, 133, 244);">localhost</a> 打不開,可能的狀況通常有:java jdk 未正確安裝或配置,8080端口被佔用,安裝路徑權限不夠等等的問題,遇到這樣的問題最好的辦法是:<strong>卸載重裝</strong>。不一樣的安裝方式,卸載方式也會不一樣。具體可參看<a href="https://www.cnblogs.com/EasonJim/p/6277708.html" rel="nofollow" style="color: rgb(66, 133, 244);">這個教程</a>。</p> <p>另,也有以下方式操做 Jenkins 服務</p> <p><a href="http://localhost:8080/exit" rel="nofollow" style="color: rgb(66, 133, 244);">http://localhost:8080/exit</a> //中止服務</p> <p><a href="http://localhost:8080/restart" rel="nofollow" style="color: rgb(66, 133, 244);">http://localhost:8080/restart</a> //重啓服務</p> <p><a href="http://localhost:8080/reload" rel="nofollow" style="color: rgb(66, 133, 244);">http://localhost:8080/reload</a> // 從新載入服務</p> <h4>配置 jenkins</h4> <h5>建立用戶</h5> <p>完成 jenkins 安裝之後,在瀏覽器種打開 <a href="http://localhost:8080" rel="nofollow" style="color: rgb(66, 133, 244);">localhost</a> ,會進入配置 jenkins 頁面。</p> <p><img src="https://upload-images.jianshu.io/upload_images/1194012-c8468fd91737f725.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700" alt="Jenkins 持續集成使用教程" style="cursor: pointer;"></p> <p>按照提示,找到/Users/Shared/Jenkins/Home/ 這個目錄下,打開 initialAdminPassword 文件,複製出密碼並填入,點擊繼續,進入安裝插件頁面。</p> <p><img src="https://upload-images.jianshu.io/upload_images/1194012-cd9979b853d14ac6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700" alt="Jenkins 持續集成使用教程" style="cursor: pointer;"></p> <p>建議選擇 Install suggested plugins。</p> <p><img src="https://upload-images.jianshu.io/upload_images/1194012-a5636c896f987a50.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700" alt="Jenkins 持續集成使用教程" style="cursor: pointer;"></p> <p>等待插件安裝完成後,jenkins 會重啓,等待重啓完成後,會進入建立用戶頁面。</p> <p><img src="https://upload-images.jianshu.io/upload_images/1194012-7ac78a54760114dd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700" alt="Jenkins 持續集成使用教程" style="cursor: pointer;"></p> <p>填寫用戶信息後,保存便可。請務必記住用戶名和密碼信息,不然重置用戶信息很是麻煩。</p> <h5>安裝插件</h5> <p>登陸 <a href="http://localhost:8080" rel="nofollow" style="color: rgb(66, 133, 244);">http://localhost:8080</a> ,選擇「系統管理」——「管理插件」,在「可選插件」中選中「GitLab Plugin」、「Gitlab Hook Plugin」、「Xcode integration"等經常使用插件,而後安裝。</p> <p>安裝完成插件後,就能夠新建項目了!</p> <p>其餘經常使用插件有:</p> <p><strong>Email Extension Plugin</strong> <br>該插件用於替換 Jenkins 自帶的郵件發送,更加的強大。</p> <p><strong>Git Plugin</strong><br>該插件容許使用GIT做爲一個構建SCM(源代碼控制管理系統)。</p> <p><strong>Post build task</strong><br>該插件容許用戶依據構建日誌的輸出執行一個shell/批處理任務。</p> <p><strong>Ruby Plugin</strong><br>該插件容許用戶在構建腳本中使用 Ruby。</p> <p><strong>Python Plugin</strong><br>添加執行 Python 腳本做爲Hudson的構建步驟。</p> <p><strong>Gradle Plugin</strong><br>該插件容許Hudson調用Gradle構建腳本做爲主體構建的步驟。</p> <p><strong>FTP-Publisher Plugin</strong><br>該插件能上傳項目構件和整個目錄到一個FTP服務器。</p> <p><strong>Extended Choice Parameter plugin</strong><br>該插件能夠擴展參數化構建過程</p> <p><strong>Extended Choice Parameter Plug-In</strong><br>該插件能夠擴展參數化構建過程</p> <p><strong>Dynamic Extended Choice Parameter Plug-In</strong><br>該插件能夠擴展參數化構建過程</p> <p><strong>git parameter Plug-in</strong><br>該插件能夠擴展參數化構建過程</p> <p><strong>fir-plugin</strong><br>該插件是 fir.im 分發平臺提供的 jenkins 插件,用於上傳安裝包到 fir.im 平臺。此插件安裝方式與上面的插件安裝方式不一樣,具體安裝方式見文末。</p> <h4>新建項目</h4> <p>點擊新建,在新建項目頁面輸入項目名稱,選擇「構建一個自由風格的軟件項目」,而後進入配置頁面</p> <p><img src="https://upload-images.jianshu.io/upload_images/1194012-06d0118a5e04dadf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700" alt="Jenkins 持續集成使用教程" style="cursor: pointer;"></p> <p><img src="https://upload-images.jianshu.io/upload_images/1194012-b52d3d102c21f004.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700" alt="Jenkins 持續集成使用教程" style="cursor: pointer;"></p> <p>在<strong>源碼管理</strong>中 選擇 Git ,填寫倉庫地址</p> <p><img src="https://upload-images.jianshu.io/upload_images/1194012-268313680eb7a9ec.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700" alt="Jenkins 持續集成使用教程" style="cursor: pointer;"></p> <p>如出現紅色警告,說明 Jenkins 訪問不了 Git 倉庫,通常是由於沒有權限致使。點擊 Credentials 項的 Add ,在打開的 Jenkins Credentails Provider:Jenkins 頁面,在 Username 和 Password 輸入 Git 倉庫的用戶名和密碼,完成添加。沒必要擔憂用戶信息外泄,Jenkins 服務和全部配置都保存在本地。</p> <p>還能夠經過配置 Git SSH 的方式賦予 Jenkins 訪問 Git 倉庫的權限,按此方法較爲麻煩,具體步驟略。</p> <p>在 Branches to build 中設置 Branch Specifier 要構建的分支,默認爲 master。</p> <p>在<strong>構建觸發器</strong>中設置構建自動化的觸發器。</p> <p><img src="https://upload-images.jianshu.io/upload_images/1194012-af09deb5eb53c6f4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700" alt="Jenkins 持續集成使用教程" style="cursor: pointer;"></p> <p>其中 Build periodically,爲定時 build,在日程表裏設置 build 時間,如設置 00 20 <em> </em> * 表示天天 20 點執行定時 build。</p> <p>Poll SCM (poll source code management) 輪詢源碼管理,表示在指定的時間點去輪詢 Git 倉庫,看是否有新的 commit,若有,則觸發構建。如設置 0/5 <em> </em> <em> </em> 表示每5分鐘輪詢一次。</p> <p>完成此步驟後,剩餘的工做,皆可經過 <strong>Execute shell</strong> 腳原本執行。不一樣類型項目執行腳本不一樣。</p> <h4>腳本構建</h4> <p>以構建 iOS 的 SellerPlatform 迷橙商家端項目上傳 fir.im 分發平臺爲例完成建立。</p> <p><strong>第一步 歸檔</strong></p> <p>archive.sh</p> <blockquote> <p>`#! bin/bash</p> <p>export LC_ALL=zh_CN.GB2312;<br>export LANG=zh_CN.GB2312</p> <p>gitMessage=<code>git log --pretty=format:"%s"</code>;</p> <p>stringTag=${gitMessage%%】<em>}; #%%[</em> 表示從右邊開始,刪除最後(最左邊)一個 [ 號及右邊的字符</p> <p>debug="【Debug";<br>preRelase="【PreRelase";<br>relase="【Relase";</p> <p>if [[ $stringTag != $debug && $stringTag != $preRelase && $stringTag != $relase ]]<br>then<br>exit 1<br>else<br>echo "觸發非參數化構建 暗號:$stringTag】"<br>fi</p> <p>echo ">>>>>>>>>>>>>>>>>>>開始構建項目,當前選擇構建類型:" + $BUILDTYPE<br>echo ">>>>>>>>>>>>>>>>>>>當前 workspace " +$WORKSPACE</p> <p>if [ $BUILDTYPE == "Release"]; then <br>xcodebuild archive -workspace $WORKSPACE/SellerPlatform/SellerPlatform.xcworkspace -archivePath $WORKSPACE/build/SellerPlatform.xcarchive -sdk iphoneos -scheme SellerPlatform -configuration Release</p> <p>else <br>xcodebuild archive -workspace $WORKSPACE/SellerPlatform/SellerPlatform.xcworkspace -archivePath $WORKSPACE/build/SellerPlatform.xcarchive -sdk iphoneos -scheme SellerPlatform -configuration Debug</p> <p>fi</p> <p>echo ">>>>>>>>>>>>>>>>>>>構建完成"</p> </blockquote> <p>以上腳本實現的功能是:當在 git commit messge 裏包含指定的標記號,便可完成對應標記號的構建。把控制權延伸到 git commit 操做裏。</p> <p>其中 【Debug】 表明打 debug 版本包,【PreRelase】表明打 preRelase 版本包,【Relase】表明打 relase 版本包。</p> <p>首先,經過執行 git log --pretty=format:"%s" 命令,格式化輸出 git commit log,<br>經過 %%】* 操做,是要拿到最後標記號【Debug】或【PreRelase】或【Relase】,若是拿到對應的標記號,則完成對應方式的構建。</p> <p>另,Jenkins 會爲每一個工程項目建立一個本地目錄空間,並提供了 \$WORKSPACE 環境變量來直接訪問。</p> <p><strong>第二步 簽名</strong></p> <p>signature.sh</p> <blockquote> <p>echo "開始簽名"</p> <p>xcodebuild -exportArchive -archivePath $WORKSPACE/build/SellerPlatform.xcarchive -exportPath $WORKSPACE/build -exportOptionsPlist $WORKSPACE/build/ExportOptions.plist -allowProvisioningUpdates</p> <p>echo "ipa文件已生成"</p> <p>echo "開始上傳fir"</p> </blockquote> <p>以上腳本實現的功能是:根據第一步歸檔出來的projectName.xcarchive 完成簽名打包。</p> <p><strong>第三步 上傳</strong></p> <p>首先,打開終端,安裝 fir-clifir 的命令行工具,用於經過命令行上傳安裝包。<br>須要先裝好 ruby 再執行</p> <p><code>gem install fir-cli</code></p> <p>上傳打 fir 命令</p> <p><code>fir publish ${ipa_path} -T fir_token -c "${commit_msg}"</code></p> <p>upload.sh</p> <blockquote> <p>echo ">>>>>>>>>>>>>>>>>>>開始上傳到 fir"<br>if [$BUILDTYPE == "Debug" ]; then </p> <p>fir publish $WORKSPACE/build/SellerPlatform.ipa -T efa7a5559110764a5037c52d4599635f --password rose888 -c $CHANGELOG</p> <p>elif [ $BUILDTYPE == "PreRelease" ]; then </p> <p>fir publish $WORKSPACE/build/SellerPlatform.ipa -T 4f43ef4e28992358b511a76eb1c27d48 --password rose888 -c $CHANGELOG</p> <p>else </p> <p>fir publish $WORKSPACE/build/SellerPlatform.ipa -T 3a9a1a361fe52b8b0d5eb59c7d0bd808 --password rose888 -c $CHANGELOG</p> <p>fi</p> <p>echo ">>>>>>>>>>>>>>>>>>>上傳成功啦"</p> </blockquote> <p>至此,就完成了持續集成的流程。</p> <p><img src="https://upload-images.jianshu.io/upload_images/1194012-60e101c4e6cc14fd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700" alt="Jenkins 持續集成使用教程" style="cursor: pointer;"></p> <p>完成後建構,還能夠經過 <strong>Email Extension Plugin</strong> 插件,郵件通知到相關人員。還能夠定製郵件內容。在郵件內容裏,還能夠放置二維碼,方便下載安裝。</p> <h4>更進一步,參數化構建</h4> <p>參數化構建,顧名思義,就是能夠指定構建參數,完成高度可定製化構建。</p> <p>首先,須要安裝 <strong>git parameter Plug-in</strong> 插件,而後項目配置頁面,在 <strong>General</strong> 中,選中‘參數化構建過程’,而後選擇 git parameter。如圖所示:</p> <p><img src="http://www.inspires.cn/resources/images/a647/4282/30e7/a647428230e77a7e99930bcd585df826.png" alt="Jenkins 持續集成使用教程" style="cursor: pointer;"></p> <p>設置 name 爲 git_branch,即爲變量名,而後 Parameter type ,就是 選擇 branch/tag/branch or tag 三種類型,在<strong>源碼管理</strong>中 Branches to build 中設置 Branch Specifier 爲 \$git_branch 。便可完成自定義分支構建。</p> <p><img src="http://www.inspires.cn/resources/images/c6fe/8035/e541/c6fe8035e5411d16b2270d1cf514e519.png" alt="Jenkins 持續集成使用教程" style="cursor: pointer;"></p> <p>一樣,能夠設置構建的版本類型。在構建的時候,進行選擇。在此再也不贅述。</p> <h4>fir-plugin 安裝與使用</h4> <p>在上面的例子裏,使用的是 fir.im 提供的命令行工具完成後建構的上傳,也能夠經過 fir-plugin 插件來完成上傳。</p> <h5>安裝</h5> <p>首先去下載<a href="http://7xju1s.com1.z0.glb.clouddn.com/fir-plugin-1.9.4.hpi" rel="nofollow" style="color: rgb(66, 133, 244);">插件</a><br>。而後,進入"系統管理"->"管理插件"->"高級"->"上傳插件"</p> <p><img src="https://upload-images.jianshu.io/upload_images/1018039-61320b82fbe69d09.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/700" alt="Jenkins 持續集成使用教程" style="cursor: pointer;"></p> <p>選擇已下載好的 fir-plugin.hpi 插件,點擊上傳。</p> <p><img src="https://upload-images.jianshu.io/upload_images/1018039-3cadd6ba2a634cd9.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/700" alt="Jenkins 持續集成使用教程" style="cursor: pointer;"></p> <p>安裝完成後重啓服務。</p> <h5>使用</h5> <p>在 <strong>構建後操做</strong> 選擇"Upload to fir.im",並添加 api token 並校驗</p> <p><img src="https://upload-images.jianshu.io/upload_images/1018039-7f682fad439c8fe0.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/700" alt="Jenkins 持續集成使用教程" style="cursor: pointer;"></p> <p><img src="https://upload-images.jianshu.io/upload_images/1018039-7cf9e4949a7154eb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700" alt="Jenkins 持續集成使用教程" style="cursor: pointer;"></p> <p>當即構建項目,成功以後,在工程裏的 Console Output 中有以下日誌</p> <p><img src="https://upload-images.jianshu.io/upload_images/1018039-466bd414464a0591.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700" alt="Jenkins 持續集成使用教程" style="cursor: pointer;"></p> <p>說明成功了。</p> <h4>其餘配置</h4> <h5>內網訪問</h5> <p>首先,拿到 jenkins 服務器所在設備上的內網 IP,執行以下命令可得到</p> <p><code>ifconfig en0</code></p> <p>進入"系統管理"->"系統設置" 找到 Jenkins Location 在 Jenkins URL 中設置地址,並加上端口號,格式如:172.0.0.1:8080/,而後保存。重啓服務,內網其餘設備輸入地址便可訪問。</p> <p>當其餘設備不能訪問時,通常緣由有:jenkins 服務器沒啓動,設備處於休眠模式,設備內網 IP 被從新分配。</p> <p>以爲不錯的話,歡迎關注個人公衆號哦!</p>html