jenkens实际上是代码上传工具

<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 &amp;&amp; $stringTag != $preRelase &amp;&amp; $stringTag != $relase ]]<br>then<br>exit 1<br>else<br>echo "触发非参数化构建 暗号:$stringTag】"<br>fi</p> <p>echo "&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;开始构建项目,当前选择构建类型:" + $BUILDTYPE<br>echo "&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;当前 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 "&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;构建完成"</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 "&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;开始上传到 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 "&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;上传成功啦"</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>。而后,进入"系统管理"-&gt;"管理插件"-&gt;"高级"-&gt;"上传插件"</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>进入"系统管理"-&gt;"系统设置" 找到 Jenkins Location 在 Jenkins URL 中设置地址,并加上端口号,格式如:172.0.0.1:8080/,而后保存。重启服务,内网其余设备输入地址便可访问。</p> <p>当其余设备不能访问时,通常缘由有:jenkins 服务器没启动,设备处于休眠模式,设备内网 IP 被从新分配。</p> <p>以为不错的话,欢迎关注个人公众号哦!</p>html

相关文章
相关标签/搜索