一、Maven
下载
wget https://dlcdn.apache.org/maven/maven-3/3.8.5/binaries/apache-maven-3.8.5-bin.tar.gz
解压
tar -zxvf apache-maven-3.8.5-bin.tar.gz -C /usr/local/maven3/
配置环境
- 编辑
vim /etc/profile
export MAVEN_HOME=/usr/local/maven3/apache-maven-3.8.5 export PATH=$PATH:$JAVA_HOME/bin:$MAVEN_HOME/bin
- 更新
source /etc/profile
验证
mvn -version
Apache Maven 3.8.5 (3599d3414f046de2324203b78ddcf9b5e4388aa0)
Maven home: /usr/local/maven3/apache-maven-3.8.5
Java version: 1.8.0_221, vendor: Oracle Corporation, runtime: /opt/java/jdk1.8/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-957.el7.x86_64", arch: "amd64", family: "unix"
二、GitLab
安装
docker pull gitlab/gitlab-ce
docker run
-d
-p 443:443
-p 9002:80
-p 222:22
-v /home/gitlab/config:/etc/gitlab
-v /home/gitlab/logs:/var/log/gitlab
-v /home/gitlab/data:/var/opt/gitlab
--name gitlab
--restart always
gitlab/gitlab-ce
设置IP访问
-
vim /home/gitlab/config/gitlab.rb
external_url 'http://192.168.1.8' # 配置ssh协议所使用的访问地址和端口 gitlab_rails['gitlab_ssh_host'] = '192.168.1.8' gitlab_rails['gitlab_shell_ssh_port'] = 222 # 此端口是run时22端口映射的222端口
docker restart 9bbb30dd12c4
初始密码
docker exec 9bbb30dd12c4 less /etc/gitlab/initial_root_password
Password: Mv6TRMdWhV106RColFJFi5CNoN29TqIOA3G2Tki9ugA=
三、Jenkins
安装
mkdir /home/jenkins
docker pull jenkins/jenkins
docker run
-d
-uroot
-p 8080:8080
-p 50000:50000
-v /home/jenkins:/var/jenkins_home
-v /etc/localtime:/etc/localtime
-v /opt/java/jdk1.8:/host/local/java
-v /usr/local/maven3/apache-maven-3.8.5:/host/local/maven3
-v /home/androidSDK:/host/local/android/SDK
-v /home/workspace/android:/host/local/workspace
-v /var/run/docker.sock:/var/run/docker.sock
-v /usr/bin/docker:/usr/bin/docker
--name jenkins
--restart always
jenkins/jenkins:latest
-d | 后台运行容器,并返回容器ID |
---|---|
-uroot | 使用 root 身份进入容器,推荐加上,避免容器内执行某些命令时报权限错误 |
-p 9001:8080 | 将主机->容器端口映射 |
--name jenkins | 设置容器名称为jenkins |
-v /home/jenkins:/var/jenkins_home | :/var/jenkins_home目录为容器jenkins工作目录 |
-v /etc/localtime:/etc/localtime | 让容器使用和服务器同样的时间设置 |
jenkins/jenkins | 镜像的名称 |
初始化
- 国内镜像加速
vim /var/jenkins_home/hudson.model.UpdateCenter.xml
<site> <id>tsinghua</id> <url>https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json</url> </site>
- 获取初始密码
docker exec 34fe40f5c60b less /var/jenkins_home/secrets/initialAdminPassword
- 访问地址 http://IP:9001/
环境配置
- 安装项目所需三方环境 Git/Java/Maven/Gradle
查找相应环境路径echo $JAVA_HOME # /opt/java/openjdk which git # /usr/bin/git
- Git登录凭据
- AndroidSDK
- 安装sdkmanager
mkdir -p /home/androidSDK/latest cd /home/androidSDK/ wget https://dl.google.com/android/repository/commandlinetools-linux-8092744_latest.zip unzip commandlinetools-linux-8092744_latest.zip mv /home/androidSDK/cmdline-tools/* /home/androidSDK/latest/ mv /home/androidSDK/latest /home/androidSDK/cmdline-tools/
- 编辑环境变量
vim /etc/profile
export ANDROID_HOME=/home/androidSDK export SDK_MANAGER_HOME=$ANDROID_HOME/cmdline-tools/latest
source /etc/profile
- 下载sdk
sdkmanager --list sdkmanager "platforms;android-23" "platforms;android-24" "platforms;android-25" "platforms;android-26" "platforms;android-27" "platforms;android-28" "platforms;android-29" "platforms;android-30" "platforms;android-31" "platforms;android-32" sdkmanager "build-tools;28.0.2" "build-tools;28.0.3" "build-tools;29.0.2" "build-tools;30.0.2" "build-tools;32.0.0" "build-tools;32.1.0-rc1" sdkmanager "platform-tools"
- 配置ADB环境变量
- 编辑环境变量
vim /etc/profile
export ADB_HOME=/$ANDROID_HOME/platform-tools export PATH=$PATH:$JAVA_HOME/bin:$MAVEN_HOME/bin:$ANDROID_HOME:$SDK_MANAGER_HOME/bin:$ADB_HOME
source /etc/profile
- 编辑环境变量
- 在Jenkins中配置AndroidSDK
Configure System ---> 全局属性 ---> 环境变量 --->ANDROID_HOME = /host/local/android/SDK
- 安装sdkmanager
项目配置
- 新建项目 FreeStyle
- 参数化构建
- git源码管理
- 构建
-
Invoke Gradle script
Taskclean assemble${BUILD_TYPE} --stacktrace --debug
- Shell
#!/bin/bash keyword="release" date=`date +%Y%m%d%H%M%S` folder=/host/local/workspace/${JOB_NAME} variant=$(echo $BUILD_TYPE | tr '[A-Z]' '[a-z]') sourcepath=${WORKSPACE}/app/build/outputs/apk/${variant}/*.apk sourcefile=$(ls ${sourcepath}) sourcefile=${sourcefile##*/} download=${JOB_URL}ws/app/build/outputs/apk/${variant}/${sourcefile} echo ${sourcepath} echo ${download} # 如果是release版则归档 if [[ ${BRANCH} =~ $keyword && ${variant} =~ $keyword ]]; then version=${BRANCH#*${keyword}/} target_folder=${folder}/${version} name=${JOB_NAME}-${BUILD_TYPE,,}-${version}.${date}.apk mkdir -p ${target_folder} && cp ${sourcepath} ${target_folder}/${name} echo "archive to /home/workspace/android/"${JOB_NAME}/${version}/${name} else echo "Not a release version, not archived." fi # 钉钉通知下载地址 if [ -n "$sourcefile" ]; then dingdingtoken=6ba2f4528389d2d9e6bd8f81d8bf163dbc5bea6cfa737c170fc1ad924d41b509 getkey=$(docker run --rm -v /home/script/:/script -w /script python python dingtalk_sign.py) timestamp=${getkey:0:13} sign=$(echo "${getkey:13:100}" | tr -d '\n') curl "https://oapi.dingtalk.com/robot/send?access_token=$dingdingtoken×tamp=$timestamp&sign=$sign" -H 'Content-Type: application/json' -d "{'msgtype': 'markdown', 'markdown': {'title': '${JOB_NAME}', 'text': '下载地址:${download}'}}" fi
-
Invoke Gradle script
其他
清空重置构建历史
脚本命令行
def String[] arr = Jenkins.instance.getJobNames()
Jenkins.instance.getJobNames().each { item ->
Jenkins.instance.getItemByFullName(item).builds.each { it ->
it.delete()
}
Jenkins.instance.getItemByFullName(item).updateNextBuildNumber(1)
}