Jenkins 自动打包上传蒲公英shell脚本

由于蒲公英的极速上传api更新,旧的api即将停止使用,故根据官方的shell,修改一下上传脚本,脚本如下:

#bin/bsah
#上传蒲公英
#!/bin/bash
#
# 通过shell脚本来实现将本地app文件通过API上传到蒲公英
# https://www.pgyer.com/doc/view/api#fastUploadApp
##
# 参数说明:
# $1: 蒲公英api_key
# $2: 要上传的文件路径(ipa/apk)
#


# ==========固定值======== #
# api_key
 api_key="xxxxxxxxxxxxxxx"
# ==========脚本的一些固定参数定义(基本不需要修改)======== #

# 获取当前脚本所在目录
script_dir="$WORKSPACE"
# 工程根目录
project_dir=$script_dir

echo "script_dir: $script_dir"
file=${script_dir}/Build/xxxx.ipa

echo "api_key: $api_key file: $file"

#readonly api_key=$1
#readonly file=$2

# Display log. 1=enable, 0=disable
LOG_ENABLE=1

printHelp() {
    echo "Usage: $0 api_key file"
    echo "Example: $0  "
}

# check api_key exists
if [ -z "$api_key" ]; then
    echo "api_key is empty"
    printHelp
    exit 1
fi

# check file exists
if [ ! -f "$file" ]; then
    echo "file not exists"
    printHelp
    exit 1
fi

if [[ $file =~ ipa$ ]]; then
    app_type="ios"
elif [[ $file =~ apk$ ]]; then
    app_type="android"
else
    echo "file type not support"
    printHelp
    exit 1
fi

# ---------------------------------------------------------------
# functions
# ---------------------------------------------------------------

log() {
    [ $LOG_ENABLE -eq 1 ]  && echo "[$(date +'%Y-%m-%d %H:%M:%S')] $*"
}

logTitle() {
    log "-------------------------------- $* --------------------------------"
}

execCommand() {
    log "$@"
    result=$(eval $@)
}

# ---------------------------------------------------------------
# 获取上传凭证
# ---------------------------------------------------------------

logTitle "获取凭证"

execCommand "curl -s -F '_api_key=${api_key}' -F 'buildType=${app_type}' -F 'buildUpdateDescription=$appUpdateInfo $SCM_CHANGELOG' http://www.pgyer.com/apiv2/app/getCOSToken"

[[ "${result}" =~ \"endpoint\":\"([\:\_\.\/\\A-Za-z0-9\-]+)\" ]] && endpoint=`echo ${BASH_REMATCH[1]} | sed 's!\\\/!/!g'`
[[ "${result}" =~ \"key\":\"([\.a-z0-9]+)\" ]] && key=`echo ${BASH_REMATCH[1]}`
[[ "${result}" =~ \"signature\":\"([\=\&\_\;A-Za-z0-9\-]+)\" ]] && signature=`echo ${BASH_REMATCH[1]}`
[[ "${result}" =~ \"x-cos-security-token\":\"([\_A-Za-z0-9\-]+)\" ]] && x_cos_security_token=`echo ${BASH_REMATCH[1]}`

if [ -z "$key" ] || [ -z "$signature" ] || [ -z "$x_cos_security_token" ] || [ -z "$endpoint" ]; then
    log "get upload token failed"
    exit  
fi

# ---------------------------------------------------------------
# 上传文件
# ---------------------------------------------------------------

logTitle "上传文件"
echo "[UPLOAD TO PGYER] --- 开始应用上传 --- "

execCommand "curl -s -o /dev/null -w '%{http_code}' --form-string 'key=${key}' --form-string 'signature=${signature}' --form-string 'x-cos-security-token=${x_cos_security_token}' -F 'file=@${file}' ${endpoint}"
if [ $result -ne 204 ]; then
    log "Upload failed"
    echo "[UPLOAD TO PGYER] ---  应用上传失败 ---  "
    exit 1
fi

# ---------------------------------------------------------------
# 检查结果
# ---------------------------------------------------------------

logTitle "检查结果"
json_data=""

function parse_json(){
    echo $json_data | sed 's/,/\n/g' | grep $1 | sed 's/":"/\n/g'  | sed '1d' | sed 's/"//g' | sed 's/}}//g'
}

function parse_json_buildVersion(){
    echo $json_data | sed 's/,/\n/g' | grep $1 | sed 's/" "/\n/g'  | sed '2d' | sed 's/"//g'| sed 's/:/\n/g' | sed '1d' 
}
 
function main(){
   
    buildName=$(parse_json "buildName")
    buildType=$(parse_json "buildType")
    buildVersion=$(parse_json_buildVersion "buildVersion")
    buildVersionNo=$(parse_json "buildVersionNo")
    buildBuildVersion=$(parse_json "buildBuildVersion")
    buildShortcutUrl=$(parse_json "buildShortcutUrl")
    buildCreated=$(parse_json "buildCreated")
    buildUpdated=$(parse_json "buildUpdated")
    buildIsLastest=$(parse_json "buildIsLastest")
    buildKey=$(parse_json "buildKey")
    buildUpdateDescription=$(parse_json "buildUpdateDescription")
    buildIdentifier=$(parse_json "buildIdentifier")
    buildQRCodeURL=$(parse_json "buildQRCodeURL")
    buildIcon=$(parse_json "buildIcon")
    
    echo "[UPLOAD TO PGYER] ---  应用上传成功 ---  "
    echo "[UPLOAD TO PGYER] - 应用名称:"$buildName
    if [ $buildType -eq 1 ]; then
        echo "[UPLOAD TO PGYER] - 应用类型:iOS"
    else
        echo "[UPLOAD TO PGYER] - 应用类型:Android"
    fi
    echo "[UPLOAD TO PGYER] - 版 本 号:"$buildVersion
    echo "[UPLOAD TO PGYER] - Xcode build号:"$buildVersionNo
    echo "[UPLOAD TO PGYER] - 蒲公英 build号:"$buildBuildVersion
    echo "[UPLOAD TO PGYER] - 应用主页:https://www.pgyer.com/"$buildShortcutUrl
    echo "[UPLOAD TO PGYER] - 应用短链接:"$buildShortcutUrl
    echo "[UPLOAD TO PGYER] - 应用上传时间:"$buildCreated
    echo "[UPLOAD TO PGYER] - 应用更新时间:"$buildUpdated
    if [ $buildIsLastest -eq 1 ]; then
         echo "[UPLOAD TO PGYER] - 是否是最新版:是"
    else
         echo "[UPLOAD TO PGYER] - 是否是最新版:否"
    fi
    echo "[UPLOAD TO PGYER] - 应用构建主页:https://www.pgyer.com/"$buildKey
    echo "[UPLOAD TO PGYER] - 应用更新说明:"$buildUpdateDescription
    echo "[UPLOAD TO PGYER] - 应用程序包名:"$buildIdentifier
    echo "[UPLOAD TO PGYER] - 应用二维码地址:"$buildQRCodeURL
    echo "[UPLOAD TO PGYER] - 应用的Icon图标key:https://www.pgyer.com/image/view/app_icons/"$buildIcon
    
    echo appName=$buildName > pgyerProperty.txt
    echo appVersion=$buildVersion >> pgyerProperty.txt
    echo appVersionNo=$buildVersionNo >> pgyerProperty.txt
    echo appBuildVersion=$buildBuildVersion >> pgyerProperty.txt
    echo appUpdated=$buildUpdated >> pgyerProperty.txt
    echo appUpdateDescription=$buildUpdateDescription >> pgyerProperty.txt
    echo appBuildURL='https://www.pgyer.com/'${buildKey} >> pgyerProperty.txt
    echo appQRCodeURL=$buildQRCodeURL >> pgyerProperty.txt
 
}

for i in {1..60}; do
    execCommand "curl -s http://www.pgyer.com/apiv2/app/buildInfo?_api_key=${api_key}\&buildKey=${key}  | native2ascii -encoding UTF-8 -reverse"
    [[ "${result}" =~ \"code\":([0-9]+) ]] && code=`echo ${BASH_REMATCH[1]}`
    if [ $code -eq 0 ]; then
        json_data=$result
        main
        break
    else
        sleep 1
    fi
done

上传成功后,我将相关属性写入到一个文件中,这样打包过程中可以获取到这次属性,用户钉钉消息发送

    echo appName=$buildName > pgyerProperty.txt
    echo appVersion=$buildVersion >> pgyerProperty.txt
    echo appVersionNo=$buildVersionNo >> pgyerProperty.txt
    echo appBuildVersion=$buildBuildVersion >> pgyerProperty.txt
    echo appUpdated=$buildUpdated >> pgyerProperty.txt
    echo appUpdateDescription=$buildUpdateDescription >> pgyerProperty.txt
    echo appBuildURL='https://www.pgyer.com/'${buildKey} >> pgyerProperty.txt
    echo appQRCodeURL=$buildQRCodeURL >> pgyerProperty.txt

你可能感兴趣的:(Jenkins 自动打包上传蒲公英shell脚本)