在Docker中自定义Jenkins镜像
一. 构建Jenkins slave.
1. 构建镜像需要三个步骤:
(1) 创建Dockerfile
(2) 构建镜像
(3)在master上改变agent的配置
2. 以下是创建一个服务Python项目的jenkins slave. 为了简单起见,该实验用的evarga/jenkins-slave镜像为基础。
(1)创建Dockerfile文件
[root@c720120 docker]# cat Dockerfile
FROM evarga/jenkins-slave
RUN apt-get update && \
apt-get install -y python
(2)构建镜像,执行以下命令
[root@c720120 docker]# docker build -t jenkins-slave-python .
(3)配置master.
在setting Docker agent章节中,把evarga/jenkins-slave 替换为jenkins-slave-python.
二. 构建Jenkins master.
1. 案例1: 创建jenkins master镜像,并已经安装docker-plugin和设置同时执行数量设置为5.
需要以下步骤:
(1)在config.xml中创建Groovy脚本和设置执行数量为5.
(2)创建Dockerfile和安装docker-plugin,执行Groovy 脚本。
(3)构建镜像
2. 创建步骤
(1)创建一个新的目录,并把executors.groovy文件放入该目录中。
[root@c720120 docker]# cat executors.groovy
import jenkins.model.*
Jenkins.instance.setNumExecutors(5)
(2)在相同的目录中创建Dockerfile。
[root@c720120 docker]# cat Dockerfile
FROM jenkins
COPY executors.groovy
/usr/share/jenkins/ref/init.groovy.d/executors.groovy
RUN /usr/local/bin/install-plugins.sh docker-plugin
(3)构建镜像
[root@c720120 docker]# docker build -t jenkins-master .
Sending build context to Docker daemon 3.072kB
Step 1/3 : FROM jenkins
---> 07b4164f9789
Step 2/3 : COPY executors.groovy /usr/share/jenkins/ref/init.groovy.d/executors.groovy
---> 4a2c176f2d1f
Step 3/3 : RUN /usr/local/bin/install-plugins.sh docker-plugin
---> Running in ed24ad803c7c
Creating initial locks...
Analyzing war...
Using version-specific update center: https://updates.jenkins.io/2.60...
Downloading plugins...
Downloading plugin: docker-plugin from https://updates.jenkins.io/2.60/latest/docker-plugin.hpi
> docker-plugin depends on workflow-api:2.23.1;resolution:=optional,workflow-cps:2.41;resolution:=optional,workflow-durable-task-step:2.17;resolution:=optional,workflow-step-api:2.14;resolution:=optional,workflow-support:2.16;resolution:=optional,apache-httpcomponents-client-4-api:4.5.3-2.0,bouncycastle-api:2.16.2,docker-commons:1.9,docker-java-api:3.0.14,durable-task:1.16,script-security:1.36;resolution:=optional,ssh-slaves:1.22,token-macro:2.3
Skipping optional dependency workflow-api
Skipping optional dependency workflow-cps
Skipping optional dependency workflow-durable-task-step
Skipping optional dependency workflow-step-api
Skipping optional dependency workflow-support
Downloading plugin: apache-httpcomponents-client-4-api from https://updates.jenkins.io/2.60/latest/apache-httpcomponents-client-4-api.hpi
Downloading plugin: bouncycastle-api from https://updates.jenkins.io/2.60/latest/bouncycastle-api.hpi
Downloading plugin: docker-commons from https://updates.jenkins.io/2.60/latest/docker-commons.hpi
Downloading plugin: docker-java-api from https://updates.jenkins.io/2.60/latest/docker-java-api.hpi
Skipping optional dependency script-security
Downloading plugin: durable-task from https://updates.jenkins.io/2.60/latest/durable-task.hpi
Downloading plugin: ssh-slaves from https://updates.jenkins.io/2.60/latest/ssh-slaves.hpi
Downloading plugin: token-macro from https://updates.jenkins.io/2.60/latest/token-macro.hpi
> token-macro depends on workflow-step-api:2.7
Downloading plugin: workflow-step-api from https://updates.jenkins.io/2.60/latest/workflow-step-api.hpi
> workflow-step-api depends on structs:1.5
Downloading plugin: structs from https://updates.jenkins.io/2.60/latest/structs.hpi
> docker-commons depends on authentication-tokens:1.3,credentials-binding:1.12,credentials:2.1.13
Downloading plugin: authentication-tokens from https://updates.jenkins.io/2.60/latest/authentication-tokens.hpi
Downloading plugin: credentials-binding from https://updates.jenkins.io/2.60/latest/credentials-binding.hpi
Downloading plugin: credentials from https://updates.jenkins.io/2.60/latest/credentials.hpi
> authentication-tokens depends on credentials:1.22
> credentials-binding depends on workflow-step-api:2.10,credentials:2.1.7,plain-credentials:1.3,ssh-credentials:1.11,structs:1.7
Downloading plugin: plain-credentials from https://updates.jenkins.io/2.60/latest/plain-credentials.hpi
Downloading plugin: ssh-credentials from https://updates.jenkins.io/2.60/latest/ssh-credentials.hpi
> credentials depends on structs:1.7
> docker-java-api depends on jackson2-api:2.6.4
Downloading plugin: jackson2-api from https://updates.jenkins.io/2.60/latest/jackson2-api.hpi
> ssh-slaves depends on credentials:2.1.2,ssh-credentials:1.6.1
> plain-credentials depends on credentials:2.1.5
> ssh-credentials depends on credentials:2.1.0WAR bundled plugins:
Installed plugins:
apache-httpcomponents-client-4-api:4.5.5-3.0
authentication-tokens:1.3
bouncycastle-api:2.16.3
credentials-binding:1.16
credentials:2.1.16
docker-commons:1.13
docker-java-api:3.0.14
docker-plugin:1.1.4
durable-task:1.22
jackson2-api:2.8.11.3
plain-credentials:1.4
ssh-credentials:1.13
ssh-slaves:1.26
structs:1.14
token-macro:2.5
workflow-step-api:2.15
Cleaning up locks
Removing intermediate container ed24ad803c7c
---> 3f54b7f95dd7
Successfully built 3f54b7f95dd7
Successfully tagged jenkins-master:latest