Jenkins之制作docker打包镜像环境
前面Jenkins 说到使用【Kubernetes】该插件(Configure Clouds),作为node 节点,那就需要在构建容器里面配置常用的构建环境
# 制作基础构建镜像
docker_images: jenkins/agent:jdk8
gradle: v6.9.0
node: v12.18.3
sdkmanager: 4.0
# 生成镜像Dockerfile
npm ,gradle , mvn 配置镜像加速地址,或者私服
maven 配置私服仓库,配置settings.xml
工具包不要通过ADD添加进去,由于镜像的分层会使得镜像很大,通过curl减少镜像的体积
ARG version=jdk8
FROM jenkins/agent:$version
ARG version
LABEL Description="This is a base image by yangfk ,blog: yfklife.cn"
ARG user=jenkins
USER root
COPY jenkins-agent /usr/local/bin/jenkins-agent
#省略配置环境。。。。。。
RUN mkdir /application \
&& curl -o /application/apache-maven-3.6.3-bin.tar.gz http://192.168.14.10/buildtools/apache-maven-3.6.3-bin.tar.gz \
&& curl -o /application/node-v12.18.3-linux-x64.tar.gz http://192.168.14.10/buildtools/node-v12.18.3-linux-x64.tar.gz \
&& curl -o /application/cmdline-tools.tar.gz http://192.168.14.10/buildtools/cmdline-tools.tar.gz \
&& curl -o /application/gradle-6.8.tar.gz http://192.168.14.10/buildtools/gradle-6.8.tar.gz \
&& tar xf /application/apache-maven-3.6.3-bin.tar.gz -C /application \
&& tar xf /application/node-v12.18.3-linux-x64.tar.gz -C /application \
&& tar xf /application/cmdline-tools.tar.gz -C /application \
&& tar xf /application/gradle-6.8.tar.gz -C /application \
&& rm -f /application/*.tar.gz \
&& chown jenkins:jenkins -R /application \
&& chmod +x /usr/local/bin/jenkins-agent && chmod +x /usr/local/bin/* \
&& ln -s /usr/local/bin/jenkins-agent /usr/local/bin/jenkins-slave \
&& ln -s /application/gradle-6.8/bin/gradle /usr/local/bin/gradle \
&& ln -s /application/cmdline-tools/latest/bin/* /usr/local/bin/ \
&& ln -s /application/cmdline-tools/latest/licenses /application/
#省略配置环境。。。。。。
USER ${user}
ENTRYPOINT ["jenkins-agent"]
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 工具下载地址
- maven-3
maven-3_Release (opens new window) / apache-maven-3.6.3-bin.tar.gz (opens new window)
- gradle
gradle_Releases (opens new window) / gradle-v6.9.0.tar.gz (opens new window)
- sdkmanager/commandlinetools-linux-7302050_latest
sdkmanager-4.0 (opens new window)
- node
node_Release (opens new window) / node-v12.18.3-linux-x64.tar.gz (opens new window)
# cmdline-tools目录制作(sdkmanager)
1,使用Dockerfile,先起一个基础容器,然后在这基础上配置Gradle环境*
2,把压缩包上传到容器"commandlinetools-linux-7302050_latest.zip",解压并重命名(在cmdline-tools下创建latest目录,把当前目录下的文件放入到latest)
4,生成licenses:
cd /application/cmdline-tools/latest/bin/
./sdkmanager --licenses #一路按 y
cd /application
ln -s /application/licenses /application/cmdline-tools/latest/licenses
2
3
4
5,上传gradle-v6.9.0.tar.gz工具包,解压到/application下,并把bin目录软链接到 /usr/local/bin下(使用root用户登录)
6,拉取android代码,执行打包,第一次会打包失败
echo 'sdk.dir=/application/cmdline-tools/latest' > local.properties
打包命令(等待一段时间,下载构建包):gradle assembleRelease
报错信息:
* What went wrong:
Could not determine the dependencies of task ':app:compileReleaseJavaWithJavac'.
> Failed to find target with hash string 'android-29' in: /application/cmdline-tools/latest
第一次打包生成的目录如下显示
jenkins@b74b64403148:~$ ls -l /application/cmdline-tools/latest/
total 84
drwxr-xr-x 2 jenkins jenkins 107 Mar 17 02:23 bin
drwxr-xr-x 3 jenkins jenkins 20 Mar 17 02:56 build-tools
drwxr-xr-x 7 jenkins jenkins 4096 Mar 17 02:55 emulator
drwxr-xr-x 16 jenkins jenkins 4096 Mar 17 02:23 lib
drwxr-xr-x 2 jenkins jenkins 198 Mar 17 02:43 licenses
-rwxr-xr-x 1 jenkins jenkins 70001 Dec 31 2009 NOTICE.txt
drwxr-xr-x 3 jenkins jenkins 16 Mar 17 02:55 patcher
drwxr-xr-x 3 jenkins jenkins 24 Mar 17 02:56 platforms
drwxr-xr-x 4 jenkins jenkins 303 Mar 17 02:55 platform-tools
-rwxr-xr-x 1 jenkins jenkins 84 Dec 31 2009 source.properties
drwxr-xr-x 6 jenkins jenkins 205 Mar 17 02:56 tools
7,把【build-tools/ platform-tools/ licenses platforms licenses/ bin/ lib/ source.properties】 这几个目录,文件拷贝到宿主机
制作压缩包:cmdline-tools.tar.gz
最终的目录显示
jenkins@e7b952031189:~$ ls -l /application/cmdline-tools/latest/
total 80
drwxr-xr-x 2 jenkins jenkins 107 Jul 19 2021 bin
drwxr-xr-x 3 jenkins jenkins 20 Mar 16 05:12 build-tools
drwxr-xr-x 16 jenkins jenkins 4096 Jul 19 2021 lib
drwxr-xr-x 2 jenkins jenkins 198 Jul 19 2021 licenses
-rwxr-xr-x 1 jenkins jenkins 70001 Dec 31 2009 NOTICE.txt
drwxr-xr-x 3 jenkins jenkins 24 Aug 23 2021 platforms
drwxr-xr-x 4 jenkins jenkins 303 Mar 16 05:10 platform-tools
-rwxr-xr-x 1 jenkins jenkins 84 Dec 31 2009 source.properties
8,把下载的几个包,都改成tar包,通过nginx代理,构建镜像,通过curl减少体积