一、配置凭证
生成令牌
管理凭证
添加凭证
填写信息
二、全局配置
docker-compose
version: '3'
services:
jenkins:
restart: always
image: jenkins:latest
container_name: jenkins
ports:
- '8888:8080'
- '50000:50000'
volumes:
- /home/qiang/docker-compose/jenkins/data/jenkins_home:/var/jenkins_home
- /home/qiang/docker-compose/jenkins/data/war/jenkins.war:/usr/share/jenkins/jenkins.war
查看位置
[root@6e169b02437d jenkins_home]# pwd
/home/qiang/docker-compose/jenkins/data/jenkins_home
[root@6e169b02437d jenkins_home]# mkdir -p software/maven_home
[root@6e169b02437d jenkins_home]# mkdir -p software/jdk_home
settings.xml
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>/var/jenkins_home/software/maven_home/repository</localRepository>
<servers>
<server>
<id>nexus-public</id>
<username>admin</username>
<password>123456</password>
</server>
<server>
<id>nexus-releases</id>
<username>admin</username>
<password>123456</password>
</server>
<server>
<id>nexus-snapshots</id>
<username>admin</username>
<password>123456</password>
</server>
</servers>
<mirrors>
<mirror>
<id>nexus-public</id>
<mirrorOf>*</mirrorOf>
<name>Nexus Public</name>
<url>http://192.168.8.245:8081/repository/maven-public/</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
</profiles>
</settings>
添加配置
三、新建任务
构建一个Maven项目
输入项目地址和刚刚添加的凭证
Maven执行打包命令
保存并尝试构建一下
构建成功
四、远程推送
填写服务器信息
配置推送到远程服务器
推送成功
五、构建镜像
开放Docker远程API
# 编辑文件
vim /etc/systemd/system/docker.service
# 修改内容
ExecStart=/usr/bin/dockerd -H unix:///var/run/docker.sock -H tcp://0.0.0.0:2375
# 重启服务
systemctl daemon-reload && systemctl restart docker
# 测试验证
docker -H 192.168.8.246:2375 images
Jenkins链接Docker
tcp://192.168.8.248:2375
Docker拥有的容器
添加镜像仓库账号密码
准备Dockerfile
# 指定基础镜像
FROM 192.168.8.247/xiaoqiangzai/openjdk:8-jre
# 作者的信息
MAINTAINER wuduoqiang
# jar包的名字
ENV jar pavilion-1.0.0.jar
# 工作的目录
ENV workDir /app/
# 进入到容器后进入到哪个目录
WORKDIR ${workDir}
# 将文件拷贝到镜像中
COPY pavilion/target/${jar} ${workDir}
# 设置时区为上海
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
# 容器启动时候执行的命令
CMD ["sh","-ec","exec java -jar ${jar} --spring.profiles.active=dev -Xms256m -Xmx256m -Xmn128m"]
构建成功
推送成功
六、定时删除
镜像策略
定时任务
七、启动服务
docker-compose
version: "3.0"
services:
pavilion:
image: 192.168.8.247/xiaoqiangzai/pavilion:{BUILD_ID}
container_name: pavilion
restart: always
privileged: true
ports:
- "8080:8080"
将之前推送的jar包改成推送docker-compose
启动脚本
echo "停止旧服务"
docker stop $(docker ps -aq -f 'name=pavilion') && docker rm $(docker ps -aq -f'name=pavilion')
echo "删掉旧镜像"
docker rmi $(docker images -aq -f 'reference=192.168.8.247/xiaoqiangzai/pavilion:*')
echo "启动新镜像"
eval sed -in 's#{BUILD_ID}#'$BUILD_ID'#' /home/qiang/jenkins/pavilion/docker-compose.yaml
rm -rf /home/qiang/jenkins/pavilion/docker-compose.yamln
docker-compose -f /home/qiang/jenkins/pavilion/docker-compose.yaml up -d
2021-08-27更新:这里的脚本有点问题,容器不存在的时候会报错,使用下面的脚本即可。
containerName='pavilion' if [ `docker ps -aq -f 'name='$containerName'' | wc -l` -ge 1 ]; then echo '容器'$containerName'运行中,停止容器' docker stop $(docker ps -aq -f 'name='$containerName'') && docker rm $(docker ps -aq -f'name='$containerName'') echo '容器'$containerName'已停止,删旧镜像' docker rmi $(docker images -aq -f 'reference=192.168.8.247/xiaoqiangzai/'$containerName':*') else echo '容器'$containerName'不存在,无需停止' fi echo "启动新镜像" eval sed -in 's#{BUILD_ID}#'$BUILD_ID'#' /home/qiang/jenkins/pavilion/docker-compose.yaml rm -rf /home/qiang/jenkins/pavilion/docker-compose.yamln docker-compose -f /home/qiang/jenkins/pavilion/docker-compose.yaml up -d
构建成功
上传成功
部署成功
八、自动构建
安装gitee插件
配置代码提交自动构建触发器
生成密钥
添加钩子
将URL复制进来
# 由于需要外网地址,这里使用了内网穿透
http://192.168.8.245:8888/jenkins/gitee-project/pavilion-service-dev -> https://www.xiaoqiangzai.xyz/jenkins/gitee-project/pavilion-service-dev
测试成功
九、常见问题
问题1:
项目启动超时,由于项目启动时间超过默认的超时时间则提示错误。
SSH: Connecting from host [4103904b7acf]
SSH: Connecting with configuration [Centos246] ...
SSH: Disconnecting configuration [Centos246] ...
ERROR: Exception when publishing, exception message [Exec timed out or was interrupted after 120,000 ms]
Build step 'Send files or execute commands over SSH' changed build result to UNSTABLE
Finished: UNSTABLE
解决1:
修改默认的超时时间即可,默认120000毫秒,以实际情况来修改。