背景
Jenkins 编译 Android apk,上传 apk 包,生成下载二维码,并推送钉钉
安装 Android 环境
安装 JDK
1# 这里使用的是openjdk 1.8.0版本,有需要的话需要到java官网上进行下载对应的JDK版本。
2$ yum install java -y
3
4# 其他版本JDK的安装方式
5$ mv jdk1.8.0_161 /usr/local/
6$ ln -s /usr/local/jdk1.8.0_161 /usr/local/jdk
7$ vim /etc/profile #配置JDK的环境变量
8export JAVA_HOME=/usr/local/jdk
9export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
10export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
11$ source /etc/profile #重新加载系统环境变量
12$ java -version #查看java版本
Android SDK 安装
1# 下载sdk工具包
2$ wget https://dl.google.com/android/repository/sdk-tools-linux-3859397.zip
3
4# 创建sdk工具文件夹和解压工具包
5$ mkdir -p /opt/android/sdk
6$ unzip sdk-tools-linux-3859397.zip -d /opt/android/sdk
7
8# 使用sdkmanager工具配置构建工具和平台版本
9$ cd /opt/android/sdk/tools/bin/
10$ ./sdkmanager "build-tools;29.0.6" "platforms;android-29" "platform-tools"
11$ ./sdkmanager --list #可以查看有哪些版本,自行选择对应的版本
12
13# 增加系统环境变量
14$ vim /etc/profile
15export ANDROID_HOME=/opt/android/sdk
16PATH=$PATH:$ANDROID_HOME:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools:$ANDROID_HOME/emulator:$ANDROID_HOME/tools/bin
17
18$ adb version
19Android Debug Bridge version 1.0.41
20Version 29.0.6-6198805
21Installed as /opt/android/sdk/platform-tools/adb
安装 gradle
1$ wget https://downloads.gradle-dn.com/distributions/gradle-6.3-all.zip
2$ mkdir /opt/gradle
3$ unzip gradle-6.3-all.zip -d /opt/gradle/
4$ vim /etc/profile
5export PATH=$PATH:/opt/gradle/gradle-6.3/bin
6$ source /etc/profile
7$ gradle -v
8------------------------------------------------------------
9Gradle 6.3
10------------------------------------------------------------
11
12Build time: 2020-03-24 19:52:07 UTC
13Revision: bacd40b727b0130eeac8855ae3f9fd9a0b207c60
14
15Kotlin: 1.3.70
16Groovy: 2.5.10
17Ant: Apache Ant(TM) version 1.10.7 compiled on September 1 2019
18JVM: 1.8.0_201 (Oracle Corporation 25.201-b09)
19OS: Linux 3.10.0-693.el7.x86_64 amd64
jenkins 流水线配置
gradle-jdk.sh 打包脚本
1#!/bin/sh
2source /etc/profile
3# /home/编译目录
4cd /home/编译目录 && gradle clean
5cd /home/编译目录 && gradle assembleRelease
6# 打包编译完成,在项目的app/build/outputs/apk中可以找到debug版本或者是release版本。
JenkinsFile 脚本
1pipeline {
2 //使用标签 'master' 的节点
3 agent {label 'master'}
4 //环境变量
5 environment {
6 //成功运行特征
7 JEN_FEATURE = ''
8 //日志路径
9 JEN_LOG = ''
10 }
11
12 stages {
13 stage ('编译打包'){
14 steps {
15 sh 'cd /home/编译目录 && sudo git pull'
16 sh 'sh gradle-apk.sh'
17
18 }
19 }
20
21 stage ('上传'){
22 steps {
23 sh 'sshpass -p "password" scp /home/编译目录/app/build/outputs/apk/release/app-release.apk user@www.nginx.com:/home/上传目录/app-release.apk'
24 }
25 }
26
27 stage ('生成qr'){
28 steps {
29 echo "生成test qr"
30 sh "pwd && myqr 'https://www.nginx.com/app-release.apk' -n qrcode-`date +'%Y-%m-%d-%H%M%S'`.png -d /home/code/image"
31 sh "cd /home/code/image && git add ."
32 sh "cd /home/code/image && git commit -m 'new images'"
33 sh "cd /home/code/image && git push origin main"
34 echo "结束 end"
35 }
36 }
37 }
38
39
40 post {
41 success {
42 script {
43 env.DATETIME = sh(script:"date", returnStdout: true).trim()
44 env.QRCODE = sh(script:"ls -lhst /home/code/image | awk 'NR==2' | awk '{print \$10}'",returnStdout: true).trim()
45 def job_name = "# ${JOB_NAME} 流水线 执行成功"
46 def jenkinsid = """构建: 第 ${BUILD_ID} 次执行"""
47 def JEN_production = "> 部署节点: jenkins"
48 def build_url = "> 部署详情: [详情](${BUILD_URL})"
49 def jen_date = "> 执行时间: ${env.DATETIME}"
50 def jen_qrcode = "![qr](https://gitlab.com/jobcher/image/-/raw/main/${env.QRCODE})"
51
52 dingtalk (
53 robot: 'e3999649-d3f-钉钉key-4c57333a327b',
54 type: 'MARKDOWN',
55 title: job_name,
56 text: [
57 job_name,
58 jenkinsid,
59 '',
60 '---',
61 jen_qrcode,
62 JEN_production,
63 '',
64 build_url,
65 '',
66 jen_date
67 ],
68 at: [
69 '手机号'
70 ]
71 )
72 }
73 }
74
75 failure {
76 script {
77 env.DATETIME = sh(script:"date", returnStdout: true).trim()
78 def job_name = "# ${JOB_NAME} 流水线 执行失败"
79 def jenkinsid = """构建: 第 ${BUILD_ID} 次执行"""
80 def JEN_production = "> 部署节点: jenkins"
81 def build_url = "> 部署详情: [详情](${BUILD_URL})"
82 def jen_date = "> 执行时间: ${env.DATETIME}"
83
84 dingtalk (
85 robot: 'e3999649-d3f-钉钉key-4c57333a327b',
86 type: 'MARKDOWN',
87 title: job_name,
88 text: [
89 job_name,
90 jenkinsid,
91 '',
92 '---',
93 JEN_production,
94 '',
95 build_url,
96 '',
97 jen_date
98 ],
99 at: [
100 '手机号'
101 ]
102 )
103 }
104 }
105 }
106}