Jenkins實現持續整合 使用Ant指令碼構建ios專案

Hext123發表於2018-06-14

原文 : tomczhen的部落格

在Jenkins平臺上使用Ant指令碼構建iOS專案, 可以做到自動構建打包, 上傳蒲公英, 構建完成傳送微信通知 等功能. 釋出開發者進行測試分發,只需要點一下即可完成一系列操作.

Jenkins構建皮膚


安裝CocoaPods

  1. 更新Ruby

     gem update --system
    複製程式碼
  2. 修改Ruby安裝源

     gem sources --remove https://rubygems.org/
     gem sources -a https://ruby.taobao.org/
    複製程式碼

    注:使用gem sources -l命令檢視源列表

  3. 安裝cocoapods

     sudo gem install cocoapods
    複製程式碼

xctool安裝

  1. 安裝brew

     ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
    複製程式碼
  2. 安裝Xcode’s Command Line Tools

     xcode-select install
    複製程式碼
  3. 使用brew安裝xctool

     brew install xctool
    複製程式碼

安裝Apache Ant

  • brew install ant
    複製程式碼

配置執行環境

在Jenkins的系統設定頁面設定ANT_HOME路徑,並在全域性屬性中新增以下鍵值。

  • LANGzh_CN.UTF-8
  • PATH系統PATH輸出值

編寫Ant指令碼

  • 官方手冊,可以檢視可用的任務或命令以及具體的引數,例項。

新增Jenkins任務

  • 新建任務型別”構建一個自由風格的軟體專案”,選擇原始碼管理的方式。在”增加構建步驟”中選擇Invoke Ant,開啟高階選項,配置好Targets,在Build File中輸入配置好的build.xml檔案路徑。

  • 注意:可以在任務中只配置原始碼管理,執行任務測試獲取程式碼是否正常,然後在伺服器上的命令列介面中使用ant命令呼叫專案中的build.xml檔案進行編譯測試。


指令碼例項

指令碼說明:

  • ant指令碼本質上還是通過呼叫xctool或xcodebuild命令進行iOS打包
<?xml version="1.0" encoding="UTF-8"?>
<project name="antProjectName" default="config.dev, modify, pack, pgy">
    <property name="project.name" value="projectName" />
    <property name="project.workspace.name" value="workspaceName" />
    <property name="project.scheme.name" value="schemeName" />
    
    <exec executable="date" outputproperty="build.time" failifexecutionfails="false" errorproperty="DateError">
        <arg value="+%Y%m%d%H%M" />
    </exec>
    
    <!-- 獲取當前 Git 版本控制資訊 -->
    <exec executable="git" outputproperty="git.tag" failifexecutionfails="false" errorproperty="GitTagError">
        <arg value="describe" />
        <arg value="--tags" />
    </exec>
    <exec executable="git" outputproperty="git.commit" failifexecutionfails="false" errorproperty="GitCommitError">
        <arg value="log" />
        <arg value="--pretty=format:%h" />
        <arg value="-n1" />
    </exec>
    <exec executable="git" outputproperty="git.log" failifexecutionfails="false" errorproperty="GitLogError">
        <arg value="log" />
        <arg value="--pretty=format:%B" />
        <arg value="-n1" />
    </exec>
    <target name="config.dev">
        <property name="project.profile.name" value="adhoc" />
        <!-- 構建開發環境配置資訊 -->
        <property name="build.type" value="Dev" />
        <property name="build.config.name" value="開發環境" />
        <property name="build.file.path" value="dev" />
        <property name="build.file.name" value="${project.name}_${git.tag}_beta${build.time}" />
        <property name="pgy.description" value="LAN-Beta${line.separator}Tag:${git.tag} Commit:${git.commit}${line.separator}${git.log}" />
        <!-- 介面伺服器地址 -->
        <property name="api.url" value="http://api.dev.server.com" />
        <!-- 第三方 Key -->
            <!-- 融雲 -->
            <property name="key.rongcloud" value="rongcloud" />
            <property name="key.rongcloud_scid" value="rongcloud_scid" />
        
        <property name="build.file.url" value="http://www.pgyer.com/pgy" />
        <property name="build.file.qrcode" value="http://www.pgyer.com/app/qrcode/pgy" />
    </target>
    
    <target name="config.beta">
        <property name="project.profile.name" value="adhoc" />
        <!-- 構建測試環境配置資訊 -->
        <property name="build.type" value="Beta" />
        <property name="build.config.name" value="測試環境" />
        <property name="build.file.path" value="beta" />
        <property name="build.file.name" value="${project.name}_${git.tag}_beta${build.time}" />
        <property name="pgy.description" value="Beta${line.separator}Tag:${git.tag} Commit:${git.commit}${line.separator}${git.log}" />
        <!-- 介面伺服器地址 -->
        <property name="api.url" value="http://api.beta.server.com" />
        <!-- 第三方 Key -->
            <!-- 融雲 -->
            <property name="key.rongcloud" value="rongcloud" />
            <property name="key.rongcloud_scid" value="rongcloud_scid" />
        
        <property name="build.file.url" value="http://www.pgyer.com/pgy" />
        <property name="build.file.qrcode" value="http://www.pgyer.com/app/qrcode/pgy" />
    </target>
    
    <target name="config.release">
        <property name="project.profile.name" value="adhoc" />
        <!-- 構建生產環境配置資訊 -->
        <property name="build.type" value="Release" />
        <property name="build.config.name" value="生產環境-蒲公英" />
        <property name="build.file.path" value="release" />
        <property name="build.file.name" value="${project.name}_${git.tag}_r${build.time}" />
        <property name="pgy.description" value="Release${line.separator}Tag:${git.tag} Commit:${git.commit}${line.separator}${git.log}" />
        <!-- 介面伺服器地址 -->
        <property name="api.url" value="http://api.release.server.com" />
        <!-- 第三方 Key -->
            <!-- 融雲 -->
            <property name="key.rongcloud" value="rongcloud" />
            <property name="key.rongcloud_scid" value="rongcloud_scid" />
        
        <property name="build.file.url" value="http://www.pgyer.com/pgy" />
        <property name="build.file.qrcode" value="http://www.pgyer.com/app/qrcode/pgy" />
    </target>
    
    <target name="config.appstore">
        <property name="project.profile.name" value="appstore" />

        <!-- 構建正式環境配置資訊 -->
        <property name="build.type" value="AppStore" />
        <property name="build.config.name" value="正式環境-AppStore" />
        <property name="build.file.path" value="release" />
        <property name="build.file.name" value="${project.name}_${git.tag}_r${build.time}" />
        <property name="pgy.description" value="Release${line.separator}Tag:${git.tag} Commit:${git.commit}${line.separator}${git.log}" />
        <!-- 介面伺服器地址 -->
        <property name="api.url" value="http://api.release.server.com" />
        <!-- 第三方 Key -->
        <!-- 融雲 -->
        <property name="key.rongcloud" value="rongcloud" />
        <property name="key.rongcloud_scid" value="rongcloud_scid" />
        
        <property name="build.file.url" value="http://download.server.com/IOS/${project.scheme.name}/${build.file.name}.ipa" />
        <!-- 二維碼圖片介面 -->
        <property name="build.file.qrcode" value="http://qr.liantu.com/api.php?text=${build.file.url}" />
    </target>
    
    <target name="modify">
    <!-- 配置介面伺服器地址 -->
        <!-- API Center Url -->
        <replaceregexp byline="true" encoding="UTF-8">
            <regexp pattern='^NSString\s+\*\s+const\s+APIServerUrl\s+=\s+(.*)' />
            <substitution expression='NSString * const APIServerUrl = @"${api.url}";' />
            <fileset dir="ProjectPath/Classes/Main/Other" includes="Const.m" />
        </replaceregexp>
    <!-- 配置第三方 Key -->
        <!-- 融雲 Key -->
        <replaceregexp byline="true" encoding="UTF-8">
            <regexp pattern='^#define\s+RONGCLOUD_IM_APPKEY\s+(.*)' />
            <substitution expression='#define RONGCLOUD_IM_APPKEY @"${key.rongcloud}"' />
            <fileset dir="ProjectPath/Classes/Main/Other" includes="Const.h" />
        </replaceregexp>
        <!-- 融雲客服 ID -->
        <replaceregexp byline="true" encoding="UTF-8">
            <regexp pattern='^#define\s+CustomerServiceId\s+(.*)' />
            <substitution expression='#define CustomerServiceId @"${key.rongcloud_scid}"' />
            <fileset dir="ProjectPath/Classes/Main/Other" includes="Const.h" />
        </replaceregexp>
        
    </target>
    
    <target name="pack">
        <property name="build.archive.path" value="build/${build.file.path}/${build.file.name}" />
        <!-- pod install-->
        <exec executable="pod" failonerror="true">
            <arg value="install" />
            <arg value="--verbose" />
            <arg value="--no-repo-update" />
        </exec>
        <!-- Clean -->
        <exec executable="xctool" failonerror="true">
            <arg value="-workspace" />
            <arg value="${project.workspace.name}.xcworkspace" />
            <arg value="-scheme" />
            <arg value="${project.scheme.name}" />
            <arg value="clean" />
        </exec>
        <!-- archive -->
        <exec executable="xctool" failonerror="true">
            <arg value="-workspace" />
            <arg value="${project.workspace.name}.xcworkspace" />
            <arg value="-scheme" />
            <arg value="${project.scheme.name}" />
            <arg value="build" />
            <arg value="archive" />
            <arg value="-archivePath" />
            <arg value="${build.archive.path}" />
        </exec>
        <!-- export ipa -->
        <exec executable="xcodebuild" failonerror="true">
            <arg value="-exportArchive" />
            <arg value="-archivePath" />
            <arg value="${build.archive.path}.xcarchive" />
            <arg value="-exportPath" />
            <arg value="${build.archive.path}.ipa" />
            <arg value="-exportFormat" />
            <arg value="ipa" />
            <arg value="-exportProvisioningProfile" />
            <arg value="${project.profile.name}" />
        </exec>
        
        <condition property="isAppStore">
            <equals arg1="${build.type}" arg2="AppStore" />
        </condition>
        <antcall target="copy2share" />
        <antcall target="pgy" />
    </target>
    
    <target name="copy2share" if="isAppStore">
        <!-- 輸出檔案到指定目錄 -->
        <copy file="${basedir}/${build.archive.path}.ipa" todir="/Jenkins/IOS/${project.scheme.name}/" />
    </target>

    <target name="pgy" unless="isAppStore">
    <!-- 釋出到蒲公英平臺 -->
        <property name="pgy.ukey" value="pgyukey" />
        <property name="pgy.api_key" value="pgyapi_key" />
        <exec executable="curl" outputproperty="pgy.json" failifexecutionfails="true">
            <arg value="-s" />
            <arg value="-F" />
            <arg value="file=@${basedir}/${build.archive.path}.ipa" />
            <arg value="-F" />
            <arg value="uKey=${pgy.ukey}" />
            <arg value="-F" />
            <arg value="_api_key=${pgy.api_key}" />
            <arg value="-F" />
            <arg value="updateDescription=${pgy.description}" />
            <arg value="http://www.pgyer.com/apiv1/app/upload"/>
        </exec>
        <echo message="${pgy.json}" />
    </target>
    
    <target name="wechat">
        <exec executable="date" outputproperty="build.report.time" failifexecutionfails="false" errorproperty="DateError">
            <arg value="+%Y年%m月%d日%H:%M" />
        </exec>
        <!-- corp.media 需要使用永久素材ID -->
        <!--
  		wechat.agentid 微信企業號應用id
			wechat.partyid 接收部門ID
			wechat.userid  接收使用者ID
		-->
		<property name="wechat.partyid" value="2" />
		<property name="wechat.agentid" value="12" />
		<property name="wechat.userid" value="" />
        <!-- 獲取微信API Token -->
        <property name="corp.id" value="wechat_id" />
        <property name="corp.secret" value="wechat_secret" />
        <property name="corp.media" value="wechat_media_id" />
        
        <exec executable="curl" outputproperty="corp.token.json" failifexecutionfails="false">
            <arg value="-s" />
            <arg value="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=${corp.id}&corpsecret=${corp.secret}"/>
        </exec>
        <!-- <echo message="${corp.token.json}" /> -->
        
        <script language="javascript">
            var jsonString = DJT.getProperty("corp.token.json");
            var json = eval ("(" + jsonString + ")");
            DJT.setProperty("corp.token",json.access_token);
        </script>
        <!-- <echo message="${corp.token}" /> -->
        
        <!-- 傳送微信資訊 -->
        <property name="build.digest" value="構建完成${line.separator}${ant.project.name}-IOS [${build.config.name}]${line.separator}Tag:${git.tag} Commit:${git.commit}" />
        <property name="build.report" value="<p>構建完成<br/>${ant.project.name}-IOS [${build.config.name}]<br/>${build.report.time}<br/>檔名稱<br/>${build.file.name}<br/>Tag:${git.tag} Commit:${git.commit}<br/>更新說明<br/>${build.type}<br/>${git.log}<br/>下載地址<br/><a href=\"${build.file.url}\" src=\"${build.file.url}\">${build.file.url}</a><br/><img src=\"${build.file.qrcode}\"/></p>" />
        
        <property name="build.report.json" value="{"touser":"","toparty":"${wechat.partyid}","totag":"","msgtype":"mpnews","agentid":${wechat.agentid},"mpnews":{"articles":[{"title":"Jenkins 構建通知","thumb_media_id":"${corp.media}","author":"Jenkins","content_source_url":"","content":"${build.report}","digest":"${build.digest}","show_cover_pic":"0"}]},"safe":"0"}" />
        <!-- <echo message="${build.report.json}" /> -->
        
        <exec executable="curl" outputproperty="corp.message.json" failifexecutionfails="false">
            <arg value="-s" />
            <arg value="-l" />
            <arg value="-H" />
            <arg value="Content-type: application/json" />
            <arg value="-X" />
            <arg value="POST" />
            <arg value="-d" />
            <arg value="${build.report.json}" />
            <arg value="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=${corp.token}"/>
        </exec>
        
        <echo message="${corp.message.json}" />
        
    </target>
</project>
複製程式碼

相關文章