由于公司的jenkins运行在k8s集群中,在jenkins打包时调用jenkins-slave 来工作,所以需先构建Android-jenkins-slave镜像。
jenkins-slave 在打包过程中会用到gradle和sdk,那么就牵涉到gradle插件和sdk持久化。
Android打包对资源要求较高,也可以单独建立jenkins工作节点,将Android打包调度到物理机节点进行。
//安装NFS服务端
[root@nfs-server ~]# yum install -y nfs-utils
[root@nfs-server ~]# mkdir -p /data/android
[root@nfs-server ~]# mkdir -p /data/gradle
[root@nfs-server ~]# vi /etc/exports
/data/android 192.168.10.0/24(rw,no_root_squash)
/data/gradle 192.168.10.0/24(rw,no_root_squash)
[root@nfs-server ~]# systemctl enable rpcbind.service
[root@nfs-server ~]# systemctl enable nfs-server.service
[root@nfs-server ~]# systemctl start rpcbind.service
[root@nfs-server ~]# systemctl start nfs-server.service
[root@nfs-server ~]# exportfs -r
//可能存在NFS无法访问,需提高权限
[root@nfs-server ~]# chmod -R 777 /data/android /data/gradle
[root@mayi-ops jenkins-slave-android]# vim Dockerfile
FROM centos:7
LABEL mAIntainer dengyoucheng
COPY jdk-8u261-linux-x64.rpm /opt
COPY gradle-6.7.1-all.zip /opt
COPY sdk-tools-linux-3859397.zip /opt
COPY slave.jar /usr/share/jenkins/slave.jar
COPY jenkins-slave /usr/bin/jenkins-slave
#init
RUN yum -y install wget && \
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo_bak && \
wget -O /etc/yum.repos.d/CentOS-Base.repo <http://mirrors.aliyun.com/repo/Centos-7.repo> && \
yum makecache && \
yum -y install epel-release && \
yum -y install gcc gcc-c++ jq.NET-tools git unzip && \
yum clean all && \
rm -rf /var/cache/yum/* && \
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
mkdir -p /opt/android/sdk && \
mkdir -p /usr/share/jenkins && \
chmod +x /usr/bin/jenkins-slave
#install JAVA
RUN yum -y install /opt/jdk-8u261-linux-x64.rpm
ENV JAVA_HOME /usr/java/jdk1.8.0_261-amd64
ENV JRE_HOME ${JAVA_HOME}/jre
ENV CLASSPATH .$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
ENV PATH $PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin
#install sdk
#./sdkmanager --list查看有哪些版本
RUN unzip /opt/sdk-tools-linux-3859397.zip -d /opt/android/sdk && \
mkdir /root/.android/ && touch /root/.android/repositories.cfg && \
cd /opt/android/sdk/tools/bin && \
./sdkmanager --list
ENV ANDROID_HOME /root/.android
ENV PATH $PATH:$ANDROID_HOME:$ANDROID_HOME/emulator:$ANDROID_HOME/platform-tools
ENV ANDROID_ROOT /opt/android/sdk
ENV PATH $PATH:$ANDROID_ROOT/tools:$ANDROID_ROOT/tools/bin
# sdkmanagerSDK指定目录,可以持久化存储
# yes | ./sdkmanager --licenses && \
# yes | ./sdkmanager "build-tools;33.0.0" "platforms;android-33" "build-tools;30.0.2" "platforms;android-30" "platform-tools" --sdk_root=/root/.android
#install gradle
RUN cd /opt/ && \
unzip gradle-6.7.1-all.zip && \
mv gradle-6.7.1 gradle
ENV PATH $PATH:/opt/gradle/bin
WORKDIR /opt
ENTRYPOINT ["jenkins-slave"]
//*************************************************
// Author : mayi-ops
// Last modified: 2021-01-06
// wechat: a403182580
// Description : 安卓打包脚本
//*************************************************
//定义java-slave的yaml
def podTemplate = """
kind: Pod
metadata:
name: android-slave
spec:
containers:
- name: jnlp
image: "${registry}/library/jenkins-slave-android:1.3"
imagePullPolicy: Always
volumeMounts:
- name: docker-cmd
mountPath: /usr/bin/docker
- name: docker-sock
mountPath: /var/run/docker.sock
- name: gradlecache
mountPath: /root/.gradle
- name: androidcache
mountPath: /root/.android
volumes:
- name: docker-cmd
hostPath:
path: /usr/bin/docker
- name: docker-sock
hostPath:
path: /var/run/docker.sock
- name: gradlecache
nfs:
server: ${nfsip}
path: ${gradle_cache}
- name: androidcache
nfs:
server: ${nfsip}
path: ${android_cache}
"""
//pipeline构建过程
pipeline {
agent {
//根据yaml创建salve
kubernetes {
label "android-slave"
yaml podTemplate
}
}
//变量
environment {
DingTalkHook = "<https://oapi.dingtalk.com/robot/send?access_token=**********>"
}
stages {
//如果是非admin、打包、打包部署,才会打包
stage('拉取代码') {
steps{
script {
/* 获取git地址 */
if ( env.GitAddress ){
if ( env.GitAddress == '' ) {
echo "请输入git分支!"
}
}else{
echo "请输入gitlab项目地址。"
exit 0
}
/* 代码分支 */
//输入分支:参数化构建过程->字符参数->名称:codeBranch->默认值:fat
if ( env.codeBranch ){
if ( env.codeBranch == '' ){
echo "请输入git分支!"
}
}else{
echo "没输入分支,默认使用master"
env.codeBranch = "master"
}
echo "git:"+env.GitAddress
echo "Branch:"+env.codeBranch
//拉取业务代码
checkout([
$class: 'GitSCM',
branches: [[name: "${codeBranch}"]],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'code']],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: "build_auth", url: "${GitAddress}"]]
])
}
}
}
stage('android打包'){
steps{
script {
dir("code"){
/* 打包脚本 */
sh "java -version"
sh "adb version"
sh "gradle -v"
echo "=================gradle打包中==============================="
sh "gradle assembleRelease"
echo "=================gradle打包完成============================="
sh "cp App/build/outputs/apk/*/*.apk /root/ttt.apk"
echo "=================gradle clean=============================="
//sh "gradle clean"
}
}
}
}
stage('push APK'){
steps{
script {
env.Apkurl = sh returnStdout: true, script: """curl -F "file=@/root/ttt.apk" -F "uKey=${env.USER_KEY}" -F "_api_key=${env.API_KEY}" <https://qiniu-storage.pgyer.com/apiv1/app/upload|jq|grep> appQRCodeURL|awk -F'"' '{print \$4}'"""
echo "蒲公英二维码地址: "+env.Apkurl
}
}
}
}
//使用post 插件必须使用pipeline,不能使用podTemplate插件
post {
//安装插件 HTTP Request Plugin和 Post build task
success{
script {
//打包成功
def ReqBody = """{
"msgtype": "markdown",
"markdown": {
"title": "APK打包信息",
"text": "## 【${env.objectname}】打包成功 ✅ \n>- **打包分支:${env.codeBranch}**\n>- **扫描二维码下载APK:**\n>- ![screenshot](${env.Apkurl}) \n>- 构建日志: [点击查看详情](${env.BUILD_URL}console)"
},
"at": {
"isAtAll": true
}
}"""
httpRequest acceptType: 'APPLICATION_JSON_UTF8',
consoleLogResponseBody: false,
contentType: 'APPLICATION_JSON_UTF8',
httpMode: 'POST',
ignoreSslErrors: true,
requestBody: ReqBody,
responseHandle: 'NONE',
url: "${env.DingTalkHook}",
quiet: true
}
}
failure{
script {
//打包失败
def ReqBody = """{
"msgtype": "markdown",
"markdown": {
"title": "APK打包信息",
"text": "## 【${env.objectname}】打包失败 ❌ \n>- **打包分支:${env.codeBranch}**\n>- 构建日志: [点击查看详情](${env.BUILD_URL}console)"
},
"at": {
"isAtAll": true
}
}"""
httpRequest acceptType: 'APPLICATION_JSON_UTF8',
consoleLogResponseBody: false,
contentType: 'APPLICATION_JSON_UTF8',
httpMode: 'POST',
ignoreSslErrors: true,
requestBody: ReqBody,
responseHandle: 'NONE',
url: "${env.DingTalkHook}",
quiet: true
}
}
}
}