Docker环境搭建Jenkins+gitlab+maven自动打包 部署容器
Posted Sonfer
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker环境搭建Jenkins+gitlab+maven自动打包 部署容器相关的知识,希望对你有一定的参考价值。
宿主机安装docker(省略)
宿主机安装maven
下载二进制包解压到/var/docker_data/jenkins/jenkins_home下面
https://maven.apache.org/download.cgi
Jenkins镜像安装
下载Jenkins镜像(一般都是Ubuntu版)
FROM jenkins USER root #清除了基础镜像设置的源,切换成腾讯云的jessie源 #使用非腾讯云环境的需要将 tencentyun 改为 aliyun RUN echo \'\' > /etc/apt/sources.list.d/jessie-backports.list \\ && echo "deb http://mirrors.tencentyun.com/debian jessie main contrib non-free" > /etc/apt/sources.list \\ && echo "deb http://mirrors.tencentyun.com/debian jessie-updates main contrib non-free" >> /etc/apt/sources.list \\ && echo "deb http://mirrors.tencentyun.com/debian-security jessie/updates main contrib non-free" >> /etc/apt/sources.list #更新源并安装缺少的包 RUN apt-get update ARG dockerGid=999 RUN echo "docker:x:${dockerGid}:jenkins" >> /etc/group
制作镜像
docker build -t jenkins:ubuntu .
启动镜像
docker run --name jenkins --privileged -d -p 8080:8080 -p 5000:5000 -v /etc/localtime:/etc/localtime -v /var/docker_data/jenkins/jenkins_home:/var/jenkins_home -v /var/docker_data/jenkins/settings:/var/settings -v /var/run/docker.sock:/var/run/docker.sock -v $(which docker):/usr/bin/docker --env PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/var/jenkins_home/maven/bin c2997ba18d6c
目录说明:
/var/jenkins_home:Jenkins用户的家目录
/var/jenkins_home/workspace:Jenkins主要工作目录,创建的视图、gitlab拉下来的代码都在这个下面
先按推荐安装再安装插件:
- Maven Integration plugin
- docker-build-step
- Docker plugin
- Gitlab Hook Plugin
- GitLab Plugin
Jenkins系统配置:
系统管理-->系统设置
配置远程主机的账号密码
配置gitlab
GitLab API token获取
在gitlab页面中鼠标点击右上角头像点击settings,访问令牌创建个人访问令牌
Jenkins全局工具配置
系统管理-->全局工具配置
配置jdk路径
jenkins镜像中的jdk应该是yum安装的,所有路径需要自己去找(/usr/lib/jvm/java-8-openjdk-amd64)
root@68decdef1ec3:~# ls -lrt /usr/bin/java lrwxrwxrwx 1 root root 22 Jul 17 2018 /usr/bin/java -> /etc/alternatives/java root@68decdef1ec3:~# ls -lrt /etc/alternatives/java lrwxrwxrwx 1 root root 46 Jul 17 2018 /etc/alternatives/java -> /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
maven配置(这个是从宿主机映射过来的)
配置凭据
创建自动打包job
源码管理
构建触发器
构建build(打包)根据pom文件打包(clean package -Dmaven.test.skip=true)
最后运行脚本
参考别人脚本
echo \'================打包完成,开始复制================\' cd /var/lib/jenkins/workspace/gpsserver scp Dockerfile-gps root@hadoop1:/data/public/gpsserver/ scp server-gps/target/gpserver.jar root@hadoop1:/data/public/gpsserver/ echo \'================复制文件完成================\' ssh root@hadoop1 << remotessh echo \'================进入主机hadoop1 shell================\' cd /data/public/gpsserver echo \'================停止容器 gpsserver================\' sudo docker stop gpsserver echo \'================删除容器 gpsserver================\' sudo docker rm gpsserver echo \'================删除镜像 gpsserver:latest================\' sudo docker rmi gpsserver:latest echo \'================build 镜像 gpsserver:latest================\' sudo docker build -t gpsserver:latest -f ./Dockerfile-gps . echo \'================运行容器 gpsserver================\' sudo docker run --name=gpsserver -e JAVA_OPTS=\' -Dspring.profiles.active=prod \' --restart always -it -d -p 10111:10111 -p 15678:15678 -v /data/public/gpsserver/logs:/logs gpsserver:latest echo "finished!" exit ###退出远程机器 remotessh ###结尾哦 echo \'================结束远程启动================\'
以上是关于Docker环境搭建Jenkins+gitlab+maven自动打包 部署容器的主要内容,如果未能解决你的问题,请参考以下文章
Docker环境搭建Jenkins+gitlab+maven自动打包 部署容器
K8S基于Docker+K8S+GitLab/SVN+Jenkins+Harbor搭建持续集成交付环境(环境搭建篇)