自动拉代码和切换分支bat

2020-12-11  本文已影响0人  liguiyun
::快速编译打包apk脚本

echo  "$$package_begin$$"
sleep 1
::执行打包命令前,需要先定位到项目根目录


::echo %pa%
::rd .idea

::先去Applibs 去切换分支拉取代码
cd ../..
cd AppLibs
git checkout master
git pull
::回到主分支 然后拉取代码
cd ..
cd AndroidLeader
git checkout master
git pull
::开始打包
::gradlew build
::执行打包命令
gradle uploadApk

echo -e "$$package success$$"

::桌面右上角弹出通知
notify-send build.sh "package down!"
pause

import groovy.json.JsonOutput

def uploadApk() {
    def apkFile = getApkPath()
    def twoHyphens = "--"
    def boundary = "*********"
    def end = "\r\n"
    //模拟表单上传 multipart/form-data
    def conn = new URL("https://www.pgyer.com/apiv2/app/upload").openConnection()
    conn.setRequestMethod('POST')
    conn.setRequestProperty("Connection", "Keep-Alive")
    conn.setRequestProperty("Charset", "UTF-8")
    conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary)
//可以允许若干 控件域 同时上传其值,每个域值使用 boundary 分割
    conn.setDoInput(true)
    conn.setDoOutput(true)

    println "*************** start upload file ***************"

    // 添加参数 api_key
    def sb = new StringBuilder();
    sb.append(twoHyphens).append(boundary).append(end)
    sb.append("Content-Disposition: form-data; name=_api_key")
    sb.append(end).append(end)
    sb.append("5e58ea99d93b16451f83793ab74db8fa").append(end)

    //添加 app_key
    sb.append(twoHyphens).append(boundary).append(end)
    sb.append("Content-Disposition: form-data; name=app_key")
    sb.append(end).append(end)
    sb.append("470e351df0e203f2451bde1f73125757").append(end)

    //添加参数:buildInstallType 设置密码安装
    sb.append(twoHyphens).append(boundary).append(end)
    sb.append("Content-Disposition: form-data; name=buildInstallType")
    sb.append(end).append(end)
    sb.append(2).append(end)

    //添加参数:buildPassword 设置密码
    sb.append(twoHyphens).append(boundary).append(end)
    sb.append("Content-Disposition: form-data; name=buildPassword")
    sb.append(end).append(end)
    sb.append("120").append(end)

    //添加参数file: 需要上传的apk文件
    sb.append(twoHyphens).append(boundary).append(end)
    sb.append("Content-Disposition: form-data; name=file;filename=").append(apkFile)
    sb.append(end).append(end)

    println "===========参数============" + sb.toString()

    def dos = new DataOutputStream(conn.getOutputStream())
    dos.writeBytes(sb.toString())
    dos.flush()
    sb.delete(0, sb.length())

    def fis = new FileInputStream(apkFile)
    byte[] bf = new byte[8192]
    int len
    while ((len = fis.read(bf)) != -1) {
        dos.write(bf, 0, len)
    }
    sb.append(end)
    sb.append(twoHyphens).append(boundary).append(end)
    dos.writeBytes(sb.toString())

    dos.flush()
    fis.close()
    dos.close()
    conn.connect()

    def text = conn.getContent().text
    def resp = new groovy.json.JsonSlurper().parseText(text)
    println resp
    println "*************** upload finish ***************"

    //浏览器中打开短连接
    println("测试" + resp.data)
    def url = "https://www.pgyer.com/" + resp.data.buildShortcutUrl
     sendMsgToDing(resp.data)
    println("上传成功,应用链接:" + url)
}
/**
 * 发送到钉钉
 */
class ContentModel {
    String text
    String title
    String picUrl
    String messageUrl
}

def sendMsgToDing(def data) {
    def conn = new URL("https://oapi.dingtalk.com/robot/send?access_token=34e7c8a2bd608cb6d86245ab67de1077b65f90d411a671905a335de0406398d1").openConnection()
    conn.setRequestMethod('POST')
    conn.setRequestProperty("Connection", "Keep-Alive")
    conn.setRequestProperty("Content-type", "application/json;charset=UTF-8")
    conn.setConnectTimeout(30000)
    conn.setReadTimeout(30000)
    conn.setDoInput(true)
    conn.setDoOutput(true)
    def dos = new DataOutputStream(conn.getOutputStream())
    HashMap<String, Object> map = new HashMap<>()
    map.put("msgtype", "link")
    ContentModel contentModel = new ContentModel()
    contentModel.text = "App已经上传至蒲公英, 可以下载使用了${getUpdateContent()}" + data.buildCreated
    contentModel.title = "${getAppName()}App${data.buildVersion}上传提醒"
    contentModel.picUrl = data.buildQRCodeURL
    contentModel.messageUrl = "https://www.pgyer.com/" + data.buildShortcutUrl
    map.put("link", contentModel)
    def JSON = new JsonOutput().toJson(map)
    println(JSON)
    dos.writeBytes(JSON)
    def input = new BufferedReader(new InputStreamReader(conn.getInputStream()))
    String line = ""
    String result = ""
    while ((line = input.readLine()) != null) {
        result += line
    }
    dos.flush()
    dos.close()
    input.close()
    conn.connect()
    println("查看发送钉钉结果" + result)
}

def getApkPath() {
    String buildType = getBuildType().toLowerCase()
    def file = new File(buildDir, "/outputs/apk/${getAreaName()}/${buildType}/app_${getApplicationId()}-${getVersionName()}-${releaseTime()}.apk")
    println('绝对路径' + file.absolutePath)
    return file.absolutePath
}


/**
 *
 * @return
 */
def getApplicationId() {
    return project.property('android').properties['defaultConfig'].properties['applicationId']
}

def getVersionName() {
    return project.property('VERSION_NAME')
}
/**
 * 获取地区名字
 */
def getAreaName() {
    return 'ZhengZhou'
}


//获取编译时间
def releaseTime() {
    return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
}

def getAppName() {
    return "领导端"
}

def getUpdateContent() {
    return "更改个人中心到最下面和自动上传打包功能"
}
/**
 * 获取版本类型
 * @return
 */
def getBuildType() {
    return "Release"
}

task uploadApk(group: "upload") {
    dependsOn("assemble${getAreaName()}${getBuildType()}")
//    等待编译完成后再执行上传操作
    doLast {
        uploadApk()
    }
}


上一篇 下一篇

猜你喜欢

热点阅读