实践Jenkins+Gradle实现Android自动化构建,并上传至fir.im

来源:互联网 发布:滑雪板品牌知乎 编辑:程序博客网 时间:2024/06/05 05:25

实践Jenkins+Gradle实现Android自动化构建,并上传至fir.im

前提条件

  • Java环境
  • Android SDK
  • tomcat

遇到的最主要的坑

第一次自己配置Jenkins,很多东西都不了解,难免会遇到一些坑,这里列一下我这次Jenkins之旅最主要的两个坑。解决方案就在文中。

  • local.properties文件找不到

  • 编译时gradlew文件permission denied

下载安装Jenkins

Jenkins官网

将下载的Jenkins.war包直接放到tomcat下的webapps目录,启动tomcat即可安装完成。

安装完成后进入Jenkins -> http://localhost:8080/jenkins

Jenkins插件安装

Jenkins首次启动应该会有选装插件页面,gradle、git……捡需要的安装就行了。

如果没有自动出现选装插件页面,则可以在系统设置->管理插件
系统设置
管理插件

创建项目

填写项目名称,选择构建一个自由风格的软件项目,点击OK
创建项目

General

填一下描述信息即可
General

源码管理

我这边用的是Git
源码管理

  • Repository URL : 项目Git地址
  • Credentials : 如果是私有项目,点后边的Add,填一下用户名密码,然后在前边“none”的下拉列表里选择刚才填写的用户名
  • Branch Specifier : 分支选择

构建触发器

构建触发器

构建的触发时机配置,这个没有仔细研究过,貌似是跟Spring的定时器类似。
每两分钟检查一次 : H/2 * * * *
定时检查的配置,Jenkins给出的注释如下:

This field follows the syntax of cron (with minor differences). Specifically, each line consists of 5 fields separated by TAB or whitespace:MINUTE HOUR DOM MONTH DOWMINUTE  Minutes within the hour (059)HOUR    The hour of the day (023)DOM The day of the month (131)MONTH   The month (112)DOW The day of the week (07) where 0 and 7 are Sunday.To specify multiple values for one field, the following operators are available. In the order of precedence,* specifies all valid valuesM-N specifies a range of valuesM-N/X or */X steps by intervals of X through the specified range or whole valid rangeA,B,...,Z enumerates multiple valuesTo allow periodically scheduled tasks to produce even load on the system, the symbol H (for “hash”) should be used wherever possible. For example, using 0 0 * * * for a dozen daily jobs will cause a large spike at midnight. In contrast, using H H * * * would still execute each job once a day, but not all at the same time, better using limited resources.The H symbol can be used with a range. For example, H H(0-7) * * * means some time between 12:00 AM (midnight) to 7:59 AM. You can also use step intervals with H, with or without ranges.The H symbol can be thought of as a random value over a range, but it actually is a hash of the job name, not a random function, so that the value remains stable for any given project.Beware that for the day of month field, short cycles such as */3 or H/3 will not work consistently near the end of most months, due to variable month lengths. For example, */3 will run on the 1st, 4th, …31st days of a long month, then again the next day of the next month. Hashes are always chosen in the 1-28 range, so H/3 will produce a gap between runs of between 3 and 6 days at the end of a month. (Longer cycles will also have inconsistent lengths but the effect may be relatively less noticeable.)Empty lines and lines that start with # will be ignored as comments.In addition, @yearly, @annually, @monthly, @weekly, @daily, @midnight, and @hourly are supported as convenient aliases. These use the hash system for automatic balancing. For example, @hourly is the same as H * * * * and could mean at any time during the hour. @midnight actually means some time between 12:00 AM and 2:59 AM.Examples:# every fifteen minutes (perhaps at :07, :22, :37, :52)H/15 * * * *# every ten minutes in the first half of every hour (three times, perhaps at :04, :14, :24)H(0-29)/10 * * * *# once every two hours at 45 minutes past the hour starting at 9:45 AM and finishing at 3:45 PM every weekday.45 9-16/2 * * 1-5# once in every two hours slot between 9 AM and 5 PM every weekday (perhaps at 10:38 AM, 12:38 PM, 2:38 PM, 4:38 PM)H H(9-16)/2 * * 1-5# once a day on the 1st and 15th of every month except DecemberH H 1,15 1-11 *

构建环境

构建环境

构建

构建

Execute shell

这个配置的主要作用是创建local.properties文件。

因为一般情况下该文件输入个人配置,不会add到git服务器上。所以Jenkins从git上clone下来的时候也不会有这个文件,如果没有这个文件编译是过不去的。

这段的主要意思是检查local.properties文件是否存在,如果不存在就创建,并且把sdk.dir=/Users/robin/Develop/android-sdk这句写到文件中,当然这个要根据电脑不同替换成不同的路径。

Invoke Gradle script

执行Gradle编译

配置的时候在这里遇到一个问题:一直报错 IOException gradlew文件permission denied.

后来把Make gradlew executable勾上就好了。

至此,Jenkins已经可以完成自动构建了。

构建后的操作

顾名思义,构建完成之后需要进行的操作,具体可配置的操作类型如下:

构建后的操作

我这边配置的是将应用上传至fir.im 以及 邮件通知相关人员

这里写图片描述

Upload to fir.im

首先需要安装fir.im插件

fir.im插件安装

ExInclude IPA/APK File Name : 凡是匹配到的文件不会被上传

照着教程来,So Easy ~

这里写图片描述

Editable Email Notification

邮件通知的配置,这个可以参考这里


感谢Google
感谢CSDN
感谢推酷
感谢无私奉献的各位前辈
最后感谢我自己


0 0