1. 程式人生 > >Android+Jenkins自動化打包+上傳蒲公英+傳送郵件(測試必會)

Android+Jenkins自動化打包+上傳蒲公英+傳送郵件(測試必會)

Android+Jenkins自動化打包+上傳蒲公英+傳送郵件

 

各位好,由於經常要修改客戶端的伺服器地址和要區分渠道為了方便測試打包學習了一下Jenkins,期間遇到一些問題,反正就是問題比較多,網上也搜了很多東西但是比較散都講了一點,特地自己整理了一下 ,也將自己遇到的坑在這裡和大家分享一下。第一次寫,寫的不好希望各位體諒!!!希望對你有幫助!!!

一、安裝 Jenkins 

官網: https://jenkins.io/  

Jenkins 是基於Java開發的一種持續整合工具,所以,Jenkins需要Java環境。

1、Jenkins安裝有兩種方式:

(1)、安裝Jenkins主要就是下載exe檔案點選執行按步驟走就可以

(2)、安裝war檔案在cmd中使用java命令執行(這個我沒有用)

 

如果實在不會可參考:https://www.cnblogs.com/edward2013/p/5269465.html

 

2、java環境變數配置:

(1)、系統變數→新建 JAVA_HOME 變數 

變數值填寫jdk的安裝目錄(本人是 E:\Java\jdk1.7.0)

系統變數→尋找 Path 變數→編輯

在變數值最後輸入 %JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;

(注意原來Path的變數值末尾有沒有;號,如果沒有,先輸入;號再輸入上面的程式碼)

(2)、JDK安裝與環境變數配置

系統變數→新建 CLASSPATH 變數

變數值填寫  .;%JAVA_HOME%\lib;%JAVA_HOME%\lib\tools.jar(注意最前面有一點)

系統變數配置完畢

(3)、JDK安裝與環境變數配置

檢驗是否配置成功 執行cmd 輸入 java -version (java 和 -version 之間有空格)

顯示版本資訊 則說明安裝和配置成功。

JDK安裝與環境變數配置

3、Gradle配置

(1)、配置環境變數 GRADLE_HOME

將下載以後得到的 gradle-2.12-all.zip 資料夾解壓到我們電腦磁碟上固定存放軟體的位置。在環境變數裡新增使用者變數 GRADLE_HOME(這一步我沒有詳細描述,相信很多朋友都會了,如果有朋友覺得需要詳細描述,歡迎大家給我留言)

(2)、然後在環境變數 path 中增加 %GRADLE_HOME%\bin;,如圖所示:

測試配置成功

測試環境變數是否配置成功的命令有

gradle -v

或者

gradle -version

4、maven配置

maven環境變數配置及測試

首先要確認,你是否已經安裝了jdk,並且已經成功配置了環境變數。

和java配置類似,計算機右鍵屬性->高階系統設定->環境變數。

新增如下變數:

變數名:maven_home 變數值:C:\maven\apache-maven-3.3.9

注意 本機maven的地址,到maven的資料夾就可以了

編輯如下變數:

變數名:path 變數值在最後新增:%maven_home%\bin;

最後可加引號可以不加,但是養成好習慣還是加吧。

測試:在命令列工具中輸入命令列。

mvn -v

如果有配置資訊顯示則證明配置成功。沒有可以檢視資料夾位置是不是寫錯了。

安裝成功後在瀏覽器中輸入http://localhost:8080

二、系統管理中需要配置的檔案介紹

1、點選上圖中“ 系統管理 ”進來如下圖所示

2、首先點選管理外掛  以下是常用的外掛

安裝的時候就會安裝外掛的

Android Emulator Plugin Starts an Android emulator with given properties before a build, then shuts it down after.3.0

Ant Plugin Adds Apache Ant support to Jenkins 1.8

Apache HttpComponents Client 4.x API PluginBundles Apache HttpComponents Client 4.x and allows it to be used by Jenkins plugins.4.5.5-2.0

Authentication Tokens API PluginThis plugin provides an API for converting credentials into authentication tokens in Jenkins.1.3

bouncycastle API Plugin This plugin provides an stable API to Bouncy Castle related tasks.2.16.2

Branch API PluginThis plugin provides an API for multiple branch based projects.2.0.20

Build TimeoutThis plugin allows builds to be automatically terminated after the specified amount of time has elapsed.1.19

build-name-setterThis plug-in sets the display name of a build to something other than #1, #2, #3, ...1.6.9

Command Agent Launcher PluginAllows agents to be launched using a specified command.1.2

Credentials Binding PluginAllows credentials to be bound to environment variables for use from miscellaneous build steps.1.16

Credentials PluginThis plugin allows you to store credentials in Jenkins.2.1.16

description setter pluginThis plugin sets the description for each build, based upon a RegEx test of the build log file.1.10

Display URL APIProvides the DisplayURLProvider extension point to provide alternate URLs for use in notifications2.2.0

Docker Commons PluginProvides the common shared functionality for various Docker-related plugins.1.12

Docker PipelineBuild and use Docker containers from pipelines.1.16

Durable Task PluginLibrary offering an extension point for processes which can run outside of Jenkins yet be monitored.1.22

Email Extension PluginThis plugin is a replacement for Jenkins's email publisher. It allows to configure every aspect of email notifications: when an email is sent, who should receive it and what the email says2.62

External Monitor Job Type PluginAdds the ability to monitor the result of externally executed jobs1.7

Folders PluginThis plugin allows users to create "folders" to organize jobs. Users can define custom taxonomies (like by project type, organization type etc). Folders are nestable and you can define views within folders. Maintained by CloudBees, Inc.6.4

Git client pluginUtility plugin for Git support in Jenkins2.7.2

Git pluginThis plugin integrates Git with Jenkins.3.9.0

GIT server PluginAllows Jenkins to act as a Git server.1.7

GitHub API PluginThis plugin provides GitHub API for other plugins.1.90

GitHub Branch Source PluginMultibranch projects and organization folders from GitHub. Maintained by CloudBees, Inc.2.3.4

GitHub pluginThis plugin integrates GitHub to Jenkins.1.29.0

GitLab PluginThis plugin integrates GitLab to Jenkins by faking a GitLab CI Server.1.5.5

Gradle PluginThis plugin allows Jenkins to invoke Gradle build scripts directly.1.28

Jackson 2 APIThis plugin exposes the Jackson 2 JSON APIs to other Jenkins plugins.2.8.11.1

Javadoc Plugin1.4

JavaScript GUI Lib: ACE Editor bundle pluginJavaScript GUI Lib: ACE Editor bundle plugin.1.1

JavaScript GUI Lib: Handlebars bundle pluginJavaScript GUI Lib: Handlebars bundle plugin.1.1.1

JavaScript GUI Lib: jQuery bundles (jQuery and jQuery UI) pluginJavaScript GUI Lib: jQuery bundles (jQuery and jQuery UI) plugin.1.2.1

JavaScript GUI Lib: Moment.js bundle pluginJavaScript GUI Lib: Moment.js bundle plugin.1.1.1

JDK Tool PluginAllows the JDK tool to be installed via download from Oracle's website.1.1

JSch dependency pluginJenkins plugin that brings the JSch library as a plugin dependency, and provides an SSHAuthenticatorFactory for using JSch with the ssh-credentials plugin.0.1.54.2

JUnit PluginAllows JUnit-format test results to be published.1.24

LDAP PluginAdds LDAP authentication to Jenkins1.20

Mailer PluginThis plugin allows you to configure email notifications for build results1.21

MapDB API PluginThis plugin provides a shared dependency on the MapDB library so that other plugins can co-operate when using this library.1.0.9.0Matrix Authorization Strategy PluginOffers matrix-based security authorization strategies (global and per-project).2.2

Matrix Project PluginMulti-configuration (matrix) project type.1.13

OWASP Markup Formatter PluginUses the OWASP Java HTML Sanitizer to allow safe-seeming HTML markup to be entered in project descriptions and the like.1.5

PAM Authentication pluginAdds Unix Pluggable Authentication Module (PAM) support to Jenkins1.3

PipelineA suite of plugins that lets you orchestrate automation, simple or complex. See Pipeline as Code with Jenkins for more details.2.5

Pipeline Graph Analysis PluginProvides a REST API to access pipeline and pipeline run data.1.6

Pipeline: APIPlugin that defines Pipeline API.2.27

Pipeline: Basic StepsCommonly used steps for Pipelines.2.7

Pipeline: Build StepAdds the Pipeline step build to trigger builds of other jobs.2.7

Pipeline: DeclarativeAn opinionated, declarative Pipeline.1.2.9

Pipeline: Declarative Agent APIReplaced by Pipeline: Declarative Extension Points API plugin.1.1.1

Pipeline: Declarative Extension Points APIAPIs for extension points used in Declarative Pipelines.1.2.9

Pipeline: GitHub Groovy LibrariesAllows Pipeline Grrovy libraries to be loaded on the fly from GitHub.1.0

Pipeline: GroovyPipeline execution engine based on continuation passing style transformation of Groovy scripts.2.53

Pipeline: Input StepAdds the Pipeline step input to wait for human input or approval.2.8

Pipeline: JobDefines a new job type for pipelines and provides their generic user interface.2.21

Pipeline: Milestone StepPlugin that provides the milestone step1.3.1

Pipeline: Model APIModel API for Declarative Pipeline.1.2.9

Pipeline: MultibranchEnhances Pipeline plugin to handle branches better by automatically grouping builds from different branches.2.18

Pipeline: Nodes and ProcessesPipeline steps locking agents and workspaces, and running external processes that may survive a Jenkins restart or slave reconnection.2.19

Pipeline: REST API PluginProvides a REST API to access pipeline and pipeline run data.2.10

Pipeline: SCM StepAdds a Pipeline step to check out or update working sources from various SCMs (version control).2.6

Pipeline: Shared Groovy LibrariesShared libraries for Pipeline scripts.2.9

Pipeline: Stage StepAdds the Pipeline step stage to delineate portions of a build.2.3

Pipeline: Stage Tags MetadataLibrary plugin for Pipeline stage tag metadata.1.2.9

Pipeline: Stage View PluginPipeline Stage View Plugin.2.10

Pipeline: Step APIAPI for asynchronous build step primitive.2.14

Pipeline: Supporting APIsCommon utility implementations to build Pipeline Plugin2.18

Plain Credentials PluginAllows use of plain strings and files as credentials.1.4

Port Allocator Plug-inThis plugin allocates free ports as environment variables.1.8

Resource Disposer PluginDispose resources asynchronously. Utility plugin for resources that require more retries or take a long time to delete.0.8

SCM API PluginThis plugin provides a new enhanced API for interacting with SCM systems.2.2.7

Script Security PluginAllows Jenkins administrators to control what in-process scripts can be run by less-privileged users.1.44

SSH Credentials PluginAllows storage of SSH credentials in Jenkins1.13

SSH Slaves pluginAllows to launch agents over SSH, using a Java implementation of the SSH protocol.1.26

Structs PluginLibrary plugin for DSL plugins that need names for Jenkins objects.1.14

Subversion Plug-in2.10.5

TimestamperAdds timestamps to the Console Output1.8.10

Token Macro PluginThis plug-in adds reusable macro expansion capability for other plug-ins to use.2.5

Windows Slaves PluginAllows you to connect to Windows machines and start slave agents on them.1.3.1

Workspace Cleanup PluginThis plugin deletes the project workspace after a build is finished.0.34

3、檢查完外掛後請前往系統設定

(1)、配置gitlab

gitlab的配置需要安裝外掛  如果沒有有下圖的gitLab請在外掛中下載gitlab外掛。

這塊要輸入使用者名稱

還有就是你們自己的gitlab地址

 

這塊主要的問題是和gitlab關聯這塊問題是 token的生成

 

點選add顯示下面

token生成步驟是前往gitlab個人設定中---訪問令牌

輸入使用者名稱 時間不用選擇  選擇api  點選建立 生成一個token祕鑰  將祕鑰輸入到jenkins中的的API Token然後測試連線就可以

(2 )、郵件的配置

 

郵件配置要注意下面這塊下面配置的郵件的地址 兩處要配置一樣的傳送郵件地址  還有就是管理員的郵件地址  不然有問題  這塊是個大坑

 

Default Content 是郵件的模板以上就是目前我在系統設定中的配置

(3)、講解全域性工具配置

 

配置jdk

 

配置git

配置gradle

配置maven

配置ant

配置完成

三、新建一個任務

新建好了 就在這裡顯示

、點選任務名稱

 

點選配置

配置介紹

1、描述

2、引數化構建過程配置

點選新增引數選擇

這塊主要是開發寫 這裡都是關鍵的東西  什麼域名渠道 Version_code、version_name、application_id、environment、伺服器的地址

伺服器地址配置 這個需要在安卓程式碼寫東西 這個看到最後就知道了

打包配置

渠道打包配置

3、配置git倉庫

這塊主要的問題還是配置ssh證書  使用http比較簡單 直接可以在add中新增 

這塊問題新增ssh證書問題比較多 如果弄不了建議就是百度一下配置的方法 

4、配置gradle

在構建中找到這個 新增Invoke Gradle script

選擇高階才會出現下面的

這塊我遇到了 一個大問題 就是這個編譯要先配置成 clean build 等第一次執行完了以後 會先編譯失敗 具體為啥我也不知道 但是必須先用build然後編譯完成後換成圖2的配置

圖1:

圖2:

clean assemble'${PRODUCT_FLAVOR_BUILD}''${ENVIRONMENT}' --stacktrace --debug

圖2表示的是:${PRODUCT_FLAVOR_BUILD}渠道名字 和你構建引數那個渠道名字要一樣、${ENVIRONMENT}'這個一樣 後面的意思就是列印堆疊的資訊 

這個一定記住第一次使用build編譯完了、才可以修改成這個 不然會一直失敗(我遇到的)   

5、郵件的配置

 

郵件模板可以在這裡配置也可以在系統設定中配置  一般否是統一的 我都會配置在系統設定中 郵件模板網上一大堆自己可以搜尋一下

下面的構建後操作都是在選擇新增的

 

歸檔成品的意思是顯示這個

下面這個配置顯示的結果就是第二種圖的那個 ,會在構建歷史裡面顯示 如果沒有下面這個選項肯定沒有外掛 。

#${BUILD_NUMBER}-${APPLICATION_ID}-${APP_VERSION}-${PRODUCT_FLAVOR_BUILD}-${ENVIRONMENT}

 

四、介紹構建打包

點選名稱進來

點選Build with Parameters 出現下圖

上面這塊就可以選擇引數配置

點選構建

會顯示執行完的安裝包並且郵件傳送

 

五、上傳蒲公英配置

下載外掛:jenkins -> 系統管理 -> 外掛管理,搜尋 Upload to pgyer,點選下載,下載完的效果如下圖:

在 jenkins 的 job 配置頁面 構建或 構建後操作中新增構建步驟 upload to pgyer 如下圖:

 

在 jenkins 的 job 配置頁面 構建或 構建後操作中新增構建步驟 upload to pgyer 如下圖:

 

外掛新增成功後,會顯示一下效果:

 

外掛填寫引數說明:引數說明

pgyer uKey蒲公英的 uKey (必填)

pgyer api_key蒲公英的 api_key (必填)

scandiripa/apk 所在目錄 (必填)

file widcard上傳檔案的萬用字元 (必填)

installType(optional)應用安裝方式,值為(1,2,3)。1:公開,2:密碼安裝,3:邀請安裝。預設為1公開(選填)

password(optional)設定App安裝密碼(選填)

updateDescription(optional)版本更新描述(選填)

qrcodePath(optional)如果你需要下載蒲公英返回的二維碼,那麼這裡填寫二維碼的儲存路徑,如果你不需要下載,那麼你不需要在這裡填寫任何內容(選填)

envVarsPath(optional)如果你想儲存蒲公英返回的上傳資訊,那麼這裡填寫儲存資訊的檔案路徑,如果你不需要儲存,那麼你不需要在這裡填寫任何內容(選填)

注意:

qrcodePath與envVarPath是儲存二維碼和應用資訊的檔案路徑地址,而不是一個資料夾的地址。例如可以這樣填寫這兩個引數:

qrcodePath: /Users/James/IOS_Integration/${BUILD_TYPE}/${BUILD_TIME}/qrcode.png

envVarPath: /Users/James/IOS_Integration/${BUILD_TYPE}/${BUILD_TIME}/envVars.txt**

執行構建,蒲公英上傳外掛將輸出相應的 log,如下圖:

上傳蒲公英成功後,可在 jenkins 中的其他構建中使用蒲公英上傳成功後返回的引數:

這款會將蒲公英返回的引數注入為jenkins的全域性變數,在其他構建步驟的使用方法直接引用這個全域性變數即可,變數名稱直接使用返回的 key值。例如:${appBuildURL}

注意:

qrcodePath與envVarPath是儲存二維碼和應用資訊的檔案路徑地址,而不是一個資料夾的地址。例如可以這樣填寫這兩個引數:

qrcodePath: /Users/James/IOS_Integration/${BUILD_TYPE}/${BUILD_TIME}/qrcode.png

envVarPath: /Users/James/IOS_Integration/${BUILD_TYPE}/${BUILD_TIME}/envVars.txt**

我的配置

 

 

六、安卓app\gradle.build配置

前往gradle.properties

新增

這個就是我們上面說的構建的引數的伺服器地址

前往app下的build.gradle

渠道打包

下面這塊主要是URL的一個介紹  這個就是伺服器的地址BASE_URL

獲取方式是

這個是打包的程式碼

到此為止。

第一次寫文章 ,寫的不好希望各位能夠提出!!我的郵箱是[email protected]有問題可以回覆郵箱。

謝謝。