import groovy.json.JsonBuilder
import groovy.json.JsonOutput
import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec
import java.security.MessageDigest
static Boolean release() { false }
def tokenR = "机器人token"//release
def tokenT = "机器人token"//test
static String secretKeyR() { "机器人签名" }//release
static String secretKeyT() { "机器人签名" }//test
static String secretKey() { release() ? secretKeyR() : secretKeyT() }
def dingDingUrl = "https://oapi.dingtalk.com/robot/send?access_token=${release() ? tokenR : tokenT}"
static String baseRemoteUrl() { "远程地址前缀" }
ext.postDingMsg = { String artifactId, String version ->
String allTips = "## 线上包发布提醒(${artifactId})\n" +
"> 项目:${rootProject.name}" + '\n\n' +
"> 版本:v$version" + '\n\n' +
"${calculateMd5()}"+ '\n\n' +
"@131xxx"
//println allTips
postAll(dingDingUrl, allTips)
}
//发送到群,@所有人
def postAll(url, tips) {
JsonBuilder builder = new JsonBuilder()
builder {
msgtype 'markdown'
markdown {
title "${rootProject.name}发布提醒"
text tips
}
at {
isAtAll true
atMobiles("131xxx", "")
}
}
String data = JsonOutput.prettyPrint(builder.toString())
postDingDing(url, data)
}
//调用接口,发送消息
def postDingDing(urlString, msg) {
if (msg == null) {
return
}
HttpURLConnection conn = null
try {
if (conn == null) {
Long timestamp = System.currentTimeMillis()
URL url = new URL(urlString + "×tamp=${timestamp}&sign=${getSecret(timestamp)}")
conn = (HttpURLConnection) url.openConnection()
}
if (conn != null) {
conn.setRequestMethod('POST')
conn.setReadTimeout(15000)
conn.setConnectTimeout(15000)
conn.setDoOutput(true)
conn.setUseCaches(false)
conn.setRequestProperty("Content-Type", "application/json; charset=utf-8")
conn.connect()
}
if (conn == null) {
return null
}
if (msg != null && msg.length() > 0) {
DataOutputStream dataOutputStream = new DataOutputStream(conn.getOutputStream())
byte[] t = msg.getBytes("utf-8")
dataOutputStream.write(t)
dataOutputStream.flush()
dataOutputStream.close()
def res = conn.content.text
println res
if (conn.getResponseCode() == 200) { //成功
InputStream input = conn.getInputStream()
StringBuffer sb = new StringBuffer()
int ss
while ((ss = input.read()) != -1) {
sb.append((char) ss)
}
} else {
println("发送消息失败----" + conn.getResponseCode())
}
}
} catch (Exception e) {
e.printStackTrace()
} finally {
if (conn != null) {
conn.disconnect()
}
}
}
static def getSecret(timestamp) {
String stringToSign = timestamp + "\n" + secretKey()
Mac mac = Mac.getInstance("HmacSHA256")
mac.init(new SecretKeySpec(secretKey().getBytes("UTF-8"), "HmacSHA256"))
byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"))
String sign = URLEncoder.encode(new String(Base64.encoder.encode(signData)), "UTF-8")
return sign
}
def calculateMd5() {
File apkFolder = file("${projectDir.absolutePath}/channel/v${VERSION_CODE}")
File[] apkFiles = apkFolder.listFiles()
def listMd5 = new ArrayList<String>()
listMd5.add("\n")
apkFiles.each { File file ->
def md5 = generateMD5(file)
def platform = file.name.substring(file.name.lastIndexOf("_") + 1, file.name.lastIndexOf("."))
def fileName = ""
if (platform.isNumber()) {
fileName = "主包"
} else if (platform == "sign") {
fileName = "加固主包"
} else {
fileName = platform + "渠道包"
}
def url = baseRemoteUrl() + "/v${VERSION_CODE}/${file.name}"
listMd5.add("- **${fileName}** [${url}](${url}) [**${md5}**] \n")
}
def result = listMd5.toString().replace(",", "")
return result.substring(1, result.length() - 1)
}
/**
* 生成md5
* @param file
* @return
*/
static def generateMD5(file) {
MessageDigest digest = MessageDigest.getInstance("MD5")
digest.update(file.bytes)
return new BigInteger(1, digest.digest()).toString(16).padLeft(32, '0')
}
task postDingDingTask {
group = "channel"
description = "推送下载地址到钉钉"
doLast {
def artifactId = "release"
def version = VERSION_NAME
postDingMsg(artifactId, version)
}
}
使用 上面代码保存文件dingding.gradle
apply from: "../dingding.gradle"
gradle 运行cmd
task uploadApkAndPushDingDing(type: Exec) {
group = "channel"
description = "task 描述"
commandLine 'cmd', "/c", "D:\\Desktop\\ossutil64\\ossutil64.exe cp -r ${projectDir.absolutePath}/channel/v${VERSION_CODE} oss://yxc-android/${oss_path_prefix}/v${VERSION_CODE}/ -u"
}
网友评论