某小型公司持续集成工具 jenkins 实践


prtyaa
prtyaa 2024-01-01 23:16:24 51662
分类专栏: 资讯

前言

本文可能是网上最全的一篇全端jenkins部署解决方案介绍的文章,一直以来,领导都想解决代码提交和打包问题,尤其是小公司,打包流程混乱,造成线上版本和代码库git或svn中代码不一致问题。加入jenkins阵营,解决众多发包难题,显得刻不容缓。

研究过程

刚开始,我一直在docker下安装jenkins,按时长租用阿里的主机,用阿里容器服务做实验,反复创建docker容器,建立多个jenkins节点。结果发现,在容器中配置各种变量比较复杂,各种开发环境不易快速部署,当然啦,是刚开始的对jenkins研究过少造成的。

后来我采用了windows主机,配java、maven、Android环境,踩了一路坑,终于搞定了。但是我知道ios开发和编译必须使用mac系统,本来想增加一个jenkins节点来实现ios的编译。

当配置好之后,我突然改变了主意,与其两台电脑开着,不如索性把整个jenkins部署到mac系统下(毕竟我们公司暂时没有c#工程)。于是,所有的操作,所有的环境我又折腾了一遍。

下面我逐一讲解最终mac下实现全端jenkins持续集成,当然不包含c#,一路踩坑下来,我觉得这都不是事,流程都差不多。

效果图

 

 

 

 

安装jenkins

我个人比较喜欢新事物,所以建议使用较高版本,我使用的2.61,目前最新版是2.75。

mac下使用brew安装,终端执行

brew install jenkins

等安装完成后,终端运行jenkins即可启动。

jenkins

安装 xcode

这个比较简单,直接从appstore下载最新版即可。安装后,将公司项目拉入xcode,先保证项目在本机编译,这里我登录了app开发者帐号,以便下载各种证书及发布。

安装 Android studio

这个到这里下载www.android-studio.org/

 

 

同理将本地项目拉入,运行。这一步主要是怕开发环境引起的坑,先把项目跑起来,后面坑会少点。

 

 

进入设置,复制出sdk文件路径,后面需要使用。我这里是/Users/Shaolei/Library/Android/sdk

 

 

安装git

因为我们公司用的是git,如果贵公司使用svn,则同理,只需保git或者svn命令可以敲出来即可。

我这里的版本比较老,尽量使用新的,我懒得换了。

mac:~ shaolei$ git version
git version 2.11.0

安装source tree

这是一个非常好用的git可视化工具,改天会具体介绍它的强大功能。

这款软件不是必须的,只是为了方便项目拉取,所以,你可以跳过此步骤。

配置jenkins环境

这里才是至关重要的一项,本文的核心

配置插件

 

 

 

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-in

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库地址

 

 

下面配置触发器,每隔1分钟检查一次git库,也可以使用subscribe订阅,这里暂不做讲解。

 

 

设置编译后,将文件拷贝到一个文件目录。这里使用smb协议连接到了一个内网文件服务器上,方便测试部门测试。

 

 

至于具体为何执行2次拷贝,是因为项目同时编译测试环境地址和真实环境地址,只有连接的服务器地址不通,其他代码都相同,具体实现可看另外一篇博文:

juejin.im/post/59302a0f

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/59648bb0

java项目job

 

 

因为公司人员少,项目版本如果让开发去控制,比较繁琐,每个开发得不停的增加版本号,这里我使用jenkins去控制版本号,这样也方便查询和回滚代码。

当然啦,实现war的升级和回滚可看另外一篇文章:

juejin.im/post/59633a12
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各项目持续继承方案,因为涉及环境过多,坑还是比较多的,希望自行填坑。

网站声明:如果转载,请联系本站管理员。否则一切后果自行承担。

本文链接:https://www.xckfsq.com/news/show.html?id=33772
赞同 0
评论 0 条
prtyaaL2
粉丝 1 发表 2553 + 关注 私信
上周热门
如何使用 StarRocks 管理和优化数据湖中的数据?  2935
【软件正版化】软件正版化工作要点  2854
统信UOS试玩黑神话:悟空  2811
信刻光盘安全隔离与信息交换系统  2702
镜舟科技与中启乘数科技达成战略合作,共筑数据服务新生态  1235
grub引导程序无法找到指定设备和分区  1205
点击报名 | 京东2025校招进校行程预告  162
华为全联接大会2024丨软通动力分论坛精彩议程抢先看!  160
2024海洋能源产业融合发展论坛暨博览会同期活动-海洋能源与数字化智能化论坛成功举办  157
华为纯血鸿蒙正式版9月底见!但Mate 70的内情还得接着挖...  154
本周热议
我的信创开放社区兼职赚钱历程 40
今天你签到了吗? 27
信创开放社区邀请他人注册的具体步骤如下 15
如何玩转信创开放社区—从小白进阶到专家 15
方德桌面操作系统 14
我有15积分有什么用? 13
用抖音玩法闯信创开放社区——用平台宣传企业产品服务 13
如何让你先人一步获得悬赏问题信息?(创作者必看) 12
2024中国信创产业发展大会暨中国信息科技创新与应用博览会 9
中央国家机关政府采购中心:应当将CPU、操作系统符合安全可靠测评要求纳入采购需求 8

加入交流群

请使用微信扫一扫!