jenkins构建脚本:
[root@localhost tomcat]# cat move.sh #!/bin/bash # author lql release_dir=/data/android_git/app/build/outputs/apk/tuji/release debug_dir=/data/android_git/app/build/outputs/apk/tuji/debug releasetest_dir=/data/android_git/app/build/outputs/apk/tuji/releaseTest function release () { cd /data/android_git && gradle clean cd /data/android_git && gradle assembleTujiRelease if [ -d $release_dir ];then cd $release_dir apk=`ls -lt|grep .apk|awk -F " " ‘{print $NF}‘` new_time=`date +%Y%m%d%H%M` version=`echo $apk | awk -F ".apk" ‘{print $1}‘` new_apk=$version-$new_time.apk mv $apk $new_apk cp -rf $release_dir/$new_apk /data/download/release python3 /home/tomcat/jenkins_android_test.py $new_apk release else python3 /home/tomcat/jenkins_android_test.py path_release_error fi } function debug () { cd /data/android_git && gradle clean cd /data/android_git && gradle assembleTujiDebug if [ -d $debug_dir ];then cd $debug_dir apk=`ls -lt|grep .apk|awk -F " " ‘{print $NF}‘` new_time=`date +%Y%m%d%H%M` version=`echo $apk | awk -F ".apk" ‘{print $1}‘` new_apk=$version-$new_time.apk mv $apk $new_apk cp -rf $debug_dir/$new_apk /data/download/debug python3 /home/tomcat/jenkins_android_test.py $new_apk debug else python3 /home/tomcat/jenkins_android_test.py path_debug_error fi } function releasetest () { cd /data/android_git && gradle clean cd /data/android_git && gradle assembleTujiReleaseTest if [ -d $releasetest_dir ];then cd $releasetest_dir apk=`ls -lt|grep .apk|awk -F " " ‘{print $NF}‘` new_time=`date +%Y%m%d%H%M` version=`echo $apk | awk -F ".apk" ‘{print $1}‘` new_apk=$version-$new_time.apk mv $apk $new_apk cp -rf $releasetest_dir/$new_apk /data/download/releasetest python3 /home/tomcat/jenkins_android_test.py $new_apk releasetest else python3 /home/tomcat/jenkins_android_test.py path_releasetest_error fi } case $1 in release) echo release runing release; ;; debug) echo debug ruing debug; ;; releasetest) echo releasetest runing releasetest; ;; *) release; debug; releasetest; ;; esac
构建成功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; } } }