jenkins+docker自动化部署

Skills / 2020-10-07

安装jenkins

拉取jenkins:

docker pull jenkins/jenkins:lts

运行容器:

docker run -p 8080:8080 -p 50000:5000 --name jenkins -u root -v /mydata/jenkins_home:/var/jenkins_home -d jenkins/jenkins:lts

查看初始密码:

docker logs jenkins

iShot2020-10-07 18.05.21-4cd96f968ea24392b78b25c335d12a14

访问8080端口

iShot2020-10-07 18.00.12-b5107d3db6d54f578a899b554fe4d4cf

账号admin,密码就是之前查看的密码

初始化

还是之前查看的密码

iShot2020-10-07 18.07.43-35c2b3b765a24cc8859e152906a96371

安装推荐插件

iShot2020-10-07 18.08.33-1272ca0bd58e46a8abfa44366118b639

创建用户:

iShot2020-10-07 18.12.23-7642ad1c5c5b4ee5a3767090cd770552

插件管理:

image-2bbf9741cdc9422a81a4b3688f6a05b3

装个插件:

image-f3c13e9ce4fc41e09b211b87fc504340

全局工具配置:

image-5c21c7be97ca4df2a2bedaf378b22e56

安装maven:

image-75e1f5532241477aae14f592ddc61076

配置SSH:

image-d3f480c01c1341c3a19d0b8959d5c061

服务器私钥:

image-b631cf5fe3004b4ab27ea3b4cfe5f1c7

新建一个自动部署:

image-25d9a7c776eb460a97daed99b2fc66f7

命名:

image-1fe4d7b2dceb4ccfb96c2a461e05ca71

添加git仓库:

image-bed43d3f217b41989da546454403f2c4

配置GitHub账号密码:

image-c6a6c2b6b9714886aa480908b467d3b9

配置触发条件:

image-bed4dd00d1fd4b3ca7bcbdbeff8fdbd6

增加构建步骤:

image-bbe385ff110a49d381452cd654127afe

image-fff84797ea7d48d5be121fcf47713724

增加构建后步骤:

image-91c7442870ac49e7800f11f39d0a7a51

image-bc6fbc17e9b8442b81d5bd0349a6e79d

Dockerfile 内容:

FROM java:8
VOLUME /tmp
ADD onlineprinter-1.0.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-Xms128m","-Xmx128m","-jar","/app.jar"]

jenkins .sh 内容:

#!/usr/bin/env bash
app_name='onlineprinter-1.0'
docker stop ${app_name}
echo '--------------------------------stop container----'
docker rm ${app_name}
echo '----------------------------------rm container----'
docker rmi ${app_name}
echo '---------------------------------rmi container----'
docker build . -t ${app_name}
echo '-------------------------------build container----'
docker run -p 8050:8050 --name ${app_name} \
-d ${app_name}
echo '-------------------------------start container----'
rm -rf ${app_name}.jar

脚本步骤:

  • 停止正在运行的容器
  • 删除容器
  • 删除镜像
  • 构建镜像
  • 创建容器

然后回到首页,点击立即构建就可以构建了,或者pullGitHub一下也会触发构建