Android Jenkins自动化打包

1、准备工作

  1. Java JDK下载与配置环境变量
  2. Adnroid SDK下载与配置环境变量
  3. GIT下载与配置环境变量
  4. Gradle 下载与环境配置
  5. Jenkins下载与安装。

如果你电脑已经装了AndroidStudio,那上面三个默认是配置好了的。
Java JDK路径:D:\androidstudio\jre
AndroidSDK路径: D:\Android\Sdk
Gradle路径:C:\Users**.gradle\wrapper\dists\gradle-5.1.1-all\97z1ksx6lirer3kbvdnh7jtjg\gradle-5.1.1

2、Jenkins的下载安装

Jinkins下载地址:https://www.jenkins.io/zh/download/
有两种安装方式:

  1. Jenkins.war
    下完在当前目录执行java -jar jenkins.war --httpPort=8080
  2. Jenkins.msi(windows)
    打开按照提示安装即可,但会默认安装在c盘系统里面。

安装完成之后在浏览器输入http://localhost:8080/就会进入引导进入页面,如果有选择安装插件界面进去后点击安装就行,插件安装失败也没关系,直接进入主界面。

3.Jenkins的设置

  1. 首先在Manage Jenkins->Configure System配置AndroidSdkGradle环境

    configuer system

  2. Manage Jenkins->Global Tool Configuration中配置JDKGitGradle

    Global Tool Configuration_1

    Global Tool Configuration_2

3、配置项目

  1. 新建任务
    新建任务

    2.配置源码,里面填上git项目链接以及添加账号密码
    源码管理

    构建的时候可以选择打包类型app:assemble'${buildType}'
    构建

    构建归档

    配置蒲公英以及钉钉
#----------------------------------------------
# 配置【蒲公英】的信息
# pgyerUserKey:蒲公英对应的User Key
# pgyerApiKey:蒲公英对应的API Key
# pgyerNewestWebsite:蒲公英最新包的网址
#----------------------------------------------
######## 开始 配置 ######## 
pgyerUserKey="xxxxx"
pgyerApiKey="xxxxx"
pgyerNewestWebsite="https://www.pgyer.com/xxxxx"
pgyerInstallPwd="xxxxx"
dingHookToken="xxxxxxxx"
dingKeyWord="xxxxx"
######## 结束 配置 ######## 

jiagu="未加固"

if [ "${buildType}" == "debugReinforce" ];then
    jiagu="360加固"
fi

if [ "${buildType}" == "releaseReinforce" ];then
    jiagu="360加固"
fi

uploadApp2pgyer()
{
#    curl -F "file=@/${WORKSPACE}/app/build/outputs/apk/release/huikao.apk" -F "uKey=${pgyerUserKey}" -F "_api_key=${pgyerApiKey}" -F "updateDescription=${pgyerUpdateDesc}" https://qiniu-storage.pgyer.com/apiv1/app/upload
  
    if [ "${buildType}" == "debug" ];then
        curl -F "file=@/${WORKSPACE}/app/build/outputs/apk/${buildType}/bv-${buildType}-${publishVersion}.apk" -F "userKey=${pgyerUserKey}" -F "_api_key=${pgyerApiKey}" -F "buildUpdateDescription=${pgyerUpdateDesc}" -F "buildInstallType=2" -F "buildPassword=${pgyerInstallPwd}" https://www.pgyer.com/apiv2/app/upload
    fi
    
    if [ "${buildType}" == "debugReinforce" ];then
        curl -F "file=@/${WORKSPACE}/app/build/outputs/apk_jiagu/debug/bv-debug-${publishVersion}.apk" -F "userKey=${pgyerUserKey}" -F "_api_key=${pgyerApiKey}" -F "buildUpdateDescription=${pgyerUpdateDesc}" -F "buildInstallType=2" -F "buildPassword=${pgyerInstallPwd}" https://www.pgyer.com/apiv2/app/upload
    fi
    
    if [ "${buildType}" == "release" ];then
        curl -F "file=@/${WORKSPACE}/app/build/outputs/apk/${buildType}/bv-${buildType}-${publishVersion}.apk" -F "userKey=${pgyerUserKey}" -F "_api_key=${pgyerApiKey}" -F "buildUpdateDescription=${pgyerUpdateDesc}" -F "buildInstallType=2" -F "buildPassword=${pgyerInstallPwd}" https://www.pgyer.com/apiv2/app/upload
    fi
    
    if [ "${buildType}" == "releaseReinforce" ];then
        curl -F "file=@/${WORKSPACE}/app/build/outputs/apk_jiagu/release/bv-release-${publishVersion}.apk" -F "userKey=${pgyerUserKey}" -F "_api_key=${pgyerApiKey}" -F "buildUpdateDescription=${pgyerUpdateDesc}" -F "buildInstallType=2" -F "buildPassword=${pgyerInstallPwd}" https://www.pgyer.com/apiv2/app/upload
    fi
}


time=$(date "+%Y-%m-%d %H:%M:%S")


msg=${pgyerUpdateDesc}
message=""
for ((i=0;$i<${#msg};i++));
do
    vAscii=`printf "%d" "'${msg:$i:1}"`
    if [ ${vAscii} == 10 ];then
        message+="  "
    fi
    message+=${msg:$i:1}
done

getValueStringByKey()
{
    key=$1
    arrKey=()
    for ((i=0;$i<=${#key};i++));
    do 
        arrKey[${i}]=${key:$i:1};
    done

    #for s in ${arrKey[@]}
    #do
    #echo "${s}"
    #done  


   arrKeyLen=$((${#arrKey[@]}-1))
    curIndex=0
    startTagIndex=-1
    endTagIndex=-1
    for ((i=0;$i<${#value};i++));
    do 
        char=${value:$i:1};
        if [ ${startTagIndex} == -1 ];then
            if [ "${char}" == "${arrKey[curIndex]}" ];then
                ((curIndex++))
                if [ ${curIndex} == ${arrKeyLen} ];then
                    startTagIndex=${i}
                fi
            else
                curIndex=0
            fi
        else
            if [ "${char}" == "\"" ];then
                endTagIndex=${i}
                break
            fi
        fi
    done

    retValue="key(${key})unfindValue"
    if [ ${startTagIndex} != -1 ];then
        if [ ${endTagIndex} != -1 ];then
            retValue=${value:$((${startTagIndex}+1)):$((${endTagIndex}-${startTagIndex}-1))}
        fi
    fi

    echo ${retValue}
}

value=$(uploadApp2pgyer)
appQRCodeUrl=$(getValueStringByKey \"buildQRCodeURL\":\")
appBuildVersion=$(getValueStringByKey \"buildBuildVersion\":\")

#----------------------------------------------
# 配置【带webhook的机器人】的信息
# webhook:钉钉机器人的webhook
# title:钉钉群消息栏显示的标题
# text:钉钉群里消息markdown格式的内容
#----------------------------------------------

######## 开始 配置 ######## 
webhook="https://oapi.dingtalk.com/robot/send?access_token=${dingHookToken}"

title="${dingKeyWord} 发布了新版本"
text="### ${dingKeyWord} - Android版\n\n##### 更新时间:${time}\n\n##### 发布版本:${publishVersion}\n\n##### 构建序列:build${appBuildVersion}\n\n##### 接口环境:${publishEnv}\n\n##### 构建环境:${buildType}\n\n##### 是否加固:${jiagu}\n\n![点击打开安装二维码(可作为历史版本记录)](${appQRCodeUrl})\n\n### 更新说明:\n\n> ${message:=暂无更新说明}\n\n[点击跳转至最新安装包地址](${pgyerNewestWebsite})"

######## 结束 配置 ######## 


######## 钉钉消息 配置 API ######## 
curl ${webhook} \
   -H "Content-Type: application/json" \
   -d "{\"msgtype\": \"text\",
        \"text\":{ 
            \"content\":\"${dingKeyWord} 更新版本:v${publishVersion} \n\n${pgyerUpdateDesc} \n\n\"},\"at\":{\"isAtAll\":true}}"
            
curl ${webhook} \
   -H "Content-Type: application/json" \
   -d "{\"msgtype\": \"markdown\",
        \"markdown\":{ 
            \"title\":\"${title}\",
            \"text\":\"${text}\"}}"

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容