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]有问题可以回复邮箱。

谢谢。

猜你喜欢

转载自blog.csdn.net/weixin_39706415/article/details/83108633