架构

jenkins自动构建ios及android架构

步骤

Step1: 构建gradle docker

$ cat ./Dockerfile
FROM openjdk:8-alpine
MAINTAINER HaoHu <haohu.th@gmail.com>

ENV SDK_TOOLS "3859397"
ENV BUILD_TOOLS "26.0.2"
ENV TARGET_SDK "26"
ENV ANDROID_HOME "/opt/android_sdk"
ENV GLIBC_VERSION "2.26-r0"
ENV GRADLE_VERSION "4.5"
ENV GRADLE_HOME "/opt/gradle"
ENV PATH "${PATH}:${GRADLE_HOME}/gradle-${GRADLE_VERSION}/bin:${ANDROID_HOME}/bin"

# Install required dependencies
RUN apk add --no-cache --virtual=.build-dependencies wget unzip ca-certificates bash && \
	wget https://raw.githubusercontent.com/sgerrand/alpine-pkg-glibc/master/sgerrand.rsa.pub -O /etc/apk/keys/sgerrand.rsa.pub && \
	wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/${GLIBC_VERSION}/glibc-${GLIBC_VERSION}.apk -O /tmp/glibc.apk && \
	wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/${GLIBC_VERSION}/glibc-bin-${GLIBC_VERSION}.apk -O /tmp/glibc-bin.apk && \
	apk add --no-cache /tmp/glibc.apk /tmp/glibc-bin.apk && \
	rm -rf /tmp/* && \
	rm -rf /var/cache/apk/*

# Download and extract Android Tools
RUN wget http://dl.google.com/android/repository/sdk-tools-linux-${SDK_TOOLS}.zip -O /tmp/tools.zip && \
	mkdir -p ${ANDROID_HOME} && \
	unzip /tmp/tools.zip -d ${ANDROID_HOME} && \
	rm -v /tmp/tools.zip

# Install SDK Packages
RUN mkdir -p ${ANDROID_HOME}/licenses/ && \
	echo "\nd56f5187479451eabf01fb78af6dfcb131a6481e" > ${ANDROID_HOME}/licenses/android-sdk-license && \
	echo "\n84831b9409646a918e30573bab4c9c91346d8abd" > ${ANDROID_HOME}/licenses/android-sdk-preview-license && \
	mkdir -p /root/.android/ && touch /root/.android/repositories.cfg && \
	cd /root/.android && \
	yes | ${ANDROID_HOME}/tools/bin/sdkmanager --update && \
	${ANDROID_HOME}/tools/bin/sdkmanager "build-tools;${BUILD_TOOLS}" "platform-tools" "platforms;android-${TARGET_SDK}" "extras;android;m2repository" "extras;google;google_play_services" "extras;google;m2repository" && \
	yes | ${ANDROID_HOME}/tools/bin/sdkmanager --licenses

# Install Gradle
RUN mkdir -p ${GRADLE_HOME} && \
	wget http://services.gradle.org/distributions/gradle-${GRADLE_VERSION}-bin.zip -O /tmp/gradle.zip && \
	unzip /tmp/gradle.zip -d ${GRADLE_HOME} && \
	rm -v /tmp/gradle.zip

RUN mkdir -p /gradle_project
VOLUME /gradle_project
WORKDIR /gradle_project
ENV CLICOLOR 1
ENV PS1 "\[\033[01;33m\]\u@alpine-android\[\033[01;31m\] \W\$\[\033[00m\] "
CMD ["/bin/bash"]

该dockerfile将构建一个包含gradle的docker,可以根据自己的项目需求定制。也可以直接使用这个docker HaoH/alpine-android

Step2: 编写Jenkinsfile

$ cat Jenkinsfile
#!/usr/bin/env groovy

pipeline {
    agent {
        docker {
            image 'registry-vpc.cn-beijing.aliyuncs.com/xxx/alpine-android:latest'
            args '-v /var/www:/var/www'
        }
    }
    environment {
        FLAVOR = build_flavor(env.BRANCH_NAME)
    }
    stages {
        stage('Build') {
            steps {
                sh "./jenkins/build.sh ${FLAVOR}"
            }
        }
        stage('Test') {
            steps {
                sh "gradle test"
            }
        }
        stage('Delivery') {
            steps {
                sh "./jenkins/deliver.sh ${FLAVOR} ${env.BRANCH_NAME}"
                archiveArtifacts artifacts: "app/build/outputs/apk/${FLAVOR}/*.apk", fingerprint: true
            }
        }
        stage('Cleanup') {
            steps {
                sh 'gradle clean'
            }
        }
    }
    post {
        failure {
            notifyFailed()
        }
        success {
            notifySuccessed()
        }
    }
}

def build_flavor(branch_name) {
    if (branch_name ==~ /r[.0-9]+/ || branch_name == 'master') {
        return 'release'
    }
    return 'debug'
}

def notifySuccessed() {
    emailext (
        to: 'dev_group@xxx.cn',
        subject: "「成功」ait_android ${env.BRANCH_NAME} 构建成功: ${currentBuild.fullDisplayName}, Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'",
        body: """<p>恭喜,最新代码构建成功!Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':</p>
            <p>请大家前往此处下载并及进行测试</p>
            <br/>
            <p><a href='http://jenkins.xxx.com:8081/android/ait-${FLAVOR}-latest.apk'>点击下载</a></p>
            <br/>
            <p>链接地址 'http://jenkins.xxx.com:8081/android/ait-${FLAVOR}-latest.apk'</p>
            <p>构建信息: <a href='${env.BUILD_URL}'>${env.JOB_NAME} [${env.BUILD_NUMBER}]</a></p>""",
      recipientProviders: [[$class: 'DevelopersRecipientProvider']]
    )
}

def notifyFailed() {
    emailext (
        to: 'dev_group@xxx.com',
        subject: "「失败」ait_android ${env.BRANCH_NAME} 构建失败: ${currentBuild.fullDisplayName}, Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'",
        body: """<p>抱歉,最新代码构建失败!Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':</p>
        <p>构建信息: <a href='${env.BUILD_URL}'>${env.JOB_NAME} [${env.BUILD_NUMBER}]</a></p>""",
        recipientProviders: [[$class: 'DevelopersRecipientProvider']]
    )
}

相关的构建脚本

cat jenkins/build.sh
#!/usr/bin/env sh

set -x;
echo "[BUILD task begin] `date '+%Y-%m-%d %H:%M:%S'`\n";

if [ $# -eq 0 ]; then
    flavor="debug";
else
    flavor=`echo $1 | tr '[A-Z]' '[a-z]'`;
fi

if [ ${flavor} = "release" ]; then
    gradle assembleRelease;
else
    gradle assembleDebug;
fi

echo "[BUILD task end] `date '+%Y-%m-%d %H:%M:%S'`\n";

相关的发布脚本如下,目前是构建完成之后放在http服务器上供大家下载试用。

$ cat jenkins/deliver.sh
#!/usr/bin/env sh

set -x;
echo "[DELIVER task begin] `date '+%Y-%m-%d %H:%M:%S'`\n";

timestamp=`date +%Y%m%d%H%M`;

if [ $# -ne 2 ]; then
    flavor="debug";
    branch_name="develop";
else
    flavor=`echo $1 | tr '[A-Z]' '[a-z]'`;
    branch_name=$2;
fi

if [ -f "app/build/outputs/apk/${flavor}/app-${flavor}.apk" ]; then
    echo `pwd`;
    mkdir -p /var/www/android;
    new_filename="app-${flavor}-${branch_name}-${timestamp}.apk";
    cp "app/build/outputs/apk/${flavor}/app-${flavor}.apk" "/var/www/android/${new_filename}";
    ln -s "/var/www/android/${new_filename}" "/var/www/android/ait-${flavor}-latest.apk";
    ln -s "/var/www/android/${new_filename}" "/var/www/android/ait-${branch_name}-latest.apk";
fi

echo "[DELIVER task end] `date '+%Y-%m-%d %H:%M:%S'`\n";

Step3: 在jenkins上建立对应的构建项目

此处省略