jenkins和docker 使用docker作为slave

使用docker作为jenkins slave。

文章来自:http://www.ciandcd.com文中的代码来自可以从github下载: https://github.com/ciandcd

参考:

https://www.cloudbees.com/blog/templating-jenkins-build-environments-docker-containers

需要安装插件:

CloudBees Docker Custom Build Environment Plugin
https://wiki.jenkins-ci.org/display/JENKINS/CloudBees+Docker+Custom+Build+Environment+Plugin

1. docker 和插件CloudBees Docker Custom Build Environment Plugin

docker是一种非常好的方法来快速生成可重现,隔离的环境。docker相对虚拟机更轻量级,需要更少的资源来运行。

docker的另一个好处是docker image可以是从hub.docker.com或私有的repo获取的编译好的docker image, 也可以下载文本格式的Dockerfile,然后在生成docker image。

Dockerfile可以与项目的源代码一个版本控制,从而实现源代码和构建环境docker image同步且可重现。

插件CloudBees Docker Custom Build Environment Plugin,通过此插件可以直接使用docker image或Dockerfile来生成job的运行环境。

2. jenkins docker images

https://hub.docker.com/r/jenkinsci/jnlp-slave/ (docker pull jenkinsci/jnlp-slave)

https://hub.docker.com/r/jenkinsci/ssh-slave/ (docker pull jenkinsci/ssh-slave)

https://hub.docker.com/r/jenkinsci/slave/  (docker pull jenkinsci/slave)

所有jenkins相关的docker https://hub.docker.com/u/jenkinsci/。

本实例中使用jnlp-slave docker image,但是我的环境没有jenkins slave,job运行在master上,docker也安装在master上。

如果你job运行在node/slave上,需要node上安装docker。

确保已经安装docker,且当前用户可以不加sudo来使用docker。

sudo groupadd docker
sudo gpasswd -a ${USER} docker
newgrp docker
sudo service docker restart

然后运行以下确保docker安装且设置正确:

osboxes@osboxes:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
osboxes@osboxes:~$ docker info
Containers: 1
Running: 0
Paused: 0
Stopped: 1
Images: 3
Server Version: 1.11.2
Storage Driver: aufs
Root Dir: /var/lib/docker/aufs

测试下载jenkins slave docker image:

osboxes@osboxes:~$ docker -H unix:///var/run/docker.sock pull jenkinsci/jnlp-slave
Using default tag: latest
latest: Pulling from jenkinsci/jnlp-slave
7ccc78f8af6d: Already exists
a3ed95caeb02: Already exists
29f19d8b362b: Already exists
a068cb6fd68b: Already exists
8f8532f99b46: Already exists
fc6378a07ddb: Already exists
debdb2a461e8: Already exists
4a4e85ec23cd: Already exists
af041ce270a0: Already exists
82928c989a2b: Already exists
a05116d5d5b4: Already exists
Digest: sha256:cfdb32e8fa5ebf965caf8244c337ca5451eb6c45c8f881f1823bd8eb3c61bb62
Status: Image is up to date for jenkinsci/jnlp-slave:latest

3. 创建freestyle job使用docker

1) 使用测试git repo:https://github.com/ciandcd/JavaHelloWorld.git

2)jenkins job 配置

使用jenkinsci/jnlp-slave docker image,注意docker host uri的配置,我的docker和jenkins都在同一测试机器上。

jenkins和docker 使用docker作为slave

3)job的配置xml如下:https://github.com/ciandcd/jenkins-awesome/blob/master/jenkins_home/jobs/docker_hello/config.xml

4) 检查job的运行log可以看到jenkins自己下载启动docker image,然后运行后停止docker。

$ docker -H unix:///var/run/docker.sock inspect jenkinsci/jnlp-slave
$ docker -H unix:///var/run/docker.sock run --rm --entrypoint /bin/true alpine:3.2
$ docker -H unix:///var/run/docker.sock run --tty --detach --workdir /home/osboxes/github/jenkins-example/jenkins_home/jobs/docker_hello/workspace --volume /home/osboxes/github/jenkins-example/jenkins_home:/home/osboxes/github/jenkins-example/jenkins_home:rw --volume /tmp:/tmp:rw --net bridge --add-host dockerhost:172.17.0.1 --env BUILD_DISPLAY_NAME=#12 --env BUILD_ID=12 --env BUILD_NUMBER=12 --env BUILD_TAG=jenkins-docker_hello-12 --env BUILD_URL=http://10.130.28.78:8008/job/docker_hello/12/ --env CLASSPATH= --env EXECUTOR_NUMBER=0 --env GIT_BRANCH=origin/master --env GIT_COMMIT=b462496ca38e2c5b17aa6f599322cebffe104c24 --env GIT_PREVIOUS_COMMIT=b462496ca38e2c5b17aa6f599322cebffe104c24 --env GIT_URL=https://github.com/ciandcd/JavaHelloWorld.git --env HUDSON_HOME=/home/osboxes/github/jenkins-example/./jenkins_home --env HUDSON_SERVER_COOKIE=f2bf6016b1befbaa --env HUDSON_URL=http://10.130.28.78:8008/ --env JENKINS_SERVER_COOKIE=f2bf6016b1befbaa --env JENKINS_URL=http://10.130.28.78:8008/ --env JOB_BASE_NAME=docker_hello --env JOB_NAME=docker_hello --env JOB_URL=http://10.130.28.78:8008/job/docker_hello/ --env NODE_LABELS=master --env NODE_NAME=master --env PATH+JDK=/usr/lib/jvm/java-8-openjdk-amd64/bin --env WORKSPACE=/home/osboxes/github/jenkins-example/jenkins_home/jobs/docker_hello/workspace jenkinsci/jnlp-slave /bin/cat
Docker container a44510f2cc21a473c26759cfa650dc26b4c0b7799d11eb37ab21d02cf5682276 started to host the build
$ docker -H unix:///var/run/docker.sock exec --tty a44510f2cc21a473c26759cfa650dc26b4c0b7799d11eb37ab21d02cf5682276 env
[workspace] $ docker -H unix:///var/run/docker.sock exec --tty --user 1000:1000 a44510f2cc21a473c26759cfa650dc26b4c0b7799d11eb37ab21d02cf5682276 env BUILD_DISPLAY_NAME=#12 BUILD_ID=12 BUILD_NUMBER=12 BUILD_TAG=jenkins-docker_hello-12 BUILD_URL=http://10.130.28.78:8008/job/docker_hello/12/ CA_CERTIFICATES_JAVA_VERSION=20140324 CLASSPATH= EXECUTOR_NUMBER=0 GIT_BRANCH=origin/master GIT_COMMIT=b462496ca38e2c5b17aa6f599322cebffe104c24 GIT_PREVIOUS_COMMIT=b462496ca38e2c5b17aa6f599322cebffe104c24 GIT_URL=https://github.com/ciandcd/JavaHelloWorld.git HOME=/home/jenkins HOSTNAME=a44510f2cc21 HUDSON_HOME=/home/osboxes/github/jenkins-example/./jenkins_home HUDSON_SERVER_COOKIE=f2bf6016b1befbaa HUDSON_URL=http://10.130.28.78:8008/ JAVA_DEBIAN_VERSION=8u66-b01-1~bpo8+1 JAVA_VERSION=8u66 JENKINS_SERVER_COOKIE=f2bf6016b1befbaa JENKINS_URL=http://10.130.28.78:8008/ JOB_BASE_NAME=docker_hello JOB_NAME=docker_hello JOB_URL=http://10.130.28.78:8008/job/docker_hello/ LANG=C.UTF-8 NODE_LABELS=master NODE_NAME=master PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin PATH+JDK=/usr/lib/jvm/java-8-openjdk-amd64/bin TERM=xterm WORKSPACE=/home/osboxes/github/jenkins-example/jenkins_home/jobs/docker_hello/workspace /usr/share/maven/bin/mvn clean verify
env: /usr/share/maven/bin/mvn: No such file or directory
Build step 'Invoke top-level Maven targets' marked build as failure
Stopping Docker container after build completion
$ docker -H unix:///var/run/docker.sock kill a44510f2cc21a473c26759cfa650dc26b4c0b7799d11eb37ab21d02cf5682276
a44510f2cc21a473c26759cfa650dc26b4c0b7799d11eb37ab21d02cf5682276
$ docker rm --force a44510f2cc21a473c26759cfa650dc26b4c0b7799d11eb37ab21d02cf5682276
a44510f2cc21a473c26759cfa650dc26b4c0b7799d11eb37ab21d02cf5682276

参考:

DOCKER_PORT 相关:http://*.com/questions/26561963/how-to-detect-a-docker-daemon-port

上一篇:JavaScript学习总结(二)——延迟对象、跨域、模板引擎、弹出层、AJAX示例


下一篇:LINQ to SQL 系列 如何使用LINQ to SQL插入、修改、删除数据