JENKINS安卓打包CI
流程截图:



jenkins构建脚本对包进行改名和移动:$1 是work目录,$2是构建包的分类,也是对应下载目录。
Dir=/download/$2
hostIp=`hostname -I|awk -F ' ' '{print $1}'`
apkName=`ls $1/app/build/outputs/apk/$2/*.apk|awk -F '/' '{print $NF}'`
cd $1/app/build/outputs/apk/$2/
nowApkName=`date +%Y-%m-%d_%H:%M:%S`_$apkName
mv $apkName $nowApkName
\cp -rf $1/app/build/outputs/apk/$2/$nowApkName /data/$Dir
msg=$hostIp$Dir/$nowApkName
curl -X POST -H "Content-Type: application/json" \
-d \
'{
"msg_type": "post",
"content": {
"post": {
"zh_cn": {
"title": "下载通知",
"content": [
[
{
"tag": "a",
"text":"'$msg'",
"href": "'$msg'"
}
]
]
}
}
}
}' \
https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxx
构建成功WEBHOOK提醒
#!/usr/local/python-3.6.4/bin/python3
# -*- coding:UTF-8-*-
# Author: lql
from urllib import request
import json
from sys import argv
import time
#获得当前时间时间戳
now = int(time.time())
#转换为其他日期格式,如:"%Y-%m-%d %H:%M:%S"
timeStruct = time.localtime(now)
strTime = time.strftime("%Y-%m-%d %H:%M:%S", timeStruct)
access_token = ""
#def send_msg(mobile, item_name):
def send_msg(time_str,apk_name,download_dir=False):
if download_dir:
content=apk_name + ' ' + time_str + ' ' + 'http://ip?/download/' + download_dir + "/"
else:
content=apk_name + ' ' + time_str
url = "https://oapi.dingtalk.com/robot/send?access_token=" + access_token
data = {
"msgtype": "text",
"text": {
"content": content
},
"at": {
#"atMobiles": [
# mobile
#],
"isAtAll": "false"
}
}
json_data= json.dumps(data).encode(encoding='utf-8')
print(json_data)
header_encoding = {"Content-Type": "application/json"}
req = request.Request(url=url, data=json_data, headers=header_encoding)
res = request.urlopen(req)
res = res.read()
print(res.decode(encoding='utf-8'))
if __name__ == "__main__":
time_str = strTime
if len(argv) == 3:
apk_name=argv[1]
download_dir=argv[2]
send_msg(time_str,apk_name,download_dir)
else:
apk_name = argv[1]
send_msg(time_str,apk_name)
nginx作为下载服务器配置:
[root@localhost ~]# cat /usr/local/nginx/conf/nginx.conf
user nobody;
worker_processes 1;
error_log logs/error.log warn;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
keepalive_timeout 65;
gzip on;
server_tokens off;
server {
listen 80;
server_name localhost;
location / {
root html;
rewrite ^/(.*) http://ip?/download redirect;
}
location ~* ^/download {
root html;
autoindex on;
autoindex_localtime on;
autoindex_exact_size off;
}
}
}
定时删除
#保留文件数
ReservedNum=20
#要删除文件的,父级目录
FileDir=/data/download/
#循环子目录列表
for element in `ls $FileDir`
do
# 拼接成完成目录 (父目录路径/子目录名)
dir_or_file=$FileDir$element
# 获取子目录下的指定文件格式的总数量
FileNum=$(ls $dir_or_file |wc -l)
# 数量大于保留数量,那就循环删除文件
echo $FileNum $ReservedNum
while(( $FileNum > $ReservedNum))
do
OldFile=$(ls $dir_or_file | head -1)
echo $element "Delete :"$OldFile
cd $dir_or_file && rm -f $OldFile
FileNum=$FileNum-1
done
done
浙公网安备 33010602011771号