Jenkins是一个广泛用于持续构建的可视化web工具,可用于自动化与构建、测试、交付或部署软件相关的各种任务。
可以通过安装包、tomcat、java、docker方式进行安装使用
官方地址:https://jenkins.io/
这里通过tomcat来启动Jenkins
到官网https://jenkins.io/download/ 下载war包
复制war包到tomcat的webapps目录下,启动tomcat
这里提示解锁Jenkins,复制 /Users/Shared/Jenkins/Home/secrets/initialAdminPassword
文件内容到页面
$ cat /Users/Shared/Jenkins/Home/secrets/initialAdminPassword
xxxxxxxxx
这里设置 admin / admin
然后提示安装插件,选择默认的安装,等待安装完成。有些插件可能安装失败,提示要求升级最新版本Jenkins,升级成功后可以安装成功。
项目地址:https://gitee.com/yimingkeji/docker-springboot
#FROM openjdk:8-jdk-alpine
FROM hub.c.163.com/dwyane/openjdk:8
VOLUME /tmp
ADD docker-springboot-1.0-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
然后上传到自己的git地址
在源码管理
里添加git仓库和用户名、密码配置,并且选择代码分支(这里是master)
在构建
步骤中,添加2个步骤
顶级maven
选择maven版本(上面的全局配置中配的maven),添加maven打包命令clean install -Dmaven.test.skip=true
执行shell
添加shell:mvn docker:build
echo "当前docker 镜像:"
docker images | grep dockerspringboot
echo "启动容器----->"
docker run -p 8001:8001 -d dockerspringboot
echo "启动服务成功!"
上面配置完成后,到Jenkins主页,选择配置好的项目,菜单中点击立即构建
构建中 在工作空间 /Users/yangzhenlong/.jenkins/workspace/docker-springboot 中
> /usr/bin/git rev-parse --is-inside-work-tree # timeout=10
...
> /usr/bin/git checkout -f de05bbef05fae5ade0c79ce1d49154723d84fa88
Commit message: "修改dockerfile" #git中最后的提交记录
> /usr/bin/git rev-list --no-walk 330c170c5829017d00942d7301834c2a196a29ac # timeout=10
[docker-springboot] $ /Users/xxxx/apache-maven-3.5.4/bin/mvn -s /Users/xxx/apache-maven-3.5.4/conf/settings.xml -gs /Users/xxx/apache-maven-3.5.4/conf/settings.xml clean install -Dmaven.test.skip=true # 执行maven脚本
[INFO] Scanning for projects...
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.412 s
[INFO] Finished at: 2018-11-19T18:01:54+08:00
[INFO] ------------------------------------------------------------------------
[docker-springboot] $ /bin/sh -xe /Users/yangzhenlong/tools/tomcat8-jenkins/temp/jenkins7206360448776968606.sh
+ mvn docker:build #构建成功后,执行shell脚本开始...
[INFO] Scanning for projects...
...
[INFO] Copying /Users/yangzhenlong/.jenkins/workspace/docker-springboot/src/main/docker/Dockerfile -> /Users/yangzhenlong/.jenkins/workspace/docker-springboot/target/docker/Dockerfile
[INFO] Building image dockerspringboot #执行Dockerfile脚本开始...
Step 1/4 : FROM hub.c.163.com/dwyane/openjdk:8
---> 96cddf5ae9f1
Step 2/4 : VOLUME /tmp
---> Using cache
---> 2e62fad6a16a
Step 3/4 : ADD docker-springboot-1.0-SNAPSHOT.jar app.jar
---> 2d1319472a12
Step 4/4 : ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
---> Running in f5d8c06b3b7d
Removing intermediate container f5d8c06b3b7d
---> 3f24ff705b46
ProgressMessage{id=null, status=null, stream=null, error=null, progress=null, progressDetail=null}
Successfully built 3f24ff705b46
Successfully tagged dockerspringboot:latest
[INFO] Built dockerspringboot
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.841 s
[INFO] Finished at: 2018-11-19T18:02:00+08:00
[INFO] ------------------------------------------------------------------------
+ echo '当前docker 镜像:' # dockerFile构建镜像成功后,shell脚本打印docker镜像
当前docker 镜像:
+ docker images
+ grep dockerspringboot
REPOSITORY TAG IMAGE ID CREATED SIZE
dockerspringboot latest 3f24ff705b46 1 second ago 657MB
+ echo '启动容器----->'
启动容器-----> #这里的中文会乱码,后面建议使用英文
+ docker run -p 8080:8080 -d dockerspringboot
e0aeafb47410a77f30bcb24b744b49dd57b251594a68da997defeef24c256f2b
+ echo $'�\220��\212��\234\215�\212��\210\220�\212\237�\201'
启动服务成功!
Finished: SUCCESS
然后浏览器访问 localhost:8080
如果下次构建该项目的时候,docker镜像和服务已存在,需要先删除镜像和服务
# 先删除之前的容器
echo "remobe old container"
docker ps -a | grep dockerspringboot | awk '{print $1}'| xargs docker rm -f
# 删除之前的镜像
echo "romove old image"
docker rmi dockerspringboot
# 构建镜像
mvn docker:build
# 打印当前镜像
echo "current docker images"
docker images | grep dockerspringboot
# 启动容器
echo "start container"
docker run -p 8001:8001 -d dockerspringboot
# 打印当前容器
echo "current container"
docker ps -a | grep dockerspringboot
echo "star service success!"
输出日志:
+ echo 'current docker images'
current docker images
+ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
dockerspringboot latest 8c9f93a324c0 Less than a second ago 657MB
3f24ff705b46 2 hours ago 657MB
e276f4ef3cc5 2 days ago 657MB
02b677ad12e1 2 days ago 657MB
nginx latest dbfc48660aeb 4 weeks ago 109MB
mongo latest a41c82c0998a 2 months ago 380MB
redis 3.2 e2e6164a20de 4 months ago 76MB
hub.c.163.com/library/nginx latest 46102226f2fd 19 months ago 109MB
mysql 5.7 9e64176cd8a2 19 months ago 407MB
hub.c.163.com/library/mysql 5.7 9e64176cd8a2 19 months ago 407MB
hub.c.163.com/dwyane/openjdk 8 96cddf5ae9f1 2 years ago 641MB
+ echo 'start container'
start container
+ docker run -p 8001:8001 -d dockerspringboot
0b79b907e9ca49831bd9259eeea2c60ec40132342b2ed3c00d4cf183756d6610
+ echo 'current container'
current container
+ docker ps -a
+ grep dockerspringboot
0b79b907e9ca dockerspringboot "java -Djava.securit…" 1 second ago Up Less than a second 0.0.0.0:8001->8001/tcp happy_johnson
+ echo 'star service success!'
star service success!
Finished: SUCCESS